12
Internet of Things with Arduino and the CC3000 WiFi chip 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 boar Written By: Feitan Internet of Things with Arduino and the CC3000 WiFi chip © 2017 www.botsbits.org Page 1 of 12

Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

Internet of Things with Arduino and the CC3000WiFi chip

In this guide, we are going to see how to connect a temperature & humidity sensor to an onlineplatform for connected objects, Xively. The sensor will be connected to an Arduino Uno board,

which will also communicate with the Adafruit CC3000 breakout boar

Written By: Feitan

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 1 of 12

Page 2: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

INTRODUCTION

The CC3000 WiFi chip from Texas Instrument is a quite versatile chip that can be used to connectyour projects to the web. However, connecting your Arduino project to a web server can be tricky:you need to know how to install & configure a web server, and know a bit about HTML & PHP.Luckily, there are other solutions to make things easier.

TOOLS:Breadboard (1)

Arduino IDE (1)

CC3000 library (1)Make sure that the libraries are correctly placed inyour /libraries folder inside your main Arduinofolder

DHT library (1)Make sure that the libraries are correctly placed inyour /libraries folder inside your main Arduinofolder

PARTS:Arduino Uno R3 or Arduino Mega or

Arduino Due (1)Adafruit CC3000 breakout board (1)

DHT22 or DHT11 sensor (1)

10K Ohm resistor (1)

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 2 of 12

Page 3: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

Step 1 — Internet of Things with Arduino and the CC3000 WiFi chip

The DHT sensor is quite easy to connect: just plug the pin number 1 to the Arduino’s 5V, pinnumber 4 to GND, and pin number 2 to Arduino pin 7. Finally, put the 10K resistor between thesensor pins number 1 and 2.

The hardware configuration of the CC3000 breakout board is relatively easy. Connect the IRQ pinof 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 Arduinoboard: MOSI, MISO, and CLK go to pins 11,12, and 13, respectively. Finally, you have to take careof the power supply: Vin goes to the Arduino 5V, and GND to GND.

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 3 of 12

Page 4: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

Step 2

The following picture summarizesthe hardware connections.

Step 3

Create your Xively account

The first step is to create your Xively account, and to configure it for this project. It is all done viatheir graphical interface, so there is no need to code anything in this part. First, you need to createan account. Just click on “Get started”

And then on “Sign up here”

You can now enter your personal informations.

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 4 of 12

Page 5: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

Step 4

Now that your account is created, you can create a device to track some data. First, click on“Develop”.

And on “Add Device”

It’s now time to enter some information about the device

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 5 of 12

Page 6: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

Step 5

Finally, you should arrive to this page corresponding to your device. What is important on this pageis the Feed ID that identify your device, and the API key that identify your account. Please notethese two values, you will need them later. Now, we need to create “Channels” to track some data.Click on “Add Channel” to create the first one.

Now enter the details about this first channel for the Temperature. The name of the channels areimportant, we will use them in the Arduino sketch.

You can now do the same for the Humidity channel. At the end, you will end up with this on yourdevice page

Your Xively account is now ready to receive some data coming from your Arduino board. Let’s nowfocus on how to connect your Arduino board, the DHT 11 sensor, and the CC3000 chip.

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 6 of 12

Page 7: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

Step 6

The Arduino sketch for this projectconsists in initializing the DHT 11sensor & the CC3000 WiFi chip, andthen in running a loop that measuresthe temperature & humidity, senddata to the Xively server, and repeatitself every 10 seconds.

You can find detailed informationabout how to connect the CC3000chip to your WiFi network in othertutorials, like our WiFi weatherstation tutorial. In this article, I willfocus on the parts that concernsXively. It starts by defining yourfeedID and API key:

#define WEBSITE "api.xively.com"

#define API_key "yourAPIKey"

#define feedID "yourFeedID"

Then, at every iteration of the loop,we need to connect to the network,and measure the temperature:

float h = dht.readHumidity();

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 7 of 12

Page 8: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

Step 7

float t = dht.readTemperature();

int temperature = (int) t;

int humidity = (int) h;

This is now the time to format thedata for the Xively website. Thereare two things to do here: first, weneed to format the data into a Stringvariable that will contain theinformation in the JSON format. Itmight seem complicated, but theXively website has many tutorials toformat your data correctly.

We also need to know the length (innumber of characters) of the data sowe can put it in the HTTP request.

int length = 0;String data = "";

data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\": [ {\"id\" :\"Temperature\",\"current_value\" :\"" + String(temperature) + "\"},"

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 8 of 12

Page 9: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

Step 8

+ "{\"id\" :\"Humidity\",\"current_value\" : \"" +String(humidity) + "\"}]}";

length = data.length();

When the client is connected, wecan send the request to the server.This is a typical HTTP PUT request,where we specify the feedID, theAPI key, and send the data at theend of the request.

if (client.connected()) {

Serial.println("Connected!");

client.println("PUT /v2/feeds/" +String(feedID) + ".json HTTP/1.0");

client.println("Host: api.xively.com");

client.println("X-ApiKey: " +String(API_key));

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 9 of 12

Page 10: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

Step 9

client.println("Content-Length: " +String(length));

client.print("Connection: close");

client.println();

client.print(data);

client.println();}

Because the temperature & humidityare slow-changing values, we canread these values & send them overto Xively every 10 seconds only.This is done by using a delay:

delay(10000);

Finally, the complete code can befound in the GitHub repository of thisproject.

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 10 of 12

Page 11: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

Step 10

Using Xively

Now that the Arduino sketch iscompleted, you can upload thesketch to your Arduino board, andtest the project. Make sure that thepage corresponding to your deviceon Xively is opened. You can nowopen your Serial Monitor and youshould see the Arduino connecting toyour network, preparing the request,and connecting to the Xivelywebsite.

If everything goes well, you shouldsee this response from the Xivelyserver:

HTTP/1.1 200 OK

Date: Mon, 14 Oct 2013 17:44:20GMT

Content-Type: application/json;charset=utf-8

Content-Length: 0

Connection: close

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 11 of 12

Page 12: Internet of Things with Arduino and the CC3000 WiFi chip · Internet of Things with Arduino and the CC3000 WiFi chip In this guide, we are going to see how to connect a temperature

This document was last generated on 2017-06-28 06:32:44 AM.

Step 11

X-Request-Id:55c792d07f4a679dfb8a1a09141264d7c98eea1e

Cache-Control: max-age=0

Vary: Accept-Encoding

And finally, have a look on yourXively page in your browser. Youshould see that you successfullysent some information to the Xivelyserver and the temperature &humidity should be displayed on thispage

After a few measurements, youshould also have the graphsdisplayed on the device’s page. Ofcourse, you can use this tutorial toconnect other sensors to Xively:motion sensors, contact switches,luminosity sensors, … thepossibilities are endless, so don’thesitate to experiment and shareyour results!

Internet of Things with Arduino and the CC3000 WiFi chip

© 2017 www.botsbits.org Page 12 of 12