[SOLVED] How to Send data to Ubidots Using Ubidots Serial Library

Hello Jose,

During the tests we found a inconsistence with the library which will be reviewed during the next days by the team. The Ubidots Libraries are built to make the get started easier for the users, but that doesn’t mean you can’t handle data with the platform, if your desire you can build your own script following the Ubidots REST API Reference.

Once the library is available I will let you know!

All the best,
Maria C.

Hi Maria,

Thank you so much for the advice. Some days ago I tried to make my own code and it became a little complicated but after some tests, it works, although sometimes the requests didn’t work and I received incomplete pack, or lost the communication with server Ubidots.
However, I’ll wait for your ad and compare both codes. If my sketch works completely, I will not hesitate to share it.

Again, thanks for all.

My best wishes,
José Santana.

Hello @jsantanac,

The library is now available in the Github repository, please reference to the following link and read the documentation to note the changes.

All the best,
Maria C.

Hi all, I had the same problems as all of you have discussed. However, I got it working after going through Maria Hernandez help (codes) in one of the posts. Here, I would like to share the code for using arduino atmega and esp8266 as telemetry. Three sensor data can be posted to UBIDOTS. THANK YOU VERY MUCH TO Maria Hernandez. Good job!


/****************************************
 * Define Constants
 ****************************************/
namespace {
  bool flow_control = true; // control the flow of the requests
  const char * USER_AGENT = "UbidotsESP8266"; // Assgin the user agent
  const char * VERSION =  "1.0"; // Assign the version
  const char * METHOD = "POST"; // Set the method
  const char * TOKEN = "............"; // Assign your Ubidots TOKEN
  const char * DEVICE_LABEL = "ESP8266"; // Assign the device label
  const char * VARIABLE_LABEL1 = "SENSOR1"; // Assign the variable label
  const char * VARIABLE_LABEL2 = "SENSOR2"; // Assign the variable label
  const char * VARIABLE_LABEL3 = "SENSOR3"; // Assign the variable label
}

char telemetry_unit[100]; // response of the telemetry unit

/* Space to store values to send */
char str_sensor1[10];
char str_sensor2[10];
char str_sensor3[10];
/****************************************
 * Main Functions
 ****************************************/
void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop() {
  char* command = (char *) malloc(sizeof(char) * 128);
  char* second_command = (char *) malloc(sizeof(char) * 128);
  char* third_command = (char *) malloc(sizeof(char) * 128);
  /* Wait for the server response to read the values and built the command */
  /* While the flag is true it will take the sensors readings, build the command,
     and post the command to Ubidots */
 
    /* Analog reading */
    float sensor1 = analogRead(A0);
    float sensor2 = analogRead(A1);
    float sensor3 = analogRead(A2);
    /* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
    dtostrf(sensor1, 4, 2, str_sensor1);
     dtostrf(sensor1, 4, 2, str_sensor2);
      dtostrf(sensor1, 4, 2, str_sensor3);
    /* Building the logger command */
    sprintf(command, "init#");
    sprintf(command, "%s%s/%s|%s|%s|", command, USER_AGENT, VERSION, METHOD, TOKEN);
    sprintf(command, "%s%s=>", command, DEVICE_LABEL);
    sprintf(command, "%s%s:%s", command, VARIABLE_LABEL1, str_sensor1);
    sprintf(command, "%s%s:%s", command, VARIABLE_LABEL2, str_sensor2);
  //  sprintf(command, "%s%s:%s", command, VARIABLE_LABEL3, str_sensor3);
    sprintf(command, "%s|end#final", command);

    /* Prints the command sent */
    Serial.println(command);// uncomment this line to print the command

    /* Sends the command to the telemetry unit */
    Serial1.print(command);
 
    /* free memory*/
    free(command);
    /* Change the status of the flag to false. Once the data is sent, the status
       of the flag will change to true again */

  //  flow_control = false;
 
  
  /* Reading the telemetry unit */
  int i = 0;
  while (Serial1.available() > 0) {
    telemetry_unit[i++] = (char)Serial1.read();
    /* Change the status of the flag; allows the next command to be built */
   // flow_control = true;
  }
 
  //if (flow_control) {
    /* Print the server response -> OK */
    Serial.write(telemetry_unit);
    i=0;
    /* free memory */
    //memset(telemetry_unit, 0, i);
  //}

   delay(2000);
//////////////////////
/* Building the logger command */
    sprintf(second_command, "init#");
    sprintf(second_command, "%s%s/%s|%s|%s|", second_command, USER_AGENT, VERSION, METHOD, TOKEN);
    sprintf(second_command, "%s%s=>", second_command, DEVICE_LABEL);
   // sprintf(command, "%s%s:%s", second_command, VARIABLE_LABEL1, str_sensor1);
    sprintf(second_command, "%s%s:%s", second_command, VARIABLE_LABEL3, str_sensor3);
  //  sprintf(command, "%s%s:%s", second_command, VARIABLE_LABEL3, str_sensor3);
    sprintf(second_command, "%s|end#final", second_command);

    /* Prints the command sent */
    Serial.println(second_command);// uncomment this line to print the command

    /* Sends the command to the telemetry unit */
    Serial1.print(second_command);
 
    /* free memory*/
  //  free(command);
    /* Change the status of the flag to false. Once the data is sent, the status
       of the flag will change to true again */

  //  flow_control = false;
  

  /* Reading the telemetry unit */
  int j = 0;
  while (Serial1.available() > 0) {
    telemetry_unit[j++] = (char)Serial1.read();
    /* Change the status of the flag; allows the next command to be built */
   // flow_control = true;
  }

  //if (flow_control) {
    /* Print the server response -> OK */
    Serial.write(telemetry_unit);
    /* free memory */
   // memset(telemetry_unit, 0, i);
  //}

  delay(2000);
}