/**************************************** * Include Libraries ****************************************/ /**************************************** * Define Constants and objects ****************************************/ const char * SERVER = "translate.ubidots.com"; const int PORT = 9012; const char * DEVICE_LABEL = "my-device"; // Replace for your device label 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 TCPClient client; /**************************************** * Auxiliar Functions ****************************************/ /**************************************** * Main Functions ****************************************/ void setup() { Serial.begin(115200); int timeout = 0; Serial.println("trying to connect"); client.connect(SERVER, PORT); // Attempts to connect for aprox 2 seconds while (!client.connected() && timeout < 2000){ Serial.print("."); client.connect(SERVER, PORT); timeout++; delay(1); if (timeout >= 2000){ Serial.println(""); break; } } client.connected() ? Serial.println("connected") : Serial.println("Not connected"); } void loop() { float value = random(1000)*1.0 + random(100)*1.0 + random(10)*1.0; char data[700]; char str_value[6]; // Construct the proper data structure to be sent according to the specified here: // https://app.intercom.io/a/apps/o73aw9yp/educate/articles/702760/show sprintf(data, ""); sprintf(data, "%s/%s|POST|%s|%s=>%s:%f|end", USER_AGENT, VERSION, TOKEN, DEVICE_LABEL, VARIABLE_LABEL, value); Serial.println("Data structure:"); Serial.println(data); // Attempts to connect for aprox 2 seconds int timeout = 0; while (!client.connected() && timeout < 2000){ Serial.print("."); client.connect(SERVER, PORT); timeout++; delay(1); if (timeout >= 2000){ Serial.println(""); break; } } // If connected, sends data if (client.connected()){ Serial.println("sending data"); client.print(data); client.stop(); }else { Serial.println("Could not connect to the server"); } delay(5000); }