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


#1

I have tried the Ubidots serial Library (telemetry and logger) with Arduino UNO finally i got success. My values are showing up on Ubidots dashboard. But the problem is 2 variables values are showing but if i send 3rd variable it doesn’t show up what is the exact problem? Am i sending wrong logger command? here is my code:

#include<SoftwareSerial.h>
 SoftwareSerial wifi(2,3);
/****************************************
 * Define Constants
 ****************************************/
namespace {
  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 = "xxxxxxxxxxxxxx"; // Assign your Ubidots TOKEN
  const char * DEVICE_NAME = "xxxxxxxx"; // Assign the desire device name 
  const char * DEVICE_LABEL = "xxxxxxx"; // Assign the device label 
  const char * VARIABLE_LABEL_1 = "temperature"; // Assign the variable label 
  const char * VARIABLE_LABEL_2 = "heart-rate"; // Assign the variable label
    const char * VARIABLE_LABEL_3 = "blood-pressure"; // Assign the variable label
}

char command[700]; // command
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);
  wifi.begin(115200);
}

void loop() {

  
  /* Analog reading */ 
  float sensor1 = 316.00;
  float sensor2 = 225.25;
   float sensor3 = 234.33;
  
  /* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
  dtostrf(sensor1, 4, 2, str_sensor1);
  dtostrf(sensor2, 4, 2, str_sensor2);
  dtostrf(sensor3, 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:%s=>", command, DEVICE_NAME, DEVICE_LABEL);
  sprintf(command, "%s%s:%s", command, VARIABLE_LABEL_1, str_sensor1);
  sprintf(command, "%s,%s:%s", command, VARIABLE_LABEL_2, str_sensor2); // uncomment this line to send sensor 2 values
  sprintf(command, "%s,%s:%s", command, VARIABLE_LABEL_3, 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 */
  wifi.print(command);

  /* Reading the telemetry unit */
  int i = 0;
  while (wifi.available() > 0) {
    telemetry_unit[i++] = (char)wifi.read(); 
  }
  Serial.println(telemetry_unit);
  i = 0;
  delay(5000);
}`

Please also check my logger command


#2

@mariahernandez and @jotathebest please help me here


#3

By using default command logger for sending 2 variable values it is working values are showing but when i send the 3rd variable value then the values don’t show up. Please check the above code and help me to resolve my issue. Am i sending wrong logger command for 3rd variable?


#4

Hello,

According to the example provided you can send two variables per request, to send more variables you have to build a new request, and send it once the first request had response.

I made a test using the Arduino MEGA to be able to debug the message and everything is working properly. I’m going to attach you the code but note that the example is built for the Arduino MEGA, so you have to make the corrections for the Arduino UNO following the structure of your code.

/****************************************
 * Define Constants
 ****************************************/
namespace {
  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"; // Assign your Ubidots TOKEN
  const char * DEVICE_NAME = "telemetry"; // Assign the desire device name 
  const char * DEVICE_LABEL = "ESP8266"; // Assign the device label 
  const char * VARIABLE_LABEL_1 = "temperature"; // Assign the variable label 
  const char * VARIABLE_LABEL_2 = "heartrate"; // Assign the variable label
  const char * VARIABLE_LABEL_3 = "bloodpressure"; // Assign the variable label

}

char first_command[700]; // command
char second_command[700];
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() {

  /* 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(sensor2, 4, 2, str_sensor2);
  dtostrf(sensor3, 4, 2, str_sensor3);

  /* Building first the logger command */ 
  sprintf(first_command, "init#");
  sprintf(first_command, "%s%s/%s|%s|%s|", first_command, USER_AGENT, VERSION, METHOD, TOKEN);
  sprintf(first_command, "%s%s:%s=>", first_command, DEVICE_NAME, DEVICE_LABEL);
  sprintf(first_command, "%s%s:%s", first_command, VARIABLE_LABEL_1, str_sensor1);
  sprintf(first_command, "%s,%s:%s", first_command, VARIABLE_LABEL_2, str_sensor2); // uncomment this line to send sensor 2 values
  sprintf(first_command, "%s|end#final", first_command);

  /* Prints the command sent */
  //Serial.println(first_command);// uncomment this line to print the command
  
  /* Sends the command to the telemetry unit */
  Serial1.print(first_command);

  /* Reading the telemetry unit */
  int i = 0;
  while (Serial1.available() > 0) {
    telemetry_unit[i++] = (char)Serial1.read(); 
  }
  Serial.println(telemetry_unit);
  i = 0;
  
  delay(1000);
  
  /* Building the second 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:%s=>", second_command, DEVICE_NAME, DEVICE_LABEL);
  sprintf(second_command, "%s%s:%s", second_command, VARIABLE_LABEL_3, 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);

  /* Reading the telemetry unit */
  while (Serial1.available() > 0) {
    telemetry_unit[i++] = (char)Serial1.read(); 
  }
  Serial.println(telemetry_unit);
  i = 0;

  delay(5000);
}

As you can see below the three variables are arriving in Ubidots without issues:

All the best,
Maria C.


#5

I’ll try it and will let you know soon… :slight_smile:


#6

This code is not working for me, when i send only 2 variables it works but when i send another logger command the values don’t show up. I have used the code you provided (modified it for UNO). 2nd logger command is not working for me. Any other alternate solution?


#7

i have tried multiple times: 1 value shows up when 1 send only 1 2 values are also showing when i send 2 variable values but when i send another logger command for 3rd variable value it doesn’t work. Would you please try this code on Arduino UNO?
@mariahernandez @jotathebest


#8

Hi

I am facing an issue to connect the Generic ESP8266 to server , terminal shows me the following result after uploading the telemetry program in ESP8266 module

…WiFi connected
IP address:
192.168.43.152
Wrong command. Verify the command and send it again.

if i enter command init#ONLINE?#final then also reply is – Wrong command. Verify the command and send it again.

Also tried the

init#UbidotsESP8266/1.0|POST|A1E-apxxxxxxxxxxxxQ7mxxxtDflo7JStD|Arduino:tele=>temp-1:100|end#final

but same result.

I am using ESP8266 generic module with arduino mega .
ESP8266 board package is 2.3.0

Also help me to understand what should be the user_agent and version in my case

Thanks …


#9

Hello @bhavsarakshay,

Regrettably, I’m not able to reproduce your issue. As you can see below it’s working properly for me:

com-video-to-gif(4)

Please, verify if you are sending a white space at the end of the command as is shown above.

All the best,
Maria C.


#10

@mariahernandez, @jotathebest . Hi, I want to know how could receive data from Udibots using this sketch. I read that I have to declare METHOD = ‘GET’ or METHOD = ‘LV’. I’ve done this with the others steps but in the serial monitor I just see “OK”. I don’t know what is the mistake and I would be so thanksful if you could help me, please.

Post part of my code.

Note: I have to change or modify the program loaded in wifi module?

namespace {
const char * USER_AGENT = “UbidotsESP8266”; // Assgin the user agent
const char * VERSION = “1.1”; // Assign the version
const char * METHOD = “POST”; // Set the method
const char * METHOD2 = “LV”; //Set the method
const char * TOKEN = “A1E-Hmk2TujdckW7sVBiqkz5YbyzZ6DNn2”; // Assign your Ubidots TOKEN
const char * DEVICE_NAME = “arduino”; // Assign the desire device name
const char * DEVICE_LABEL = “arduino-ethernet”; // Assign the device label
const char * VARIABLE_LABEL_1 = “temperatura”; // Assign the variable label
const char * VARIABLE_LABEL_2 = “temperatura_setpoint”; // Assign the variable label
}

void loop()
{
char command[700]; // command
char command2[700]; // command
char telemetry_unit[100]; // response of the telemetry unit

sprintf(command2, “init#”);
sprintf(command2, “%s%s/%s|%s|%s|”, command2, USER_AGENT, VERSION, METHOD2, TOKEN);
sprintf(command2, “%s%s:%s”, command2, DEVICE_LABEL, VARIABLE_LABEL_2);
sprintf(command2, “%s|end#final”, command2);

Serial.println(command2);
/* Reading the telemetry unit */
int i = 0;

while (Serial1.available() > 0)
{
telemetry_unit[i++] = (char)Serial1.read();
}

Serial.println(telemetry_unit);
i = 0;
delay(3000);
}


#11

Hi there @jsantanac, I do not have right now an arduino MEGA with me, but I let as constant inside the library this request:

UbidotsESP8266/1.1|LV|p4uuT2OLLFJwv7nxRVfoVqcfImwRQW|truck:speed|end

With this test I simulated a GET request to the endpoint translate.ubidots.com and I obtained this console answer:

imagen

I am not sure what your problem is, but it may be in the way as you store your array of data, please replace your server answer print function with this:

while (Serial1.available() > 0)
{
  Serial.write((char)Serial1.read());
}

And check If you obtain the entire answer.

Regards


#12

Hi, @jotathebest. Thank you so much for your answer, it was very helpful and I could solve the problem. I have one more question. I need recieve and send data, how can I use my Mega with GET/LV and POST at the same time for this?

Thanks for your time.


#13

Greetings @jsantanac, simply implement two commands, one with the GET request that you already have and the other one with the POST request, if you do not know how to do the POST request please refer to this example: https://github.com/ubidots/ubidots-esp8266-serial/blob/master/examples/Arduino_Logger/Arduino_Logger.ino.

The pseudocode of the routine should be something like this:

build GET_COMMAND;
Serial.print(GET_COMMAND);
Wait for telemetry answer
if answer is 'OK':
   parse and store the value
else
   Serial.println("Error obtaining value");
read Analog PIN
build POST_COMMAND
Waits for telemetry answer

Regards


#14

Thank you so much! I will try this and I’ll told you how is it going. But, on the other hand, when I see the serial monitor there are occasions where the messages such as “Wrong command. Verify the command and send it again” or “Waiting answer from ESP - Timeout reached. Command aborted” appear. I’ve thought is about the module, but I don’t know what’s the problem exactly. Could you explain me about this, please? I want to know how solve this incovennient.

This is the program in my ESP8266

#include <UbidotsESP8266.h>
/****************************************

  • Include Libraries
    ****************************************/

/****************************************

  • Define Constants
    ****************************************/
    namespace {
    const char * WIFISSID = “WIFISSID”; // Assign your WiFi SSID
    const char * PASSWORD = “********”; // Assign your WiFi password
    const char * TOKEN = “A1E-Hmk2TujdckW7sVBiqkz5YbyzZ6DNn2”; // Assign your Ubidots TOKEN
    }

Ubidots client(TOKEN);

/****************************************

  • Main Functions
    ****************************************/
    void setup() {
    Serial.begin(115200);
    client.wifiConnection(WIFISSID, PASSWORD);
    }

void loop() {
client.readData(); // Reads the command from the logger
delay(1000);
}

The problem is because of the delay?

Again, thank you for your time and all support.


#15

Greetings dear user, below a small explanation to your questions:

  • Wrong command. Verify the command and send it again

This message means that your command was not received properly or parsed by the ESP8266.

  • Waiting answer from ESP - Timeout reached. Command aborted

This message means that the server’s took more than 5 seconds to answer, if this happens the command is not sent. This guard was implemented in the library to avoid infinite loops once you try to send data.

Have a nice rest of day


#16

Hi, again. I’m so sorry but I have the same problem yet. The program works fine at first, but then, I don’t know, 5 minutes later, the program failed and I don’t know why. The error messages are “Wrong command. Verify the command and send it again”, “Waiting answer from ESP - Timeout reached. Command aborted” and appear very often. This is part of my code and I hope you can help me please. Thank you so much.

I’m using digital sensor DS18B20. By the way, in my ESP8266 the delay after each read is 1000 ms, it could be a problem or the reason for these error messages?

  sensorDS18B20.requestTemperatures();
  sensortanque=sensorDS18B20.getTempCByIndex(0);
  //sensorcolector=sensorDS18B20.getTempCByIndex(0);
  dtostrf(sensortanque, 4, 2, str_sensor1); //convierte número a caracter
  delay(500);
  
  //POST
  sprintf(command, "init#");
  sprintf(command, "%s%s/%s|%s|%s|", command, USER_AGENT, VERSION, METHOD, TOKEN);
  sprintf(command, "%s%s:%s=>", command, DEVICE_NAME, DEVICE_LABEL);
  sprintf(command, "%s%s:%s", command, SensorTanque, str_sensor1);
  sprintf(command, "%s|end#final", command);
  delay(1000);
  Serial2.print(command);
  i = 0;
  while (Serial2.available() > 0) 
  {
    telemetry_unit[i++] = (char)Serial2.read(); 
  }
  i = 0;
  Serial.println(telemetry_unit);
  delay(1000);

  //GET
  sprintf(recepcion1, "init#");
  sprintf(recepcion1, "%s%s/%s|%s|%s|", recepcion1, USER_AGENT, VERSION, METHOD2, TOKEN);
  sprintf(recepcion1, "%s%s", recepcion1, TemperaturaSP);
  sprintf(recepcion1, "%s|end#final", recepcion1);
  
  //Serial.println(recepcion1);
  delay(1000);
  Serial2.print(recepcion1);
  
  /* Reading the telemetry unit */
  while (Serial2.available() > 0) 
  {
    telemetry_rec[i++] = (char)Serial2.read();
  }
  Serial.println(telemetry_rec);
  
  //Serial.println((int)(telemetry_rec[3]));
  control=(int)(telemetry_rec[3]-48);
 
  i = 0;  
  
  delay(1000);

#17

Hello @jsantanac!

The device need a certain period of time to be able to free the memory without bring any inconvenience that’s why the examples provided in the library have a 5000 ms delay. In order of this, can you try again assigning a 5000 ms delay instead of the 1000 ms and let us know if the issue persist?

All the best,
Maria C.


#18

Hi, @mariahernandez, thanks for your answer. And yes, I check the library and I could see that. I changed the delay in my program like you said, nevertheless the problem persist after few minutes. I try to send two variables and recieve three, but I can’t. It’s necessary remark that the error messages appear but some requests are performed. I mean, in certain moments I can send one variable and recieve just two variables, as if the other requests did not exist. By the way, for example, when I try to save the second data in a variable, the first one is actually save.

Seriously, thank you for your help and interest.


#19

Thanks so much for the info. I would like to try to reproduce the issue but regrettably by the moment I don’t a board with more that one serial port to debug the messages incoming, to find where can be the issue. If you don’t mind wait for a couple of days would be nice for me because I will have the boards back over the next week so I can make the tests by my side :wink:

All the best,
Maria C.


#20

Thanks so much for your help and interest, I’ll wait for your answer.

Please, don’t forget my problem, I really appreciate all your support.

José Santana.