My problem is that it seems like it works, and it will send the data successfully once (sometimes 2 or 3 times, and once I got 6 in a row, but haven't gotten close to that again) then it hangs after connecting the next time... and it seems to stop where it connects to send the data to Xively. It doesn't ever fail, it's like the sketch just locks up because it doesn't know what to do, and it won't do anything past that point. I suspect it's just having trouble either sending or receiving data, but rather than having some means of failing, it just hangs the sketch.
Ultimately I'm trying to build a temperature controller that will connect to some relays to turn on heaters, so it's important that the code not just "lock up" at any point because that could result in a dangerous situation if the relays are left on. I have a sketch that already works fine for the relay control using the data logger shield (which logs the time and temperature for me), but in this one I'm just trying to send the temperature data to xively so I can record it (one piece of the puzzle at a time)... but the behavior I'm getting now worries me a great deal.
Here's my code:
Code: Select all
/***************************************************
* This is a sketch to use the CC3000 WiFi chip & Xively
*
* Written by Marco Schwartz for Open Home Automation
****************************************************/
// Libraries
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include<stdlib.h>
// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
// 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 "Easter.wifi"
#define WLAN_PASS "drowssap"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
// Xively parameters
#define WEBSITE "api.xively.com"
#define API_key "Sn9BFZGkmsPwPhC0elE7L0yiLXZX9QZP6DZTSKpkFEfIDxO8"
#define feedID "1587261525"
uint32_t ip;
// Data wire is plugged into port 6 on the Arduino
#define ONE_WIRE_BUS 6
#define TEMPERATURE_PRECISION 12
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
// Initialize
Serial.begin(115200);
Serial.println(F("\nInitializing..."));
if (!cc3000.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}
}
void loop(void)
{
// Connect to WiFi network
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
Serial.println(F("Connected!"));
/* 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);
Serial.println();
sensors.requestTemperatures();
// Get data & transform to string value
float t = DallasTemperature::toFahrenheit(sensors.getTempCByIndex(0));
float t2 = DallasTemperature::toFahrenheit(sensors.getTempCByIndex(1));
char tmp[10];
char tmp2[10];
dtostrf(t,5,2,tmp);
dtostrf(t2,5,2,tmp2);
String temp = tmp;
String temp2 = tmp2;
// Prepare JSON for Xively & get length
int length = 0;
String data = "";
data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Henry\",\"current_value\" : \"" + temp + "\"}," + "{\"id\" : \"Webster\",\"current_value\" : \"" + temp2 + "\"}]}";
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();
Serial.println("Data Sent!");
}
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("\n\nDisconnecting"));
cc3000.disconnect();
// Wait 10 seconds until next update
delay(10000);
}
Code: Select all
Initializing...
Started AP/SSID scan
Connecting to Easter.wifi...Waiting to connect...Connected!
Request DHCP
api.xively.com -> 216.52.233.120
Data length129
PUT /v2/feeds/1587261525.json HTTP/1.0
Host: api.xively.com
X-ApiKey: Sn9BFZGkmsPwPhC0elE7L0yiLXZX9QZP6DZTSKpkFEfIDxO8
Content-Length: 129
Connection: close
{"version":"1.0.0","datastreams" : [ {"id" : "Top","current_value" : "73.29"},{"id" : "Bottom","current_value" : "72.84"}]}
Connect to 216.52.233.120:80
Connected!
Data Sent!
-------------------------------------
HTTP/1.1 200 OK
Date: Fri, 10 Jan 2014 03:33:09 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 0
Connection: close
X-Request-Id: b50d2d5238abc230eb3a93e2b088f28b1a1b611a
Cache-Control: max-age=0
Vary: Accept-Encoding
-------------------------------------
Disconnecting
Started AP/SSID scan
Connecting to Easter.wifi...Waiting to connect...Connected!
Request DHCP
api.xively.com -> 216.52.233.120
Data length127
PUT /v2/feeds/1587261525.json HTTP/1.0
Host: api.xively.com
X-ApiKey: Sn9BFZGkmsPwPhC0elE7L0yiLXZX9QZP6DZTSKpkFEfIDxO8
Content-Length: 127
Connection: close
{"version":"1.0.0","datastreams" : [ {"id" : "Top","current_value" : "73.18"},{"id" : "Bottom","current_value" : "72.84"}]}
Connect to 216.52.233.120:80
Connected!
I wrote all of this post up last night, and then just as I was about to post it, I thought to add some watchdog timers to my code to see if that could help it from hanging, which has resulted in it "running" all night reading temperatures (because the arduino now resets every couple of minutes thanks to the watchdog), but it's not really in a way that would make it very functional for controlling my relays - I guess what I am asking is if there a better solution for handling what I'm experiencing?