This problem seems complex to me and I'll outline it as best I can below, but the short of it is that I am trying to troubleshoot a 400 server response. I've looked at the data string (the POST request) that the Arduino sends, and it looks ok, but I need to see what the server is receiving. I suspect the POST data is not sending as it should, but I don't know how to read this from my php page. Is there a PHP command to echo something other than 400 if the POST string is not correct?
I have an ethernet connection POSTing data to a PHP page. It is working fine on a Pro Mini with Optiboot, but now I am porting it to a self-made board with a 328 (which I've been told the standard bootloader is Optiboot, but watchdog, itself, is not the problem).
The system posts to 4 different tables: 1)Startup report; 2)"heartbeat"; 3)Pump runtime and amps; and 4)Switch changes.
Though they all use similar code, the Runtime and change tables return a 400 message (below). Also, if I have the watchdog enabled, this causes a reset bc it takes 15 seconds (which is probably normal since it is waiting for the server).
I suspect that the problem with the code has to do with the conversion of the raw data into the String, but I do not know how to go about troubleshooting this as I can't find it with a simple Serial.print.
Here is the code I am sending the server (the server address is changed for privacy):
Serial.print("Out of delay"); // -------------
Serial.println(millis());
byte runtime = analogRead(A3);
float amps = analogRead(A2) / 100; // changed for simplicity
char cruntime[4]; // create character array for runtime
char camps[5]; // create character array for amps
dtostrf(runtime, 4, 0, cruntime); // convert to string
dtostrf(amps, 2, 1, camps);
String sruntime = String(cruntime);
sruntime.trim(); // NOT SURE IF THIS IS NEEDED
String samps = String(camps);
samps.trim();
int memory = freeMemory();
Serial.print("Free Memory: ");
Serial.println(memory);
String strmem = String(memory);
String cwpdata = "serial=test&runtime=" + sruntime + "&s=" + samps;
Serial.println(cwpdata);
wdt_reset();
Serial.print("Sending: ");
Serial.println(millis());
if (client.connect(server, 80)) {
postData(cwpdata, "addcwp");
}
Serial.print("Sent: ");
Serial.println(millis());
With the postData function:
void postData(String inputData, String filename) {
Serial.println("connected");
// Make a HTTP request:
client.println("POST /dev/telemetry/test/" + filename + ".php HTTP/1.1");
client.println("Host: www.SERVER.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(inputData.length());
Serial.println(inputData);
Serial.print("Data Length: ");
Serial.println(inputData.length());
client.println("Connection: close");
client.println();
client.println(inputData);
client.println();
wdt_reset();
unsigned long startMillis = millis();
unsigned long endMillis = startMillis + timeout;
while(client.connected() && (millis() < endMillis)) {
while(client.available()) {
char ch = client.read();
Serial.write(ch);
}
wdt_reset();
}
// if the server's disconnected, stop the client:
Serial.println();
client.stop();
int memory = freeMemory();
if (memory < 275) { // if memory is low, reset uC
delay(15000);
}
}
Here is the response. The numbers 48000 to 64000 are the millis() readings at each point.
Starting Loop: 43794
Out of delay48794 // millis
Free Memory: 308
serial=test&runtime=15.0&s=5.0
Sending: 48796 // millis
connected
serial=test&runtime=15.0&s=5.0
Data Length: 33
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 02 Dec 2015 16:17:55 GMT
Content-Type: text/html
Content-Length: 166
Connection: close
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Sent: 64086 // millis
And my PHP code is simply:
<?php
include("connect.php");
$link=Connection();
$serial=$_POST["serial"];
$runtime=$_POST["runtime"];
$amps = $_POST["amps"];
$query = "INSERT INTO `test_cwp` (`serial`, `runtime`, `amps`)
VALUES ('".$serial."','".$runtime."','".$amps."')";
mysql_query($query,$link);
mysql_close($link);
echo "Pump Add Success!!!";
?>