Arduino sketch

We can now dive into the home automation part of this project. The goal is to get the sensor’s data, send it via WiFi to a server (running on your computer), and display the information. The code for each part is quite long, so I will only discuss the important parts. To get the complete code, simply go to the GitHub repository of the project.

To make this part work, you will also need to install the the CC3000 MDNS library and the aREST library.

First, the Arduino sketch. You need to import the right libraries:

Download: file
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <CC3000_MDNS.h>
#include <aREST.h>
#include "DHT.h"

Then, you need to define inside the code what is specific to your configuration, like your WiFi name & password:

Download: file
#define WLAN_SSID "yourNetwork"
#define WLAN_PASS "yourPassword"
#define WLAN_SECURITY WLAN_SEC_WPA2
We can then create the CC3000 instance:
Download: file
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
 SPI_CLOCK_DIV2);

We also need to create an instance of the aREST library, which we will use to access the data on the device:

Download: file
aREST rest = aREST();

We will also create a server running on our board:

Download: file
Adafruit_CC3000_Server restServer(80);

And let’s not forget to create the instance for the DHT sensor:

Download: file
DHT dht(DHTPIN, DHTTYPE);
In the setup() part of the sketch, we need to initialize the DHT sensor:
Download: file
dht.begin();
And to connect the CC3000 to the network:
Download: file
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);

In the loop() part, we need to get data from the sensor, and then accept incoming connections to our server. The first part is simple:

Download: file
temperature = (uint8_t)dht.readTemperature();
humidity = (uint8_t)dht.readHumidity();

At this point you can already test the project. Upload the sketch to your Arduino board (make sure you changed the WiFi name & password!) and then open the Serial monitor. You should see the IP address of the board displayed. 

Then, go a web browser, and type:

192.168.1.104/temperature

You should be greeted with the following message:

{"temperature": 24, "id": "1", "name": "weather_station", "connected": true}

If you can see that, it mean you project is working! If you want to learn more about the aREST framework that we are using here, you can visit the corresponding GitHub repository:

https://github.com/marcoschwartz/aREST

This guide was first published on Sep 25, 2013. It was last updated on Sep 25, 2013. This page (Arduino sketch) was last updated on May 04, 2015.