From 10df3722937f9a07e37f3808cb49fc27b137fbe0 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Sat, 18 Mar 2017 23:20:42 +0100 Subject: [PATCH] Bunch of hack to link diode with a button on a sonoff --- WifiControlSensor/WifiControlSensor.ino | 21 ++++++++++++++++++++- WifiControlSensor/config_device.h | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index 40fe6a1..ca5551e 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -71,6 +71,9 @@ const int gpioControlled[] = CONFIG_CONTROLLED_GPIO; const int gpioObserved[] = CONFIG_OBSERVED_GPIO; const int pwmControlled[] = CONFIG_CONTROLLED_PWM; +uint8_t shouldSwitch = 0; +uint8_t switchSide = 0; + /* Set these to your desired credentials. */ const char *ssid = CONFIG_SSID_NAME; @@ -96,6 +99,15 @@ void onLongButtonPressed(uint8_t pin){ WiFi.softAP(ssid); } } + +void onShortButtonPressed(uint8_t pin, int state){ + SKETCH_DEBUG_PRINTLN("OnShortButtonPressed"); + if(pin == CONFIG_SETUP_BUTTON){ + shouldSwitch = 1; + switchSide = digitalRead(12) ? 0 : 1; + } +} + #endif void WifiSetup(productConfig conf) { @@ -188,7 +200,7 @@ void OTASetup() { void setup() { #ifdef CONFIG_SETUP_BUTTON - new HIB(CONFIG_SETUP_BUTTON, HIGH, NULL, NULL, onLongButtonPressed); + new HIB(CONFIG_SETUP_BUTTON, HIGH, NULL, onShortButtonPressed, onLongButtonPressed, 5000, 100); #endif pinMode(3, OUTPUT); @@ -250,6 +262,7 @@ void setup() { #ifdef CONFIG_ENABLE_POWER_SAVE wifi_set_sleep_type(LIGHT_SLEEP_T); #endif + pinMode(13, OUTPUT); } uint nbCycle = CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS; @@ -264,6 +277,12 @@ void loop() { } delay(CONFIG_WEB_DELAY_MS); + if (shouldSwitch) { + shouldSwitch = 0; + MqttChangeGpioValue(12, switchSide); + } + digitalWrite(13, digitalRead(12) ? 0 : 1); + nbCycle++; if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) { if (!BMP180GetTempAndPressure(temp, pressure)) { diff --git a/WifiControlSensor/config_device.h b/WifiControlSensor/config_device.h index c727a56..c7286c1 100644 --- a/WifiControlSensor/config_device.h +++ b/WifiControlSensor/config_device.h @@ -51,7 +51,7 @@ // GPIO that can be set or get by mqtt and set via http // Should have less value than MAXSUBSCRIPTIONS -//#define CONFIG_CONTROLLED_GPIO {12,13} +#define CONFIG_CONTROLLED_GPIO {12,13} // GPIO that can be get by mqtt and http // Pin 6 to 11 and 16 can not be used for mqtt