65 lines
2.1 KiB
C++
65 lines
2.1 KiB
C++
#ifdef ENABLE_SONOFF
|
|
|
|
/*
|
|
* Interface with other modules
|
|
*/
|
|
void newcmd(String cmd) {
|
|
cmd.toLowerCase();
|
|
if(cmd.equals("off")) {
|
|
PRINTLN_SERIAL("Lights off");
|
|
}
|
|
if(cmd.equals("on")) {
|
|
PRINTLN_SERIAL("Lights on");
|
|
}
|
|
}
|
|
|
|
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
|