I am having trouble with a sketch that I did when I was following the Adafruit article http://learn.adafruit.com/adafruit-cc30 ... and-xively.
The sketch runs fine, it uploads the data to Xively and produces beautiful graphs, but after a few hours the connection hangs and it stops uploading to Xively.
The only way I can recover the process is to reset the entire unit.
I have tried the CC3000 with an arduino Uno and an Arduino Mega 2560 and the problem exists on both boards.
What am I doing wrong?
Here is the code I am using on the Uno at the moment and it is having the hanging issue.
Code: Select all
/***************************************************
This is a sketch to use the CC3000 WiFi chip & Xively
Written by Marco Schwartz for Open Home Automation
****************************************************/
// Libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include<stdlib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
//onewire device setup
#define ONE_WIRE_BUS 7 //data wire is plugged into pin 7 on the Arduino
#define TEMPERATURE_PRECISION 12
OneWire oneWire(ONE_WIRE_BUS); //Setup a oneWire instance to communicate with any OneWire devices.
DallasTemperature sensors(&oneWire); //pass our oneWire reference to the Dallas Temperature.
DeviceAddress AirThermometer ={0x28, 0xB2, 0xCF, 0xCB, 0x04, 0x00, 0x00, 0x09};
DeviceAddress insidecase ={0x28, 0xB8, 0x9E, 0xCB, 0x04, 0x00, 0x00, 0x9E};
DeviceAddress Tower1Temp ={0x28, 0xF1, 0xDA, 0xCD, 0x04, 0x00, 0x00, 0x82};
DeviceAddress GrowbedTemp ={0x28, 0xAE, 0xA3, 0xCA, 0x04, 0x00, 0x00, 0x90};
DeviceAddress Tower2Temp ={0x28, 0x76, 0x50, 0xCE, 0x04, 0x00, 0x00, 0x13};
DeviceAddress FishtankTemp ={0x28, 0x21, 0x50, 0xCE, 0x04, 0x00, 0x00, 0xB8};
// Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIV2); // you can change this clock speed
// WLAN parameters
#define WLAN_SSID "XXXXX"
#define WLAN_PASS "XXXXX"
#define WLAN_SECURITY WLAN_SEC_WPA2
// Xively parameters
#define WEBSITE "api.xively.com"
#define API_key "BANNED"
#define feedID XXXXX
uint32_t ip;
void setup(void)
{
pinMode(0,OUTPUT);
// Initialize
Serial.begin(115200);
Serial.println(F("\nInitializing..."));
if (!cc3000.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}
sensors.begin();
sensors.setResolution(AirThermometer, TEMPERATURE_PRECISION);
sensors.setResolution(Tower1Temp, TEMPERATURE_PRECISION);
sensors.setResolution(insidecase, TEMPERATURE_PRECISION);
sensors.setResolution(GrowbedTemp, TEMPERATURE_PRECISION);
sensors.setResolution(Tower2Temp, TEMPERATURE_PRECISION);
sensors.setResolution(FishtankTemp, TEMPERATURE_PRECISION);
}
void loop(void)
{
// Connect to WiFi network
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
Serial.println(F("Connected!"));
digitalWrite(8, HIGH);
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100);
}
// Get the website IP & print it
ip = 0;
Serial.print(WEBSITE); Serial.print(F(" -> "));
while (ip == 0) {
if (! cc3000.getHostByName(WEBSITE, &ip)) {
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
cc3000.printIPdotsRev(ip);
// Get data & transform to integers
sensors.requestTemperatures();
float t = sensors.getTempC(AirThermometer);
float t2 = sensors.getTempC(Tower1Temp);
float t3 = sensors.getTempC(insidecase);
float t4 = sensors.getTempC(GrowbedTemp);
float t5 = sensors.getTempC(Tower2Temp);
float t6 = sensors.getTempC(FishtankTemp);
int temperature = (int) t;
int temptower1 = (int) t2;
int insidecase = (int) t3;
int GrowbedTemp = (int) t4;
int Tower2Temp = (int) t5;
int FishtankTemp = (int) t6;
// Prepare JSON for Xively & get length
int length = 0;
String data = "";
data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"AmbientTemp\",\"current_value\" : \"" + String(temperature) + "\"},"
+ "{\"id\" : \"InsideCaseTemp\",\"current_value\" : \"" + String(insidecase) + "\"},"
+ "{\"id\" : \"GrowbedTemp\",\"current_value\" : \"" + String(GrowbedTemp) + "\"},"
+ "{\"id\" : \"FishtankTemp\",\"current_value\" : \"" + String(FishtankTemp) + "\"},"
+ "{\"id\" : \"WaterTower2\",\"current_value\" : \"" + String(Tower2Temp) + "\"},"
+ "{\"id\" : \"WaterTower1\",\"current_value\" : \"" + String(temptower1) + "\"}]}";
length = data.length();
Serial.print("Data length");
Serial.println(length);
Serial.println();
// Print request for debug purposes
Serial.print("PUT /v2/feeds/");
Serial.print(feedID);
Serial.println(".json HTTP/1.0");
Serial.println("Host: api.xively.com");
Serial.print("X-ApiKey: ");
Serial.println(API_key);
Serial.print("Content-Length: ");
Serial.println(length, DEC);
Serial.print("Connection: close");
Serial.println();
Serial.print(data);
Serial.println();
// Send request
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
if (client.connected()) {
Serial.println("Connected!");
client.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.0");
client.println("Host: api.xively.com");
client.println("X-ApiKey: " + String(API_key));
client.println("Content-Length: " + String(length));
client.print("Connection: close");
client.println();
client.print(data);
client.println();
} else {
Serial.println(F("Connection failed"));
return;
}
Serial.println(F("-------------------------------------"));
while (client.connected()) {
while (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.close();
Serial.println(F("-------------------------------------"));
Serial.println(F("Transmission complete, Waiting 60 seconds till next transmission"));
cc3000.disconnect();
digitalWrite(8, LOW);
// Wait 60 seconds until next update
delay(60000);
}