0

I have an Arduino with few sensors, and I want to send all of data to my API over ESP8285 what is connected to the wifi.

Its almost working fine, except one thing.

I have these AT commands:

String postRequest =
    "POST /hello HTTP/1.1" + String("\r\n") +
    "Host: 192.168.88.186" + "\r\n" +
    "Cache-Control: no-cache" + "\r\n" +
    "Content-Type: application/x-www-form-urlencoded" + "\r\n" +
    "Content-Length: " + String(data.length()) + "\r\n\r\n" +
    String(data) + "\r\n";

void connectWifi()
{
  espclient.println("AT+CWMODE=1");
  String connectcmd = "AT+CWJAP=\"" + ssid + "\",\"" + w_password + "\"";
  espclient.println(connectcmd);

  delay(3000);
  if (espclient.find("OK"))
  {
    Serial.println("Connected!");
  }
  else
  {
    connectWifi();
    Serial.println("Cannot connect to wifi");
  }
}

void espStatus()
{
    String tmpResp = espclient.readString();
    Serial.print("ESPLOG:");
    Serial.println(tmpResp);
}

void httppost()
{
  espclient.println("AT+CIPMUX=0");
  espclient.println("AT+CIPSTART=\"TCP\",\"" + server + "\",5000"); //start a TCP connection.
  if (espclient.find("OK"))
  {
    Serial.println("TCP connection ready");
  } else {
    espStatus();
  }
  espclient.println("AT+CIPSTATUS");
  espStatus();

  Serial.println("PRINT REQUEST ==================================");
  Serial.println(postRequest);
  Serial.println("PRINT REQUEST ==================================");

  espclient.println("AT+CIPSEND="+String(postRequest.length()));
  do {
    delay(1000);
    if (espclient.find(">"))
    {
      break;
    }
  }
  while(true);
  espclient.print(postRequest);
  delay(500);
  espStatus();
  espclient.println("AT+CIPCLOSE");
}

SETUP:

void setup() {
  Serial.begin(9600);
  Serial.println("Starting the modules....");

  espclient.begin(9600);
  connectWifi();
  Serial.println("ESP Module is ready");
}

LOOP:

void loop() {
    httppost();
    delay(5000);
}

And I get this error(?) message:

TCP connection ready
ESPLOG: 
AT+CIPSTATUS

STATUS:2

OK

PRINT REQUEST ==================================
POST /hello HTTP/1.1
Host: 192.168.88.186
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
Content-Length: 13
name=ESPModul

PRINT REQUEST ==================================
ESPLOG: Q�J5U�S�!*HZYlo QQA��HV�^\��
H��銒)       �&�C��y \�R
�r�+����H�

TCP connection ready
ESPLOG: 
AT+CIPSTATUS

STATUS:2

OK

PRINT REQUEST ==================================
POST /hello HTTP/1.1
Host: 192.168.88.186
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
Content-Length: 13
name=ESPModul

PRINT REQUEST ==================================
ESPLOG: Q�J5U�S�!*HZYlo QQA��HV�^\��
H����)      �&AC��y \�R
�r�+����H�

UPDATE The new /error/ message:

ESPLOG:Q�J5U�SS�CT��he���EE))���H��
����    )�H��HK�
Ca����E�K�V,YCCb

UPDATE2 I restarted the arduino, and now I am getting this:

TCP connection ready
PRINT REQUEST ==================================
POST /hello HTTP/1.1
Host: 192.168.88.186
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
Content-Length: 13

name=ESPModul

PRINT REQUEST ==================================
ESPLOG:
AT+CIPSTATUS

STATUS:2

OK
AT+CIPSEND=155

link is not vAT+CIPCLOSE


ERROR

UPDATE3: This method is working

void testHTTP() 
{
  Serial.println("Establishing TCP connection");
  espclient.println("AT+CIPSTART=\"TCP\",\"" + server + "\",5000");
  delay(1000);
  Serial.println("Sending out data…");
  espclient.println("AT+CIPSEND=16");
  espclient.println("GET / HTTP/1.1");
  espclient.println();
  espclient.println("AT+CIPCLOSE");
}

If I connect the ESP to my PC with TTL adapter, and I write the commends manually, then its working fine. And yes, the ESP is connecting correctly to the wifi, I tested it.

The main solution was to put "delay(1000);" after "CIPSEND=".

I hope my fumbling can help to someone in the future.

  • add one more \r\n for mandatory empty line after HTTP request header. "Content-Length: " + String(data.length()) + "\r\n\r\n" +. – Juraj May 1 '19 at 20:32
  • Serial.println("ESPLOG: "+tmpResp), prints some random memory content at address calculated by the +. use Serial.print("ESPLOG:"); Serial.println(tmpResp); – Juraj May 1 '19 at 20:38
  • wait for > after CIPSEND – Juraj May 1 '19 at 20:41
  • @Juraj: No, why? Overload resolution for "ESPLOG: "+tmpResp will interpret it as String("ESPLOG: ") + tmpResp, which uses overloaded binary + for String type. Everything is correct in that expression: it does concatenate two Strings into a new String. It is unnecessarily "heavy", but otherwise perfectly correct. – AnT May 1 '19 at 22:13
  • @Juraj Thanks for the advice, I tried what you said, but I have the same issue. The new error message is a bit different. I added the new message to the post. – G3ph4z May 1 '19 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.