Last active
May 10, 2017 19:25
-
-
Save jotathebest/d0f4b389e4a8b97fdc525600e09f0c35 to your computer and use it in GitHub Desktop.
Basic example for sending data to Ubidots using ESP8266 based devices
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 harcoded data to Ubidots and serves as example for users that has devices | |
* based on ESP8266 chips that we don't support with a library. | |
* | |
* This example is given AS IT IS without any warranty | |
* | |
* Made by Jose García. | |
*************************************************************************************************/ | |
/******************************** | |
* Libraries included | |
*******************************/ | |
#include <ESP8266WiFi.h> | |
/******************************** | |
* Constants and objects | |
*******************************/ | |
char const * SSID_NAME = "...."; // Put here your SSID name | |
char const * SSID_PASS = "...."; // Put here your password | |
char const * HTTPSERVER = "things.ubidots.com"; | |
int HTTPPORT = 80; | |
/* Keep in mind that for robust operation you should avoid to use String variables */ | |
String TOKEN = "...."; // Put here your TOKEN | |
String VAR_ID = "59132fc976254212a50eb4dc"; // Your variable ID | |
String USER_AGENT = "ESP8266"; | |
String VERSION = "1.0"; | |
WiFiClient clientUbi; | |
/******************************** | |
* Auxiliar Functions | |
*******************************/ | |
/******************************** | |
* Main Functions | |
*******************************/ | |
void setup() { | |
Serial.begin(115200); | |
/* Connects to AP */ | |
WiFi.begin(SSID_NAME, SSID_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
WiFi.setAutoReconnect(true); | |
Serial.println(F("WiFi connected")); | |
Serial.println(F("IP address: ")); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() { | |
/* Keep in mind that for robust operation you should avoid to use String variables */ | |
/* Adapt your value array following the structure specified at https://ubidots.com/docs/api/index.html#send-values-to-one-variable */ | |
float sensor_value = random(0, 1000)*1.0; | |
String str_value = String(sensor_value, 4); | |
String value = "[{\"variable\":\"" | |
+ VAR_ID | |
+ "\",\"value\":" | |
+ str_value | |
+"}]"; | |
int i = value.length(); | |
String postString = "POST /api/v1.6/collections/values/?force=true HTTP/1.1\r\n" | |
"Host: things.ubidots.com\r\n" | |
"User-Agent:" + USER_AGENT + "/" + VERSION + "\r\n" | |
"X-Auth-Token: " + TOKEN + "\r\n" | |
"Connection: close\r\n" | |
"Content-Type: application/json\r\n" | |
"Content-Length: " + String(i) + "\r\n" | |
"\r\n" | |
+ value + | |
"\r\n"; | |
clientUbi.connect(HTTPSERVER, HTTPPORT); | |
if (clientUbi.connect(HTTPSERVER, HTTPPORT)) { | |
Serial.println(F("Posting your variables: ")); | |
Serial.println(postString); | |
clientUbi.print(postString); | |
} | |
while (clientUbi.available()) { | |
char c = clientUbi.read(); | |
Serial.write(c); | |
} | |
clientUbi.stop(); | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment