[SOLVED] How to POST data to ubidots, I'm using ESP8266 and ARDUINO UNO


#1

I’m using ESP8266 and ARDUINO UNO.
I’m using AT commands to send data to the server.
test

AT+CIPSEND=POST /api/v1.6/devices/esp8266/?token=A1E-

IcAmTnS42aTf79zQsmiNnk0kZ9aBVy HTTP/1.1\r\nHost: things.ubidots.com\r

\nContent-Type: application/json\r\nContent-Length: 153\r\n\r\n

{"temperature": 30, "humidity": 35, "heartbeat": 72)}\r\n\r\n

this is the command I’m using to post the data.

can anyone help me to find the mistake I had done?

this is the code im running in my arduino uno

#include <DHT.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>


LiquidCrystal lcd(13,12,11,10,9,8);

SoftwareSerial esp(2, 3);                   //RX,TX

#define DHTPIN 5                            // Digital Pin 5
#define DHTTYPE DHT11                       // We are Using DHT11
DHT dht(DHTPIN, DHTTYPE);                   // Initialising Pin and Type of DHT

String ssid = "JioFi2_05";                           //wifi name  
String password = "80199902";                       //wifi password

String server = "things.ubidots.com";                         //ubidots server name

String token = "A1E-IcAmTQsmiNnk0kZ9aBVy";                          //ubidots token
String esp8266_d_id = "5a6c03f97401ad193ee";                   //esp8266device id

String temperature_id = "5a8e926897071ebcfede";                 //temperature variable id
String humidity_id = "5a8af973fb8083e6b";                    //humidity variable id
String heartbeat_id = "5a8973f614e9049";                   //heartbeat variable id

void connectWifi()                           //wificonnection
{

  String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
  esp.println(cmd);
  delay(4000);
  
  if(esp.find("OK")){
    Serial.println("WIFI Connected!");
    lcd.setCursor(0, 0);
    lcd.println("WIFI Connected!");
    delay(1000);
  }
  else {
  
  Serial.println("Cannot connect to wifi");
  connectWifi();
  }
}

void reset() {

  esp.println("AT+RST");
  delay(1000);
  if(esp.find("OK") ) Serial.println("Module Reset");

}

void esp_buad_rate_change()
{
  esp.println("AT+CIOBAUD=9600");                  //change buad rate
  Serial.println("AT+CIOBAUD=9600");
}

void wifisetup()
{
  esp.println("AT");
  esp.println("AT+CWMODE=3");
  Serial.println("Wifi ready");
  
}
//void ubi_send_data(String, String);
//int heartbeat_get();

void setup()
{
  Serial.begin(115200);
  esp.begin(115200);
  reset();
  esp_buad_rate_change();             //changing buad rate
  Serial.begin(9600);
  esp.begin(9600);
  lcd.begin(16,2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.println("Wifi Setup");
  wifisetup();                         //wifi setup
  connectWifi();
}


void loop() {

  float hum =  dht.readHumidity();             // Reading Humidity Value

  float tem = dht.readTemperature();        // Reading Temperature Value

  int    heart = 72;//heartbeat_get();                 //  reading heartbeat value
  
  float  val=11.21;
  double lati=17.367896,longi=83.798975;
  
  long tm=1000199992;
  
  //top:
  String cmd = "AT+CIPSTART=\"TCP\",\"" + server + "\",80";
  esp.println(cmd);
  delay(3000);

  if(esp.find("OK"))
  {
    Serial.println("server found");
  String poster= "{\"temperature\": "+String(tem)+", \"humidity\": "+String(hum)+", \"heartbeat\": "+String(heart)+"}\r\n\r\n";
  int len=poster.length();
  Serial.println(poster);
  String post = "POST /api/v1.6/devices/esp8266/?token="+token+" HTTP/1.1\r\n";
         
         post+= "Host: things.ubidots.com\r\nContent-Type: application/json\r\nContent-Length: ";
         delay(1000);
         post+= String(len)+"\r\n\r\n";
         //", \"gps\":{\"value\":"+String(val)+", \"context\":{\"lat\":"+String(lati)+", \"lng\":"+String(longi)+"}, \"timestamp\":"+String(tm)+"}
         post+=poster;
         
  Serial.print(post);
  String sendData = "AT+CIPSEND="+String(post.length());
  Serial.print(sendData);
  esp.print(sendData);
  Serial.print(sendData);
  delay(6000);
    if (esp.find(">"))
    {
      Serial.println("SENDING  data"); 
    }
    else
    {
       Serial.println("NOT SENDING");
       esp.println("AT+CIPCLOSE");
    }
  }
  else
  {
    Serial.println("server not found");
    //goto top;
  }
  delay(10000);
}


#2

Greetings @anil801999,

I see a 408 timeout response from your image so this seems to be an issue at your device side, I advise you to make first all the pre-flight checks for the ESP8266 to discard any device or network issue and once you do that successfully try to connect sending a request that fits the Ubidots REST API.

BTW, inside your payload I see an error:

{"temperature": 30, "humidity": 35, "heartbeat": 72)}

there is a parentesis after the number ‘72’, which is invalid for json http format so delete it.

Regards