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