The first step in this project is really to send data to the web so it is stored online. For this, we'll use a service called dweet.io. You can check it out at https://dweet.io/.
This is the main welcome page:
This is the complete Arduino code for this project:
// Libraries #include <Adafruit_CC3000.h> #include <SPI.h> #include "DHT.h" #include <avr/wdt.h> // Define CC3000 chip pins #define ADAFRUIT_CC3000_IRQ 3 #define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_CS 10 // DHT sensor #define DHTPIN 6 #define DHTTYPE DHT11 // 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 // DHT instance DHT dht(DHTPIN, DHTTYPE); // WLAN parameters #define WLAN_SSID "yourWiFiSSID" #define WLAN_PASS "yourWiFiPassword" #define WLAN_SECURITY WLAN_SEC_WPA2 // Dweet parameters #define thing_name "mySecretThing" // Variables to be sent int temperature; int humidity; int light; int motion; uint32_t ip; void setup(void) { // Initialize Serial.begin(115200); Serial.println(F(" Initializing...")); 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); } // Start watchdog wdt_enable(WDTO_8S); } void loop(void) { // Measure from DHT float t = dht.readTemperature(); float h = dht.readHumidity(); temperature = (int)t; humidity = (int)h; // Measure light level float sensor_reading = analogRead(A0); light = (int)(sensor_reading/1024*100); // Get motion sensor reading motion = digitalRead(7); Serial.println(F("Measurements done")); // Reset watchdog wdt_reset(); // Get IP uint32_t ip = 0; Serial.print(F("www.dweet.io -> ")); while (ip == 0) { if (! cc3000.getHostByName("www.dweet.io", &ip)) { Serial.println(F("Couldn't resolve!")); } delay(500); } cc3000.printIPdotsRev(ip); Serial.println(F("")); // Reset watchdog wdt_reset(); // Check connection to WiFi Serial.print(F("Checking WiFi connection ...")); if(!cc3000.checkConnected()){while(1){}} Serial.println(F("done.")); wdt_reset(); // Send request Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80); if (client.connected()) { Serial.print(F("Sending request... ")); client.fastrprint(F("GET /dweet/for/")); client.print(thing_name); client.fastrprint(F("?temperature=")); client.print(temperature); client.fastrprint(F("&humidity=")); client.print(humidity); client.fastrprint(F("&light=")); client.print(light); client.fastrprint(F("&motion=")); client.print(motion); client.fastrprintln(F(" HTTP/1.1")); client.fastrprintln(F("Host: dweet.io")); client.fastrprintln(F("Connection: close")); client.fastrprintln(F("")); Serial.println(F("done.")); } else { Serial.println(F("Connection failed")); return; } // Reset watchdog wdt_reset(); Serial.println(F("Reading answer...")); while (client.connected()) { while (client.available()) { char c = client.read(); Serial.print(c); } } Serial.println(F("")); // Reset watchdog wdt_reset(); // Close connection and disconnect client.close(); Serial.println(F("Closing connection")); Serial.println(F("")); // Reset watchdog & disable wdt_reset(); }
Now let's look at the most important parts of the code. First, we need to include the required libraries, such as the CC3000 library and the DHT library:
#include <Adafruit_CC3000.h> #include <SPI.h> #include "DHT.h" #include <avr/wdt.h>
Then, we define which pin the DHT11 sensor is connected to:
#define DHTPIN 6 #define DHTTYPE DHT11
You also need to enter your Wi-Fi name and password:
#define WLAN_SSID "yourWiFiSSID" #define WLAN_PASS "yourWiFiPassword" #define WLAN_SECURITY WLAN_SEC_WPA2
Then, you can define a name for your thing that is, the virtual object that will store the data online:
#define thing_name "mySecretThing"
In the setup()
function of the sketch, we initialize the CC3000 chip:
if (!cc3000.begin()) { Serial.println(F("Couldn't begin()! Check your wiring?")); while(1); }
We also connect to the Wi-Fi network:
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
Finally, we initialize the watchdog to 8
seconds. This will basically reset the Arduino automatically if we don't refresh it before this delay. It is basically here to prevent the project from getting stuck:
wdt_enable(WDTO_8S);
In the loop()
function of the sketch, we first measure data from the DHT sensor:
float t = dht.readTemperature(); float h = dht.readHumidity(); temperature = (int)t; humidity = (int)h;
After that, we measure the ambient light level:
float sensor_reading = analogRead(A0); light = (int)(sensor_reading/1024*100);
And finally we get the status of the motion sensor:
motion = digitalRead(7);
Then, we try to get the IP address of the dweet.io website:
while (ip == 0) { if (! cc3000.getHostByName("www.dweet.io", &ip)) { Serial.println(F("Couldn't resolve!")); } delay(500); }
Then, we connect the project to this IP address:
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
We can now send the data, following the format given by dweet.io:
client.fastrprint(F("GET /dweet/for/")); client.print(thing_name); client.fastrprint(F("?temperature=")); client.print(temperature); client.fastrprint(F("&humidity=")); client.print(humidity); client.fastrprint(F("&light=")); client.print(light); client.fastrprint(F("&motion=")); client.print(motion); client.fastrprintln(F(" HTTP/1.1"));
After that, we read the answer from the server:
while (client.connected()) { while (client.available()) { char c = client.read(); Serial.print(c); } }
And we close the connection:
client.close();
It's now time to test the project! Make sure you grab all the code, copy it inside the IDE, and change the Wi-Fi details and thing name. Then, upload the code to the board and open the Serial monitor:
You should see that the project is sending measurements to dweet.io and then getting an answer. The most important part is the one indicating that data was recorded:
{"this":"succeeded","by":"dweeting","the":"dweet","with":{"thing":"mySecretThing","created":"2015-09-03T09:38:07.051Z","content":{"temperature":28,"humidity":32,"light":87,"motion":0}}}
You can also check online to make sure data was recorded:
Now that we are sure that data is being recorded, we can move to the next step: spying on this data remotely from any web browser!
18.188.115.155