[SOLVED] Sending data to Ubidots from Arduino Nano and ESP 8266

I am learning to send data to Ubidots from my Arduino Nano.I am using the following program.

#include "SoftwareSerial.h"
String ssid ="MySSID";  

String password="Password";

SoftwareSerial esp(5, 4);// RX, TX   

String data;

String server = "https://app.ubidots.com"; 

String uri = "/api/v1.6/variables/(id of the variable)/values/?token=(token taken from API credential)";
void setup() {


esp.begin(9600);

Serial.begin(9600);

reset();

connectWifi();

}

void reset() {

esp.println("AT+RST");

delay(1000);

if(esp.find("OK") ) Serial.println("Module Reset");

}


void connectWifi() {

String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";

esp.println(cmd);

delay(4000);

if(esp.find("OK")) {

Serial.println("Connected!");

}

else {

connectWifi();

Serial.println("Cannot connect to wifi"); }

}


void loop () {


httppost();

delay(1000);

}

void httppost () {

esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.

if( esp.find("OK")) {

Serial.println("TCP connection ready");

} delay(1000);
data="humidity=" + 10;

String postRequest =

"POST " + uri + " HTTP/1.1\r\n" +

"Host: things.ubidots.com \r\n" +

"Content-Length: " + data.length() + "\r\n" +

"Content-Type: application/json \r\n" +

"\r\n" + data+"\r\n" ;

String sendCmd = "AT+CIPSEND=";//determine the number of characters to be sent.

esp.print(sendCmd);

esp.println(postRequest.length() );

delay(500);

if(esp.find(">")) { Serial.println("Sending.."); esp.print(postRequest);

if( esp.find("SEND OK")) { Serial.println("Packet sent");

while (esp.available()) {

String tmpResp = esp.readString();

Serial.println(tmpResp);

}

// close the connection

esp.println("AT+CIPCLOSE");

}

}}

However, the code doesn’t work and the output in the serial monitor is

Module Reset
Connected
Cannot connect to Wifi
Cannot connect to Wifi
Sending..
Packet sent

+IPD,294:HTTP/1.1 400 BAD REQUEST
Server:nginx
Date://date and time
Content-Type: application/json
Transfer-Encoding:chunked
Connection:keep-alive
Vary:Accept, Cookie
Allow:GET,POST,HEAD,OPTIONS

40
{"detail":"JSON parse error-No JSON object could be decoded"}
0

TCP connection ready


The same thing continues on and on.
If anyone could look through this it would be amazing.Thank you so much.

Hello, taking a quick look I saw that you have a wrong server, it should be things.ubidots.com instead of app.ubidots.com

Regards

@jotathebest Thanks a lot for going through it.I changed the server name.
I also made a few changes in the POST section so that it matches the format in the Ubidots API example section.The changes I made are as follows:-

String postRequest =

“POST " + uri + " HTTP/1.1\r\n” +

“Host: things.ubidots.com \r\n” +

“Content-Type: application/json \r\n” +

“Content-Length: 17 \r\n” +

“{“value”: 50.5}\r\n”;

But now the output in the Serial monitor is something like this

Module Reset
Connected
Cannot connect to Wifi
Cannot connect to Wifi
Sending…
Packet sent
+IPD,311:HTTP/1.1 400 BAD REQUEST
Server:nginx
Date://date and time
Content-Type: text/html
Content-Length:166
Connection: close

400 Bad Request 400 Bad Request
nginx CLOSED

Sending…
Packet sent

+IPD…and the same thing continues

Also,the value is still not posted in the Ubidots variable.
Are the content length and value ok?

Thanks again for your help.Cheers.

Hello, this should be a proper string to post a value:

String value = "{\"value\": 30}";
String varId = "" // Your variable ID
String token = "" // Your Token

String postString = "POST /api/v1.6/variables/" + varId + "/values HTTP/1.1\r\n"
+ "Content-Type: application/json\r\n"
+ "X-Auth-Token: " + token + "\r\n"
+ "Host: things.ubidots.com\r\n\n"
+ value + "\r\n";

If this doesn’t work for you your problem comes in the way that you send your JSON dictionary. You can try too with our translate service which is easier to implement, take a look to it here http://help.ubidots.com/developers/send-data-to-ubidots-over-tcp-or-udp

@jotathebest Thanks a lot again for the info. However the problem still persists.
Do the spaces in the POST format also matter? Also, is there anyway I can know if my POST format is correct by posting it in my web browser?
I’ll keep trying.
Thanks a lot.

Hello @Sushii, spaces matters like carriage returns and lines feed, the formatted string that I sent you takes care of that so I believe that your issue can be on your hardware side.

About the other question, here is a tutorial to send data and test your JSON dictionary: https://www.youtube.com/watch?annotation_id=annotation_753887069&feature=iv&src_vid=50t3EvBuEk8&v=RsyJJbCinIY

I also recommend you to use one of our libraries to send your data through the esp: http://help.ubidots.com/connect-your-devices/connect-the-esp8266-as-stand-alone-module-with-ubidots

Regards

@jotathebest I actually tried the library a few days ago but I think theres some problem with the library and that it hasn’t been updated.Even if it has I doubt it has been updated for Arduino Nano.
You also mentioned that the problem could lie in the hardware.Could it be because I’m using the SoftwareSerial (ports 4 and 5) of the Arduino Nano?
I also checked the translate service.However,I don’t know how I am supposed to implement the url,port and payload in the program.
Thanks a lot for all the help.

Hello, our ESP8266 library was recently updated, please give a try to the examples of the library and let me know your feedback.

Regards

Hello,@jotathebest I did try the library.After a number of errors in downloading the library I managed to succeed but there was an error in CIPMODE.

I discarded this method and took your advice and used hurl.it(which was shown in the video tutorial) to test the JSON dictionary.
Apparently,my url was wrong.So I used the following url
String url=" /api/v1.6/devices/{LABEL_DEVICE}/?token={TOKEN}";

and used the following format for POST
String value ="{“value”: “35.5”}";

String postRequest =“POST " + url + " HTTP/1.1\r\n” +

“Host: things.ubidots.com \r\n” +

“Content-Type: application/json \r\n” +

"Content-Length: " + 17 + “\r\n\n” +

  • value +"\r\n" ;

Finally,my data was posted on Ubidots.
Thanks a lot for your help.

Hi, I’m glad to know that you could fix it, hope you enjoy your project with Ubidots! :slight_smile:

Regards

Hi, could you say me how did you do the request and which is the server? I’m trying to do this but I don’t get the propperly results. In the serial monitor appear like the request was send, but when I check my Ubidots, nothing change.

My string is this:

POST /api/v1.6/devices/arduino_ethernet/?token=A1E-Hmk2EbtdckW7sVBiqkz4erwo3DNn2 HTTP/1.1
Host: things.ubidots.com
Content-Type: application/json
Content-Length: 13

Thanks for your help

Hi,

Are you using an Arduino board along with the ESP 8266 to send data to Ubidots?
If so,then kindly refer to the first post in this topic.I have used that program to post a value in Ubidots.

Also, I suggest you use hurl.it to crosscheck the string that you are using.Often,the error can be as small as the presence of an unwanted space in the URL.

Let me know if this works.

Cheers!


I m unable to send data to ubidot …get me solution as i had send image of error and I m using espcommunity of version 2.4.0