I have a motion sensor connected to the A0 pin of the core.And a RISING interrupt is assigned to this pin. So upon detection of motion it calls an ISR and this ISR does a POST request to a PHP page on my server. This PHP file is sending a push notification to my phone via Google cloud messaging.
Everything works fine for the first time. I do receive the push notification on my phone but after that the core freezes indefinitely and any line below the http.post() doesnot get executed.
Below are the codes
#include "HttpClient/HttpClient.h"
int interval = 30000; //30 seconds
volatile boolean motion = false;
volatile unsigned long lastmotiontime = 0;
HttpClient http;
#define HOSTNAME "mydonain.in"
#define PATHNAME "/push2.php"
http_header_t headers[] = {
{ "Accept" , "*/*"},
{ "Referer" , HOSTNAME},
{ "Content-Type", "application/x-www-form-urlencoded" },
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
boolean MOTION = false;
void setup(){
Spark.variable("motion", &MOTION , BOOLEAN);
delay(20000);
Serial.begin(9600);
while(!Serial.available()) // Wait here until the user presses ENTER
SPARK_WLAN_Loop();
Serial.print("Setup done\r\n");
attachInterrupt(A0, motionInterupt, RISING);
}
void loop(){
noInterrupts();
unsigned long currentMillis = millis();
//fallback if millis() function overflows.
if (currentMillis < lastmotiontime){
lastmotiontime = 0 ;
motion = false;
}
if (motion & (currentMillis - lastmotiontime > interval)){
motion = false;
}
interrupts();
MOTION = motion;
delay(1000);
}
void motionInterupt(){
Serial.print("Motion detected.\r\n");
motion=true;
lastmotiontime = millis();
pushNotify("Alert","Motion detected..");
}
void pushNotify(const char* title,const char* msg){
Serial.print("inside pushNotify methode..\r\n");
char queryString[100]="title=";
strcat(queryString,title);
strcat(queryString,"&message=");
strcat(queryString,msg);
request.hostname = HOSTNAME;
request.port = 80;
request.path = PATHNAME;
// The library also supports sending a body with your request:
request.body = queryString;
Serial.print("request created\r\n");
// POST request
http.post(request, response, headers);
//Below this point nothing is executed and the core freezes
Serial.print("POSTED\r\n");
}
The code gets executed till
http.post(request, response, headers);
and then the core freezes.It doesn’t even print “POSTED” to serial out.Below is a screenshot of the output
After that the core becomes unresponsive.
Any help as to why I am facing this issue??