Created
August 14, 2018 18:43
-
-
Save jotathebest/90adb0796d93b5e1c19e1e9817846cb0 to your computer and use it in GitHub Desktop.
Script to send data to Ubidots using WiDo boards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************************************************* | |
* This Example sends data to Ubidots and serves as example for users that makes use of WiDo boards | |
* | |
* This example is given AS IT IS without any warranty | |
* | |
* Based on the official Adafruit WebClient example: | |
* https://github.com/Arduinolibrary/DFRobot_WiDo_Arduino_IOT_Board_DFR0321/blob/master/Adafruit_CC3000_Library/Adafruit_CC3000_Library-master/examples/Others_From_Adafruit/WebClient/WebClient.ino | |
* | |
* Made by Jose García. | |
*************************************************************************************************/ | |
/******************************** | |
* Libraries included | |
*******************************/ | |
#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" | |
/******************************** | |
* Constants and objects | |
*******************************/ | |
#define WLAN_SECURITY WLAN_SEC_WPA2 | |
#define TCP_TIMEOUT 3000 | |
char const * WLAN_SSID = ""; // cannot be longer than 32 characters! | |
char const * WLAN_PASS = ""; // For connecting router or AP, don't forget to set the SSID and password here!! | |
char const * DEVICE_LABEL = "test-device"; // Put here your device label | |
/* Put here your variable's labels*/ | |
char const * VAR_1 = "var-1"; | |
char const * VAR_2 = "var-2"; | |
/* Ubi Setup*/ | |
char const * TOKEN = ""; // your Account TOKEN | |
char const * USER_AGENT = "Wido"; | |
char const * VERSION = "1.0"; | |
uint32_t ip = 0; // Stores Ubidots ip address | |
unsigned long RetryMillis = 0; // Stores the millis time | |
// Adafruit client Objects | |
Adafruit_CC3000_Client WidoClient; | |
Adafruit_CC3000 Wido = Adafruit_CC3000(Wido_CS, Wido_IRQ, Wido_VBAT,SPI_CLOCK_DIVIDER); | |
/******************************** | |
* Auxiliar Functions | |
*******************************/ | |
void sendUbidots(char* device, char* payload){ | |
// Variables for storing the length of http package body | |
//Make an HTTP request to the Ubidots server | |
Serial.print(F("Sending Http Request...")); | |
if (WidoClient.connected()) { | |
WidoClient.fastrprint(F("POST /api/v1.6/devices/")); | |
WidoClient.fastrprint(device); | |
WidoClient.fastrprintln(F("/?force=true HTTP/1.1")); | |
WidoClient.fastrprint(F("User-Agent:")); | |
WidoClient.fastrprint(USER_AGENT); | |
WidoClient.fastrprint(F("/")); | |
WidoClient.fastrprintln(VERSION); | |
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(strlen(payload)); | |
WidoClient.fastrprintln(F("")); | |
WidoClient.println(payload); | |
Serial.println(F("Done.....")); | |
} else { | |
Serial.println("The client is not connected"); | |
} | |
} | |
void check_connection() { | |
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 = 0; | |
// Try looking up the website's IP address | |
Serial.print("industrial.api.ubidots.com"); Serial.print(F(" -> ")); | |
while (ip == 0) { | |
if (! Wido.getHostByName("industrial.api.ubidots.com", &ip)) { | |
Serial.println(F("Couldn't resolve DNS host!")); | |
} | |
delay(500); | |
} | |
Wido.printIPdotsRev(ip); | |
WidoClient = Wido.connectTCP(ip, 80); | |
Serial.println(F("\nSuccessfully connected to Ubidots.")); | |
} | |
} | |
/******************************** | |
* Main Functions | |
*******************************/ | |
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! | |
} | |
} | |
void loop(){ | |
static unsigned long uploadtStamp = 0; | |
static unsigned long sensortStamp = 0; | |
check_connection(); | |
if(WidoClient.connected() && millis() - uploadtStamp > 1000){ | |
// If the device is connected to the cloud server, upload the data every 1000ms. | |
uploadtStamp = millis(); | |
/*---- Simulates the values of the sensors -----*/ | |
float sensor_value_1 = random(0, 1000)*1.0; | |
float sensor_value_2 = random(0, 1000)*1.0; | |
/*---- Transforms the values of the sensors to char type -----*/ | |
char str_val_1[30]; | |
char str_val_2[30]; | |
/* 4 is mininum width, 2 is precision; float value is copied onto str_val*/ | |
dtostrf(sensor_value_1, 4, 2, str_val_1); | |
dtostrf(sensor_value_2, 4, 2, str_val_2); | |
/* Builds the payload with structure: {"temperature":25.00,"humidity":50.00} for first 4 variables*/ | |
// Important: Avoid to send a very long char as it is very memory space costly, send small char arrays | |
char payload[100]; | |
sprintf(payload, "{\""); | |
sprintf(payload, "%s%s\":%s", payload, VAR_1, str_val_1); | |
sprintf(payload, "%s,\"%s\":%s", payload, VAR_2, str_val_2); | |
sprintf(payload, "%s}", payload); | |
// send http data stream to Ubidots | |
sendUbidots(DEVICE_LABEL, payload); | |
/********** 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment