Files
ESPGadget/60_module_sonoff.ino
Florian Overkamp 1d6201eb25 Adding to repo
2019-03-30 08:39:24 +01:00

69 lines
2.2 KiB
C++

#ifdef ENABLE_SONOFF
/*
* Interface with other modules
*/
void newcmd(String cmd) {
cmd.toLowerCase();
if(cmd.equals("off")) {
settings.mode = 0;
PRINTLN_SERIAL("Lights off");
state.text = "Sonoff switched off";
}
if(cmd.equals("on")) {
settings.mode = 1;
PRINTLN_SERIAL("Lights on");
state.text = "Sonoff switched off";
}
}
int sonoff_buttontoggle = 0; // counter for the number of button presses
int sonoff_buttonstate = LOW; // current state of the button
int sonoff_lastbuttonstate = LOW; // previous state of the button
/*
* Setup and loop code. Be cooperative! No delays, timers.
*/
void sonoff_setup() {
PRINTLN_SERIAL("initialising module Sonoff");
pinMode(SONOFF_LED, OUTPUT); // Note that this LED is powered when voltage is LOW
digitalWrite(SONOFF_LED, HIGH); // turn the LED off (HIGH is the voltage level)
pinMode(SONOFF_RELAY, OUTPUT); // Note that this relay is powered when voltage is HIGH
digitalWrite(SONOFF_RELAY, LOW); // turn the relay off (HIGH is the voltage level)
}
void sonoff_loop() {
// This is a matter of last resort kind of thing: the built-in button will toggle power on or off, regardless of MQTT or network availability
// As good measure, we might try reporting some kind of state when connecting later on. @@@FIXME@@@
// read the pushbutton input pin:
sonoff_buttonstate = digitalRead(SONOFF_BUTTON);
// compare the buttonState to its previous state
if (sonoff_buttonstate != sonoff_lastbuttonstate) {
// if the state has changed, increment the counter
if (sonoff_buttonstate == LOW) {
// if the current state is LOW then the button went from off to on:
sonoff_buttontoggle = 1-sonoff_buttontoggle;
PRINTLN_SERIAL("Button pressed");
if (sonoff_buttontoggle == 1) {
newcmd("on");
} else {
newcmd("off");
}
} else {
// if the current state is LOW then the button went from on to off:
PRINTLN_SERIAL("Button released");
}
// Delay a little bit to avoid bouncing
delay(50); // @@@FIXME@@@ delays are undesirable!
}
// save the current state as the last state, for next time through the loop
sonoff_lastbuttonstate = sonoff_buttonstate;
}
#endif