From 0f118c2020d47765492f0732a77a3e2779eb7065 Mon Sep 17 00:00:00 2001 From: QuagmireMan Date: Thu, 23 Aug 2018 15:07:29 -0400 Subject: [PATCH] GPIOInput Third Time is a Charm! (#258) * Added 2 files via upload * Update OpenMQTTGateway.ino * Update Test_config.h --- OpenMQTTGateway.ino | 13 +++++++- ZsensorGPIOInput.ino | 79 ++++++++++++++++++++++++++++++++++++++++++++ config_GPIOInput.h | 38 +++++++++++++++++++++ tests/Test_config.h | 3 ++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 ZsensorGPIOInput.ino create mode 100644 config_GPIOInput.h diff --git a/OpenMQTTGateway.ino b/OpenMQTTGateway.ino index 5c8f77c..5fcd609 100644 --- a/OpenMQTTGateway.ino +++ b/OpenMQTTGateway.ino @@ -106,7 +106,9 @@ #ifdef ZgatewayRFM69 #include "config_RFM69.h" #endif - +#ifdef ZsensorGPIOInput + #include "config_GPIOInput.h" +#endif // array to store previous received RFs, IRs codes and their timestamps #if defined(ESP8266) || defined(ESP32) #define array_size 12 @@ -370,6 +372,9 @@ void setup() #ifdef ZsensorHCSR501 setupHCSR501(); #endif + #ifdef ZsensorGPIOInput + setupGPIOInput(); + #endif } @@ -606,6 +611,9 @@ void loop() #ifdef ZsensorHCSR501 MeasureHCSR501(); #endif + #ifdef ZsensorGPIOInput + MeasureGPIOInput(); + #endif #ifdef ZsensorADC MeasureADC(); //Addon to measure the analog value of analog pin #endif @@ -733,6 +741,9 @@ void stateMeasures(){ #ifdef ZsensorHCSR501 modules = modules + ZsensorHCSR501; #endif + #ifdef ZsensorGPIOInput + modules = modules + ZsensorGPIOInput; + #endif SYSdata["modules"] = modules; trc(modules); char JSONmessageBuffer[100]; diff --git a/ZsensorGPIOInput.ino b/ZsensorGPIOInput.ino new file mode 100644 index 0000000..563fb2a --- /dev/null +++ b/ZsensorGPIOInput.ino @@ -0,0 +1,79 @@ +/* + OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Send and receiving command by MQTT + + GPIO Input derived from HC SR-501 reading Addon and https://www.arduino.cc/en/Tutorial/Debounce + + This reads a high (open) or low (closed) through a circuit (switch, float sensor, etc.) connected to ground. + + Copyright: (c)Florian ROBERT + + Contributors: + - 1technophile + - QuagmireMan + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + OpenMQTTGateway is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifdef ZsensorGPIOInput + +unsigned long lastDebounceTime = 0; +int InputState = 3; // Set to 3 so that it reads on startup +int lastInputState = 3; + + +void setupGPIOInput() { + pinMode(GPIOInput_PIN, INPUT_PULLUP); // declare GPIOInput pin as input_pullup to prevent floating. Pin will be high when not connected to ground +} + +void MeasureGPIOInput(){ + int reading = digitalRead(GPIOInput_PIN); + + // check to see if you just pressed the button + // (i.e. the input went from LOW to HIGH), and you've waited long enough + // since the last press to ignore any noise: + + // If the switch changed, due to noise or pressing: + if (reading != lastInputState) { + // reset the debouncing timer + lastDebounceTime = millis(); + + } + + if ((millis() - lastDebounceTime) > GPIOInputDebounceDelay) { + // whatever the reading is at, it's been there for longer than the debounce + // delay, so take it as the actual current state: + #if defined(ESP8266) || defined(ESP32) + yield(); + #endif + // if the Input state has changed: + if (reading != InputState) { + InputState = reading; + + if (InputState == HIGH) { + trc(F("GPIO HIGH")); + client.publish(subjectGPIOInputtoMQTT,"HIGH"); + } + if (InputState == LOW) { + trc(F("GPIO LOW")); + client.publish(subjectGPIOInputtoMQTT,"LOW"); + } + } + } + + // save the reading. Next time through the loop, it'll be the lastInputState: + lastInputState = reading; + +} +#endif diff --git a/config_GPIOInput.h b/config_GPIOInput.h new file mode 100644 index 0000000..145aba4 --- /dev/null +++ b/config_GPIOInput.h @@ -0,0 +1,38 @@ +/* + OpenMQTTGateway - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Send and receiving command by MQTT + + This files enables to set your parameter for the GPIOInput sensor + + Copyright: (c)Florian ROBERT + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenMQTTGateway is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/*----------------------------USER PARAMETERS-----------------------------*/ +/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ +#define subjectGPIOInputtoMQTT Base_Topic Gateway_Name "/GPIOInputtoMQTT" +#define GPIOInputDebounceDelay 60 //debounce time, increase if there are issues + +/*-------------------PIN DEFINITIONS----------------------*/ +#if defined(ESP8266) || defined(ESP32) + #define GPIOInput_PIN 5 //D5 +#else + #define GPIOInput_PIN 7 +#endif + diff --git a/tests/Test_config.h b/tests/Test_config.h index 971e3ba..7763925 100644 --- a/tests/Test_config.h +++ b/tests/Test_config.h @@ -103,6 +103,7 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress #define ZsensorTSL2561 "TSL2561" #define ZsensorDHT "DHT"// If you uncomment this you can't use I2C due to the fact that I2C use also D1 #define ZgatewayRFM69 "RFM69"// If you uncomment this you can't use RF and BT due to the fact that RF use also D8 and BT use also D6/D7 + #define ZsensorGPIOInput "GPIOInput" #elif ESP32 #define ZgatewayRF "RF" #define ZgatewayRF "RF315" @@ -118,6 +119,7 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress //#define ZsensorBME280 #define ZsensorDHT "DHT"// If you uncomment this you can't use I2C due to the fact that I2C use also D1 //#define ZgatewayRFM69 // If you uncomment this you can't use RF and BT due to the fact that RF use also D8 and BT use also D6/D7 + //#define ZsensorGPIOInput "GPIOInput" //untested #else // for arduino mega + W5100 #define ZgatewayRF "RF" //#define Zgateway2G (not tested yet) @@ -133,6 +135,7 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress //#define ZsensorHCSR501 //#define ZsensorADC //#define ZgatewayRFM69 not tested + //#define ZsensorGPIOInput //not tested #endif /*----------------------------OTHER PARAMETERS-----------------------------*/ #ifdef ZgatewaySRFB