-1

I want to send temperature data from an esp8266. I've adapted my code from this Instructable; this is my code:

#include "SoftwareSerial.h"
#include "ESP8266.h"

String ssid = "Red Wi-Fi de Alexis" ;
String password = "adrian2003" ;

SoftwareSerial wifi(4, 2) ;

String Host = "mail.interseccion.com.mx:8901" ;
String Url = "/Ws_Temperatura" ;

void setup()
{
  wifi.begin(9600) ;
  Serial.begin(9600) ; 
  wifi.println("AT+RST") ;
  wifi.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"") ;
}

void loop()
{
  float tmp = 22.22 ;

  //URL Temperatura
  //URL: mail.interseccion.com.mx:8901/Ws_Temperatura?Id_temp=0&Id_Device=1&Valor=00.00&Temperatura_Action=Insert
  String urluno = String("Id_temp=0&Id_Device=2&Valor=");
  String temp = String(tmp);
  String urldos = String("&Temperatura_Action=Insert");
  String urlfinal = String(String(urluno) + String(tmp) + String(urldos));

  int tamano = urlfinal.length() ;

  wifi.println("AT+CIPSTART=\"TCP\",\"" + Host + "\", 8901") ; 

  String Post = "POST" + Url + "HTTP/1.1\r\n" + 
  "Host: " + Host + "\r\n" + 
  "Content-Type: application/x-www-form-urlencoded" +
  "Content-Length: " + tamano + "\r\n" + urlfinal  ;


  wifi.println("AT+CIPSEND= " + Post.length()) ;

  wifi.println("AT+CIPCLOSE") ;
}

I tried to send with esp8266 library but it doesn't work.

Haldean Brown
  • 12,411
  • 5
  • 43
  • 58
Maclos
  • 7
  • 1
  • 3

1 Answers1

1

I read the code, and I detected a little error in these lines:

wifi.println("AT+CIPSEND= " + Post.length()) ;

wifi.println("AT+CIPCLOSE") ;

Your ESP8266 is ready to receive data, but you are not sending anything, so add this line :)

wifi.println("AT+CIPSEND= " + Post.length()) ;

wifi.println(Post); // this line

wifi.println("AT+CIPCLOSE") ;

The process is as follows for example

ARDUINO sends:

AT + CIPSTART = "TCP", www.google.com, 80

ESP8266 responds:

CONNECT
ok

ARDUINO sends:

AT + CIPSEND = 45

where 45 is the size of the entire frame

ESP8266 responds:

>

> means that that it expects to receive data. The data is sent here.

and finally

ARDUINO sends:

AT + CIPCLOSE

ESP8266 responds:

Closed

ok
gre_gor
  • 6,669
  • 9
  • 47
  • 52