Last active
July 24, 2017 20:32
-
-
Save jotathebest/9608245b79498c0974f426b8a1dd84da to your computer and use it in GitHub Desktop.
This scripts sends data to Ubidots using TCP with a NodeMCU
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 have devices | |
* based on ESP8266 chips that we don't support with a library. This script is intended to send | |
* data through TCP | |
* | |
* 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 = "translate.ubidots.com"; | |
int HTTPPORT = 9012; | |
/* Keep in mind that for robust operation you should avoid to use String variables */ | |
char* TOKEN = "..."; // Put here your TOKEN | |
char* VAR_LABEL = "test-var"; // Your variable ID | |
char* DEVICE_LABEL = "test-tcp"; // Your variable ID | |
char* USER_AGENT = "ESP8266"; | |
char* VERSION = "1.0"; | |
char* DEVICE_NAME = "Particle"; | |
char str_value[6]; | |
char* allData = (char *) malloc(sizeof(char) * 700); | |
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 http://help.ubidots.com/developers/send-data-to-ubidots-over-tcp-or-udp */ | |
float sensor_value = random(0, 1000)*1.0; | |
/* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/ | |
dtostrf(sensor_value, 4, 2, str_value); | |
/* Creates the socket payload */ | |
sprintf(allData, "%s/%s|POST|%s|%s:%s=>", USER_AGENT, VERSION, TOKEN, DEVICE_LABEL, DEVICE_NAME); | |
sprintf(allData, "%s%s:%s", allData, VAR_LABEL, str_value); | |
sprintf(allData, "%s|end", allData); | |
clientUbi.connect(HTTPSERVER, HTTPPORT); | |
if (clientUbi.connect(HTTPSERVER, HTTPPORT)) { | |
Serial.println(F("Posting your variables: ")); | |
Serial.println(allData); | |
clientUbi.print(allData); | |
} | |
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