The WiDo board packs an Arduino Leonardo and a WiFi chip into a single board. For $29.90 USD you’ll get the equivalent to an Arduino and a WiFi module, which would cost more if bought separately.
In this guide we’ll learn how to send a temperature reading from the WiDo IoT Node to Ubidots. This board is designed and sold by DF Robot.
Following this guide you will be able to POST data to Ubidots using the DFRobot WiDo in just a couple of minutes!
Requirements
Step-by-Step
Setting up Arduino IDE
Sending (POST) Data to Ubidots
- Send multiple values to UbidotsSummary
1. Setting up Arduino IDE
1. Download the Forked library of CC3000 library and install it. For a detailed explanation of how to install libraries using the Arduino IDE, refer to this guide.
ISSUES NOTE: If you get a message saying you already have a library called “Adafruit_CC3000_Library-master” this is because you already have an installation for the CC3000 Adafruit module. To solve this, open the .ZIP file from WiDo, and change the name of the folder to something like “Adafruit_CC3000_Library-Wido”, then try installing the library again.
2. Select the "Arduino Leonardo" as the board you're working with in the Arduino IDE. To select it, press Tools –> Boards > Arduino Leornado.
2. Sending (POST) data to Ubidots
With the following sample code you will be able to POST the ANALOG readings taken from WiDo board analog port A0.
1. To post your first value in Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you will need to assign your unique Ubidots TOKEN and the Variable ID of the variables you will be updating in Ubidots. If you do not have the variable IDs, simply create a shell variables and then update your firmware to contain the correct Variable IDs. To locate the variable IDs, simply click here.
/***************************************************
* This is an example for the DFRobot Wido - Wifi Integrated IoT lite sensor and control node
* Product Page & More info: http://www.dfrobot.com/index.php?route=product/product&product_id=1159
* Designed specifically to work with the DFRobot Wido products:
*
* The library is forked from Adafruit
*
* Contributed by James
* BSD license, all text above must be included in any redistribution
* Modified by Agustin Pelaez for Ubidots, Inc.
****************************************************/
/*
This example code is used to connect the Ubidots cloud service (Official homepage: http://www.ubidots.com).
The device required is just:
1. LM35 low cost temperature sensor or any device you used to upload data
2. And Wido
Note: Please don't forget to change the setting below before using!
1. WLAN_SSID & WlAN_PASS
2. API_key
3. Device ID
*/
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#define Wido_IRQ 7
#define Wido_VBAT 5
#define Wido_CS 10
#include "utility/debug.h"
Adafruit_CC3000 Wido = Adafruit_CC3000(Wido_CS, Wido_IRQ, Wido_VBAT,SPI_CLOCK_DIVIDER); // you can change this clock speed
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
#define WLAN_SSID "Abriles_LTE" // cannot be longer than 32 characters!
#define WLAN_PASS "12345678" // For connecting router or AP, don't forget to set the SSID and password here!!
#define TCP_TIMEOUT 3000
#define TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
// Update you Ubidots token
#define VARIABLE_ID "xxxxxxxxxxxxxxxxxxxxxx" // Replace with the id of the variable you wish to stream data to
void setup(){
Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n"));
/* Initialise the module */
Serial.println(F("\nInitialising the CC3000 ..."));
if (!Wido.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
while(1);
}
/* Attempt to connect to an access point */
char *ssid = WLAN_SSID; /* Max 32 chars */
Serial.print(F("\nAttempting to connect to "));
Serial.println(ssid);
/* NOTE: Secure connections are not available in 'Tiny' mode!
By default connectToAP will retry indefinitely, however you can pass an
optional maximum number of retries (greater than zero) as the fourth parameter.
*/
if (!Wido.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!Wido.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
}
uint32_t ip = 0; // Store Ubidots ip address
float temp = 0; // Store temporary sensor data for post
Adafruit_CC3000_Client WidoClient;
void loop(){
static unsigned long RetryMillis = 0;
static unsigned long uploadtStamp = 0;
static unsigned long sensortStamp = 0;
if(!WidoClient.connected() && millis() - RetryMillis > TCP_TIMEOUT){
// Update the time stamp for reconnecting the ip
RetryMillis = millis();
Serial.println(F("Trying to connect to Ubidots..."));
// Connect to Ubidots
ip = Wido.IP2U32(50,23,124,68);
WidoClient = Wido.connectTCP(ip, 80);
Serial.println(F("Successfully connected to Ubidots."));
}
if(WidoClient.connected() && millis() - uploadtStamp > 1000){
// If the device is connected to the cloud server, upload the data every 1000ms.
uploadtStamp = millis();
// send http data stream to Ubidots
sendstream2Ubidots(VARIABLE_ID, String(analogRead(0))); // send the temperature sensor reading from LM35 sensor to server
/********** Get the http page feedback and print the response ***********/
unsigned long rTimer = millis();
Serial.println(F("Reading Cloud Response...\r\n"));
while (millis() - rTimer < 2000) {
while (WidoClient.connected() && WidoClient.available()) {
char c = WidoClient.read();
Serial.print(c);
}
}
delay(1000); // Wait for 1s to finish posting the data stream
WidoClient.close(); // Close the service connection
RetryMillis = millis(); // Reset the timer stamp for applying the connection with the service
}
}
void sendstream2Ubidots(String variable, String value){
Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
// Variables for storing the length of http package body
int length = 0;
char lengthstr[5];
String httpBodyPackage = "{\"value\":" + value + "}";
Serial.println(httpBodyPackage); // Debug the http body stream
//Make an HTTP request to the Ubidots server
Serial.print(F("Sending Http Request..."));
WidoClient.fastrprint(F("POST /api/v1.6/variables/"));
WidoClient.fastrprint(VARIABLE_ID);
WidoClient.fastrprintln(F("/values HTTP/1.1"));
WidoClient.fastrprintln(F("Host: industrial.api.ubidots.com"));
WidoClient.fastrprint(F("X-Auth-Token: "));
WidoClient.fastrprintln(TOKEN);
WidoClient.fastrprintln(F("Content-Type: application/json"));
WidoClient.fastrprint(F("Content-Length: "));
WidoClient.println(String(httpBodyPackage.length()));
WidoClient.fastrprintln(F(""));
WidoClient.println(httpBodyPackage);
Serial.println(F("Done....."));
}
2. Verify your code within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the "Check Mark" icon; press it to verify your code.
3. Upload the code into your WiDo (Arduino Leoanado). To do this, choose the "right-arrow" icon beside the "check mark" icon.
4. To verify the connectivity of the device and the data sent, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs.
NOTE: If no response is seen in the serial monitor, try unplugging your WiDo and then plugging it again. Also, make sure the baud rate of the Serial monitor is set to the same one specified in your code 115200
At this point, the variables assigned in the code will be updating the analog readings the taken from the board.
Sending multiple values to Ubidots
If you’re looking to send more than a variable at the time, use the collections API endpoint by replacing the function “sendstream2Ubidots(String var, Sting val)” with this function:
void send3Ubidots(String var1, String val1, String var2, String val2, String var3, String val3){
// Variables for storing the length of http package body
String httpBodyPackage = "[{\"variable\":\"" + var1 + "\",\"value\":" + val1 + "}, {\"variable\":\"" + var2 + "\",\"value\":" + val2 + "}, {\"variable\":\"" + var3 + "\",\"value\":" + val3 + "}]";
Serial.println(httpBodyPackage); // Debug the http body stream
//Make an HTTP request to the Ubidots server
Serial.print(F("Sending Http Request..."));
WidoClient.fastrprintln(F("POST /api/v1.6/collections/values/ HTTP/1.1"));
WidoClient.fastrprintln(F("Host: industrial.api.ubidots.com"));
WidoClient.fastrprint(F("X-Auth-Token: "));
WidoClient.fastrprintln(TOKEN);
WidoClient.fastrprintln(F("Content-Type: application/json"));
WidoClient.fastrprint(F("Content-Length: "));
WidoClient.println(String(httpBodyPackage.length()));
WidoClient.fastrprintln(F(""));
WidoClient.println(httpBodyPackage);
Serial.println(F("Done....."));
}
3. Summary
With this simple tutorial you are able to POST data to Ubidots with the ease of the Arduino IDE and a DFRobot Wido board.
Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution! :)
Other readers have also found useful...