The DHT library is at https://github.com/adafruit/DHT-sensor-library and CC3000 is at https://github.com/adafruit/Adafruit_CC3000_Library but be sure to follow the tutorials for both of those products first!
#include <Adafruit_CC3000.h> #include <SPI.h> #include "DHT.h" #include <avr/wdt.h>
#define ADAFRUIT_CC3000_IRQ 3 #define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_CS 10
#define DHTPIN 7 #define DHTTYPE DHT11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2);
DHT dht(DHTPIN, DHTTYPE);
#define WLAN_SSID "yourNetwork" #define WLAN_PASS "yourPass" #define WLAN_SECURITY WLAN_SEC_WPA2
#define WEBSITE "api.xively.com" #define API_key "yourAPIKey" #define feedID "yourFeedID"
Serial.println(F("\nInitializing...")); if (!cc3000.begin()) { Serial.println(F("Couldn't begin()! Check your wiring?")); while(1); } // Connect to WiFi network Serial.print(F("Connecting to WiFi network ...")); cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); Serial.println(F("done!")); // Wait for DHCP to complete Serial.println(F("Request DHCP")); while (!cc3000.checkDHCP()) { delay(100); }
uint32_t ip = 0; Serial.print(F("api.xively.com -> ")); while (ip == 0) { if (! cc3000.getHostByName("api.xively.com", &ip)) { Serial.println(F("Couldn't resolve!")); while(1){} } delay(500); } cc3000.printIPdotsRev(ip); Serial.println(F(""));
This is why we need to use the Arduino watchdog. This will basically reset the Arduino if no reset signal is received after a given delay. Here, we will initialise the watchdog with the maximum delay of 8 seconds:
wdt_enable(WDTO_8S);
float h = dht.readHumidity(); float t = dht.readTemperature(); int temperature = (int) t; int humidity = (int) h;
// JSON data String data = ""; data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ " + "{\"id\" : \"Temperature\",\"current_value\" : \"" + String(temperature) + "\"}," + "{\"id\" : \"Humidity\",\"current_value\" : \"" + String(humidity) + "\"}]}"; // Get length length = data.length();
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80); if (client.connected()) { Serial.println(F("Connected to Xively server."));
Serial.print(F("Sending headers")); client.fastrprint(F("PUT /v2/feeds/")); client.fastrprint(feedID); client.fastrprintln(F(".json HTTP/1.0")); Serial.print(F(".")); client.fastrprintln(F("Host: api.xively.com")); Serial.print(F(".")); client.fastrprint(F("X-ApiKey: ")); client.fastrprintln(API_key); Serial.print(F(".")); client.fastrprint(F("Content-Length: ")); client.println(length); Serial.print(F(".")); client.fastrprint(F("Connection: close")); Serial.println(F(" done.")); // Reset watchdog wdt_reset();
The size of a chunk is defined in the buffer_size variable. Depending on your connection speed, you might have to change this variable so the watchdog doesn't reset the sketch every time.
Serial.print(F("Sending data ...")); client.fastrprintln(F("")); sendData(client,data,buffer_size); client.fastrprintln(F("")); Serial.println(F("done."));
while (client.connected()) { while (client.available()) { char c = client.read(); Serial.print(c); } }
client.close(); Serial.println(F("Closing connection")); // Reset watchdog & disable wdt_reset(); wdt_disable();
wait(10000)