hi to everybody
i'm trying to update a .txt file with data acquired by an arduino.
i have connected an esp8622 to an arduino mega (on serial1) and throught at commands im trying to POST data on a php page which will update the .txt file with the recieved data.
ARDUINO CODE:
String ssid = "ssid";
String password = "ssid_pwd";
String server = "server_ip_address";
String uri = "/test.php";
byte dat[5];
String temp;
void setup() {
Serial1.begin(115200);
Serial.begin(115200);
reset();
connectWifi();
}
//reset the Serial18266 module
void reset() {
Serial1.println("AT+RST");
delay(1000);
if (Serial1.find("OK")) Serial.println("Module Reset");
}
//connect to your wifi network
void connectWifi() {
String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
Serial.println(cmd);
Serial1.println(cmd);
delay(100);
if (Serial1.find("OK")) {
Serial.println("Connected!");
}else {
Serial.println("Cannot connect to wifi ...");
Serial.println("re-trying ...");
connectWifi();
}
Serial.flush();
while (Serial1.available()) {
Serial1.read();
}
}
void loop() {
// data sent must be under this form //name1=value1&name2=value2.
String data = "temperature=23";// + String(analogRead(A0));
httppost(data);
delay(10000);
}
void httppost(String data) {
Serial1.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
if (Serial1.find("OK")) {
Serial.println("TCP connection ready");
}
Serial.flush();
while (Serial1.available()) {
Serial1.read();
}
delay(100);
String postRequest =
"POST " + uri + " HTTP/1.0\r\n" +
"Host: " + server + "\r\n" +
"Content - Type: application/x-www-form-urlencoded\r\n" +
"Content - Length: " + data.length() + "\r\n" + "\r\n"+
data + "\r\n";
Serial.println(postRequest);
String sendCmd = "AT+CIPSEND=" + String(postRequest.length());//determine the number of caracters to be sent.
Serial1.println(sendCmd);
delay(100);
if (Serial1.find(">")) {
Serial.println("Sending..");
Serial1.println(postRequest);
if (Serial1.find("SEND OK")) {
Serial.println("Packet sent");
while (Serial1.available()) {
Serial.println(Serial1.readString());
}
}
}
// close the connection
Serial1.println("AT+CIPCLOSE");
Serial.flush();
while (Serial1.available()) {
Serial1.read();
}
}
PHP PAGE CODE:
<?php
$temperature = filter_input(INPUT_GET, 'temperature');
$myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
fwrite($myfile, $temperature);
fwrite($myfile, " C\n");
fclose($myfile);
?>
when i type on the browser htttp://server_ip_address/test.php?temperatura=23
i can see that the "newfile.txt" has been updated properly.
this bring me to think that the php code works fine. Am i correct?
the problem is that when i upload and run i get the following response over seral:
AT+CWJAP="ssid","ssid_pwd"
Connected!
TCP connection ready
POST /test.php HTTP/1.1
Host: xxx.xxx.xxx.xxx
Content - Type: application/x-www-form-urlencoded
Content - Length: 14
temperature=34
Sending..
Packet sent
+IPD,418:HTTP/1.1 400 Bad Request
Date: Tue, 02 Oct 2018 20:52:04 GMT
Server: Apache/2.4.34 (Win64) PHP/7.2.10
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.
</p>
</body></html>
CLOSED
and the .txt does not update.
what is that i'm missing?
wrong AT code?
mistakes in the http post?
is it correct to type html1.0?
should i follow another path?
is there anythig i sholud set on the apache server?