14
Make a WiFi Weather Station With Arduino and the CC3000 chip 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-pow Written By: Feitan Make a WiFi Weather Station With Arduino and the CC3000 chip © 2017 www.botsbits.org Page 1 of 14

Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

  • Upload
    others

  • View
    14

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Make a WiFi Weather Station With Arduino andthe CC3000 chip

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-pow

Written By: Feitan

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 1 of 14

Page 2: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

TOOLS:WAMP/MAMP (1)

Arduino IDE (1)

Adafruit's CC3000 library (1)To install a library, just download the folders, andput them into your /Arduino/libraries/ folder.

DHT sensor library (1)To install a library, just download the folders, andput them into your /Arduino/libraries/ folder.

PARTS:Arduino Uno (1)

Adafruit CC3000 breakout board (1)

DHT22 or DHT11 sensor (1)

10K Ohm resistor (1)

Breadboard (1)

some jumper wires (1)

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 2 of 14

Page 3: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 1 — Make a WiFi Weather Station With Arduino and the CC3000 chip

The hardware configuration for thisproject is actually not thatcomplicated, thanks to the goodinformations that you will find on theCC3000 breakout board. Connectthe IRQ pin of the CC3000 board topin number 3 of the Arduino board,VBAT to pin 5, and CS to pin 10.

Then, you need to connect the SPIpins to the Arduino board: MOSI,MISO, and CLK go to pins 11,12,and 13, respectively. Finally, takecare of the power supply: Vin goesto the Arduino 5V, and GND to GND.

The DHT sensor is easier toconnect: just plug the pin number 1to the Arduino’s 5V, pin number 4 toGND, and pin number 2 to Arduinopin 7. Finally, put the 10K resistorbetween the sensor pins number 1and 2.

The picture summarizes thehardware connections.

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 3 of 14

Page 4: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 2

As this tutorial is really about theCC3000 chip, I don’t want to spendtoo much time on the DHT sensor.You will find a good test sketchinside the library, and I also includeda test sketch inside the project’sGitHub repository.

To see if your WiFi chip is correctlywired and operational, I recommendto use the test sketches that comewith the Adafruit library. I used forexample the one called WebClient.Just open it from the library’s folder,and save it to a new file (you need tobe able to modify the sketch to enteryour WiFi network name &password).

Then, modify the sketch with thecorrect data for you WiFi network,and upload to the board. You canthen open your serial monitor, and ifeverything was wired correctly (andyour Internet connection is working!)you should see your Arduinoconnecting to the web, thenconnecting to the Adafruit’s website,and printing some information.

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 4 of 14

Page 5: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 3

We can now dive into the homeautomation part of this project. Thegoal is to get the sensor’s data, sendit via WiFi to a server (running onyour computer), and display theinformation. The code for each partis quite long, so I will only discussthe important parts. To get thecomplete code, simply go to theGitHub repository of the project.

First, the Arduino sketch. If you hada look at the example file fromAdafruit, you should already have anidea of what are the important partsof the sketch. Of course, you needto import the right libraries:

#include <Adafruit_CC3000.h>

#include <ccspi.h>

#include <SPI.h>

#include <string.h>

#include "utility/debug.h"

#include "DHT.h"

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 5 of 14

Page 6: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 4

#include <stdlib.h>

Then, you need to define inside thecode what is specific to yourconfiguration: WiFi name &password, IP address of yourcomputer, and port of your server(usually 80):

#define WLAN_SSID "yourNetwork"

#define WLAN_PASS"yourPassword"

#define WLAN_SECURITYWLAN_SEC_WPA2

uint32_t ip =cc3000.IP2U32(192,168,1,2);

int port = 80;

We can then create the CC3000instance:

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 6 of 14

Page 7: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 5

Adafruit_CC3000 cc3000 =Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ,ADAFRUIT_CC3000_VBAT,

SPI_CLOCK_DIV2);

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

DHT dht(DHTPIN, DHTTYPE);

In the setup() part of the sketch, weneed to initialize the DHT sensor:

dht.begin();

And to connect the CC3000 to thenetwork:

cc3000.connectToAP(WLAN_SSID,WLAN_PASS, WLAN_SECURITY);

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 7 of 14

Page 8: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 6

In the loop() part, we need to getdata from the sensor, and to send itto the server by doing a GETrequest. The first part is reallysimple:

float h = dht.readHumidity();

float t = dht.readTemperature();

You just need to take care ofconverting these floats to Strings,because you want to insert thisinformation into an HTTP request. Inthis case, we just convert the floatsto integers, and then transform themto strings:

String temperature = String((int) t);

String humidity = String((int) h);

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 8 of 14

Page 9: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 7

Finally, I send the request with thissend_request() function. Thisfunction basically converts theString of the request to an array ofchars (for speed reasons), andconnect to the server with:

Adafruit_CC3000_Client client =cc3000.connectTCP(ip, port);

And then send the request with thisline:

client.fastrprintln(requestBuf);

Finally, it reads out the answer(which we don’t use in this project,but you could perfectly get data backfrom the server):

while (client.available()) {

// Read answer

char c = client.read(); }

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 9 of 14

Page 10: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 8

We then wait 10 seconds betweeneach temperature & humidity update.Once again, to get the completecode, just go to the GitHubrepository of the project.

It’s now time to write the server part.As usual, I used a combination ofHTML, JavaScript, and PHP. ThePHP part will receive the data fromthe board at each GET request, andsave it into files:

<?php

// Store data

if ($_GET["temp"] && $_GET["hum"]){

$myFile = "temp_data.txt";

$fh = fopen($myFile, 'w');

fwrite($fh, $_GET["temp"]);

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 10 of 14

Page 11: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 9

fclose($fh);

$myFile = "hum_data.txt";

$fh = fopen($myFile, 'w');

fwrite($fh, $_GET["hum"]);

fclose($fh);

} ?>

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 11 of 14

Page 12: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 10

Of course, you could use moreelaborate ways to save the data likeXML or SQL but I wanted to keepthings simple. Then, I used two otherPHP scripts to actually display thedata on the main HTML page. This isfor example the one for thetemperature:

<?php

$myFile = "temp_data.txt";

$fh = fopen($myFile, 'r');

$line = fgets($fh);

fclose($fh);

echo $line; ?>

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 12 of 14

Page 13: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

Step 11

Finally, the main page just organizesall this information in two separateblocks. This is for example one ofthem:

<div class="data">

<div class="dataTitle">Temperature:</div>

<divid="temperatureDisplay">Waiting fordata ...</div>

</div>

Step 12

I also used a simple JavaScript partto update the display every second,just where we need it:

setInterval(function()

{$("#temperatureDisplay").load('temperature_display.php');

$("#humidityDisplay").load('humidity_display.php'); }, 1000);

It is based on jQuery, which is alsoincluded in the GitHub repository ofthe project. I also put some CSS inthe page so it doesn’t look to ugly, Ilet you see the details in therepository.

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 13 of 14

Page 14: Make a WiFi Weather Station With Arduino and the CC3000 chip · 2017-06-26 · Make a WiFi Weather Station With Arduino and the CC3000 chip As open-source hardware users and makers,

This document was last generated on 2017-06-26 12:14:43 PM.

Step 13

That’s all for the software part, so itis time to test it out! Upload thesketch to the Arduino board, openthe serial monitor to check thateverything is working correctly, andalso open the html page inside yourfavorite browser.

What will appear should besomething similar with the picture.

You can play with the sensor to seethe data being automatically updatedon the page. Congratulations, youjust built a simple WiFi-connectedweather station!

I used temperature & humidity as the“data” in this tutorial, but it can beanything you want: data from amotion detector, door/windowcontact … You can also modify theserver part so that it sends someinformation back to the board, forexample to control a relay.

Make a WiFi Weather Station With Arduino and the CC3000 chip

© 2017 www.botsbits.org Page 14 of 14