0

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:

Module Reset
Collegamento
AT+CWJAP_CUR="NETGEAR80","calmgrasshopper675"
In attesa..
Connected!
POST /km/db.php HTTP/1.0
Host: www.skeetty.com
Accept: */*
Content-Length: 17
Content-Type: application/x-www-form-urlencoded

id=11&humidity=10
POST /km/db.php HTTP/1.0
Host: www.skeetty.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?


EDIT 26/04/2019 This is the output if I manually write the AT commands directly to the ESP8266 module through the Serial Monitor.

AT+CWJAP="NETGEAR80","my_password"

WIFI CONNECTED
WIFI GOT IP

OK
AT+CIPMUX=1


OK

AT+CIPSTART=1,"TCP","www.server.com",80

1,CONNECT

OK

If I use the Arduino code, it never connects to the server and the string "TCP connection ready" do not show, so I think it is not able to correctly connect to the server. Maybe, it's for a formatting error? I can't find the reason.


EDIT2 with @Juraj code.

    #include <SoftwareSerial.h>

SoftwareSerial Serial1(10, 11);
#define Trash "Trash3"
#define DHTTYPE DHT11
#define DHTPIN 2
#define TRIGGER_PIN 4
#define ECHO_PIN 3



float humidity, temp_f;
int distance;
long duration;


String buff(64);
String getStr(128);

void setup() {

Serial.begin(9600);

Serial1.begin(9600);
//Serial1.resetESP();
delay(2000);
Serial1.setTimeout(5000);


if (!connectWiFi()) {
Serial.println("Can not connect to the WiFi.");
while (true)
; // do nothing
}
Serial.println("OK, Connected to WiFi.");

sendCommand("AT+CIPSTA?");
sendCommand("AT+CIPDNS_CUR?");
sendCommand("AT+CIPSTAMAC?");
}

void loop() {

temp_f = 11;


// connect to server
if (sendCommand("AT+CIPSTART=\"TCP\",\"www.skeetty.com\",80")) {
Serial.println("connected to Cloud");

// build HTTP request
getStr = "GET /km/db.php?id=";
getStr += temp_f;
getStr += " HTTP/1.1\r\n";
getStr += "Host: expository-spoke.000webhostapp.com\r\n\r\n";

// open send buffer
buff = "AT+CIPSEND=";
buff += getStr.length();
if (sendCommand(buff.c_str()) && Serial1.find(">")) { // AT firmware is ready to accept data

// send HTTP request
Serial.println(getStr);
Serial1.print(getStr);

// print HTTP response
if (Serial1.find("+IPD,")) { // response received
int l = Serial1.parseInt();
while (l > 0) {
if (Serial1.available()) {
Serial.write(Serial1.read());
l--;
}
}
Serial.println("--------------");
} else {
Serial.println("no response");
}
} else {
Serial.println("send error");
}
sendCommand("AT+CIPCLOSE");
} else {
Serial.println("Error connecting");
}
}

bool connectWiFi() {

if (!sendCommand("ATE0")) // echo off
return false;
if (!sendCommand("AT+CIPMUX=0")) // set single connection mode
return false;
if (!sendCommand("AT+CWMODE=1")) // set STA mode
return false;
return sendCommand("AT+CWJAP=\"NETGEAR80\",\"calmgrasshopper675\"");
}

bool sendCommand(const char* cmd) {
Serial.println(cmd);
Serial1.println(cmd);
while (true) {
buff = Serial1.readStringUntil('\n');
buff.trim();
if (buff.length() > 0) {
Serial.println(buff);
if (buff == "OK" || buff == "SEND OK" || buff == "ALREADY CONNECTED")
return true;
if (buff == "ERROR" || buff == "FAIL" || buff == "SEND FAIL")
return false;
}
}
}

it never connects to the Wi-Fi network because sendCommand() always return false.

  • Comments are not for extended discussion; this conversation has been moved to chat. – Majenko Apr 26 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.