Thanks @bko and @Hootie81 for your help
So if I understood correctly after making a http request I will have to wait for a while till there is some response then read everything from the TI WiFi chip buffer using and flushing it by
while (client.available()) client.read();
client.flush();
Then again wait for a while till the request is complete and then call client.stop()
.
So if my understanding is correct please let me know if my below code implementation is correct
void pushNotify(const char* title,const char* msg){
Serial.print("inside pushNotify methode..\r\n");
char line[255];
char response[512];
int pos=0;
Serial.print("connected to ");
Serial.println(HOSTNAME);
client.connect(HOSTNAME,80);
sprintf(line, "POST %s HTTP/1.1\r\n", PATHNAME);
client.write( (const uint8_t*)line, strlen(line) );
client.write( (const uint8_t*)"Connection: close\r\n", strlen("Connection: close\r\n") );
sprintf(line, "Host: %s:%d\r\n", HOSTNAME, 80);
client.write( (const uint8_t*)line, strlen(line) );
sprintf(line, "Content-Length: %d\r\n", strlen(title) + strlen(msg) + 12);
client.write( (const uint8_t*)line, strlen(line) );
sprintf(line, "Content-Type: application/x-www-form-urlencoded\r\n");
client.write( (const uint8_t*)line, strlen(line) );
sprintf(line, "title=%s&msg=%s\r\n", title, msg); //this is the POST data
client.write( (const uint8_t*)line, strlen(line) );
//Wait till there is any data and then read it
unsigned long lastTime = millis();
while( client.available()==0 && millis()-lastTime<20000) { //a 20 second timeout
char c=client.read();
response[pos] = c;
pos++;
}
response[pos]='\0';
Serial.println(response);
//Again check if there is still anything left in the buffer
while (client.available()) client.read();
client.flush();
delay(500);//wait till the request is completed to avoid race condition
client.stop();
}
Again how can I be sure as to how long I will have to wait before performing a read()
and stop()
. Because you can never be sure as how long your script is going to take.
Also this function is called from a interrupt routine and if I am correct I cant use delay()
inside an ISR.
Please let me know your opinion on this.
[UPDATE] : Getting multiple red flash after calling the function and the core restarts.The response I get from the php page is "Motion detected @ Jan 16th 03:44:49 PM". This is just to let you know that the response does not use much memory.
Can anybody please please help me for a working implementation of the above function.
Thanks