Software: Arduino 1.0.5
As open-source hardware users and makers, we love playing with new chips, boards and tools. And there is one chip which is quite popular these days: the CC3000 WiFi chip from TI. This chip comes with many promises: cheap (around $10), easy to use, low-power … It was featured in many articles around the web, but somehow it was quite hard to use with Arduino as there was no breakout board or library available. Luckily, Adafruit solved that for us with a nice breakout board and a working library for Arduino. In this article, I will show you how to use this chip for home automation purposes. Remember tha tweather station project? We are going to do the same: measure the temperature and the humidity. But this time we won’t display the information on an LCD screen. Instead, we will transmit the data wirelessly via WiFi to your computer and display it there. Excited ? Let’s get started!
Hardware requirements
The whole project is based on the Arduino platform, so of course you will need an Arduino board. I really recommend using the Arduino Uno board for this project, as it is the only board that is currently compatible with the CC3000 library at the time this article was written.
Then, you need the famous CC3000 chip. There are also many alternatives to do that. What I recommend is using the Adafruit CC3000 breakout board, which is the only one I tested that worked without problem. It is nice and compact, has voltage regulators onboard, as well as an onboard antenna. I tried the official TI CC3000 board, but it never worked properly, and you have the use level shifters as well (the CC3000 works with 3.3V, and the Arduino Uno with 5V). The other alternative is to make your own breakout board, there are many PCB layout available online.
You also need a DHT temperature & humidity sensor. I used the DHT11 sensor, but this project would also work fine with a DHT22, you would just have one line of code to change. You will 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.
- Arduino Uno
- Adafruit CC3000 breakout board
- DHT11 sensor
- 10K Ohm resistor
- Breadboard and some jumper wires
Software requirements
For this project, you need the usual Arduino IDE. You will also need the Adafruit’s CC3000 library, as well as the DHT sensor library. To install a library, just download the folders, and put them into your /Arduino/libraries/ folder.
You will also need a web server running on your computer. There are thousands of tutorial for that, but I recommend using MAMP if you are on OS X.
Hardware configuration
The hardware configuration for this project is actually not that complicated, thanks to the good informations that you will find on the CC3000 breakout board. 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 to the Arduino board: MOSI, MISO, and CLK go to pins 11,12, and 13, respectively. Finally, take care of the power supply: Vin goes to the Arduino 5V, and GND to GND.
The DHT sensor is easier 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.
Testing the WiFi chip
As this tutorial is really about the CC3000 chip, I don’t want to spend too much time on the DHT sensor. You will find a good test sketch inside the library, and I also included a test sketch inside the project’s GitHub repository.
To see if your WiFi chip is correctly wired and operational, I recommend to use the test sketches that come with the Adafruit library. I used for example the one called WebClient. Just open it from the library’s folder, and save it to a new file (you need to be able to modify the sketch to enter your WiFi network name & password). Then, modify the sketch with the correct data for you WiFi network, and upload to the board. You can then open your serial monitor, and if everything was wired correctly (and your Internet connection is working!) you should see your Arduino connecting to the web, then connecting to the Adafruit’s website, and printing some information.
Putting it all together
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.
First, the Arduino sketch. If you had a look at the example file from Adafruit, you should already have an idea of what are the important parts of the sketch. Of course, you need to import the right libraries:
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include "DHT.h"
#include<stdlib.h>
Then, you need to define inside the code what is specific to your configuration: WiFi name & password, IP address of your computer, and port of your server (usually 80):
1
2
3
4
5
6
7
|
#define WLAN_SSID "yourNetwork" #define WLAN_PASS "yourPassword" #define WLAN_SECURITY WLAN_SEC_WPA2 uint32_t ip = cc3000.IP2U32(192,168,1,2); int port = 80; |
We can then create the CC3000 instance:
And let’s not forget to create the instance for the DHT sensor:
1
|
DHT dht(DHTPIN, DHTTYPE); |
In the setup() part of the sketch, we need to initialize the DHT sensor:
1
|
dht.begin(); |
And to connect the CC3000 to the network:
1
|
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); |
In the loop() part, we need to get data from the sensor, and to send it to the server by doing a GET request. The first part is really simple:
1
2
|
<span style= "font-family: Titillium, Helvetica, Arial, Verdana, sans-serif;" > float h = dht.readHumidity();</span> float t = dht.readTemperature(); |
You just need to take care of converting these floats to Strings, because you want to insert this information into an HTTP request. I wrote a simple function for that, you can look at the details in the code:
1
2
|
<span style= "font-family: Titillium, Helvetica, Arial, Verdana, sans-serif;" >String temperature = floatToString(t);</span> String humidity = floatToString(h); |
Finally, I send the request with this send_request() function. This function basically converts the String of the request to an array of chars (for speed reasons), and connect to the server with:
1
|
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port); |
And then send the request with this line:
1
|
client.fastrprintln(requestBuf); |
Finally, it reads out the answer (which we don’t use in this project, but you could perfectly get data back from the server):
1
2
3
4
5
|
<span style= "font-family: Titillium, Helvetica, Arial, Verdana, sans-serif;" > while (client.available()) {</span> // Read answer char c = client.read(); } |
We then wait 10 seconds between each temperature & humidity update. Once again, to get the complete code, just go to the GitHub repository of the project.
It’s now time to write the server part. As usual, I used a combination of HTML, JavaScript, and PHP. The PHP part will receive the data from the board at each GET request, and save it into files:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php // Store data if ( $_GET [ "temp" ] && $_GET [ "hum" ]) { $myFile = "temp_data.txt" ; $fh = fopen ( $myFile , 'w' ); fwrite( $fh , $_GET [ "temp" ]); fclose( $fh ); $myFile = "hum_data.txt" ; $fh = fopen ( $myFile , 'w' ); fwrite( $fh , $_GET [ "hum" ]); fclose( $fh ); } ?>; |
Of course, you could use more elaborate ways to save the data like XML or SQL but I wanted to keep things simple. Then, I used two other PHP scripts to actually display the data on the main HTML page. This is for example the one for the temperature:
1
2
3
4
5
6
7
|
<!--?php $myFile = "temp_data.txt" ; $fh = fopen ( $myFile , 'r' ); $line = fgets ( $fh ); fclose( $fh ); echo $line ; ?--> |
Finally, the main page just organizes all this information in two separate blocks. This is for example one of them:
1
2
3
4
|
< div class = "data" > < div class = "dataTitle" >Temperature: </ div > < div id = "temperatureDisplay" >Waiting for data ...</ div > </ div > |
I also used a simple JavaScript part to update the display every second, just where we need it:
1
2
3
4
5
|
setInterval( function () { $( "#temperatureDisplay" ).load( 'temperature_display.php' ); $( "#humidityDisplay" ).load( 'humidity_display.php' ); }, 1000); |
It is based on jQuery, which is also included in the GitHub repository of the project. I also put some CSS in the page so it doesn’t look to ugly, I let you see the details in the repository.
That’s all for the software part, so it is time to test it out! Upload the sketch to the Arduino board, open the serial monitor to check that everything is working correctly, and also open the html page inside your favorite browser. Wait a bit so that the board can send the first set of data, and this is what you should see:
You can play with the sensor to see the data being automatically updated on the page. Congratulations, you just built a simple WiFi-connected weather station! I used temperature & humidity as the “data” in this tutorial, but it can be anything you want: data from a motion detector, door/window contact … You can also modify the server part so that it sends some information back to the board, for example to control a relay.
As always, please comment below if you have any question, a suggestion on how to improve the article, or if you find some nasty bugs in the code! Finally, this is the list of the parts that were used in this project:
Leave a Reply
You must be logged in to post a comment.