从esp8266发送HTTP post请求

时间:2016-11-18 20:50:47

标签: http-post arduino-uno at-command esp8266

我想从esp8266发送温度数据。我改编了this Instructable的代码;这是我的代码:

#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") ;
}

我尝试使用esp8266库发送,但它不起作用。

1 个答案:

答案 0 :(得分:1)

我读了代码,我在这些行中发现了一点错误:

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

wifi.println("AT+CIPCLOSE") ;

您的ESP8266已准备好接收数据,但您没有发送任何内容,请添加此行:)

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

wifi.println(Post); // this line

wifi.println("AT+CIPCLOSE") ;

该过程如下,例如

ARDUINO发送:

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

ESP8266回复:

CONNECT
ok

ARDUINO发送:

AT + CIPSEND = 45

其中45是整个框架的大小

ESP8266回复:

>

>表示它希望接收数据。数据在此处发送。

最后

ARDUINO发送:

AT + CIPCLOSE

ESP8266回复:

Closed

ok