I'm trying to insert some values into MySQL database using nodemcu esp8266, but I'm getting 404 bad request your browser sent invalid request. I have applied all the solutions which i have seen in the arduino forum but nothing works for me. Please help me. Here is the code.
#include <ESP8266WiFi.h>
const char* ssid = "RAHUL";
const char* password = "emorababu";
const char* host = "www.screccs.com";
const char* passcode = "xxxxxxx";
// Wake from sleep, in seconds.
#define wakeuptime 30
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(5000);
// Connect to host
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
}
void loop() {
WiFiClient client;
const int httpPort = 80;
if (client.connect(host, httpPort))
{
// Create a URL for the request. Modify YOUR_HOST_DIRECTORY so that you're pointing to the PHP file.
String id="14kq1a1229";
String msg="hello";
String txData = "id="+id+"&msg="+msg;
Serial.println("\nconnected...");
Serial.println("ARDUINO: forming HTTP request message");
client.println("POST /db.php HTTP/1.1 \r");
client.println("Host: www.screccs.com \r");
//client.println("From: Arduino1 ");
// client.println("User-Agent: HTTPTool/1.0");
client.println("Accept: */** \r");
client.print("Content-Length:");
client.println(txData.length());
client.println("Content-Type: application/x-www-form-urlencoded \r");
// client.println("Connection: close");
// client.println(txData);
client.println("/r");
Serial.println("ARDUINO: HTTP message sent");
delay(3000);
if(client.available())
{
Serial.println("ARDUINO: HTTP message received");
Serial.println("ARDUINO: printing received headers and script response...\n");
while(client.available())
{
char c = client.read();
Serial.print(c);
}
}
else
{
Serial.println("ARDUINO: no response received / no response received in time");
}
client.stop();
}
else
{
Serial.println("connection failure");
}
delay(2000);
/*
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
*/
Serial.println();
Serial.println("Closing connection");
// Sleep
Serial.println("Going to sleep");
delay(5000);
//ESP.deepSleep(wakeuptime * 1000000, WAKE_RF_DEFAULT);
delay(5000);
}
And I'm receiving the below response from server
ARDUINO: HTTP message received
ARDUINO: printing received headers and script response...HTTP/1.0 400 Bad request
Cache-Control: no-cache
Connection: close
Content-Type: text/html400 Bad request
Your browser sent an invalid request.Closing connection
Going to sleep