Adafruit CC3000 WiFi and Xively

Similar documents
Internet of Things with Arduino

WIFI CC3000 Module Breakout

Make a Simple Weather Station Using Arduino

2.2" TFT Display. Created by lady ada. Last updated on :19:15 PM UTC

Adafruit CAP1188 Breakout

Adafruit 1-Wire GPIO Breakout - DS2413

Send Raspberry Pi Data to COSM

Adafruit 1-Wire Thermocouple Amplifier - MAX31850K

Adafruit MAX31865 RTD PT100 or PT1000 Amplifier

Adafruit DS3231 Precision RTC Breakout

Adafruit BMP280 Barometric Pressure + Temperature Sensor Breakout

Adafruit 1-Wire Thermocouple Amplifier - MAX31850K

Monitor your home remotely using the Arduino

Create your own wireless motion sensor with

TLC5947 and TLC59711 PWM LED Driver Breakouts

Remote Control with the Huzzah + Adafruit.io

Adafruit VL53L0X Time of Flight Micro-LIDAR Distance Sensor Breakout

Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout

Adafruit BME680. Created by lady ada. Last updated on :10:23 AM UTC

#include "DHT.h" DHT dht(dhtpin, DHTTYPE); // Date and time functions using a DS1307 RTC connected via I2C and Wire lib

Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout

Adafruit Metro Mini. Created by lady ada. Last updated on :12:28 PM UTC

Yun Shield Quick Start Guide VERSION: 1.0. Yun Shield Quick Start Guide 1 / 14.

Adafruit HTU21D-F Temperature & Humidity Sensor

ipot Phase 1 1

Home Automation With Arduino

Arduino Package Tracker

Adafruit SHT31-D Temperature & Humidity Sensor Breakout

AT42QT1010 Capacitive Touch Breakout Hookup Guide

Adafruit USB Power Gauge Mini-Kit

Inclusions required for the DMD

Arduino Platform Part I

Arduino: LCD Diagrams & Code Brown County Library

Adafruit MAX31865 RTD PT100 or PT1000 Amplifier

Adafruit Ethernet FeatherWing

Stand-alone programming AVRs using CircuitPython

Adafruit 2.4" Color TFT Touchscreen Breakout

Adafruit Mini TFT " 160x80

1.5" & 2.1" Monochrome 128x64 OLED Display Module

Adafruit PDM Microphone Breakout

Arduino Lesson 15. DC Motor Reversing

The Riverside Robotic Society June 2016 ESP8266

Adafruit Optical Fingerprint Sensor

Hardware Overview. Onboard Sensors. Pressure, Humidity, and Temperature. Air Quality and Temperature

Adafruit CC3000 WiFi. Created by Rick Lesniak. Last updated on :40:07 PM EDT

CSCI 6907 PROJECT PROPOSAL LIGHTS OUT MANAGEMENT

Adafruit HMC5883L Breakout - Triple-Axis Magnetometer Compass Sensor

How to Use an Arduino

Adafruit 3.5" 320x480 Color TFT Touchscreen Breakout

Bill of Materials: Turn Off the Lights Reminder PART NO

2.3" Monochrome 128x32 OLED Display Module

Adafruit INA219 Current Sensor Breakout

ArduCAM-M-2MP ESP8266 Nano V2 Evaluation Kit User Guide

This tutorial will show you how to take temperature readings using the Freetronics temperature sensor and an Arduino Uno.

Objectives: Learn how to input and output analogue values Be able to see what the Arduino is thinking by sending numbers to the screen

AT42QT101X Capacitive Touch Breakout Hookup Guide

Adafruit Terminal Block Breakout FeatherWing

TSL2561 Luminosity Sensor

Getting Started with the nrf8001 Bluefruit LE Breakout

Arduino Uno. Arduino Uno R3 Front. Arduino Uno R2 Front

Adafruit NFC/RFID on Raspberry Pi

Black Box DCX3000 / DCX1000 Using the API

ARDUINO MEGA 2560 REV3 Code: A000067

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Adafruit PowerBoost Charger

FPGA RGB Matrix. Created by lady ada. Last updated on :15:42 PM UTC

LIS3DH Hookup Guide. Introduction. SparkFun Triple Axis Accelerometer Breakout - LIS3DH SEN Required Materials

ARDUINO UNO REV3 Code: A000066

Register FAQ Calendar Today's Posts Search

2.3" Monochrome 128x32 OLED Display Module

ARDUINO UNO REV3 SMD Code: A The board everybody gets started with, based on the ATmega328 (SMD).

Arducam ESP32 UNO board

ArduCAM-M-2MP ESP8266 Evaluation Kit User Guide. Rev 1.1, March 2017

Digital Pins and Constants

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Proper Debugging of ATSAMD21 Processors

Trinket RGB Shield Clock

CORTESIA ELECTRONICCA

micro:bit Lesson 2. Controlling LEDs on Breadboard

Naming Things in Adafruit IO

ARDUINO MEGA ADK REV3 Code: A000069

EPT-200TMP-TS-U2 TMP102 Temperature Sensor Docking Board Data Sheet

free ebooks ==>

Adafruit PowerBoost Charger

ARDUINO MICRO WITHOUT HEADERS Code: A000093

Arduino: Piezo Diagrams & Code Brown County Library. Projects 01 & 02: Scale and Playing a Tune Components needed: Arduino Uno board piezo

CAN-BUS MCP2515 MCP2551 OBD-II

36mm LED Pixels. Created by Phillip Burgess. Last updated on :45:20 PM EDT

TA0013 ARDUINO RFID UNO STARTER KIT

1.5" & 2.1" Monochrome 128x64 OLED Display Module

Adafruit Analog Accelerometer Breakouts

Bosch BMP085 Breakout Board

Yun Shield User Manual VERSION: 1.0. Yun Shield User Manual 1 / 29.

Adafruit MAX98357 I2S Class-D Mono Amp

Adafruit Powerboost 1000 Basic

Guide to practical classes in IoT design and integration

The Qulet. Quantifying Residential Electricity Consumption BREE-495 [BREE-495] Hyunjoong Kim, Kai Park, Dr. Clark April 14, 2015

Arduino ADK Rev.3 Board A000069

Adafruit NFC/RFID on Raspberry Pi

X Board V2 (SKU:DFR0162)

Transcription:

Adafruit CC3000 WiFi and Xively Created by Marc-Olivier Schwartz Last updated on 2018-08-22 03:37:52 PM UTC

Guide Contents Guide Contents Introduction Setting up your Xively account Connections DHT11 sensor CC3000 Breakout Board Arduino sketch Using Xively 2 3 5 10 10 10 12 16 Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 2 of 18

Introduction Please Note: Xively no longer has free developer access to their system, so this tutorial is only for historical research. Please check out our other IoT tutorials for alternative services! The CC3000 WiFi chip from Texas Instrument is a quite versatile chip that can be used to connect your 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. 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 board for the WiFi connectivity. But instead of communicating with a local server, the CC3000 chip will communicate directly with the Xively server and send the data over there. At the end, you will be able to monitor the data sent by the server directly from your browser, wherever you are in the world, just by logging into the Xively website. Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 3 of 18

Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 4 of 18

Setting up your Xively account Please Note: Xively no longer has free developer access to their system, so this tutorial is only for historical research. Please check out our other IoT tutorials for alternative services! The first step is to create your Xively account, and to configure it for this project. It is all done via their graphical interface, so there is no need to code anything in this part. First, you need to create an account. Just click on "Get started": And then on "Sign up here": Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 5 of 18

You can now enter your personal informations: Now that your account is created, you can create a device to track some data. First, click on "Develop": And on "Add Device": Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 6 of 18

It's now time to enter some information about the device: Finally, you should arrive to this page corresponding to your device. What is important on this page is the Feed ID that identify your device, and the API key that identify your account. Please note these 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: Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 7 of 18

Now enter the details about this first channel for the Temperature. The name of the channels are important, 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 your device page: Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 8 of 18

Your Xively account is now ready to receive some data coming from your Arduino board. Let's now focus on how to connect your Arduino board, the DHT 11 sensor, and the CC3000 chip. Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 9 of 18

Connections Please Note: Xively no longer has free developer access to their system, so this tutorial is only for historical research. Please check out our other IoT tutorials for alternative services! The whole project is based on the Arduino platform, so you will need an Arduino board. I really recommend using the Arduino Uno board for this project, as it is one of the only board that is compatible with the CC3000 library at the time this tutorial was written. Then, you need the Adafruit CC3000 breakout board (http://adafru.it/1469) to make the WiFi communication, and the DHT11 temperature & humidity sensor (you can also use the DHT22 or the AM2302 sensors which are almost identical to wire up but higher quality). You 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. DHT11 sensor The DHT sensor is quite easy 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. CC3000 Breakout Board The hardware configuration of the CC3000 breakout board is relatively easy. 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 of the board to the corresponding pins on the Arduino board: MOSI, MISO, and CLK go to pins 11,12, and 13, respectively. Finally, you have to take care of the power supply: Vin goes to the Arduino 5V, and GND to GND. Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 10 of 18

The following picture summarizes the hardware connections: Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 11 of 18

Arduino sketch Please Note: Xively no longer has free developer access to their system, so this tutorial is only for historical research. Please check out our other IoT tutorials for alternative services! The Arduino sketch for this project consists in initializing the DHT 11 sensor & the CC3000 WiFi chip, and then in running a loop that measures the temperature & humidity, send data to the Xively server, and repeat itself every 10 seconds. It starts by importing the right libraries. The DHT library is at https://github.com/adafruit/dht-sensor-library (https://adafru.it/ajx) and CC3000 is at https://github.com/adafruit/adafruit_cc3000_library (https://adafru.it/cfn) but be sure to follow the tutorials for both of those products first! #include <Adafruit_CC3000.h> #include <SPI.h> #include "DHT.h" #include <avr/wdt.h> We can then define the correct pins for the CC3000 breakout board: #define ADAFRUIT_CC3000_IRQ 3 #define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_CS 10 And the correct pin for the DHT sensor, as well as the sensor type: #define DHTPIN 7 #define DHTTYPE DHT11 We can then create the CC3000 instance: Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2); And the DHT sensor instance: DHT dht(dhtpin, DHTTYPE); The next set of parameters is something your have to modify with your parameters: your WiFi network name, your password, and the type of security. #define WLAN_SSID "yournetwork" #define WLAN_PASS "yourpass" #define WLAN_SECURITY WLAN_SEC_WPA2 The next set of parameters concerns Xively. If you remember, I asked you to write down some parameters of your Xively account: your API key and your feedid. Just enter them in these fields, it will be used to make the request to the Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 12 of 18

Xively server. #define WEBSITE "api.xively.com" #define API_key "yourapikey" #define feedid "yourfeedid" Now, we enter the setup() part of the sketch. As we will connect and disconnect from the Xively server every time we want to send data, the setup() we will only include the initialization of the CC3000 chip, and the connection to the WiFi network: Serial.println(F("\nInitializing...")); 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); } Then, in the loop() part of the sketch, we need to get the IP of the Xively server: uint32_t ip = 0; Serial.print(F("api.xively.com -> ")); while (ip == 0) { if (! cc3000.gethostbyname("api.xively.com", &ip)) { Serial.println(F("Couldn't resolve!")); while(1){} } delay(500); } cc3000.printipdotsrev(ip); Serial.println(F("")); We also need to keep in mind that we want the project to run continuously day after day, whatever happens. For example, we want to deploy this project in a remote location and have it run for months without any human intervention. So if the Arduino cannot connect to the Xively server or crashes when sending the data, we don't want it to freeze and do nothing anymore. This is why we need to use the Arduino watchdog. This will basically reset the Arduino if no reset signal is received after a given delay. Here, we will initialise the watchdog with the maximum delay of 8 seconds: wdt_enable(wdto_8s); Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 13 of 18

We can now measure the temperature & humidity using the dht instance, and convert these values into integers: float h = dht.readhumidity(); float t = dht.readtemperature(); int temperature = (int) t; int humidity = (int) h; This is now the time to format the data for the Xively website in the JSON format. It might seem complicated, but the Xively website has many tutorials to format your data correctly. We also need to know the total length (in number of characters) of the data so we can put it in the HTTP request: // JSON data String data = ""; data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ " + "{\"id\" : \"Temperature\",\"current_value\" : \"" + String(temperature) + "\"}," + "{\"id\" : \"Humidity\",\"current_value\" : \"" + String(humidity) + "\"}]}"; // Get length length = data.length(); We can now connect to the Xively server with the following piece of code. If it is successful, we print a message on the Serial monitor: Adafruit_CC3000_Client client = cc3000.connecttcp(ip, 80); if (client.connected()) { Serial.println(F("Connected to Xively server.")); When the client is connected, we can send the request to the server. This is a typical HTTP PUT request, where we specify the feedid, the API key, and send the data at the end of the request. We first send the headers, and we reset the watchdog after this step: Serial.print(F("Sending headers")); client.fastrprint(f("put /v2/feeds/")); client.fastrprint(feedid); client.fastrprintln(f(".json HTTP/1.0")); Serial.print(F(".")); client.fastrprintln(f("host: api.xively.com")); Serial.print(F(".")); client.fastrprint(f("x-apikey: ")); client.fastrprintln(api_key); Serial.print(F(".")); client.fastrprint(f("content-length: ")); client.println(length); Serial.print(F(".")); client.fastrprint(f("connection: close")); Serial.println(F(" done.")); // Reset watchdog wdt_reset(); We can now transmit the core of the data itself. We transmit the JSON data in several chunks using a dedicated Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 14 of 18

function called senddata. This function cuts the data into small pieces, send these pieces one by one, and reset the watchdog after each chunk is sent. This way, we are protected in case the transmission doesn't work and makes the Arduino freeze. The size of a chunk is defined in the buffer_size variable. Depending on your connection speed, you might have to change this variable so the watchdog doesn't reset the sketch every time. Serial.print(F("Sending data...")); client.fastrprintln(f("")); senddata(client,data,buffer_size); client.fastrprintln(f("")); Serial.println(F("done.")); After the request is sent, we read the answer from the server to be sure that everything is ok, and we print it on the Serial monitor. This is be useful when trying to debug the project. while (client.connected()) { while (client.available()) { char c = client.read(); Serial.print(c); } } After the answer has been received from the server, we close the connection, and disable the watchdog until the next loop(): client.close(); Serial.println(F("Closing connection")); // Reset watchdog & disable wdt_reset(); wdt_disable(); Because the temperature & humidity are slow-changing values, we can read these values & send them over to Xively only a few number of times every hour. For this guide, I used a delay of 10 seconds to make sure that the sketch is working, but you can use any value you want. This part is done by using a dedicated function called wait, which is also protected by the watchdog in case the Arduino sketch crashes: wait(10000) Finally, the complete code can be found in the GitHub repository of this project (https://adafru.it/cpv). Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 15 of 18

Using Xively Please Note: Xively no longer has free developer access to their system, so this tutorial is only for historical research. Please check out our other IoT tutorials for alternative services! Now that the Arduino sketch is completed, you can upload the sketch to your Arduino board, and test the project. Make sure that the page corresponding to your device on Xively is opened. You can now open your Serial Monitor and you should see the Arduino connecting to your network, preparing the request, and sending it to the Xively website: Initializing WiFi chip... Connecting to WiFi network...done! Request DHCP Checking WiFi connection...done. Pinging Xively server...done. Connected to Xively server. Sending headers... done. Sending data... If everything goes well, you should see this response from the Xively server: HTTP/1.1 200 OK Date: Mon, 14 Oct 2013 17:44:20 GMT Content-Type: application/json; charset=utf-8 Content-Length: 0 Connection: close X-Request-Id: 55c792d07f4a679dfb8a1a09141264d7c98eea1e Cache-Control: max-age=0 Vary: Accept-Encoding And finally, have a look on your Xively page in your browser. You should see that you successfully sent some information to the Xively server and the temperature & humidity should be displayed on this page: Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 16 of 18

After a some measurements, you should also have the graphs displayed on the device's page: Adafruit Industries https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively Page 17 of 18

Finally, I also made a video showing the different aspects of the project: Of course, you can use this tutorial to connect other sensors to Xively: motion sensors, contact switches, luminosity sensors,... the possibilities are endless, so don't hesitate to experiment and share your results! Adafruit Industries Last Updated: 2018-08-22 03:37:48 PM UTC Page 18 of 18