Hi,
I am having issues with my spark core becoming responsive.
when trying to access the API I get a time out.
{
"error": "Timed out."
}
The project here is pretty simple, I am using a dht11 temperature and humidity sensor, with the values being stored in a char array in JSON format.
There are also two PIR sensors connected to interrupt pins for detecting weather the room is empty and calling a PHP script on a remote server when there is change in state.
I suspect that is the client connection section of the code that is causing the freezes.
Has anyone had any problems with similar issues ?
Thanks,
Mark.
Here is the code running on the spark core:
/*
Using Parallax PIR sensor
*/
/// Motion Related Variables.
//both these vars are set to volatile because they will be changed when there is an interupt
volatile boolean motion = false;
volatile uint32_t lastmotiontime = 0;
uint32_t interval = 900000; //15min
boolean currentMotion = true; //variable that is going to be available through the spark api a nonvolatile var is required
//char server[] = "torrentslave.local";
IPAddress serverIP(192,168,1,99);
TCPClient client;
// Temperature and humidity variables
int dht = D0;
int rh = 0;
int temp = 0;
char resultstr[64];
void setup(){
RGB.control(true);
Spark.variable("motion", ¤tMotion , BOOLEAN);
Spark.variable("result", &resultstr, STRING);
pinMode(dht, INPUT_PULLUP); // enable ping D0 as input with internal pullup resistor enabled.
unsigned long Ltime = millis(); //wait 30 sec for the PIR to calibrate itself.
while(Ltime < 30000){
RGB.color(255, 0, 0);
delay(750);
RGB.color(0,0,0);
delay(250);
Ltime = millis();
}
//attaching interrupt pins for both my motion sensors.
attachInterrupt(A0, motionInterupt, RISING);
attachInterrupt(A1, motionInterupt, RISING);
}
void loop(){
noInterrupts();
unsigned long currentMillis = millis();
//catch for when millis() overflowing
if (currentMillis < lastmotiontime){
lastmotiontime = 0 ;
}
//reseting motion variable if no motion has been detected in a while
if (motion && (currentMillis - lastmotiontime > interval)){
motion = false;
}
//call server script when there is a change to room being empty.
if (currentMotion != motion){
//allow us to react to change run the testNatLight script on remote web server.
if(client.connect(serverIP, 80)){
client.println("GET /phpHue/testNatLight.php HTTP/1.0");
client.println();
client.flush();
delay(50); //wait for caches to clear.
client.stop();
}
}
currentMotion = motion; //store the current state in a non volatile variable - for access from spark.variable
rh = read_dht(dht, &temp);
// format your data as JSON, don't forget to escape the double quotes
sprintf(resultstr, "{\"Temperature\":%d,\"Humidity\":%d,\"Presence\":%d}", temp, rh, motion);
interrupts();
delay(500);
}
void motionInterupt(){
motion=true;
lastmotiontime = millis();
RGB.color(0, 0, 50);
}
int read_dht(int pin, int *temperature)
{
uint8_t data[5] = {0, 0, 0, 0, 0};
noInterrupts();
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(20);
pinMode(pin, INPUT_PULLUP);
while (digitalRead(pin) == HIGH) {
delayMicroseconds(10);
}
while (digitalRead(pin) == LOW) {
delayMicroseconds(10);
}
while (digitalRead(pin) == HIGH) {
delayMicroseconds(10);
}
for (uint8_t i = 0; i < 40; i++) {
uint8_t counter = 0;
while (digitalRead(pin) == LOW) {
delayMicroseconds(10);
}
while (digitalRead(pin) == HIGH) {
delayMicroseconds(10);
counter++;
}
data[i/8] <<= 1;
if (counter > 4) {
data[i/8] |= 1;
}
}
interrupts();
if (data[4] != ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
*temperature = -254;
return -1;
}
*temperature = data[2];
return data[0];
}