1

Project goal is to control a relay over web. I made a php page that receives the post data from Arduino then store the values sent into a mysql database (which is the relay status now). Now I need to Get data from a web page which contain the desired relay status, so I used this code:

#include <SoftwareSerial.h>
String ssid ="ABC";
String password="a14102016a";
const byte rxPin = 6;
const byte txPin = 7;
SoftwareSerial ESP8266 (rxPin, txPin);
unsigned long lastTimeMillis = 0;
void setup() {
 Serial.begin(115200); 
 ESP8266.begin(115200);
 reset();
 connectWifi();
 delay(2000);
}
//reset the esp8266 module
void reset() {
ESP8266.println("AT+RST");
delay(1000);
if(ESP8266.find("OK") ) Serial.println("Module Reset");
}
//connect to your wifi network
void connectWifi() {
String cmd = "AT+CWJAP=\"" +ssid+"\",\"" +password+ "\"";
ESP8266.println(cmd);
delay(4000);
if(ESP8266.find("OK")) {
Serial.println("Connected!");
}
else {
connectWifi();
Serial.println("Cannot connect to wifi"); }
}
void printResponse() {
 while (ESP8266.available()) {
 Serial.println(ESP8266.readStringUntil('\n')); 
 }
}
void loop() {
 if (millis() - lastTimeMillis > 30000) {
 lastTimeMillis = millis();
 ESP8266.println("AT+CIPMUX=1");
 delay(1000);
 printResponse();
 ESP8266.println("AT+CIPSTART=4,\"TCP\",\"192.168.1.161\",80");
 delay(1000);
 printResponse();
 String cmd = "GET /ard/sensor.html HTTP/1.1";
 ESP8266.println("AT+CIPSEND=4," + String(cmd.length() +4));
 delay(1000);
 ESP8266.println(cmd);
 delay(1000);
 ESP8266.println(""); 
 }
 if (ESP8266.available()) {
 Serial.write(ESP8266.read());
 }
}

But the response from server was

 Module Reset
Connected!
AT+CIPMV⸮=1
OK
AT+CIPSTART=4,#TCP","192.068.1.161",80
4,CONOECT
OK
AZ⸮⸮R5NDOi⸮j
O⸮C⸮⸮
Qecv ⸮&⸮ѕ⸮5
SEND OK
+IPD,4,501:HTTP/1.0 400 Bad Repuest
Date: Mon, 02 Aps 04eci5e3iC: -
T- /<eql>hs>retrubh>4Pea8
m4,CLOSED
AT+CIPMTX=1
OK
AT+CIPSTART=4,"TCP","192.068.1.161",80
4,CONNDCT
OK
AR⸮⸮R5⸮DOi⸮j
O⸮C⸮⸮
Qecv 33 bytes
SEND OK
+IPD,4,501;HTTP/1.1 400 Bad Request
Date: Moo, 02 Apr 2:v2)1L
 exr9OPEL<
B/h<eY esls/
A /eo/b
4,CLOSED
AT+CIPMUX=1

Sometimes I get HTTP/1.0 400 Bad Repuest and sometimes HTTP/1.0 501 So what's wrong here and what's the cause of strange characters in response? How to retrieve the correct data from web page?

VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
asked Apr 3, 2018 at 0:04
5
  • 2
    a valid HTTP request needs more the a GET line. you must send some mandatory HTTP headers. Commented Apr 3, 2018 at 4:45
  • 2
    You cannot use SoftwareSerial at such a high baud rate. Try 9600. Commented Apr 3, 2018 at 7:57
  • 1
    The http headers need to end with two newlines. Also use HTTP 1.0 instead, to prevent persistent connections. So try "GET /ard/sensor.html HTTP/1.0\r\n\r\n" Commented Apr 3, 2018 at 9:33
  • 2
    @Juraj GET is the only mandatory request header for HTTP 1.0. In HTTP 1.1 Host is also required. Commented Apr 3, 2018 at 9:36
  • 1
    @Juraj Thanks.. I've changed my request to be String cmd ="GET /ard/sensor.html HTTP/1.1\r\nUser-Agent: curl/7.37.0\r\nHost: %s\r\nAccept: */*\r\n\r\n"; ESP8266.println("AT+CIPSEND=4," + String(cmd.length())); Still no clear response Commented Apr 3, 2018 at 23:59

1 Answer 1

1

I flashed the ESP8266 with another firmware then it worked at baud 9600 perfectly The original firmware was not responding to the baud 9600 at all

answered Apr 7, 2018 at 0:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.