I've got problem :/ I want to use ESP8266 in my Arduino project, but I can't send any data to my server using POST or GET...(My testing code below). I attached my Arduino serial print. ESP8266 work fine when I sending data from my website using GET http request(using jquery),but from another side no... Why? I will be thankful for help.
ESP8266 firmware version - ESP8266 v0.9.5.0 AT
#include "SoftwareSerial.h"
SoftwareSerial esp(2, 3);// RX, TX
String data;
String server = "www.xxxxx.cba.pl";
String uri = "/skrypty/add_kettleUse.php";
void setup()
{
esp.begin(9600);
Serial.begin(9600);
reset();
connectWifi();
}
//reset the esp8266 module
void reset()
{
esp.println("AT+RST");
delay(1000);
if(esp.find("OK")) Serial.println("Module Reset");
}
//connect to your wifi network
void connectWifi()
{
String cmd = "AT+CWJAP=\"SSID\",\"hasło\"";
esp.println(cmd);
delay(4000);
if(esp.find("OK"))
{
Serial.println("Connected!");
}else{
connectWifi();
Serial.println("Cannot connect to wifi");
}
}
void loop ()
{
data = "waterVolume=250&kettleStatus=1";// data sent must be under this form //name1=value1&name2=value2.
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);
String postRequest =
"GET www.xxxxx.cba.pl/skrypty/add_kettleUse.php?waterVolume=250&kettleStatus=1 HTTP/1.1\r\n" +
"Host:" + server + "\r\n\r\n";
//String postRequest =
//
//"POST " + uri + " HTTP/1.1\r\n" +
//"Host: " + server + "\r\n" +
//"Accept: *" + "/" + "*\r\n" +
//"Content-Length: " + data.length() + "\r\n" +
//"Content-Type: application/x-www-form-urlencoded\r\n" +
//"\r\n" + data;
esp.print("AT+CIPSEND=");
esp.println(postRequest.length() );
delay(500);
if(esp.find(">"))
{
Serial.println("Sending..");
esp.print(postRequest);
// long int time = millis();
// String response="";
// while ((time + 1000) > millis()) {
//
// while (esp.available()) {
// char c = esp.read(); // read the next character.
// response += c;
//
// }}
// Serial.println(response);
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");
}
}
}
@EDIT
I was able to send a GET request, but I recived "Bad Request 400" and it work only for message lenght set at 100 char(AT+CIPSEND=4,100) any other length value don't work.... Why!?
void httppost () {
esp.println("AT+CIPSTART=4,\"TCP\",\"" + server + "\",80");//start a TCP connection.
if( esp.find("OK")) {
Serial.println("TCP connection ready");
}
postRequest = "GET www.xxx.cba.pl/skrypty/add_kettleUse.php?waterVolume=250&kettleStatus=1 HTTP/1.1\r\n Host: www.xxx.cba.pl\r\n Connection: close\r\n";
String sendCmd = "AT+CIPSEND=4,";//determine the number of caracters to be sent.
esp.print(sendCmd);
esp.println(100);
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");
}
}}
@EDIT
Problem is probably connection with "\r\n" instruction and Arduino Serial port but why? When I try using ESP8266 in Realterm everything work fine. Any one can help? I tried firmware 0.9.2.2 and 0.9.5.0 and it's the same problem....