0

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?

  • 2
    a valid HTTP request needs more the a GET line. you must send some mandatory HTTP headers. – Juraj Apr 3 '18 at 4:45
  • 2
    You cannot use SoftwareSerial at such a high baud rate. Try 9600. – tttapa Apr 3 '18 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" – Gerben Apr 3 '18 at 9:33
  • 2
  • @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 – ali_m Apr 3 '18 at 23:59
0

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.