-->
Page 1 of 2

I can't send any data to web server using GET or POST

PostPosted: Wed Aug 31, 2016 2:54 pm
by kluszon
Hello everyone!

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

Code: Select all#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!?

Code: Select allvoid 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....

Re: I can't send any data to web server using GET or POST

PostPosted: Thu Sep 15, 2016 3:08 am
by kluszon
Anyone can help?

Re: I can't send any data to web server using GET or POST

PostPosted: Fri Sep 16, 2016 9:39 am
by martinayotte
Your request should only include the URL path, not the hostname.

Code: Select allpostRequest = "GET /skrypty/add_kettleUse.php?waterVolume=250&kettleStatus=1 HTTP/1.1\r\n Host: www.xxx.cba.pl\r\n Connection: close\r\n";

Re: I can't send any data to web server using GET or POST

PostPosted: Mon Sep 19, 2016 5:04 am
by kluszon
I tried it, but it don't work too.

@EDIT - Problem Solved!

1. You can't manual send GET or POST request using Arduino IDE and Arduino Serial Monitor, becauese there is problem with coding "\r\n" - carriage return and new line. ( At least I can't do this, though I spent a lot of time on it)
2. Very important things are delays ( delay() ) between AT commands.
3. Correct GET request.

I correct all errors and everything is working fine! :)

Below I attached my code:

Code: Select all#include "SoftwareSerial.h"

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

String server = "adres_serwera"; // np. www.examble.pl albo adres ip 129.75.1.32

void setup()
{
  ESP8266.begin(115200); //odpowiednia prędkość dla twojego modułu
  Serial.begin(115200);
  Serial.print("Start");
  reset();
  ESP8266.println("AT+CWMODE=1");
  delay("1000");
  ESP8266.println("AT+CIPMUX=1");
  delay("1000");
 
  connectWifi();
}

void loop()
{
  String cmd;
  cmd = "AT+CIPSTART=1,\"TCP\",\"";
  delay(500);
  cmd += server;
  delay(500);
  cmd += "\",80";
  delay(500);
  ESP8266.println(cmd);
  delay(500);
 
  if(ESP8266.find("OK"))
  {
    Serial.println("Connected to the web!");
  }
 
  float weight = 257;
  bool state = 1;
  cmd =  "GET /skrypty/add_k.php?w=" + (String)weight + "&k=" + (String)state + " HTTP/1.1\r\n"; 
  delay(500);
  cmd += "Host: www.kluszon.cba.pl\r\n\r\n";
  delay(500); 
  ESP8266.print("AT+CIPSEND=1,");
  delay(500);
  ESP8266.println(cmd.length()); 
  delay(500);
 
  if(ESP8266.find(">"))
  {
    Serial.println(">");
  }
 
  ESP8266.println(cmd);
  delay(500);
 
  if(ESP8266.find("OK"))
  {
    Serial.println("send!");
  }
}

void connectWifi()
{

  String cmd = "AT+CWJAP=\"My_wifi\",\"hasło\"";
  ESP8266.println(cmd);
  delay(4000);
 
  if(ESP8266.find("OK"))
  {
    Serial.println("Connected!");
  }else{
    connectWifi();
    Serial.println("Cannot connect to wifi");
  }
}

void reset()
{
  ESP8266.println("AT+RST");
  delay(1000);
  if(ESP8266.find("OK")) Serial.println("Module Reset");
}