I am prototyping a piece of my project that will send temperature data to a database on a remote website. For this I have setup a MySQL database and website on the free hosting service 000webhost. I have written a very simple server side PHP script that should add a row of temp data to the database via a HTTP POST.
I have tested all the web server side functionality and confirmed that it is working just fine by using requestmaker.com to "manually" send the POST request.
But every attempt I have made to update the table via my Arduino code has failed with a 400 Bad Request response from the server. The response doesn't really give me a lot of details to go on so my troubleshooting so far has been just pretty much random trial and error in forming the POST request and I am getting nowhere.
If anyone can spot an obvious problem in my code, or suggest some better ways to go about troubleshooting I would greatly appreciate it.
Here is the Arduino sketch:
#include "SoftwareSerial.h"
#include "Arduino.h"
//connection info for WiFi router
String ssid = "my_SSID";
String password = "my_password";
//create SoftSerial object for communication with ESP8266
SoftwareSerial esp(2, 3); // RX, TX
String data; //data for the POST request
String server = "rksiii.000webhostapp.com"; // www.example.com
String uri = "add.php"; //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);
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 the actual text response I get back from the server as it shows up in the serial monitor:
+IPD,424:HTTP/1.1 400 Bad Requeerver: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Request-ID: 698d62d8b91ac68f1257d93fe0677488
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Looking forward to any help I can get or new directions to try. Thank you.