Greeting all,
I am working on a simple IoT device (ESP 8266)
Tried to send request on local Server (XAMPP). After following serveral if not all similar topics I was able to send data successfully from dht11 to xampp database.
The problem is after successful entry of Data into database, the HTTP response is followed by a 400 Bad request. It's quite disturbing since data has been sent.
I would like to know where I am going wrong or an explanation as to why i get two responses from one request.
Thanks in advance!
Below is my code
float humidity;
float temperature;
String ssid = "Wifi";
String psk = "psk;
String host = " 192.168.45.110";
String getData;
String myHost = "Host:"+host;
void setup() {
Serial.begin(9600);
Serial.println("Serial Connected!");
dht.begin();
mySerial.begin(9600);
while(!mySerial){}
delay(1000);
sendCmd("AT", "OK");
sendCmd("AT+CWMODE_CUR=1", "OK");
sendCmd("AT+CWJAP_CUR=\""+ssid+"\",\""+psk+"\"", "OK");
delay(500);
}
void loop() {
temperature = dht.readTemperature();
humidity = dht.readHumidity();
getData = "GET /esp/index.php?temp="+String(temperature)+"&humid="+String(humidity)+"\r\nHTTP/1.1\r\n"+myHost+"\r\nConnection:close\r\n\r\n";
sendCmd("AT+CIPSTART=\"TCP\",\""+host+"\",80", "OK");
sendCmd("AT+CIPSEND="+String(getData.length()), ">");
mySerial.print(getData);
checkSerial();
}
void checkSerial(){
if (mySerial.available()) {
Serial.println(mySerial.readString());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
void sendCmd(String cmd, char reply[]){
Serial.println(cmd);
int maxTime = 10;
int counter = 0;
while(counter<maxTime){
mySerial.println(cmd);
if(mySerial.find(reply)){
Serial.println("Success");
break;
}else{
while(mySerial.available()){
Serial.write(mySerial.read());
}
}
counter++;
delay(500);
}
if(counter == maxTime){
Serial.println("Connection Timed out");
}
}
Recv 95 bytes
SEND OK
+IPD,382:DB Created
TBL Created
Data inserted
//output from .php file if data is inserted into db.
Bad Request
Your browser sent a request that this server could not understand.
Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.14 Server at localhost Port 80 CLOSED