In this guide, we are going to see how to connect a temperature & humidity sensor to an online platform for connected objects, Xively. The sensor will be connected to an Arduino Uno board, which will also communicate with the Adafruit CC3000 breakout board for the WiFi connectivity. But instead of communicating with a local server, the CC3000 chip will communicate directly with the Xively server and send the data over there. At the end, you will be able to monitor the data sent by the server directly from your browser, wherever you are in the world, just by logging into the Xively website.
In this guide, we are going to see how to connect a temperature & humidity sensor to an online platform for connected objects, Xively. The sensor will be connected to an Arduino Uno board, which will also communicate with the Adafruit CC3000 breakout board for the WiFi connectivity. But instead of communicating with a local server, the CC3000 chip will communicate directly with the Xively server and send the data over there. At the end, you will be able to monitor the data sent by the server directly from your browser, wherever you are in the world, just by logging into the Xively website.
Setting up your Xively account
Connections
Then, you need the Adafruit CC3000 breakout board to make the WiFi communication, and the DHT11 temperature & humidity sensor (you can also use the DHT22 or the AM2302 sensors which are almost identical to wire up but higher quality). You also need a 10K Ohm resistor to be used with the DHT sensor.
Finally, you need a breadboard and some jumper wires to make the connections between the different parts.
DHT11 sensor
The DHT sensor is quite easy to connect: just plug the pin number 1 to the Arduino’s 5V, pin number 4 to GND, and pin number 2 to Arduino pin 7. Finally, put the 10K resistor between the sensor pins number 1 and 2.
CC3000 Breakout Board
The hardware configuration of the CC3000 breakout board is relatively easy. Connect the IRQ pin of the CC3000 board to pin number 3 of the Arduino board, VBAT to pin 5, and CS to pin 10.Then, you need to connect the SPI pins of the board to the corresponding pins on the Arduino board: MOSI, MISO, and CLK go to pins 11,12, and 13, respectively.
Finally, you have to take care of the power supply: Vin goes to the Arduino 5V, and GND to GND.
Arduino sketch
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)
Using Xively
Initializing WiFi chip... Connecting to WiFi network ...done! Request DHCP Checking WiFi connection ...done. Pinging Xively server ...done. Connected to Xively server. Sending headers.... done. Sending data...
HTTP/1.1 200 OK Date: Mon, 14 Oct 2013 17:44:20 GMT Content-Type: application/json; charset=utf-8 Content-Length: 0 Connection: close X-Request-Id: 55c792d07f4a679dfb8a1a09141264d7c98eea1e Cache-Control: max-age=0 Vary: Accept-Encoding
This guide was first published on Oct 15, 2013. It was last updated on Oct 15, 2013.