HI,
I'm begginer in arduino programming. I have the code below, and the idea is to send the information to the server every minute, however, I'm having several errors in getting the correct HTTP response.
the response is not always complete, and I search for the string SEND OK to continue
I've tried the following ways, and I get the same result
tmpResp = esp8266.readString();
tmpResp = esp8266.readStringUntil('\r');
I've tried put some delay too.
My code
boolean waitAndEcho(unsigned long t, String s) {
String buffer = "";
unsigned long start = millis();
unsigned long last = 0;
unsigned int n = s.length();
bool ret = false;
delay(200);
do {
if (esp8266.available()) {
buffer += (char)esp8266.read();
last = millis();
if (buffer.length() >= n) {
if (buffer.substring(buffer.length() - (n)).equals(s)) {
//if (buffer.indexOf(s)) {
ret = true;
break;
}
}
}
} while (millis() < start + t);
buffer.replace("\r", "\\r");
buffer.replace("\n", "\\n");
if (vDebug == true) {
Serial.println(String(ret ? "+" : "-") + "(" + String(last - start) + "/" + String(t) + "):" + buffer);
}
return ret;
}
while (!sendData(vLed, vValorDb));
bool sendData(byte vLed, float vValorDb) {
Serial.println(F("Enviando dados"));
String values = "idd=[D]&idl=[L]&ide=[E]&vdb=[DB]&vref=[RE]";
values.replace("[D]", String(vDisp));
values.replace("[L]", String(vLed));
values.replace("[E]", String(vIdEmpresa));
values.replace("[DB]", String(vValorDb));
values.replace("[RE]", String(vDbMax));
String postString = "POST /receptor/som HTTP/1.1\r\n"
"Host: [S]\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: [L]\r\n"
"Accept: */*\r\n"
//"User-Agent: Arduino/1.0\r\n"
"User-Agent: [D]/1.0\r\n"
"Connection: close\r\n\r\n"
"[V]\r\n\r\n";
postString.replace("[L]", String(values.length()));
postString.replace("[V]", values);
postString.replace("[S]", vServer);
postString.replace("[D]", String(vDisp));
//Serial.println("Send post" + postString);
esp8266.println("AT+CIPMUX=1");
if (waitAndEcho(3000, "OK\r\n")) {
esp8266.println("AT+CIPSTART=4,\"TCP\",\"" + vServer + "\"," + vServerPort);
if (waitAndEcho(5000, "OK\r\n")) {
esp8266.println("AT+CIPSEND=4," + String(postString.length()));
if (waitAndEcho(5000, ">")) {
esp8266.print(postString);
delay(500);
String tmpResp;
if (esp8266.available()) {
while (esp8266.available()) {
tmpResp = esp8266.readString();
delay(1);
}
}
//Serial.println(tmpResp);
//Serial.println("Resp Send"+ tmpResp);
if (tmpResp.indexOf("SEND OK") > 0) {
return true;
}
}
}
}
Serial.println(F("Falha no envio da informacao"));
return false;
}
i´m using arduino uno + esp8266 and <AltSoftSerial.h>.
the baud rates
Serial.begin(115200);
while (!Serial); // wait for Arduino Serial Monitor to open
esp8266.begin(9600);
tks.