Once again, I’d use serial print messages to determine where the code progresses to before appearing to hang.
Pete.
Once again, I’d use serial print messages to determine where the code progresses to before appearing to hang.
Pete.
Trying to read the current status of pin D7. I already have digital read but storing that result in variable for later use is not working for me.
can you please have a look at why I am not able to move past first loop??
The line with // ??? is where I am thinking something is incorrect but cant figure it out.
You seem to have gotten into a bit of a mess by putting semicolons after if
comparisons and putting opening and closing curly brackets in the wrong locations.
This should work…
oid switchstate()
{
int pinStatus = digitalRead(pirPin);
if (pinStatus)
{
if (lockLow)
{
Serial.println("loop 1 prestart");
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("loop1 start");
Blynk.virtualWrite(2, "Normal!");
Serial.println("No Fire serial Detected");
Serial.println((String)"D7 pin status loop1 " + pinStatus);
delay(50);
}
takeLowTime = true;
Serial.println("loop1 end");
}
if (!pinStatus) // could simply be an else
{
Serial.println((String)"LOCATION_A " + pinStatus);
if (takeLowTime)
{
Serial.println((String)"LOCATION_B");
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause, we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause)
{
Serial.println((String)"LOCATION_C");
//makes sure this block of code is only executed again after a new motion sequence has been detected
lockLow = true;
Serial.println("loop 2 started");
// Serial.println((String)"loop 2 started" +lockLow +takeLowTime); // 1 0
// Blynk.notify("Fire Detected in the Home!");
Blynk.setProperty(1, "color", "#D3435C"); //red color
Blynk.virtualWrite(2, "Fire Detected!!!!!");
Serial.println("Fireeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee Detected");
Serial.println((String)"D7 pin status after fire" + pinStatus); //new
Mytimer.setTimeout(20000L, []()
{
Blynk.setProperty(1, "color", "#23C48E"); // reset to green color
Serial.println("Lambda timer has completed");
lockLow = false; // timer reset imp
});
Serial.println("loop 2 ends");
}
}
}
Pete.
when wemos starts in sustained alarm mode (9V battery connected to circuit to simulate fire) it seems to be stuck at location A in the code. as soon as i remove one of the battery leads from the circuit, the code runs normal from there.
[6389] Connected to WiFi
15:03:04.587 -> [6390] IP: 10.0.0.24
15:03:04.587 -> [6390]
15:03:04.587 -> ___ __ __
15:03:04.634 -> / _ )/ /_ _____ / /__
15:03:04.681 -> / _ / / // / _ \/ '_/
15:03:04.681 -> /____/_/\_, /_//_/_/\_\
15:03:04.728 -> /___/ v0.6.1 on ESP8266
15:03:04.774 ->
15:03:04.774 -> [6462] Connecting to blynk-cloud.com:80
[6682] Ready (ping: 81ms).
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
LOCATION_A locklow: 1takelowtime: 0pinstatus: 0
loop1 start
15:03:18.922 -> loop1 begins
15:03:18.922 -> LOCATION_3 locklow: 0takelowtime: 0pinstatus: 1
15:03:18.969 -> loop1 end
loop1 begins
15:03:19.937 -> LOCATION_3 locklow: 0takelowtime: 1pinstatus: 1
loop1 end
loop1 begins
15:03:20.945 -> LOCATION_3 locklow: 0takelowtime: 1pinstatus: 1
loop1 end
loop1 begins
15:03:21.940 -> LOCATION_3 locklow: 0takelowtime: 1pinstatus: 1
loop1 end
loop1 begins
15:03:22.924 -> LOCATION_3 locklow: 0takelowtime: 1pinstatus: 1
15:03:22.971 -> loop1 end
loop1 begins
Then I introduced locklow = false just before the location A serial print in the code as following and the code seems to move forward and detects the fire as soon wemos starts up however as soon as the first lambda timer is completed it seems to go back to the lambda timer in a loop.
similarly the led works on the blynk app okay from startup till the first 20 second timer and then seems to flash green-red–green-red in a fast continous loop.
if (!pinStatus) // could simply be an else
{
lockLow = false; // <<----
Serial.println((String)"LOCATION_A locklow: " +lockLow+ "takelowtime: " +takeLowTime+
62] Connecting to PIKIN-22
[6390] Connected to WiFi
15:20:02.835 -> [6390] IP: 10.0.0.24
15:20:02.881 -> [6391]
15:20:02.881 -> ___ __ __
15:20:02.928 -> / _ )/ /_ _____ / /__
15:20:02.928 -> / _ / / // / _ \/ '_/
15:20:02.975 -> /____/_/\_, /_//_/_/\_\
15:20:03.022 -> /___/ v0.6.1 on ESP8266
15:20:03.022 ->
15:20:03.022 -> [6463] Connecting to blynk-cloud.com:80
[6685] Ready (ping: 79ms).
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:04.241 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:04.287 -> loop 2 started
15:20:04.287 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:05.225 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:05.318 -> loop 2 started
15:20:05.318 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:06.256 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:06.302 -> loop 2 started
15:20:06.302 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:07.240 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:07.287 -> loop 2 started
15:20:07.287 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:08.256 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:08.303 -> loop 2 started
15:20:08.303 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:09.240 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:09.287 -> loop 2 started
15:20:09.287 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:10.224 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:10.318 -> loop 2 started
15:20:10.318 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:11.256 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:11.303 -> loop 2 started
15:20:11.303 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:12.241 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:12.288 -> loop 2 started
15:20:12.288 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:13.261 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:13.308 -> loop 2 started
15:20:13.308 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:14.246 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:14.293 -> loop 2 started
15:20:14.293 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:15.230 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:15.324 -> loop 2 started
15:20:15.324 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:16.261 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:16.308 -> loop 2 started
15:20:16.308 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:17.245 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:17.292 -> loop 2 started
15:20:17.292 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:18.230 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:18.324 -> loop 2 started
15:20:18.324 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:19.261 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:19.308 -> loop 2 started
15:20:19.308 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:20.246 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:20.293 -> loop 2 started
15:20:20.293 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:21.231 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:21.325 -> loop 2 started
15:20:21.325 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:22.262 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:22.309 -> loop 2 started
15:20:22.309 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:23.246 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:23.293 -> loop 2 started
15:20:23.293 -> loop 2 ends
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:24.231 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:24.325 -> loop 2 started
15:20:24.325 -> loop 2 ends
15:20:24.325 -> Lambda timer has completed
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:25.263 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:25.310 -> loop 2 started
15:20:25.310 -> loop 2 ends
15:20:25.357 -> Lambda timer has completed
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:26.235 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:26.329 -> loop 2 started
15:20:26.329 -> loop 2 ends
15:20:26.329 -> Lambda timer has completed
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:27.257 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:27.304 -> loop 2 started
15:20:27.304 -> loop 2 ends
Lambda timer has completed
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:28.241 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:28.288 -> loop 2 started
15:20:28.288 -> loop 2 ends
Lambda timer has completed
LOCATION_A locklow: 0takelowtime: 0pinstatus: 0
15:20:29.220 -> LOCATION_C locklow: 0takelowtime: 0pinstatus: 0
15:20:29.313 -> loop 2 started
15:20:29.313 -> loop 2 ends
15:20:29.360 -> Lambda timer has completed
loop1 begins
15:20:30.205 -> LOCATION_3 locklow: 0takelowtime: 0pinstatus: 1
15:20:30.252 -> loop1 end
15:20:30.252 -> Lambda timer has completed
loop1 begins
15:20:31.190 -> LOCATION_3 locklow: 0takelowtime: 1pinstatus: 1
loop1 end
15:20:31.283 -> Lambda timer has completed
loop1 begins
15:20:32.220 -> LOCATION_3 locklow: 0takelowtime: 1pinstatus: 1
loop1 end
code
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer Mytimer;
char auth[] = "";
//the time we give the sensor to calibrate (10-60 secs according to the datasheet) // which sensor is Matt referring to here?
int calibrationTime = 10;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low before we assume all motion has stopped
long unsigned int pause = 100;
boolean lockLow = true;
boolean takeLowTime;
//the digital pin connected to the PIR sensor's output
int pirPin = 13; //d7 pin on wemos d1 mini
unsigned long lastMillis = 0;
#define BLYNK_GREEN "#23C48E"
#define BLYNK_RED "#D3435C"
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "xxxx", "xxx");
Mytimer.setInterval(1000L, switchstate);
pinMode(pirPin, INPUT_PULLUP); // sets pirPin as input
digitalWrite(pirPin, LOW); // sets pirPin as
Blynk.virtualWrite(V1, 255); // Turn the LED on
Blynk.setProperty(1, "color", "#23C48E"); // reset to green color
}
void switchstate()
{
int pinStatus = digitalRead(pirPin);
if (pinStatus)
//Serial.println((String)"LOCATION_1 locklow: " + lockLow+ "takelowtime: " + takeLowTime+ "pinstatus: " + pinStatus);
{
if (lockLow)
Serial.println("loop1 start");
//Serial.println((String)"LOCATION_2 locklow: " + lockLow+ "takelowtime: " + takeLowTime+ "pinstatus: " + pinStatus);
{
lockLow = false;
Serial.println("loop1 begins");
Serial.println((String)"LOCATION_3 locklow: " + lockLow+ "takelowtime: " + takeLowTime+ "pinstatus: " + pinStatus);
Blynk.virtualWrite(2, "Normal!");
// Serial.println("No Fire serial Detected");
//Serial.println((String)"LOCATION 4 locklow: " + lockLow+ "takelowtime: " + takeLowTime+ "pinstatus: " + pinStatus);
delay(50);
}
takeLowTime = true;
Serial.println("loop1 end");
// Serial.println((String)"LOCATION_5 locklow: " + lockLow+ "takelowtime: " + takeLowTime+ "pinstatus: " + pinStatus);
}
if (!pinStatus) // could simply be an else
{
lockLow = false; // <<----
Serial.println((String)"LOCATION_A locklow: " +lockLow+ "takelowtime: " +takeLowTime+ "pinstatus: " +pinStatus);
if (takeLowTime)
{
Serial.println((String)"LOCATION_B locklow: " +lockLow+ "takelowtime: " +takeLowTime+ "pinstatus: " +pinStatus);
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
if (!lockLow && millis() - lowIn > pause)
{
Serial.println((String)"LOCATION_C locklow: " +lockLow+ "takelowtime: " +takeLowTime+ "pinstatus: " +pinStatus);
lockLow = true;
Serial.println("loop 2 started");
// Blynk.notify("Fire Detected in the Home!");
Blynk.setProperty(1, "color", "#D3435C"); //red color
Blynk.virtualWrite(2, "Fire Detected!!!!!");
// Serial.println("Fireeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee Detected");
// Serial.println((String)"D7 pin status after fire" + pinStatus); //new
Mytimer.setTimeout(20000L, []()
{
Blynk.setProperty(1, "color", "#23C48E"); // reset to green color
Serial.println("Lambda timer has completed");
lockLow = false; // timer reset imp
});
Serial.println("loop 2 ends");
}
}
}
void loop()
{
Blynk.run();
Mytimer.run();
}
I think you’ve lost sight of what these flag variables are supposed to be doing. Maybe if they had more meaningful names then their function would be more obvious.
I’d suggest that you step through the code on paper and track the variable values to understand the code flow better, as the serial print option clearly isn’t working for you.
Pete.