Is it possible to configure a NodeMCU in ESPHome so that it does something only if two inputs go high together?
I have some motion detectors (programmed through the Arduino) running on NodeMCUs that use both PIR and microwave sensors to reduce false alarms and I would like to convert them to ESPHome.
As a secondary function, it also flashes an LED slowly if neither the PIR or microwave sensor is triggered, faster if one is triggered and solid on if both are triggered. This is not essential but would be good to emulate.
I know I can do this in HA after getting the various inputs from the NodeMCU via ESPHome but it feels easier to do it locally on the NodeMCU - or am I incorrect?
Existing code below for interest.
Thanks.
Richard
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiAP.h>
#include <ESP8266WiFiGeneric.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WiFiScan.h>
#include <ESP8266WiFiSTA.h>
#include <ESP8266WiFiType.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
//define constants
const char* ssid = "Secret";
const char* password = "Secret";
const int Pin_PIR = 5; //pin for input from PIR
const int Pin_MW = 12; //pin for input from MW
const int Pin_Alarm = 14; //pin for alarm indicator LED
const long Interval_Normal = 500; // interval at which to blink (milliseconds)
const long Interval_Alert = 150; // interval at which to blink (milliseconds)
//define variables
int Trig_MW = LOW; // variable to hold MW trigger state
int Trig_PIR = LOW; // variable to hold PIR trigger state
int Send = 0; // used to call HTTP function
int LED_Flash = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time flashing LED was updated
unsigned long previousLEDMillis = 0; // will store last time Main LED was updated
unsigned long interval = 150; // interval at which to blink (milliseconds)
void setup ()
{
Serial.begin(9600);
Serial.println("Hello World");
WiFi.begin(ssid, password);
//set up in states - options INPUT, OUTPUT, INPUT_PULLUP (for D0 - D15) and INPUT_PULLDOWN (for D16)
pinMode(Pin_Alarm, OUTPUT);
pinMode(Pin_PIR, INPUT);
pinMode(Pin_MW, INPUT);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println(WiFi.localIP());
yield();
delay(5000);
Serial.println("Connecting..");
}
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis; // save the last time you blinked the LED
// if the LED is off turn it on and vice-versa:
if (LED_Flash == LOW)
{
LED_Flash = HIGH;
}
else
{
LED_Flash = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(Pin_Alarm, LED_Flash);
}
Trig_MW = digitalRead(Pin_MW);
Trig_PIR = digitalRead(Pin_PIR);
if ((Trig_MW == 1) && (Trig_PIR ==1))
{
Serial.println("Fully Triggered");
digitalWrite(Pin_Alarm, HIGH); // Turn LED on
Send=SendGet(); //uncomment this to send the camera trigger
yield();
delay(2000);
}
else if ((Trig_MW == 1) || (Trig_PIR ==1))
{
if (Trig_PIR == 1)
{
Serial.println("PIR Triggered");
interval = Interval_Alert;
Serial.println(interval);
}
if (Trig_MW == 1)
{
Serial.println("MW Triggered");
interval = Interval_Alert;
Serial.println(interval);
}
}
else
{
Serial.println("Not Triggered");
digitalWrite(Pin_Alarm, LOW); //reset LED
interval = Interval_Normal;
Serial.println(interval);
yield();
}
}
int SendGet()
{
if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
//Serial.println(WiFi.localIP());
HTTPClient http; //Declare an object of class HTTPClient
http.begin("http://192.168.30.200:1685/admin?camera=Shed&trigger&user=secret&pw=secret; //Specify request destination
int httpCode = http.GET(); //Send the request
if (httpCode > 0) //Check the returning code
{
String payload = http.getString(); //Get the request response payload
}
http.end(); //Close connection
}
}