I'm having problems with my Arduino UNO and the ESP8266 module. I need to send values from Arduino to my PHP web page in this way:
http://www.server.com/km/db.php?id=53
the ESP8266 module is working fine, it powers up and connects to my Wi-Fi without problems, but I'm not able to send the values to my PHP page.
This is my code:
#include "SoftwareSerial.h"
String ssid ="NETGEAR80";
String password="mypassword";
SoftwareSerial esp(10, 11);// RX, TX
String data;
String server = "http://www.server.com";
String uri = "/km/db.php";//
int DHpin = 8;//sensor pin
byte dat [5];
String temp ,hum;
void setup() {
pinMode (DHpin, OUTPUT);
esp.begin(9600);
Serial.begin(9600);
reset();
connectWifi();
}
void reset() {
esp.println("AT+CWMODE_DEF=1");
esp.println("AT+RST");
delay(1000);
if(esp.find((char*)"OK") ) Serial.println("Module Reset");
}
void connectWifi() {
Serial.println("Collegamento");
String cmd = "AT+CWJAP_CUR=\"" +ssid+"\",\"" + password + "\"";
esp.println(cmd);
Serial.println(cmd);
delay(4000);
if(esp.find((char*)"OK")) {
Serial.println("Connected!");
}
else {
connectWifi();
Serial.println("Cannot connect to wifi"); }
}
void loop () {
temp=11;
data = "id=" + temp;// data sent must be under this form
httppost();
delay(1000);
}
void httppost () {
esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
if( esp.find((char*)"OK")) {
Serial.println("TCP connection ready");
} delay(1000);
String postRequest =
"POST " + uri + " HTTP/1.0\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(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((char*)">")) { Serial.println("Sending.."); esp.print(postRequest);
if(esp.find((char*)"SEND OK")) { Serial.println("Packet sent");
while (esp.available()) {
String tmpResp = esp.readString();
Serial.println(tmpResp);
}
// close the connection
esp.println("AT+CIPCLOSE");
}
}
}
This is the output on the Serial Terminal:
AT+CWJAP_CUR="NETGEAR80","my_password"
Connected!
POST /km/db.php HTTP/1.0
Host: http://www.server.com
Accept: */*
Content-Length: 17
Content-Type: application/x-www-form-urlencoded
The code works fine till the POST, but then no answer is received.
This condition:
if(esp.find((char*)">")) { Serial.println("Sending.."); esp.print(postRequest);
is never verified.
I can't understand if there is something wrong with the POST format, with the URL or something else.
Can you help me, please?