My NodeMCU is connected to the Output of a house alarm. When ths house alarm is activated it pulls D1 high and the nodeMCU then alerts me via Blynk. Works perfectly.
However once or twice a day I'm getting a spurious alert. The nodeMCU D1 pin believes its been triggered. Previously I had a proper GSM module that would do the alarm reporting. That did not suffer from the same spurious alerts.
In an attempt to address this I adjusted my code to incorporate a 4 second delay, to test for the pin condition a second time before alerting. Of course this is nonsense as the timer is firing every second anyway. What can I do to addrss this spurious alerting? Anyone come across a similar problem?
Code:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
char auth[] = "x"; //Auth code sent via Email
char ssid[] = "x"; //Wifi name
char pass[] = "x"; //Wifi Password
int flag=0;
void notifyOnFire()
{
int pinState = digitalRead(D1);
if (pinState==0 && flag==0) {
delay(4000);
int pinStateAgain = digitalRead(D1);
if (pinStateAgain==0) {
Serial.println("Alarm has gone off");
Blynk.notify("House Alarm!!!");
flag=1;
}
}
else if (pinState==1)
{
flag=0;
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(D1,INPUT_PULLUP);
timer.setInterval(1000L,notifyOnFire);
}
void loop()
{
//Serial.println(WiFi.localIP());
Blynk.run();
timer.run();
}
That is a generic solution to a generic problem. Typically you should use internal pull up resistors which can be enabled in software, with switches. But when a pin is connected to a sensor or other circuit, the input is typically not "floating" because the driving circuit has a low impedance.
So in order to foreshorten wild speculations, please post a wiring diagram and also provide some electrical information about the alarm system output that you are monitoring.