-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdigoo433Weather.ino
369 lines (331 loc) · 13.1 KB
/
digoo433Weather.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
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <AsyncMqttClient.h> // https://github.com/marvinroger/async-mqtt-client (requires: https://github.com/me-no-dev/ESPAsyncTCP)
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "config.h" // rename sample_config.h and edit any values needed
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <homeGW.h>
#include <weather.h>
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// Input Pins //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
#define RF_RECEIVER_PIN D2 // D2
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// Output Pins //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
#define intLED1Pin D4 // D4 Wemos Mini D1 and NodeMCU
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// Verb/State Conversions //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
#define LEDon LOW
#define LEDoff HIGH
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// VARS Begin //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
#define rePushPoll 120 // push all sensor data every xx seconds - helpful if retain isn't used as this sketch only pushes data if a sensor changes.
#define mqttQOSLevel 2
char mcuHostName[64];
char sctTopic[96];
char lwtTopic[96];
char buildTopic[96];
char rssiTopic[96];
char sSCTcur[5];
char sSCTcurTemp[5];
unsigned long wifiLoopNow = 0;
int mqttTryCount = 0;
bool initBoot = true;
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
#ifdef DEBUGTELNET
WiFiServer telnetServer(23);
WiFiClient telnetClient;
#endif
AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;
Ticker rePushTick;
Ticker led1FlipTick;
Ticker wifiReconnectTimer;
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
HomeGW gw(1);
weather station1;
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// connectToWifi //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
void connectToWifi() {
debugLn(F("WIFI: Attempting to Connect."));
wifiLoopNow = millis(); // mark start time
WiFi.mode(WIFI_STA);
WiFi.hostname(mcuHostName);
WiFi.begin(wifiSSID, wifiPass);
delay(100);
// toggle on board LED as WiFi comes up
digitalWrite(intLED1Pin, LEDoff);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(intLED1Pin, !(digitalRead(intLED1Pin))); //Invert Current State of LED
delay(70);
digitalWrite(intLED1Pin, !(digitalRead(intLED1Pin)));
delay(45);
if (millis() > wifiLoopNow + 30000) { // WiFi Stuck? Reboot ESP and start over...
debugLn(F("ESP: WiFi Failed. Restarting ESP."));
delay(100);
ESP.restart();
}
}
digitalWrite(intLED1Pin, LEDoff);
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// onWifiConnect //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
long rssi = WiFi.RSSI();
debugLn(String(F("WIFI: Connected - IP: ")) + WiFi.localIP().toString() + " - RSSI: " + String(rssi) );
connectToMqtt();
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// onWifiDisconnect //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
debugLn(F("WIFI: Disconnected."));
mqttReconnectTimer.detach(); // don't reconnect to MQTT while reconnecting to Wi-Fi
wifiReconnectTimer.once(2, connectToWifi);
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// connectToMqtt //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
void connectToMqtt() {
debugLn(String(F("MQTT: Attempting connection to ")) + String(mqttHost) + " as " + mcuHostName);
mqttClient.connect();
mqttTryCount++;
if (mqttTryCount > 15) {
debugLn(F("ESP: MQTT Failed too many times. Restarting ESP."));
delay(100);
ESP.restart();
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// onMqttConnect //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
void onMqttConnect(bool sessionPresent) {
debugLn(F("MQTT: Connected"));
mqttTryCount = 0;
mqttClient.publish(lwtTopic, 2, true, mqttBirth);
// Setup Sensor Polling
rePushTick.attach(rePushPoll, rePushVals);
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// onMqttDisconnect //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
debugLn(F("MQTT: Disconnected."));
if (WiFi.isConnected()) {
mqttReconnectTimer.once(2, connectToMqtt);
}
rePushTick.detach();
}
void flipLED1() {
digitalWrite(intLED1Pin, !(digitalRead(intLED1Pin))); //Invert Current State of LED
}
const char *getDeviceID() {
char *identifier = new char[30];
os_strcpy(identifier, hostName);
strcat_P(identifier, PSTR("-"));
char cidBuf[7];
sprintf(cidBuf, "%06X", ESP.getChipId());
os_strcat(identifier, cidBuf);
return identifier;
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// ESP Setup //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
void setup() {
#ifdef DEBUGSERIAL
Serial.begin(115200);
while(!Serial) {} // Wait
Serial.println();
#endif
debugLn(String(F("digoo433Weather - Build: ")) + F(__DATE__) + " " + F(__TIME__));
// build hostname with last 6 of MACID
os_strcpy(mcuHostName, getDeviceID());
// ~~~~ Set MQTT Topics
sprintf_P(lwtTopic, PSTR("%s/LWT"), mcuHostName);
sprintf_P(sctTopic, PSTR("%s/SCT"), mcuHostName);
sprintf_P(rssiTopic, PSTR("%s/RSSI"), mcuHostName);
sprintf_P(buildTopic, PSTR("%s/BUILD"), mcuHostName);
// ~~~~ Set PIN Modes
pinMode(intLED1Pin,OUTPUT);
delay(100);
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
// setup MQTT
mqttClient.setWill(lwtTopic,2,true,mqttDeath,0);
mqttClient.setCredentials(mqttUser,mqttPass);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.setServer(mqttHost, mqttPort);
mqttClient.setMaxTopicLength(512);
mqttClient.setClientId(mcuHostName);
connectToWifi();
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
#ifdef DEBUGTELNET
// Setup telnet server for remote debug output
telnetServer.setNoDelay(true);
telnetServer.begin();
debugLn(String(F("Telnet: Started on port 23 - IP:")) + WiFi.localIP().toString());
#endif
wdt_reset();
// OTA Flash Sets
ArduinoOTA.setPort(OTAport);
ArduinoOTA.setHostname(mcuHostName);
ArduinoOTA.setPassword((const char *)OTApassword);
ArduinoOTA.onStart([]() {
Serial.println("Starting");
});
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();
// Setup HTTP Flash Page
httpUpdater.setup(&httpServer, "/flash", update_username, update_password);
httpServer.on("/restart", []() {
debugLn(F("HTTP: Restart request received."));
httpServer.sendHeader("Access-Control-Allow-Origin", "*");
httpServer.send(200, "text/plain", "Restart command sent to ESP Chip..." );
delay(100);
ESP.restart();
});
httpServer.begin();
// Setup RF Pin
pinMode(RF_RECEIVER_PIN, OUTPUT);
digitalWrite(RF_RECEIVER_PIN, LOW);
delay(1000);
pinMode(RF_RECEIVER_PIN, INPUT);
digitalWrite(RF_RECEIVER_PIN, LOW);
gw.setup(RF_RECEIVER_PIN);
gw.registerPlugin(&station1);
debugLn(F("ESP: Boot completed - Starting main loop"));
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// rePushVals //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
void rePushVals() {
debugLn(F("ESP: RePushingVals to MQTT"));
mqttClient.publish(rssiTopic, mqttQOSLevel, false, String(WiFi.RSSI()).c_str());
mqttClient.publish(buildTopic, mqttQOSLevel, false, String(String(F("digoo433Weathers - Build: ")) + F(__DATE__) + " " + F(__TIME__) + " - " + WiFi.localIP().toString()).c_str());
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// Telnet //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
#ifdef DEBUGTELNET
void handleTelnetClient()
{
if (telnetServer.hasClient())
{
// client is connected
if (!telnetClient || !telnetClient.connected())
{
if (telnetClient)
telnetClient.stop(); // client disconnected
telnetClient = telnetServer.available(); // ready for new client
}
else
{
telnetServer.available().stop(); // have client, block new connections
}
}
// Handle client input from telnet connection.
if (telnetClient && telnetClient.connected() && telnetClient.available())
{
// client input processing
while (telnetClient.available())
{
// Read data from telnet just to clear out the buffer
telnetClient.read();
}
}
}
#endif
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// Serial and Telnet Log Handler //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
void debugLn(String debugText)
{
String debugTimeText = "[+" + String(float(millis()) / 1000, 3) + "s] " + debugText;
#ifdef DEBUGSERIAL
Serial.println(debugTimeText);
Serial.flush();
#endif
#ifdef DEBUGTELNET
if (telnetClient.connected())
{
debugTimeText += "\r\n";
const size_t len = debugTimeText.length();
const char *buffer = debugTimeText.c_str();
telnetClient.write(buffer, len);
handleTelnetClient();
}
#endif
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// ESP Loop //
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
uint64_t prev_p = 0;
char nstationID[96];
float lastTemp;
char stationID[96];
void loop() {
ArduinoOTA.handle();
httpServer.handleClient();
#ifdef DEBUGTELNET
handleTelnetClient();
#endif
if (initBoot) { // on first loop pull sensors
initBoot = false;
if (WiFi.isConnected() && mqttClient.connected()) {
delay(100);
mqttClient.publish(rssiTopic, mqttQOSLevel, false, String(WiFi.RSSI()).c_str());
mqttClient.publish(buildTopic, mqttQOSLevel, false, String(String(F("digoo433Weather - Build: ")) + F(__DATE__) + " " + F(__TIME__) + " - " + WiFi.localIP().toString()).c_str());
debugLn(String(F("MQTT: digoo433Weather - Build: ")) + F(__DATE__) + " " + F(__TIME__) + " - " + WiFi.localIP().toString());
}
}
uint64_t p = 0;
char pubTopic[96];
if(station1.available())
if((p = station1.getPacket())) {
if(p == prev_p) {
sprintf(nstationID, "%s-%s", String(station1.getId(p)).c_str(), String(station1.getChannel(p)).c_str());
if (nstationID != "0-1") {
if ((stationID != nstationID) || (lastTemp != station1.getTemperature(p)/2)) {
lastTemp = station1.getTemperature(p)/2;
digitalWrite(intLED1Pin, LEDon);
sprintf(stationID, "%s-%s", String(station1.getId(p)).c_str(), String(station1.getChannel(p)).c_str());
sprintf(pubTopic, "%s/%s/battery", mcuHostName, stationID);
mqttClient.publish(pubTopic, mqttQOSLevel, false, String(station1.getBattery(p)).c_str());
sprintf(pubTopic, "%s/%s/temperature", mcuHostName, stationID);
mqttClient.publish(pubTopic, mqttQOSLevel, false, String(station1.getTemperature(p)/2).c_str());
sprintf(pubTopic, "%s/%s/humidity", mcuHostName, stationID);
mqttClient.publish(pubTopic, mqttQOSLevel, false, String(station1.getHumidity(p)/2).c_str());
debugLn(String(F("MQTT: Publish: ID:")) + String(stationID).c_str());
digitalWrite(intLED1Pin, LEDoff);
}
}
p = 0;
}
prev_p = p;
}
delay(250);
}