-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIoT_Sensor.ino
439 lines (364 loc) · 10.7 KB
/
IoT_Sensor.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
IoT Sensor for Home Automation System
MQTT Topics
devices/poke = prompts all devices to report their state
devices/update = allows the device to receive OTA updates if the message matches its MAC
pub_motion = sends 'alert' if motion is detected
pub_temperature = send temperature values
WTFPL license
If this helps you in any way feel free to buy me a drink
https://www.buymeacoffee.com/manythanks
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <dht.h>
//struct
//{
// uint32_t total;
// uint32_t ok;
// uint32_t crc_error;
// uint32_t time_out;
// uint32_t connect;
// uint32_t ack_l;
// uint32_t ack_h;
// uint32_t unknown;
//} stat = { 0,0,0,0,0,0,0,0};
//==============================
// HARDWARE SETTINGS
//==============================
//#define DHTPIN 4 // what digital pin the DHT22 is conected to
//#define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
#define DHT22_PIN 4
const int LED = D4; // LED pin
const int TEMP_PIN = A0; // TEMP pin
const int PIR_PIN = D1; // PIR pin - SAFE to be LOW or HIGH on BOOT on D1 MINI
//==============================
// NETWORK SETTINGS
//==============================
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* mqtt_server = "MQTT_IP";
//==============================
// GLOBAL VARIABLES
//==============================
WiFiClient espClient;
PubSubClient client(espClient);
String JSON;
StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
StaticJsonBuffer<500> jsonErrorBuffer;
JsonObject& error_report = jsonErrorBuffer.createObject();
//DHT dht(DHTPIN, DHTTYPE);
dht DHT;
String MAC;
String IP;
// DEVICE LOCATION
//==============================
String FLOOR;
String ROOM;
String TYPE;
String LOCATION;
// MQTT TOPICS
//==============================
String sub_devices_poke = "devices/poke";
String sub_devices_update = "devices/update";
String pub_devices_error = "devices/error";
String pub_devices = "devices";
String pub_motion;
String pub_temperature;
String pub_humidity;
String Firmware = "1.2";
bool motion_published = false;
int prev_temp = -999;
unsigned long temp_lastpubtime = 0;
unsigned long humi_lastpubtime = 0;
unsigned long sensor_pubrate = 60000;
bool Allow_OTA = false;
unsigned long OTA_Timeout = 60000;
unsigned long OTA_Timer = 0;
unsigned long timeSinceLastTempHumiRead = 0;
//==============================
// SETUP FUNCTIONS
//==============================
void setup() {
Serial.begin(115200);
SetupPins();
SetupWiFi();
SetupOTA();
SetupMQTT();
SetLocation("0","hall","sensors");
GetDeviceInfo();
reconnect();
}
void SetupPins(){
pinMode(LED, OUTPUT);
pinMode (TEMP_PIN, INPUT);
pinMode (PIR_PIN, INPUT_PULLUP);
digitalWrite(LED, HIGH);
Serial.println("PINS Ready");
}
void SetupWiFi() {
delay(10);
// We start by connecting to a WiFi network
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void SetupOTA(){
ArduinoOTA.setPassword((const char *)"OTA_PASSWORD");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
ESP.restart();
});
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("OTA Ready");
}
void SetupMQTT(){
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.println("MQTT Ready");
}
void AllowOTA(){
Allow_OTA = true;
OTA_Timer = millis();
}
void publishDevice(){
//just randomly delay between 1-1000ms to avoid
//everything hitting the broker at the same time
delay(random(1,1000));
client.publish(pub_devices.c_str(), JSON.c_str());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MAC.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
//publish the device info to the device channel
publishDevice();
// ... and resubscribe
client.subscribe(sub_devices_poke.c_str());
client.subscribe(sub_devices_update.c_str());
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void SetLocation(String Floor, String Room, String Type){
//Set the location of the device
FLOOR = Floor;
ROOM = Room;
TYPE = Type;
LOCATION = FLOOR + "/" + ROOM + "/" + TYPE + "/";
BuildPubTopics();
}
void BuildPubTopics(){
//Allow us to set location
pub_motion = LOCATION + "motion";
pub_temperature = LOCATION + "temp";
pub_humidity = LOCATION + "humidity";
}
//==============================
// DEVICES SPECIFIC Fn
//==============================
void callback(char* topic, byte* payload, unsigned int length) {
if (String(topic) == sub_devices_poke){
//don't care what the message is
publishDevice();
} else if (String(topic) == sub_devices_update){
if (!strncmp((char *)payload, MAC.c_str(), length)) {
AllowOTA();
}
}
}
void GetDeviceInfo(){
byte mac[6];
WiFi.macAddress(mac);
char mac_[20];
sprintf(mac_, "%2X%2X%2X%2X%2X%2X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
//network and device info
IP = WiFi.localIP().toString();
MAC = String(mac_);
//populate JSON with device info
root["description"] = "multi-sensor";
root["ip"] = IP.c_str();
root["mac"] = MAC.c_str();
root["version"] = Firmware.c_str();
//populate JSON with description of the data the device sends
JsonObject& f0 = root.createNestedObject("function0");
f0["type"] = "temperature sensor";
f0["value"] = "celsius";
f0["control"] = "pub";
f0["topic"] = pub_temperature.c_str();
JsonObject& f1 = root.createNestedObject("function1");
f1["type"] = "humidity sensor";
f1["value"] = "%";
f1["control"] = "pub";
f1["topic"] = pub_humidity.c_str();
JsonObject& f2 = root.createNestedObject("function2");
f2["type"] = "motion sensor";
f2["value"] = "alert";
f2["control"] = "pub";
f2["topic"] = pub_motion.c_str();
root.printTo(JSON);
}
void publishError(String error){
//populate JSON with device info
error_report["description"] = "multi-sensor";
error_report["ip"] = IP.c_str();
error_report["mac"] = MAC.c_str();
error_report["location"] = LOCATION.c_str();
error_report["error"] = error.c_str();
String ErrorJson;
error_report.printTo(ErrorJson);
client.publish(pub_devices_error.c_str(), ErrorJson.c_str());
}
bool MeasureEnvironment(unsigned long *_time, float *temp, float *humidity){
if((millis() - *_time) > 10000) {
//*humidity = dht.readHumidity();
//*temp = dht.readTemperature();
//delay(10);
//if (isnan(*humidity) || isnan(*temp)) {
// Serial.println("Failed to read from DHT sensor!");
// *_time = 0;
// return false;
//}
int chk = DHT.read22(DHT22_PIN);
//stat.total++;
switch (chk)
{
case DHTLIB_OK:
//stat.ok++;
Serial.println("OK\t");
*temp = DHT.temperature;
*humidity = DHT.humidity;
*_time = millis();
return true;
break;
case DHTLIB_ERROR_CHECKSUM:
//stat.crc_error++;
Serial.println("Checksum error\t");
publishError("DHT22 Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
//stat.time_out++;
Serial.println("Time out error\t");
publishError("DHT22 Time out error");
break;
default:
//stat.unknown++;
Serial.println("Unknown error\t");
publishError("DHT22 Unknown error");
break;
}
*_time = millis();
return false;
}
return false;
}
float readHumidity(){
return -1;
}
void publishHumidity(){
if(millis() - humi_lastpubtime > sensor_pubrate) {
humi_lastpubtime = millis();
int current_humidity = (int)readHumidity();
client.publish(pub_humidity.c_str(), String(current_humidity).c_str(), true);
}
}
float readTemp(){
int analogValue = analogRead(TEMP_PIN);
float millivolts = (analogValue/1024.0) * 3300; //3300 is the voltage provided by NodeMCU
//float celsius = millivolts/10;
float celsius = (millivolts - 500) / 10 ;
Serial.print("in DegreeC= ");
Serial.println(celsius);
return celsius;
}
void publishTemp(){
if(millis() - temp_lastpubtime > sensor_pubrate) {
temp_lastpubtime = millis();
int current_temp = (int)readTemp();
client.publish(pub_temperature.c_str(), String(current_temp).c_str(), true);
}
}
void publishEnviromentalData(){
float temp;
float humi;
if (MeasureEnvironment(&timeSinceLastTempHumiRead, &temp, &humi)){
client.publish(pub_temperature.c_str(), String(temp).c_str(), true);
client.publish(pub_humidity.c_str(), String(humi).c_str(), true);
}
}
void publishMotion(){
//Read PIR input
int PIR_VAL = digitalRead(PIR_PIN);
//PIR_VAL == 1 is Open circuit (pull up) = alarm
//PIR_VAL == 0 is Short circuit = normal
if (PIR_VAL == 1) {
//Open circuit = alarm
digitalWrite(LED, LOW); //inverted logic for wemos
if (!motion_published){
client.publish(pub_motion.c_str(), "alert");
motion_published = true;
}
}else{
//Closed circuit = safe
digitalWrite(LED, HIGH); //inverted logic for wemos
motion_published = false;
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
//Perform MQTT loop
client.loop();
publishEnviromentalData(); //publish temp humi every 10s
publishMotion(); //publish motion changes
//Enable OTA for 60s
if (Allow_OTA){
uint16_t elapsed = 0;
while (elapsed < OTA_Timeout){
ArduinoOTA.handle();
elapsed = millis() - OTA_Timer;
delay(10);
}
Allow_OTA = false;
}
}