Created
July 16, 2015 23:06
-
-
Save AgustinPelaez/010a8fee1fd704830c39 to your computer and use it in GitHub Desktop.
Código WiDo - Mariano
This file contains hidden or 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 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 "Atom_Med" // cannot be longer than 32 characters! | |
#define WLAN_PASS "atommed2015" // For connecting router or AP, don't forget to set the SSID and password here!! | |
#define TCP_TIMEOUT 3000 | |
#define TOKEN "xxxxxxOVsr3dbQqAx0fxvKm8Z6gs8" | |
// Update you Ubidots token | |
#define VARIABLE_ID1 "559c9c947625421d93c2e61b" | |
#define VARIABLE_ID2 "559c9c947625421d93c2e61b" | |
#define VARIABLE_ID3 "559c9c947625421d93c2e61b" | |
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 | |
send3Ubidots(VARIABLE_ID1, String(analogRead(0)), VARIABLE_ID2, String(analogRead(1)), VARIABLE_ID3, String(analogRead(2))); // 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.print(variable); | |
WidoClient.fastrprintln(F("/values HTTP/1.1")); | |
WidoClient.fastrprintln(F("Host: things.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.....")); | |
} | |
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: things.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.....")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment