forked from jamct/sonoffSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonoffSocket.ino
355 lines (304 loc) · 8.71 KB
/
sonoffSocket.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <Ticker.h>
//To use MQTT, install Library "PubSubClient" and switch next line to 1
#define USE_MQTT 0
//If you don`t want to use local button switch to 0
#define USE_LOCAL_BUTTON 1
// Allow WebUpdate
#define USE_WEBUPDATE 1
#if USE_WEBUPDATE == 1
#include <ESP8266HTTPUpdateServer.h>
#endif
#if USE_MQTT == 1
#include <PubSubClient.h>
//Your MQTT Broker
const char* mqtt_server = "your mqtt broker";
const char* mqtt_in_topic = "socket/switch/set";
const char* mqtt_out_topic = "socket/switch/status";
#endif
//Yout Wifi SSID
const char* ssid = "your_ssid";
//Your Wifi Key
const char* password = "your_key";
ESP8266WebServer server(80);
#if USE_WEBUPDATE == 1
ESP8266HTTPUpdateServer httpUpdater;
#endif
#if USE_MQTT == 1
WiFiClient espClient;
PubSubClient client(espClient);
bool status_mqtt = 1;
#endif
int gpio13Led = 13;
int gpio12Relay = 12;
bool lamp = 0;
bool relais = 0;
//Test GPIO 0 every 0.1 sec
Ticker checker;
bool status = 1;
// Timer
unsigned long stopAt = 0;
unsigned long startAt = 0;
// "0" means timer not running
// reference is millis()
void setup(void){
Serial.begin(115200);
delay(5000);
Serial.println("");
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
pinMode(gpio13Led, OUTPUT);
digitalWrite(gpio13Led, lamp);
pinMode(gpio12Relay, OUTPUT);
digitalWrite(gpio12Relay, relais);
// Wait for WiFi
Serial.println("");
Serial.print("Verbindungsaufbau mit: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if(lamp == 0){
digitalWrite(gpio13Led, 1);
lamp = 1;
}else{
digitalWrite(gpio13Led, 0);
lamp = 0;
}
Serial.print(".");
}
Serial.println("Verbunden");
Serial.print("IP-Adresse: ");
Serial.println(WiFi.localIP());
digitalWrite(gpio13Led, HIGH);
#if USE_MQTT == 1
client.setServer(mqtt_server, 1883);
client.setCallback(MqttCallback);
#endif
server.on("/", [](){
String msg = "<H1>Sonoff</H1>\n";
msg += "uptime " + String(millis() / 1000) + "s<br />\n";
msg += "<H2>Befehle</H2>\n<p>";
msg += "<a href=\"ein\">Einschalten</a><br />\n";
msg += "<a href=\"aus\">Ausschalten</a><br />\n";
msg += "<a href=\"toggle\">Umschalten</a><br />\n";
msg += "<a href=\"state\">Schaltstatus</a><br />\n";
msg += "<a href=\"timer\">Timer</a><br />\n";
#if USE_WEBUPDATE == 1
msg += "<a href=\"update\">Update</a><br />\n";
#endif
msg += "</p>\n";
msg += "<H2>WiFi</H2>\n<p>";
msg += "IP: " + WiFi.localIP().toString() + "<br />\n";
msg += "Mask: " + WiFi.subnetMask().toString() + "<br />\n";
msg += "GW: " + WiFi.gatewayIP().toString() + "<br />\n";
msg += "MAC: " + WiFi.macAddress() + "<br />\n";
msg += "SSID: " + String(WiFi.SSID()) + "<br />\n";
msg += "RSSI: " + String(WiFi.RSSI()) + "<br />\n";
msg += "</p>\n";
msg += "<H2>Status</H2>\n";
if ((startAt) > 0)
{
msg += "Timer bis zum Einschalten noch " + String((startAt - millis()) / 1000) + "s. ";
}
if ((stopAt) > 0)
{
msg += "Timer bis zum Ausschalten noch " + String((stopAt - millis()) / 1000) + "s. ";
}
if (relais == 0) {
server.send(200, "text/html", msg + "Schaltsteckdose ist aktuell aus.<p><a href=\"ein\">Einschalten</a></p>");
} else {
server.send(200, "text/html", msg + "Schaltsteckdose ist aktuell ein.<p><a href=\"aus\">Ausschalten</a></p>");
}
server.send ( 302, "text/plain", "");
});
//German Path, left for compatibility
server.on("/ein", [](){
server.send(200, "text/html", "Schaltsteckdose ist aktuell ein.<p><a href=\"aus\">Ausschalten</a></p>");
Switch_On();
delay(1000);
});
server.on("/aus", [](){
server.send(200, "text/html", "Schaltsteckdose ist aktuell aus.<p><a href=\"ein\">Einschalten</a></p>");
Switch_Off();
delay(1000);
});
server.on("/on", [](){
server.send(200, "text/html", "Schaltsteckdose ist aktuell ein.<p><a href=\"aus\">Ausschalten</a></p>");
Switch_On();
delay(1000);
});
server.on("/off", [](){
server.send(200, "text/html", "Schaltsteckdose ist aktuell aus.<p><a href=\"ein\">Einschalten</a></p>");
Switch_Off();
delay(1000);
});
server.on("/state", [](){
if(relais == 0){
server.send(200, "text/html", "off");
}else{
server.send(200, "text/html", "on");
}
});
server.on("/toggle", [](){
if(relais == 0){
server.send(200, "text/html", "Schaltsteckdose ist aktuell ein.<p><a href=\"aus\">Ausschalten</a></p>");
Switch_On();
delay(1000);
}else{
server.send(200, "text/html", "Schaltsteckdose ist aktuell aus.<p><a href=\"ein\">Einschalten</a></p>");
Switch_Off();
delay(1000);
}
server.send ( 302, "text/plain", "");
});
// Timer
server.on("/timer", []() {
String message = "";
for (int i = 0; i < server.args(); i++) {
//message += "Arg nº" + (String)i + " –> ";
//message += server.argName(i) + ": ";
//message += server.arg(i) + " <br />\n";
if (server.argName(i) == "off") {
int dauer = server.arg(i).toInt();
startAt = millis() + (dauer * 1000);
message += "Aus die nächsten " + String(dauer) + "s";
server.send(200, "text/html", message);
Switch_Off();
delay(100);
}
if (server.argName(i) == "on") {
int dauer = server.arg(i).toInt();
stopAt = millis() + (dauer * 1000);
message += "An die nächsten " + String(dauer) + "s";
server.send(200, "text/html", message);
Switch_On();
}
}
if (message == "") //No Parameter
{
message += "<form action=\"timer\" id=\"on\">Timer bis zum Ausschalten <input type=\"text\" name=\"on\" id=\"on\" maxlength=\"5\"> Sekunden<button type=\"submit\">Starten</button></form>";
message += "<form action=\"timer\" id=\"off\">Timer bis zum Einschalten <input type=\"text\" name=\"off\" id=\"off\" maxlength=\"5\"> Sekunden <button type=\"submit\">Starten</button></form>";
server.send(200, "text/html", message);
server.send ( 302, "text/plain", "");
}
});
#if USE_WEBUPDATE == 1
httpUpdater.setup(&server);
#endif
server.begin();
Serial.println("HTTP server started");
checker.attach(0.1, check);
}
#if USE_MQTT == 1
void MqttCallback(char* topic, byte* payload, unsigned int length) {
// Switch on
if ((char)payload[0] == '1') {
Switch_On();
//Switch off
} else {
Switch_Off();
}
}
void MqttReconnect() {
String clientID = "SonoffSocket_"; // 13 chars
clientID += WiFi.macAddress();//17 chars
while (!client.connected()) {
Serial.print("Connect to MQTT-Broker");
if (client.connect(clientID.c_str())) {
Serial.print("connected as clientID:");
Serial.println(clientID);
//publish ready
client.publish(mqtt_out_topic, "mqtt client ready");
//subscribe in topic
client.subscribe(mqtt_in_topic);
} else {
Serial.print("failed: ");
Serial.print(client.state());
Serial.println(" try again...");
delay(5000);
}
}
}
void MqttStatePublish() {
if (relais == 1 and not status_mqtt)
{
status_mqtt = relais;
client.publish(mqtt_out_topic, "on");
Serial.println("MQTT publish: on");
}
if (relais == 0 and status_mqtt)
{
status_mqtt = relais;
client.publish(mqtt_out_topic, "off");
Serial.println("MQTT publish: off");
}
}
#endif
void Switch_On(void) {
relais = 1;
digitalWrite(gpio13Led, LOW);
digitalWrite(gpio12Relay, relais);
startAt = 0;
}
void Switch_Off(void) {
relais = 0;
digitalWrite(gpio13Led, HIGH);
digitalWrite(gpio12Relay, relais);
stopAt = 0;
}
void check(void)
{
// Check if timers are finished
if (stopAt != 0)
{
if (millis() >= stopAt )
{
Switch_Off();
}
else if ((millis() % 3000) < 300) digitalWrite(gpio13Led, HIGH);
else digitalWrite(gpio13Led, LOW);
}
if (startAt != 0)
{
if (millis() >= startAt )
{
Switch_On();
}
else if ((millis() % 3000) < 300) digitalWrite(gpio13Led, LOW);
else digitalWrite(gpio13Led, HIGH);
}
#if USE_LOCAL_BUTTON == 1
//check gpio0 (button of Sonoff device)
if ((digitalRead(0) == 0) and not status)
{
status=(digitalRead(0) == 0);
if (relais == 0) {
Switch_On();
//Switch off
} else {
Switch_Off();
}
}
status=(digitalRead(0) == 0);
#endif
}
void loop(void){
//Webserver
server.handleClient();
#if USE_MQTT == 1
//MQTT
if (!client.connected()) {
MqttReconnect();
}
if (client.connected()) {
MqttStatePublish();
}
client.loop();
#endif
}