Hello guys!
I have noticed a strange behaviour in my web server using the function below.
char *Username = "Username";
char Auth1[50];
float tempC;
float tempH;
float tempA;
float PHA;
float PHR;
int ORP;
int DEN;
byte status_parametros;
byte temporizador_status;
byte Status;
boolean nivel_status;
byte tpa_status;
prog_char string0[] PROGMEM = "POST /api/temp HTTP/1.1";
prog_char string1[] PROGMEM = "Host: www.mysite.com";
prog_char string2[] PROGMEM = "Authorization: Basic ";
prog_char string3[] PROGMEM = "Cache-Control: no-cache";
prog_char string4[] PROGMEM = "Content-Type: application/x-www-form-urlencoded";
prog_char string5[] PROGMEM = "Connection: Keep-Alive";
prog_char string6[] PROGMEM = "Content-Length: ";
char* tabela_strings[] PROGMEM =
{
string0, string1, string2, string3,
string4, string5, string6
};
void enviar_dados()
{
char dados[6];
EthernetClient client;
Serial.println("Connecting...");
String PostData =
"{\"userName\":\""+
String(Username)+
"\",\"minute\":\""+
rtc.getTimeStamp()+
"\",\"A\":"+ // Temp. da água
dtostrf(tempC,5,2,dados)+
",\"B\":"+ // Temp. dissipador
dtostrf(tempH,5,2,dados)+
",\"C\":"+ // Temp. ambiente
dtostrf(tempA,5,2,dados)+
",\"D\":"+ // PH do aquário
dtostrf(PHA,4,2,dados)+
",\"E\":"+ // PH do reator de cálcio
dtostrf(PHR,4,2,dados)+
",\"F\":"+ // Densidade
String(DEN)+
",\"G\":"+ // ORP
String(ORP)+
",\"H\":"+ // Status chiller, 0 = desligado e 1 = ligado
String(bitRead(status_parametros,0))+
",\"I\":"+ // Status aquecedor, 0 = desligado e 1 = ligado
String(bitRead(status_parametros,1))+
",\"J\":"+ // Status reator de cálcio, 0 = desligado e 1 = ligado
String(bitRead(status_parametros,5))+
",\"K\":"+ // Status ozonizador, 0 = desligado e 1 = ligado
String(bitRead(status_parametros,7))+
",\"L\":"+ // Status reposição de água doce, 0 = desligado e 1 = ligado
String(bitRead(Status,1))+
",\"M\":"+ // Status niveis, 0 = baixo e 1 = normal
String(nivel_status)+
",\"N\":"+ // Status TPA, 0 = desligado e 1 = ligado
String(bitRead(tpa_status,0))+
",\"O\":"+
String(bitRead(temporizador_status,1))+ // Status timer 1, 0 = desligado e 1 = ligado
",\"P\":"+
String(bitRead(temporizador_status,2))+ // Status timer 2, 0 = desligado e 1 = ligado
",\"Q\":"+
String(bitRead(temporizador_status,3))+ // Status timer 3, 0 = desligado e 1 = ligado
",\"R\":"+
String(bitRead(temporizador_status,4))+ // Status timer 4, 0 = desligado e 1 = ligado
",\"S\":"+
String(bitRead(temporizador_status,5))+ // Status timer 5, 0 = desligado e 1 = ligado
",\"T\":"+ // Sinaliza falha na TPA
String(bitRead(tpa_status,2))+
"}";
if (client.connect("www.mysite.com", 80))
{
Serial.println(">> Connected <<");
strcpy_P(buffer, (char*)pgm_read_word_near(&(tabela_strings[0]))); // "POST /api/temp HTTP/1.1"
client.println(buffer);
strcpy_P(buffer, (char*)pgm_read_word_near(&(tabela_strings[1]))); // "Host: www.mysite.com"
client.println(buffer);
strcpy_P(buffer, (char*)pgm_read_word_near(&(tabela_strings[2]))); // "Authorization: Basic "
client.print(buffer);
client.println(Auth1);
strcpy_P(buffer, (char*)pgm_read_word_near(&(tabela_strings[3]))); // "Cache-Control: no-cache"
client.println(buffer);
strcpy_P(buffer, (char*)pgm_read_word_near(&(tabela_strings[4]))); // "Content-Type: application/x-www-form-urlencoded"
client.println(buffer);
strcpy_P(buffer, (char*)pgm_read_word_near(&(tabela_strings[5]))); // "Connection: close"
client.println(buffer);
strcpy_P(buffer, (char*)pgm_read_word_near(&(tabela_strings[6]))); // "Content-Length: "
client.print(buffer);
client.println(PostData.length());
client.println();
client.println(PostData);
Serial.println(PostData);
delay(5);
client.stop();
}
else
{
Serial.println("Can't connect!");
}
}
For now I can’t show my full code because it have more than 10,000 lines.
The sketch size is 212.066 Kbytes.
A previous version can be found here.
I’ll add this new function to this code.
Let me try explain the problem.
The function above send a HTTP POST to a server.
This code works fine during 1 or 2 days.
After this time the server doesn’t receive more data but all others functions keep working.
The serial monitor print “Clnnecting…” instead “Connecting…” even after send the code to the board.
if remove “Serial.println(“Connecting…”);”, the function (enviar_dados()) doesn’t works anymore.
Looking the traffic on wireshark the function try connect to “wtw.mysite.com” instead “www.mysite.com”.
The board have 2456 bytes of free memory.
Also I would like to know a best way to build the string “PostData”.
I can’t use a “client.print()” to each value because I’ll have problem to know the content length.
Someone can give some suggestion to this problems?
Thanks in advance.