Help with http response

Hello,
I use in my project a arduino nano with a esp8266.
For the ESP I use a voltage regulator LM317 for 3.3v, and a resistor for RX to stabilize in 3.3v.
When I start the code, everything seems to work, except for the response from the web server (apache).

the problem is in sendData() and getDispositivoInfo() to read http response;

my code:

const bool vDebug = true;

//Biblioteca serial para comunicação
//#include <AltSoftSerial.h>
#include <SoftwareSerial.h>

SoftwareSerial esp8266(8, 9);

const String vWifi = "wifi";
const String vPassWd = "98765432";

const String vServer = "xxxx";
const String vServerPort = "8081";

unsigned short vDisp = 1;
unsigned short vIdEmpresa = 1;

unsigned short vTempo = 1;

unsigned short vDbMax = 70;

const byte pinLedVerde = 7;
const byte pinLedAmarelo = 6;
const byte pinLedVermelho = 5;

const int pinMic = A0;

double vValorDb = 0;

byte vLed = 1;

byte vLimiteErro = 10;

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;
}

bool espConfig() {

  esp8266.println("AT+RST");
  if (waitAndEcho(3000, "ready\r\n")) {
    esp8266.println("AT+CWQAP");
    if (waitAndEcho(3000, "OK\r\n")) {
      esp8266.println("AT+CWMODE=1");
      if (waitAndEcho(3000, "OK\r\n")) {
        esp8266.println("AT+CWJAP=\"" + vWifi + "\",\"" + vPassWd + "\"");
        if (waitAndEcho(10000, "OK\r\n")) {
          return true;
        }
      }
    }
  }

  Serial.println(F("Falha na conexao!"));
  return false;
}

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(1000);
        String tmpResp="";
          while (esp8266.available()>0) {
            tmpResp += esp8266.readStringUntil('\r');
          }
        Serial.println("Received" + tmpResp);
        //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;
}

bool getDispositivoInfo() {

  Serial.println(F("Lendo conf no servidor"));

  String values = "idd=[D]";
  values.replace("[D]", String(vDisp));

  String postString = "POST /api/dispositivo/getconfig 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));

  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(3000, ">")) {
        esp8266.print(postString);

        String tmpResp="";
        
        uint32_t http_timeout = millis(); // timeout counter
  while ( millis() - http_timeout <= 1000 ) { // some websites take longer to reply than others, i find the timeout helps for those. This seems to work well.
    while ( esp8266.available() > 0 ) { // only if characters are available during the timeout
      tmpResp+=esp8266.readStringUntil('\r');
      delay(1);
    }
  }
        
        Serial.println("Received" + tmpResp);

        if (tmpResp.indexOf(">") > 0) {
          tmpResp = tmpResp.substring(tmpResp.indexOf("<") + 1, tmpResp.lastIndexOf(">"));
          for (byte i = 0; i < tmpResp.length(); i++) {
            if (tmpResp.substring(i, i + 1) == ";") {
              vDbMax = tmpResp.substring(0, i).toInt();
              vTempo = tmpResp.substring(i + 1).toInt();
              break;
            }
          }

          Serial.print(F("Vlr db: "));
          Serial.println(vDbMax);
          Serial.print(F("Vlr min: "));
          Serial.println(vTempo);
          return true;
        }
      }
    }
  }
  Serial.println(F("Falha na leitura de conf"));
  return false;
}

byte getValorDb() {

  Serial.println(F("Calculando som"));

  }

/*

void setup() {

  //Inicio das comunicacoes
  Serial.begin(57600);
  while (!Serial); // wait for Arduino Serial Monitor to open

  //Inicio do Wifi
  esp8266.begin(9600);

  Serial.println(F("Iniciando SETUP"));

  //Define pinos led como saida
  pinMode(pinLedVerde, OUTPUT);
  pinMode(pinLedAmarelo, OUTPUT);
  pinMode(pinLedVermelho, OUTPUT);

  //define o pino leitor de som
  pinMode(pinMic, INPUT);

  //inicia o wifi
  while (!espConfig());

  //Obtem informacoes do dispositivo no servidor
  while (!getDispositivoInfo());
}

void loop() {

  Serial.println(F("--Inicio da medicao--"));

  byte n = getValorDb();  //Chamada de função que faz a leitura do ruído

  if (n == 1) {
    if (vLed + 1 < 4) {
      vLed++;
    }
  }
  else if (n == 2) {
    if (vLed - 1 > 0) {
      vLed--;
    }
  }

  if (vLed == 1) {
    digitalWrite(pinLedVerde, HIGH);
    digitalWrite(pinLedAmarelo, LOW);
    digitalWrite(pinLedVermelho, LOW);
  }
  else if (vLed == 2) {
    digitalWrite(pinLedVerde, LOW);
    digitalWrite(pinLedAmarelo, HIGH);
    digitalWrite(pinLedVermelho, LOW);
  }
  else if (vLed == 3) {
    digitalWrite(pinLedVerde, LOW);
    digitalWrite(pinLedAmarelo, LOW);
    digitalWrite(pinLedVermelho, HIGH);
  }

  while (!sendData(vLed, vValorDb));

  Serial.println(F("--Fim da medicao--"));

  delay(vTempo * (60000));

}

I get the strange answer 'Received. P⸮Q⸮'

+(203/3000):AT+CIPSEND=4,230\r\r\n\r\nOK\r\n>
Received. P⸮Q⸮

And the server apache response:

<55;1>

I tried to read the answer in several ways, but I always have the same problem. my esp baud is 9600;

tks

Why don't you use your ESP8266 with library that does most of the work for you.

WiFiEsp library works for me with serial comms between arduino and esp8266.

But this library require specific AT firmware to be running on the ESP8266:

i tried with this code

but I get this error.

if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}

and i´m using : ai-thinker-0.9.5.2-9600.bin

tks

Exactly what error are you getting? WiFi shield not present?
Try swap Rx/TX pins, it looks like arduino is not finding your ESP.

Yes.

but the original problem is the response strange, when a utilize AT commands

+(203/3000):AT+CIPSEND=4,230\r\r\n\r\nOK\r\n>
Received. P⸮Q⸮

but the original problem is the response strange, when a utilize AT commands

It looks like you are using one baud rate to talk to the device, and it is using a different baud rate to talk to you. Is your device really using a stone age baud rate?

fgaliazzo:
Yes.

but the original problem is the response strange, when a utilize AT commands

+(203/3000):AT+CIPSEND=4,230\r\r\n\r\nOK\r\n>
Received. P⸮Q⸮

I don't know why, could be because your code use Strings. Strings is a bad idea in arduino, you will run out of memory.
Use instead bportaluri library, i use it without problems:

  1. ESP baud rate to 9600
  2. try webclient example
  3. to print HTTP response on screen that example works ok. But to parse HTTP response i had to change SoftareSerial buffer size from 64 to 128 because of buffer underrun (response were coming fast than my parse function could process them)

boylesg:
Why don't you use your ESP8266 with library that does most of the work for you.

WiFiEsp library works for me with serial comms between arduino and esp8266.

But this library require specific AT firmware to be running on the ESP8266:

ESP8266_Simple/firmware at master · sleemanj/ESP8266_Simple · GitHub

hi boylesg,

i changed all my code, using wifi esp and the firmware 1.5.4

"Supports ESP SDK version 1.1.1 and above (AT version 0.25 and above)."

This is my new code

bool getDispositivoInfo() {

Serial.println(F("Lendo conf no servidor"));

String tmpResp="";
bool x=false;

if (client.connect(vServer, vServerPort)) {

String values = "idd=[D]";
values.replace("[D]", String(vDisp));

// send the HTTP POST request
client.println("POST /api/dispositivo/getconfig HTTP/1.1");
client.println("Accept: /");
client.println("Content-Length: "+String(values.length()));
client.println("Content-Type: application/x-www-form-urlencoded");
client.println();
client.println(values);

//while (!client.available())
//{
// Serial.println("Waiting for server...");
//}

while(client.connected())
{
while(client.available() > 0)
{
char c = client.read();
if(c=='<') {
x=true;
}
if(x){
tmpResp+=c;
}

Serial.print(c);
}
delay(100);
}

if (!client.connected()) {
client.stop();
}

}
else{
Serial.println(F("Erro na conexao com o servidor"));
}

Serial.println(F("Falha na leitura de conf"));
validaErros();
return false;
}

but I have no response from the server.

if i use

//while (!client.available())
//{
// Serial.println("Waiting for server...");
//}

the looping is infinity.

and it's strange, because in the apache log I have "200", that is, success

186.199.172.253 - - [09/Feb/2018:18:28:26 -0200] "POST /api/dispositivo/getconfig HTTP/1.1" 200 8 "-" "Arduino-1/1.0"
186.199.172.253 - - [09/Feb/2018:18:28:51 -0200] "POST /api/dispositivo/getconfig HTTP/1.1" 200 8 "-" "Arduino-1/1.0"
186.199.172.253 - - [09/Feb/2018:18:29:03 -0200] "POST /api/dispositivo/getconfig HTTP/1.1" 200 8 "-" "Arduino-1/1.0"
186.199.172.253 - - [09/Feb/2018:18:29:15 -0200] "POST /api/dispositivo/getconfig HTTP/1.1" 200 8 "-" "Arduino-1/1.0"
186.199.172.253 - - [09/Feb/2018:18:29:27 -0200] "POST /api/dispositivo/getconfig HTTP/1.1" 200 8 "-" "Arduino-1/1.0"
186.199.172.253 - - [09/Feb/2018:18:29:40 -0200] "POST /api/dispositivo/getconfig HTTP/1.1" 200 8 "-" "Arduino-1/1.0"
186.199.172.253 - - [09/Feb/2018:18:29:52 -0200] "POST /api/dispositivo/getconfig HTTP/1.1" 200 8 "-" "Arduino-1/1.0"
186.199.172.253 - - [09/Feb/2018:18:30:04 -0200] "POST /api/dispositivo/getconfig HTTP/1.1" 200 8 "-" "Arduino-1/1.0"
186.199.172.253 - - [09/Feb/2018:18:30:17 -0200] "POST /api/dispositivo/getconfig HTTP/1.1" 200 8 "-" "Arduino-1/1.0"
186.199.172.253 - - [09/Feb/2018:18:30:29 -0200] "POST /api/dispositivo/getconfig HTTP/1.1" 200 8 "-" "Arduino-1/1.0"

and

tks

update: using the library wifiesp in debug mode, i have the response. so my problem is to read the response...

⸮IPD,3,317,54.191.211.66,8081:HTTP/1.1 200 OK
Date: Sat, 10 Feb 2018 16:53:07 GMT
Server: Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28
X-Powered-By: PHP/5.6.28
Cache-Control: no-cache, private
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
Content-Length: 34
Content-Type: text/html; charset=UTF-8

<55;1>

so my problem is to read the response...

What is the problem with doing that? Clearly you read the response in order to print it in order to show it to us.

Now, saving the response is a bit harder, and parsing the response is harder still, but there are tons of examples around to do that.

If you have any control over the server, making it return something more parser-friendly than <55;1> would be the first step.

Hi Paul,

I changed my answer to <55; 1>, but I do not know why I can not read it. I've tried it in several ways and I do not get the result.
The intriguing is that the debug-mode library displays the response from the server correctly

fgaliazzo:
Hi Paul,

I changed my answer to <55; 1>, but I do not know why I can not read it. I've tried it in several ways and I do not get the result.
The intriguing is that the debug-mode library displays the response from the server correctly

same here, have you found a solution?

bialabs:
same here, have you found a solution?

Do you suppose that the OP is hanging around more than a year later?

Post YOUR code and your serial output.