I built a small weather station that sends the current temperature, humidity and uv to data.sparkfun.com. I have it set to do it every 10000 milliseconds as you can only send new data 100 times in a 15 minute period. Anyways, my code works, it sends the info and I can view it but after about 2 hours or so it just stops working. And I have no idea why. I can solve the problem by resetting the Arduino which to me points to some sort of memory leak. Here is the code, I have a CC3000, DHT22 temperature sensor and the Sparkfun UV sensor all working together. The Code gathers the reading and then the CC3000 posts it to the server, then waits 10000 milliseconds. I have removed both my public and private key from the code as well as my WiFi network info, you'll have to trust me that I actually have them in that actual code.
#include <SPI.h>
#include <SFE_CC3000.h>
#include <SFE_CC3000_Client.h>
#include "DHT.h"
#define CC3000_INT 2 // Needs to be an interrupt pin (D2/D3)
#define CC3000_EN 7 // Can be any digital pin
#define CC3000_CS 10 // Preferred is pin 10 on Uno
#define IP_ADDR_LEN 4 // Length of IP address in bytes
#define DHTPIN 3 //Temp sensor is connected to pin 3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
/*************
WiFi Constants
*************/
char ap_ssid[] = "SSID NAME"; // SSID of network
char ap_password[] = "NETWORK PASSWORD"; // Password of network
unsigned int ap_security = WLAN_SEC_WPA2; // Security of network
// ap_security can be any of: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
// WLAN_SEC_WPA, or WLAN_SEC_WPA2
unsigned int timeout = 30000; // Milliseconds
char server[] = "data.sparkfun.com"; // Remote host site
// Initialize the CC3000 objects (shield and client):
SFE_CC3000 wifi = SFE_CC3000(CC3000_INT, CC3000_EN, CC3000_CS);
SFE_CC3000_Client client = SFE_CC3000_Client(wifi);
//Set up of server stuff
const String publicKey = "PUBLIC KEY";
const String privateKey = "PRIVATE KEY";
const byte NUM_FIELDS = 3;
const String fieldNames[NUM_FIELDS] = {"humidity", "temp", "uv"};
String fieldData[NUM_FIELDS];
int humidity;
int temp;
int uv;
int UVOUT = A0; //Output from the UV sensor
int REF_3V3 = A1; //3.3V power on the Arduino board for UV sensor
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(UVOUT, INPUT);
pinMode(REF_3V3, INPUT);
// Set Up WiFi:
setupWiFi();
Serial.println(F("=========== Ready to Stream ==========="));
Serial.println(F("Press the button (D3) to send an update"));
//Start the temp/humidity sensor
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
humidity = h; //Assign the humidity reading to h
temp = t; //Assign the temp reading to t
int hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
int hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
int uvLevel = averageAnalogRead(UVOUT);
int refLevel = averageAnalogRead(REF_3V3);
//Use the 3.3V power pin as a reference to get a very accurate output value from UV sensor
float outputVoltage = 3.3 / refLevel * uvLevel;
float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level
Serial.print("output: ");
Serial.print(refLevel);
Serial.print("ML8511 output: ");
Serial.print(uvLevel);
Serial.print(" / ML8511 voltage: ");
Serial.print(outputVoltage);
Serial.print(" / UV Intensity (mW/cm^2): ");
Serial.print(uvIntensity);
Serial.println();
uv = uvIntensity; Assign the uvIntensity reading to uv
delay(9000); //delay for 9000 miliseconds so that the server is not overwelmed
// Gather data:
fieldData[0] = String(humidity);
fieldData[1] = String(temp);
fieldData[2] = String(uv);
Serial.println("Posting!");
postData(); // the postData() function does all the work,
// check it out below.
delay(1000);
}
void postData()
{
// Make a TCP connection to remote host
if ( !client.connect(server, 80) )
{
// Error: 4 - Could not make a TCP connection
Serial.println(F("Error: 4"));
}
// Post the data! Request should look a little something like:
// GET /input/publicKey?private_key=privateKey&light=1024&switch=0&time=5201 HTTP/1.1\n
// Host: data.sparkfun.com\n
// Connection: close\n
// \n
client.print("GET /input/");
client.print(publicKey);
client.print("?private_key=");
client.print(privateKey);
for (int i = 0; i < NUM_FIELDS; i++)
{
client.print("&");
client.print(fieldNames[i]);
client.print("=");
client.print(fieldData[i]);
}
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
while (client.connected())
{
if ( client.available() )
{
char c = client.read();
Serial.print(c);
}
}
Serial.println();
}
void setupWiFi()
{
ConnectionInfo connection_info;
int i;
// Initialize CC3000 (configure SPI communications)
if ( wifi.init() )
{
Serial.println(F("CC3000 Ready!"));
}
else
{
// Error: 0 - Something went wrong during CC3000 init!
Serial.println(F("Error: 0"));
}
// Connect using DHCP
Serial.print(F("Connecting to: "));
Serial.println(ap_ssid);
if (!wifi.connect(ap_ssid, ap_security, ap_password, timeout))
{
// Error: 1 - Could not connect to AP
Serial.println(F("Error: 1"));
}
// Gather connection details and print IP address
if ( !wifi.getConnectionInfo(connection_info) )
{
// Error: 2 - Could not obtain connection details
Serial.println(F("Error: 2"));
}
else
{
Serial.print(F("My IP: "));
for (i = 0; i < IP_ADDR_LEN; i++)
{
Serial.print(connection_info.ip_address[i]);
if ( i < IP_ADDR_LEN - 1 )
{
Serial.print(".");
}
}
Serial.println();
}
}
int averageAnalogRead(int pinToRead)
{
byte numberOfReadings = 8;
unsigned int runningValue = 0;
for(int x = 0 ; x < numberOfReadings ; x++)
runningValue += analogRead(pinToRead);
runningValue /= numberOfReadings;
return(runningValue);
}
//The Arduino Map function but for floats
//From: http://forum.arduino.cc/index.php?topic=3922.0
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}