-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSonoff-MQTT.ino
381 lines (328 loc) · 8.63 KB
/
Sonoff-MQTT.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Copyright (c) 2017 Juergen Schilling [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
der GNU General Public License, wie von der Free Software Foundation,
Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
veröffentlichten Version, weiterverbreiten und/oder modifizieren.
Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
Siehe die GNU General Public License für weitere Details.
Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <PubSubClient.h>
#include <EEPROM.h>
/****************************************
* Generic Esp8266
* CPU 80MHz
* Flash 1M(64k SPIFFS)
****************************************/
const char *ssid = "yourSSID";
const char *password = "yourPassword";
const char *mqtt_server = "your-brocker-server";
String Hostname = "Sonoff-";
const String Version ="SonoffSchalter V1.1 \n(C) Schimmi 2017";
int altMilli;
#define ZEin 1
#define ZAus 0
int Zustand = ZAus;
#define EE_ADDRESS 10
#define PIN_Relais 12
#define PIN_LED 13
#define PIN_TASTER 0
#define Relais_Ein HIGH
#define Relais_Aus LOW
#define LED_Ein digitalWrite(PIN_LED, LOW )
#define LED_Aus digitalWrite(PIN_LED, HIGH)
ESP8266WebServer server(80);
ESP8266HTTPUpdateServer httpUpdater;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
float temp;
byte addr[8];
int i;
int t1;
int j=0;
uint8_t MAC_array[6];
int schritt=0;
void setup(void) {
int Timeout;
Serial.begin(115200);
Serial.println(Version);
/* get last state from EEPROM */
EEPROM.begin(256);
int eeprom_data = EEPROM.read(EE_ADDRESS);
if (eeprom_data == 0xFF)
{
Zustand = ZAus;
Serial.println("Init: Aus");
}
else
{
Zustand = eeprom_data;
Serial.printf("Init: ");
if (Zustand == ZEin)
{
Serial.println("EEPROM data Ein");
schritt = 10;
}
else
{
Serial.println("EEPROM data Aus");
schritt = 0;
}
}
pinMode(PIN_Relais,OUTPUT);
digitalWrite(PIN_Relais, Zustand==1?Relais_Ein:Relais_Aus);
pinMode(PIN_LED,OUTPUT);
LED_Ein;
pinMode(PIN_TASTER, INPUT);
WiFi.macAddress(MAC_array);
Hostname = Hostname + String(MAC_array[4], HEX);
Hostname = Hostname + String(MAC_array[5], HEX);
Serial.println("Initialized");
WiFi.mode(WIFI_STA);
//wifiMulti.addAP("schimmi2", "ot3458to"); // hier müssen natürlich die richtigen Angaben rein
//wifiMulti.addAP("SSID2", "PW2");
//wifiMulti.addAP("SSID3", "PW3");
WiFi.hostname(Hostname);
LED_Ein;
WiFi.begin ( ssid, password );
Serial.println("Booting");
Serial.println ( "" );
// Wait for connection
while ( WiFi.status() != WL_CONNECTED )
{
delay ( 500 );
Serial.print ( "." );
}
/* while (wifiMulti.run() != WL_CONNECTED)// Warten auf Verbindung
{
delay(500);
Timeout++;
if ((Timeout & 1)>0) LED_Aus;
else LED_Ein;
if (Timeout>100) break;
Serial.print(".");
} */
LED_Aus;
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
// ArduinoOTA.setHostname("myesp8266");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(Hostname);
Serial.println();
delay(1000);
httpUpdater.setup(&server);
server.on("/", Ereignis_Info);
server.on("/Ein", Ereignis_Ein);
server.on("/Aus", Ereignis_Aus);
server.onNotFound(Ereignis_Info);
server.begin(); // Starte den Server
client.setServer(mqtt_server, 1883); // starte den client
client.setCallback(callback);
altMilli = millis();
}
void Ereignis_Ein()
{
digitalWrite(PIN_Relais, Relais_Ein);
//LED_Ein;
schritt = 10;
Zustand = ZEin;
EEPROM.write(EE_ADDRESS, Zustand);
EEPROM.commit();
server.send(200, "text/plain", "Ein");
Serial.println("Ein ");
}
void Ereignis_Aus()
{
digitalWrite(PIN_Relais, Relais_Aus);
//LED_Aus;
schritt = 0;
Zustand = ZAus;
EEPROM.write(EE_ADDRESS, Zustand);
EEPROM.commit();
server.send(200, "text/plain", "Aus");
Serial.println("Aus ");
}
void Ereignis_Info()
{
String T = "";
T = Version + "\n\n";
T = T+ "SSID: "+WiFi.SSID()+" \n";
T = T+ "RSSI: "+String(WiFi.RSSI())+" \n";
T = T+ "Hostname: "+WiFi.hostname()+" \n";
T = T+ "Zustand : ";
if (Zustand == ZAus)
{
T = T+" Aus \n";
}
else
{
T = T+" Ein \n" ;
}
T = T+ "/Ein oder /Aus\n";
server.send(200, "text/plain", T);
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the relay if an 1 was received as first character
if ((char)payload[0] == '1')
{
Ereignis_Ein(); // Turn the relay on
}
else
{
Ereignis_Aus(); // Turn the rely off
}
}
void reconnect()
{
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client"))
{
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop()
{
int mi = millis();
String T = " ";
ArduinoOTA.handle();
if(digitalRead(PIN_TASTER) == LOW)
{
while(digitalRead(PIN_TASTER) == LOW)
{}
if(Zustand == ZEin)
{
Ereignis_Aus();
}
else
{
Ereignis_Ein();
}
}
if((mi-altMilli)>=500)
{
altMilli=mi;
Serial.print(".");
switch (schritt)
{
case 0: // Aus
LED_Ein;
Serial.print('E');
break;
case 1:
LED_Aus;
Serial.print('A');
case 2:
case 3:
case 4:
break;
case 5: schritt=-1;
break;
case 10: // Ein
LED_Aus;
break;
case 11:
LED_Ein;
case 12:
case 13:
case 14:
break;
case 15: schritt=9;
break;
default:
schritt = 0;
break;
}
Serial.println(schritt);
schritt++;
}
server.handleClient();
if (!client.connected())
{
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000)
{
lastMsg = now;
++value;
snprintf (msg, 75, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
}