I have a esp8266 connected to arduino to do post to an apache2 server, but it seems that in arduino it does not get return from the server, the apache log never registers anything and when it registers it is a 408 error (quite rare to happen! PING to the IP of the ESP and it responds), as I am doing the tests on my network, I looked at the devices connected in the router and the IP of esp8266 does not appear.
On the linux server that has apache, I ran the iptraf program to see what happens and it appeared in the Flags column:
S-A- (server) and --A- (ESP) .
I've tested everything and so I come here to ask for help for you.
This is the complete code:
#include <SoftwareSerial.h>
#include <avr/pgmspace.h>
// RX, TX wifi ESP-01
SoftwareSerial esp(2, 3);
#define CF(x) ((const __FlashStringHelper*)x)
const char ssid[] PROGMEM = "andre";
const char password[] PROGMEM = "xxxxxx";
boolean tcpConnect = false;
void setup() {
Serial.begin(115200);
esp.begin(19200);
delay(500);
esp.println("AT+CIOBAUD=19200");
esp.print("\r\n");
delay(1000);
resetEsp();
connectEsp();
}
void resetEsp() {
boolean go = false;
int contErrorReset = 0;
Serial.println(F("Reiniciando ESP..."));
esp.println("AT+RST");
esp.print("\r\n");
delay(1000);
}
void connectEsp() {
int contErrorConnection = 0;
Serial.println(F("Conectando ESP..."));
do {
esp.flush();
esp.print("AT+CWJAP=\"");
esp.print(CF(ssid));
esp.print("\",\"");
esp.print(CF(password));
esp.print("\"");
esp.print("\r\n");
delay(2000);
if (esp.readString().indexOf("OK")) {
break;
} else if (checkConection() == true) {
break;
} else {
// se não conecta na rede, incrementa
contErrorConnection++;
}
} while (contErrorConnection < 30);
if (contErrorConnection == 30) {
// reset
asm volatile("jmp 0");
}
}
boolean checkConection() {
esp.print("AT+CIPSTATUS");
esp.print("\r\n");
delay(2000);
if (esp.readString().indexOf("OK")) {
return true;
}
return false;
}
void loop() {
requestPost();
delay(20000);
}
void requestPost() {
// verifica conexao
if (checkConection() == true) {
Serial.println(F("CONECTADO..."));
} else {
Serial.println(F("NAO CONECTADO..."));
delay(300);
asm volatile("jmp 0");
}
delay(500);
checkIP();
// esp.flush();
// conexão tcp
esp.print("AT+CIPSTART=\"TCP\",\"192.168.1.7\",80");
esp.print("\r\n");
int contErrorTCP = 0;
do {
delay(500);
if( esp.readString().indexOf("OK")) {
Serial.println("TCP connection ready");
tcpConnect = true;
} else {
Serial.println("TCP connection not ready...");
contErrorTCP++;
}
if(contErrorTCP==5) {
Serial.println("Reiniciando");
asm volatile("jmp 0");
}
}while(tcpConnect==false);
delay(10000);
esp.print("AT+CIPMUX=0");
esp.print("\r\n");
delay(500);
Serial.println(freeRam());
String sendVal = "valpost=123";
String postRequest = "";
postRequest += "POST /climatempo/sensor_test.php HTTP/1.1\r\n";
postRequest += "Host: 192.168.1.7\r\n";
postRequest += "Accept: */*\r\n";
postRequest += "Content-Length: " + String(sendVal.length()) + "\r\n";
postRequest += "Content-Type: application/x-www-form-urlencoded\r\n";
postRequest += "Cache-Control: no-cache\r\n";
postRequest += "\r\n"+sendVal;
// quantidade de caracteres na requisição
String cmd = "AT+CIPSEND=";
Serial.println("CARACTERES REQUEST: "+cmd);
esp.println(cmd);
esp.println(postRequest.length());
esp.print("\r\n");
delay(10000);
Serial.println(esp.readString());
delay(500);
if (esp.readString().indexOf(">")) {
Serial.println(postRequest);
esp.print(postRequest);
esp.print("\r\n");
delay(1000);
Serial.println(esp.readString());
if( esp.readString().indexOf("SEND OK")) {
Serial.println(F("Packet sent"));
while (esp.available()) {
Serial.println(F("RESULT:"));
String tmpResp = esp.readString();
Serial.println(tmpResp);
}
}
}
Serial.println(F("Vai encerrar"));
delay(2000);
// encerra conexão
esp.print("AT+CIPCLOSE");
esp.print("\r\n");
}
void checkIP() {
do {
esp.print("AT+CIFSR");
esp.print("\r\n");
Serial.print(esp.readString());
delay(1000);
Serial.println(F("VERIFICANDO IP..."));
} while(!esp.readString().indexOf("OK"));
Serial.print(esp.readString());
}
int freeRam () {
Serial.print("Free SRAM in bytes: ");
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}