-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSonOffBasic.ino
140 lines (126 loc) · 4.34 KB
/
SonOffBasic.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <Homie.h>
#include <ArduinoOTA.h>
const int PIN_RELAY = 12;
const int PIN_LED = 13;
const int PIN_BUTTON = 0;
#define FW_NAME "sonoff-basic"
#define FW_VERSION "1.0.0"
unsigned long WiFifix = 0;
unsigned long problemDetected = 0;
int problemCount = 0;
String problemCause;
unsigned long buttonDownTime = 0;
byte lastButtonState = 1;
byte buttonPressHandled = 0;
HomieNode relayNode("switch", "switch");
bool relayHandler(const HomieRange& range, const String& value) {
String on = value;
on.toLowerCase();
if (on == "true") {
digitalWrite(PIN_RELAY, HIGH);
}
else if (on == "false") {
digitalWrite(PIN_RELAY, LOW);
}
else if (on == "reset") {
relayNode.setProperty("on/set").send(" ");
delay(1000);
Homie.reset();
}
else if (on == "reboot") {
relayNode.setProperty("on/set").send(" ");
delay(1000);
Homie.reboot();
}
else if (on == "ota-restart") {
relayNode.setProperty("on/set").send(" ");
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
}
relayNode.setProperty("on").send(value);
}
void toggleRelay() {
bool on = digitalRead(PIN_RELAY) == HIGH;
digitalWrite(PIN_RELAY, on ? LOW : HIGH);
relayNode.setProperty("on").send(on ? "false" : "true");
Homie.getLogger() << "Switch is " << (on ? "off" : "on") << endl;
}
void Buttonhandle() {
byte buttonState = digitalRead(PIN_BUTTON);
if ( buttonState != lastButtonState ) {
if (buttonState == LOW) {
buttonDownTime = millis();
buttonPressHandled = 0;
}
else {
unsigned long dt = millis() - buttonDownTime;
if ( dt >= 90 && dt <= 900 && buttonPressHandled == 0 ) {
toggleRelay();
buttonPressHandled = 1;
}
}
lastButtonState = buttonState;
}
}
void fixWiFi() {
// Posts every 10 seconds the state of WiFi.status(), Homie.getMqttClient().connected() and Homie.isConfigured()
// Within this interval the connectivity is checked and logged if a problem is detected
// Then it disconnects Wifi, if Wifi or MQTT is not connected for 1 Minute (but only if Homie is configured)
if ( WiFifix == 0 || ((millis() - WiFifix) > 10000)) {
if (Homie.isConfigured() == 1) {
float rssi = WiFi.RSSI();
Homie.getLogger() << endl;
relayNode.setProperty("quality").send(String(rssi));
Homie.getLogger() << "Wifi-state:" << WiFi.status() << " | Wi-Fi signal quality:" << rssi << " | MQTT-state:" << Homie.getMqttClient().connected() << " | HomieConfig-state:" << Homie.isConfigured() << endl;
if (!Homie.getMqttClient().connected() || WiFi.status() != 3) {
if (0 == problemDetected) {
if (WiFi.status() != 3) {
problemCause = "WiFi: Disconnected ";
}
if (!Homie.getMqttClient().connected()) {
problemCause += "MQTT: Disconnected";
}
Homie.getLogger() << "Connectivity in problematic state --> " << problemCause << endl;
problemDetected = millis();
}
else if ((millis() - problemDetected) > 120000 && (problemCount >= 5)) {
Homie.getLogger() << "Connectivity in problematic state --> This remained for 10 minutes. Rebooting!" << endl;
Homie.reboot();
}
else if ((millis() - problemDetected) > 120000 && problemCount < 5) {
problemCount = (problemCount + 1);
Homie.getLogger() << "Connectivity in problematic state --> " << problemCause << "/n This remained for 2 minutes. Disconnecting WiFi to start over." << endl;
problemDetected = 0;
problemCause = "";
WiFi.disconnect();
}
}
else if (problemCount != 0 && Homie.getMqttClient().connected() || WiFi.status() == 3) {
problemCount = 0;
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
}
}
WiFifix = millis();
}
}
void setup() {
Serial.begin(115200);
Serial << endl << endl;
pinMode(PIN_RELAY, OUTPUT);
digitalWrite(PIN_RELAY, LOW);
Homie_setBrand(FW_NAME);
Homie_setFirmware(FW_NAME, FW_VERSION);
Homie.setLedPin(PIN_LED, LOW);
Homie.setResetTrigger(PIN_BUTTON, LOW, 5000);
relayNode.advertise("on").settable(relayHandler);
Homie.setup();
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
}
void loop() {
Homie.loop();
ArduinoOTA.handle();
Buttonhandle();
fixWiFi();
}