/************************************************************************************************* * This Example sends harcoded data to Ubidots and serves as example for users that has devices * from Particle * * This example is given AS IT IS without any warranty * * Made by Jose GarcĂ­a. *************************************************************************************************/ /**************************************** * Include Libraries ****************************************/ /**************************************** * Define Constants and objects ****************************************/ const char * HTTPSERVER = "things.ubidots.com"; const int HTTPPORT = 80; const char * VARIABLE_LABEL = "my-var"; // Replace for your variable label const char * USER_AGENT = "Particle-test"; const char * VERSION = "0.1"; const char * TOKEN = "...."; // Put here your TOKEN const char * DEVICE_LABEL = "my-device"; // Replace for your device label const char * VARIABLE_LABEL_1 = "var-test-1"; // Space to store values to send char payload[700]; char topic[1000]; // TCP Object TCPClient clientUbi; /**************************************** * Auxiliar Functions ****************************************/ void SendToUbidots(char* payload) { int i = strlen(payload); /* Builds the request POST - Please reference this link to know all the request's structures https://ubidots.com/docs/api/ */ sprintf(topic, "POST /api/v1.6/devices/%s/?force=true HTTP/1.1\r\n", DEVICE_LABEL); sprintf(topic, "%sHost: things.ubidots.com\r\n", topic); sprintf(topic, "%sUser-Agent: %s/%s\r\n", topic, USER_AGENT, VERSION); sprintf(topic, "%sX-Auth-Token: %s\r\n", topic, TOKEN); sprintf(topic, "%sConnection: close\r\n", topic); sprintf(topic, "%sContent-Type: application/json\r\n", topic); sprintf(topic, "%sContent-Length: %d\r\n\r\n", topic, i); sprintf(topic, "%s%s\r\n", topic, payload); /* Connecting the client */ clientUbi.connect(HTTPSERVER, HTTPPORT); Serial.println(topic); if (clientUbi.connected()) { /* Sends the request to the client */ clientUbi.print(topic); Serial.println("Connected to Ubidots - POST"); } else { Serial.println("Connection Failed ubidots - Try Again"); } /* Reads the response from the server */ while (clientUbi.available()) { char c = clientUbi.read(); Serial.print(c); // Uncomment this line to visualize the response on the Serial Monitor } /* Disconnecting the client */ clientUbi.stop(); } /**************************************** * Main Functions ****************************************/ void setup() { Serial.begin(115200); } void loop() { /* Adapt your value array following the structure specified at https://ubidots.com/docs/api/index.html#send-values-to-one-variable */ /*---- Simulates the values of the sensors -----*/ float sensor_value_1 = random(0, 1000)*1.0; float sensor_value_2 = random(0, 1000)*1.0; float sensor_value_3 = random(0, 1000)*1.0; float sensor_value_4 = random(0, 1000)*1.0; float sensor_value_5 = random(0, 1000)*1.0; /*---- Simulates timestamps -----*/ long timestamp = Time.now(); // Time in seconds /*---- Constructs the payload ----*/ //Important: Avoid to send a very long char as it is very memory space costly, send small char arrays sprintf(payload, "{\""); sprintf(payload, "%s%s\":[{\"value\":%f, \"timestamp\":%lu%s},", payload, VARIABLE_LABEL_1, sensor_value_1, timestamp, "000"); // Sends original timestamp sprintf(payload, "%s{\"value\":%f, \"timestamp\":%lu%s},", payload, VARIABLE_LABEL_1, sensor_value_2, timestamp, "100"); // Sends timestamp with a difference of 100 ms sprintf(payload, "%s{\"value\":%f, \"timestamp\":%lu%s},", payload, VARIABLE_LABEL_1, sensor_value_3, timestamp, "200"); // Sends timestamp with a difference of 200 ms sprintf(payload, "%s{\"value\":%f, \"timestamp\":%lu%s},", payload, VARIABLE_LABEL_1, sensor_value_4, timestamp, "300"); // Sends timestamp with a difference of 300 ms sprintf(payload, "%s{\"value\":%f, \"timestamp\":%lu%s}", payload, VARIABLE_LABEL_1, sensor_value_5, timestamp, "400"); // Sends timestamp with a difference of 400 ms sprintf(payload, "%s]}", payload); /* Calls the Ubidots Function POST */ SendToUbidots(payload); delay(1000); }