Hello.
I'm facing a problem with my ESP8266 (ESP-01) connected to Arduino Uno. I can connect to my wifi module, AT command returns ok, then I can connect to my wifi, but the problem is when I try to send request to any server.
Literally I don't receive any response after GET or POST request is send. Any "SEND OK" or errors, nothing.
One thing which is received after request is just chain of weird characters.
I tried several codes, written by myself and from Internet and always it's the same issue. Here is current code which I use (from this thread: Forming HTTP POST request on Arduino w/ ESP8266 Wifi - Programming Questions - Arduino Forum):
#include "SoftwareSerial.h"
#include "Arduino.h"
//connection info for WiFi router
String ssid = "wifi_name";
String password = "wifi_password";
//create SoftSerial object for communication with ESP8266
SoftwareSerial esp(10, 11); // RX, TX
String data; //data for the POST request
String server = "httpbin.org"; // www.example.com
String uri = "post"; //server side script to call
//reset the esp8266 module
void reset() {
esp.println("AT+RST");
delay(3000);
if (esp.find("OK"))
Serial.println("Module Reset");
}
//connect to your wifi network
void connectWifi() {
String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
esp.println(cmd);
delay(4000);
if (esp.find("OK")) {
Serial.println("Connected!");
}
else {
connectWifi();
Serial.println("Cannot connect to wifi");
}
}
void setup() {
esp.begin(9600); //start SoftSerial for ESP comms
Serial.begin(9600); //start regular serial for PC -> Arduino comms
reset(); //call the ESP reset function
connectWifi(); //call the ESP connection function
}
void loop() {
int temp1 = 73;
int temp2 = 231;
int temp3 = 12;
data = String("temp1=");
data = data + temp1 + "&temp2=" + temp2 + "&temp3=" + temp3;
httppost();
delay(7000);
}
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 = String("POST ");
postRequest = postRequest + 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;
Serial.println("Post Request text: "); //see what the fully built POST request looks like
Serial.println(postRequest);
String sendCmd = "AT+CIPSEND="; //determine the number of caracters to be sent.
esp.print(sendCmd);
esp.println(postRequest.length());
delay(500);
if(esp.find(">")) {
Serial.println("Sending..");
esp.print(postRequest);
Serial.println("Data to send: ");
Serial.println(data);
Serial.print("Data length: ");
Serial.println(data.length());
Serial.print("Request length: ");
Serial.println(postRequest.length());
Serial.println("Request Sent:");
Serial.println(postRequest);
while (esp.available()) {
String tmpResp = esp.readString();
Serial.println(tmpResp);
}
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");
}
}
}
And here is output from Serial Monitor:
Module Reset
Module Reset
Connected!
TCP connection ready
Post Request text:
POST post HTTP/1.1
Host: httpbin.org
Accept: */*
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
temp1=73&temp2=231&temp3=12
Sending..
Data to send:
temp1=73&temp2=231&temp3=12
Data length: 27
Request length: 150
Request Sent:
POST post HTTP/1.1
Host: httpbin.org
Accept: */*
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
temp1=73&temp2=231&temp3=12
P⸮⸮⸮⸮BE))⸮.'h艥⸮R⸮i⸮tܵ⸮⸮⸮⸮⸮ˋU⸮⸮ ⸮⸮⸮2%⸮⸮⸮⸮YYt⸮⸮ݚ2⸮
TCP connection ready
Post Request text:
POST post HTTP/1.1
Host: httpbin.org
Accept: */*
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
temp1=73&temp2=231&temp3=12
TCP connection ready
Post Request text:
POST post HTTP/1.1
Host: httpbin.org
Accept: */*
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
temp1=73&temp2=231&temp3=12
Sending..
Data to send:
temp1=73&temp2=231&temp3=12
Data length: 27
Request length: 150
Request Sent:
POST post HTTP/1.1
Host: httpbin.org
Accept: */*
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
temp1=73&temp2=231&temp3=12
P⸮⸮⸮⸮BE))⸮.'⸮tॹzR⸮i⸮tܵ⸮⸮⸮⸮⸮ˋU⸮:⸮⸮⸮⸮⸮⸮tdKW⸮⸮⸮YYt⸮⸮ݚ2⸮
So as you can see I try to send something to the test server httpbin.org/post which should return the same what I sent, but I don't receive anything. Those weird characters are obviosly visible only when I remove this
if( esp.find("SEND OK"))
if statement, because otherwise I would see nothing, because "SEND OK" is never present.
I don't have any idea what I'm doing wrong, please help me and show right direction