Hey,
This has been bothering me for a while. I've successfully learned to push data to data.sparkfun.com using my Arduino Uno r3, CC3000 wifi shield and a DHT22 sensor.
It works great for a limited time, about 1-3hours until it finally freezes.
Is there any easy way of forcing a restart of the programme if it ends up freezing? I looked at using the watchdog but it seems its limited to <8s laps.
Might be I have plenty of bugs in the code which causes the freeze or even inside the DHT library I pulled from Adafruit's github. But, how can I solve this in the easiest way possible for a beginner like me?
/ Seb
https://data.sparkfun.com/streams/bGngMoGlRvilpooY81W9
/////////////////////////////////////////////////////////////////////////
//////////////////////////// CODE TO CONNECT TO SPARKFUN /////////////////
//////////////////////////////PUSHES DATA TO SERVER////////////////////////
/// https://data.sparkfun.com/streams/bGngMoGlRvilpooY81W9
// ****************************************************/
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "utility/debug.h"
#include "utility/socket.h"
// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT 5 //Along with the SPI interface, there is a power-enable type pin called VBAT_EN which we use to start the module properly
#define ADAFRUIT_CC3000_CS 10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed
#define WLAN_SSID "XX" // cannot be longer than 32 characters!
#define WLAN_PASS "XX"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
#define LISTEN_PORT 23 // What TCP port to listen on for connections.
/////////////////////////////////////////////////////////////////////////
//////////////////////////// CODE TO CONNECT TO SPARKFUN /////////////////
/////////////////////////////////////////////////////////////////////////
Adafruit_CC3000_Client client;
char server[] = "data.sparkfun.com"; // name address for data.sparkFun (using DNS)
/////////////////
// Phant Stuff //
/////////////////
const String publicKey = "bGngMoGlRvilpooY81W9";
const String privateKey = "XX";
const byte NUM_FIELDS = 2; // Define how many data fields to push
const String fieldNames[NUM_FIELDS] = {"temp", "rh"}; //Must be the same as specified at data.sparkfun
String fieldData[NUM_FIELDS];
// Define correct string position and name. MUST BE SAME AS cont STRING fieldNames....
#define temp 0
#define rh 1
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
//////DEFINE DATA FOR SENSORS ////////////////////////
//////////////////////////////////////////////////////
#include "DHT.h"
#define DHTPIN 8 // what digital pin used to read DHT22
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
void setup(void)
{
Serial.begin(9600);
Serial.println(F("Hello, CC3000!\n"));
Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
/* Initialise the module */
Serial.println(F("\nInitializing..."));
if (!cc3000.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while (1);
}
Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while (1);
}
Serial.println(F("Connected!"));
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
/* Display the IP address DNS, Gateway, etc. */
while (! displayConnectionDetails()) {
delay(1000);
}
}
void loop(void) {
/////////////////// READ THE SENSORS//////////////////////////////////////////////////
float h = dht.readHumidity();
float t = dht.readTemperature();
/////////////////// ADD SENSOR VALUES INTO STRING//////////////////////////////////////////////////
fieldData[0] = String(t);
fieldData[1] = String(h);
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
///////////////// POST TO SPARKFUN//////////////////
postData();
delay(30000);
////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////// CODE TO CONNECT TO SPARKFUN AND PUSHES THE DATA /////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
void postData()
{
// Make a TCP connection to remote host
if (client.connect(server, 80))
{
// Post the data! Request should look a little something like:
// GET /input/publicKey?private_key=privateKey&light=1024&switch=0&name=Jim 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();
}
else
{
Serial.println(F("Connection failed"));
}
// Check for a response from the server, and route it
// out the serial port.
while (client.connected())
{
if ( client.available() )
{
char c = client.read();
Serial.print(c);
}
}
Serial.println();
client.stop();
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/**************************************************************************/
/*!
@brief Tries to read the IP address and other connection details
*/
/**************************************************************************/
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if (!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else
{
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}