-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostel-iot.ino
463 lines (382 loc) · 15.2 KB
/
hostel-iot.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
Hostel-IoT
Send sensor data to from hostel room to server with MQTT
author: Abu Azzam
v1 Monday, 22 April 2019
initial
v2.0 Saturday, 27 April 2019
with wifi manager
v2.1 Sunday, 5 May 2019
- more parameter
- led notif
v2.2 Wednesday, 8 May 2019
- Device ID based on Chip ID
- more CSS
idea?
-
*/
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <MQTT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Ticker.h>
/* PIN esp01 wemos */
const int pinTemperature = 2; //D2; // DS18B20 Sensor suhu
const int pinMotion = 0; //D7; // PIR motion sensor
const int pinLED = 1; //LED_BUILTIN; // LED
const int pinButton = 3; //D1; // Button
/* sersors enable */
boolean motionEnable = true;
boolean motionDummy = false;
boolean tempetureEnable = true;
boolean tempetureDummy = false;
boolean debug = false;
//define your default values here, if there are different values in config.json, they are overwritten.
char mqtt_server[40] = "52.74.248.240";
char mqtt_port[6] = "1883";
char mqtt_user[32];
char mqtt_pass[32];
char hostel_client[40];
char hostel_region[40];
char hostel_location[40];
char hostel_room[40];
char led_mon[3] = "on"; // LED monitor enable
/* interval get sensor data */
char sense_min[3] = "1"; // setiap n menit
char sense_sec[3] = "0"; // setiap n detik
/* interval sending data */
char pub_min[3] = "10"; // setiap n menit
char pub_sec[3] = "0"; // setiap n detik
//flag for saving data
bool shouldSaveConfig = true;
/* ------------------------------------------------------ */
String prefixDeviceID = "Ndoware-";
// init variables
unsigned long lastSensMillis = 0;
unsigned long lastPushMillis = 0;
unsigned long lastButtonLow = 0;
byte motionFlag = 0;
int bufferMotion = 0;
int bufferTemp = 0;
int bufferTempCount = 0;
String topic;
int iSensMenit = 0;
int iSensDetik = 0;
int iPushMenit = 0;
int iPushDetik = 0;
OneWire oneWire(pinTemperature);
DallasTemperature sensors(&oneWire);
WiFiClient net;
MQTTClient client;
Ticker ticker;
void tick() {
//toggle state
int state = digitalRead(pinLED); // get the current state of GPIO1 pin
digitalWrite(pinLED, !state); // set pin to the opposite state
}
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
//gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
//entered config mode, make led toggle faster
ticker.attach(0.2, tick);
}
void mqttConnect() {
Serial.print("Connecting ");
String chipID = String(ESP.getChipId(), HEX);
chipID.toUpperCase();
String stDeviceID = prefixDeviceID + chipID;
char deviceID[stDeviceID.length()];
stDeviceID.toCharArray(deviceID, sizeof(deviceID) + 1);
Serial.print(deviceID);
while (!client.connect(deviceID, mqtt_user, mqtt_pass)) {
Serial.print(".");
digitalWrite(pinLED, LOW);
delay(500);
digitalWrite(pinLED, HIGH);
delay(500);
}
Serial.println("\nconnected!");
}
void joggingLed() {
digitalWrite(pinLED, HIGH); delay(700);
for (int i = 10; i < 46; i++) {
digitalWrite(pinLED, !digitalRead(pinLED));
delay(i);
}
for (int i = 46; i > 10; i--) {
digitalWrite(pinLED, !digitalRead(pinLED));
delay(i);
}
Serial.println("I'm refreshed, ready for action boss!");
digitalWrite(pinLED, HIGH); delay(700);
}
void setup() {
// put your setup code here, to run once:
if (debug) {
Serial.begin(115200);
}
Serial.println();
pinMode(pinButton, INPUT_PULLUP);
pinMode(pinLED, OUTPUT);
pinMode(pinTemperature, INPUT_PULLUP);
pinMode(pinMotion, INPUT);
joggingLed();
// start ticker with 0.6 because we start in AP mode and try to connect
ticker.attach(0.6, tick);
//read configuration from FS json
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nparsed json");
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(mqtt_port, json["mqtt_port"]);
strcpy(mqtt_user, json["mqtt_user"]);
strcpy(mqtt_pass, json["mqtt_pass"]);
strcpy(hostel_client, json["hostel_client"]);
strcpy(hostel_region, json["hostel_region"]);
strcpy(hostel_location, json["hostel_location"]);
strcpy(hostel_room, json["hostel_room"]);
strcpy(led_mon, json["led_mon"]);
strcpy(sense_min, json["sense_min"]);
strcpy(sense_sec, json["sense_sec"]);
strcpy(pub_min, json["pub_min"]);
strcpy(pub_sec, json["pub_sec"]);
} else {
Serial.println("failed to load json config");
}
configFile.close();
}
}
} else {
Serial.println("failed to mount FS");
}
//end read
WiFiManagerParameter custom_text_hostel("<small>Topics</small>");
WiFiManagerParameter custom_hostel_client("client", "hostel client", hostel_client, 40);
WiFiManagerParameter custom_hostel_region("region", "hostel region", hostel_region, 40);
WiFiManagerParameter custom_hostel_location("location", "hostel location", hostel_location, 40);
WiFiManagerParameter custom_hostel_room("room", "hostel room", hostel_room, 40);
WiFiManagerParameter custom_text_mqtt("<small>Host</small>");
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 32);
WiFiManagerParameter custom_mqtt_pass("pass", "mqtt pass", mqtt_pass, 32, "type=\"password\"");
WiFiManagerParameter custom_text_led("<small>Motion Sensor Light</small>");
String typeTag = (String(led_mon) == "on") ? "type=\"checkbox\" checked" : "type=\"checkbox\"";
char typeTagBuffer[30];
typeTag.toCharArray(typeTagBuffer, 30);
WiFiManagerParameter custom_led_mon("led", "led enable", "on", 3, typeTagBuffer);
WiFiManagerParameter custom_text_dev("<hr/><small>(dev only)</small><br/>");
WiFiManagerParameter custom_text_sense("<small>Interval SENSE room (minutes, seconds)</small>");
WiFiManagerParameter custom_sense_min("sminutes", "sense minutes", sense_min, 3);
WiFiManagerParameter custom_sense_sec("sseconds", "sense seconds", sense_sec, 3);
WiFiManagerParameter custom_text_pub("<small>Interval PUBLISH data (minutes, seconds)</small>");
WiFiManagerParameter custom_pub_min("pminutes", "publish minutes", pub_min, 3);
WiFiManagerParameter custom_pub_sec("pseconds", "publish seconds", pub_sec, 3);
//WiFiManager
WiFiManager wifiManager;
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wifiManager.setAPCallback(configModeCallback);
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//add all your parameters here
wifiManager.addParameter(&custom_text_hostel);
wifiManager.addParameter(&custom_hostel_client);
wifiManager.addParameter(&custom_hostel_region);
wifiManager.addParameter(&custom_hostel_location);
wifiManager.addParameter(&custom_hostel_room);
wifiManager.addParameter(&custom_text_mqtt);
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_mqtt_port);
wifiManager.addParameter(&custom_mqtt_user);
wifiManager.addParameter(&custom_mqtt_pass);
wifiManager.addParameter(&custom_text_led);
wifiManager.addParameter(&custom_led_mon);
wifiManager.addParameter(&custom_text_dev);
wifiManager.addParameter(&custom_text_sense);
wifiManager.addParameter(&custom_sense_min);
wifiManager.addParameter(&custom_sense_sec);
wifiManager.addParameter(&custom_text_pub);
wifiManager.addParameter(&custom_pub_min);
wifiManager.addParameter(&custom_pub_sec);
wifiManager.setCustomHeadElement("<style>body{ background-image: linear-gradient(to left, #ffffff 0%, #eeeeee 75%);}h1{font-family: serif;text-shadow: 2px 2px rgba(0,0,0,0.19)}h3{color: #dddddd;}button{background-color:#ec741f; box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);}input#led { position: relative; -webkit-appearance: none; outline: none; width: 50px; height: 30px; background-color: #fff; border: 1px solid #D9DADC; border-radius: 50px; box-shadow: inset -20px 0 0 0 #D9DADC;}input#led:after { content: ""; position: absolute; top: 1px; left: 1px; background: transparent; width: 26px; height: 26px; border-radius: 50%; box-shadow: 2px 4px 6px rgba(0,0,0,0.2);}input#led:checked { box-shadow: inset 20px 0 0 0 #2196F3;border-color: #2196F3;}input#led:checked:after {left: 20px;box-shadow: -2px 4px 3px rgba(0,0,0,0.05);}</style>");
//wifiManager.setMinimumSignalQuality();
//wifiManager.setTimeout(120);
String chipID = String(ESP.getChipId(), HEX);
chipID.toUpperCase();
String stDeviceID = prefixDeviceID + chipID;
char deviceID[stDeviceID.length()];
stDeviceID.toCharArray(deviceID, sizeof(deviceID) + 1);
WiFi.hostname(deviceID);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect(deviceID, "password")) {
ticker.attach(0.6, tick);
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
ticker.attach(0.6, tick);
//read updated parameters
strcpy(mqtt_server, custom_mqtt_server.getValue());
strcpy(mqtt_port, custom_mqtt_port.getValue());
strcpy(mqtt_user, custom_mqtt_user.getValue());
strcpy(mqtt_pass, custom_mqtt_pass.getValue());
strcpy(hostel_client, custom_hostel_client.getValue());
strcpy(hostel_region, custom_hostel_region.getValue());
strcpy(hostel_location, custom_hostel_location.getValue());
strcpy(hostel_room, custom_hostel_room.getValue());
strcpy(led_mon, custom_led_mon.getValue());
strcpy(sense_min, custom_sense_min.getValue());
strcpy(sense_sec, custom_sense_sec.getValue());
strcpy(pub_min, custom_pub_min.getValue());
strcpy(pub_sec, custom_pub_sec.getValue());
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["mqtt_server"] = mqtt_server;
json["mqtt_port"] = mqtt_port;
json["mqtt_user"] = mqtt_user;
json["mqtt_pass"] = mqtt_pass;
json["hostel_client"] = hostel_client;
json["hostel_region"] = hostel_region;
json["hostel_location"] = hostel_location;
json["hostel_room"] = hostel_room;
json["led_mon"] = led_mon;
json["sense_min"] = sense_min;
json["sense_sec"] = sense_sec;
json["pub_min"] = pub_min;
json["pub_sec"] = pub_sec;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
/* interval get sensor data */
iSensMenit = atoi(sense_min); //minute
iSensDetik = atoi(sense_sec); //s
/* interval sending data */
iPushMenit = atoi(pub_min); //minute
iPushDetik = atoi(pub_sec); //
// MQTT
client.begin(mqtt_server, String(mqtt_port).toInt(), net);
mqttConnect();
topic = "hostel-iot/" + String(hostel_client) + "/" + String(hostel_region) + "/" + String(hostel_location) + "/" + String(hostel_room) + "/";
ticker.detach();
// Dallas sensor
sensors.begin();
joggingLed();
digitalWrite(pinMotion, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
client.loop();
delay(10); // <- fixes some issues with WiFi stability
if (!client.connected()) {
mqttConnect();
}
// TEMPERATURE
int temperaturSense = 0;
if (!tempetureDummy) {
sensors.requestTemperatures(); // Send the command to get temperatures
temperaturSense = sensors.getTempCByIndex(0);
} else {
temperaturSense = random(16, 37);
}
// MOTION
byte motion = motionDummy ? random(2) : digitalRead(pinMotion);
if (motion) {
motionFlag = 1;
}
if (String(led_mon) == "on") {
digitalWrite(pinLED, !motion);
}
// SENSE surrounding roughly every interval
unsigned long intervalSense = ((iSensMenit * 60) + iSensDetik) * 1000;
if ((lastSensMillis == 0) || (millis() - lastSensMillis > intervalSense)) {
lastSensMillis = millis();
bufferTemp = bufferTemp + temperaturSense;
bufferTempCount++;
Serial.println("Temp: " + String(temperaturSense) + " Buffer: " + String(bufferTemp) + " total data : " + String(bufferTempCount));
bufferMotion = bufferMotion + motionFlag;
motionFlag = 0;
Serial.println("Motion: " + String(motion) + " Buffer: " + String(bufferMotion));
}
// PUBLISH a message roughly every interval
unsigned long intervalPush = ((iPushMenit * 60) + iPushDetik) * 1000;
if ((lastPushMillis == 0) || ((millis() - lastPushMillis + 500) > intervalPush)) {
lastPushMillis = millis() + 500;
// motion detector sensor data
if (motionEnable) {
String deviceType1 = "motion";
String topic1 = topic + deviceType1 + "/";
String payload1 = String(bufferMotion);
client.publish(topic1, payload1);
Serial.println("outgoing: " + topic1 + " - " + payload1);
bufferMotion = 0;
}
// temperature sensor data
if (tempetureEnable) {
String deviceType2 = "temp";
String topic2 = topic + deviceType2 + "/";
String payload2 = String(bufferTemp / bufferTempCount);
client.publish(topic2, payload2);
Serial.println("outgoing: " + topic2 + " - " + payload2);
bufferTemp = 0;
bufferTempCount = 0;
}
}
// RESET button
byte buttonPressed = !digitalRead(pinButton);
if (buttonPressed && ((millis() - lastButtonLow) > 5000)) {
joggingLed();
WiFi.disconnect(); // disconnect & reset
delay(2000);
ESP.reset();
delay(5000);
}
if (!buttonPressed) {
lastButtonLow = millis();
}
}