-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnodemcu-laundry.ino
449 lines (390 loc) · 13.4 KB
/
nodemcu-laundry.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
/*
Author: Nolan Gilley
License: CC-BY-SA, https://creativecommons.org/licenses/by-sa/2.0/
Date: 7/30/2016
File: laundry_esp8266.ino
This sketch is for a NodeMCU wired up with 2 accelerometers and
2 reed swithches. An MPU6050 and a reed switch are attached to
both the wash and dryer. This sketch will send mqtt messages to
the defined topics which describe the state of the washer and dryer
and the details of the accelerometer data.
1) Update the WIFI ssid and password below
2) Update the thresholds for washer and dryer movement
The thresholds are also configurable over mqtt!
The ESP8266 will publish mqtt commands to:
sensor/dryer/(EMPTY,RUNNING,COMPLETE)
sensor/washer/(EMPTY,RUNNING,COMPLETE)
sensor/dryer/detail: dryer %d, %d, %d, %d, %d", dryer_door_status, dryer_state, dryer_ax_range, dryer_ay_range, dryer_az_range
sensor/washer/detail: dryer %d, %d, %d, %d, %d", washer_door_status, washer_state, washer_ax_range, washer_ay_range, washer_az_range
And it SUBSCRIBES to:
sensor/dryer/set/ax
Ax value for dryer (int)
sensor/dryer/set/ay
Ay value for dryer (int)
sensor/dryer/set/az
Az value for dryer (int)
sensor/washer/set/ay
Ay value for washer (int)
sensor/dryer/set/detected
# of dryer detections out of 15 cycles
sensor/washer/set/detected
# of washer detections of of 15
sensor/dryer/set/detail
0 or 1 (off/on)
sensor/washer/set/detail
0 or 1
*/
// MPU6050 Includes
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
// ESP8266 Includes
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// DEFINE NODEMCU PINS
#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)
// DEFINE GPIO PINS
#define DRYER_DOOR D7 // number of reed sensor for dryer
#define WASHER_DOOR D8 // number of reed sensor for dryer
// DEFINE STATES
#define CLOSED 0 //washer/dryer door closed
#define OPEN 1 //washer/dryer door open
#define EMPTY 0 //washer/dryer empty
#define RUNNING 1 //washer/dryer running
#define COMPLETE 2 //washer/dryer complete
#define MOVEMENT_DETECTED 0
#define MOVEMENT_NOT_DETECTED 1
// ESP8266 WIFI ----------------------------------------------------------------
const char* ssid = "Internet";
const char* password = "xxx";
const char* mqtt_server = "192.168.1.52";
const char* mqtt_username = "nolan";
const char* mqtt_password = "xxx";
const char* mqtt_topic_dryer_detail = "sensor/dryer/detail";
const char* mqtt_topic_washer_detail = "sensor/washer/detail";
const char* mqtt_topic_washer = "sensor/washer";
const char* mqtt_topic_dryer = "sensor/dryer";
WiFiClient espClient;
PubSubClient client(espClient);
// END ESP8266 WIFI ------------------------------------------------------------
// MPU-6050 Accelerometers ------------------------------------------------------
MPU6050 MPU_DRYER(0x68); //DRYER
MPU6050 MPU_WASHER(0x69); //WASHER
int16_t dryer_ax, dryer_ay, dryer_az;
int16_t dryer_gx, dryer_gy, dryer_gz;
int16_t dryer_ax_min = 0, dryer_ax_max = 0, dryer_ax_range;
int16_t dryer_ay_min = 0, dryer_ay_max = 0, dryer_ay_range;
int16_t dryer_az_min = 0, dryer_az_max = 0, dryer_az_range;
int16_t washer_ax, washer_ay, washer_az;
int16_t washer_gx, washer_gy, washer_gz;
int16_t washer_ax_min = 0, washer_ax_max = 0, washer_ax_range;
int16_t washer_ay_min = 0, washer_ay_max = 0, washer_ay_range;
int16_t washer_az_min = 0, washer_az_max = 0, washer_az_range;
// end MPU-6050-------------------------------------------------------------------
// accelerometer sensor 1 (Dryer) ===============================================
int dryer_reading = 0; //reading = 1 mean no noise, 0=noise
int dryer_reading_previous = 0;
int dryer_state = EMPTY; //1 = running, 0 = empty, 2 = complete
int last_dryer_state = EMPTY;
unsigned long sample_time = 0; //millis of last reading
int dryer_detector_count = 0; //number of captures
int dryer_detected_count = 0; //number of readings showing sound
int dryer_door_status = 0;
// accelerometer sensor 2 (Washer) ==============================================
int washer_reading = 0; //reading = 1 mean no noise, 0=noise
int washer_reading_previous = 0;
int washer_state = EMPTY; //1 = running, 0 = empty, 2 = complete
int last_washer_state = EMPTY;
int washer_detector_count = 0; //number of captures
int washer_detected_count = 0; //number of readings showing sound
int washer_door_status = 0;
char dryerString[50];
char washerString[50];
// CONFIGURABLE THRESHOLDS
int dryer_ax_threshold = 17500;
int dryer_ay_threshold = 2000;
int dryer_az_threshold = 3300;
int washer_ay_threshold = 18000;
int dryer_detected_threshold = 9;
int washer_detected_threshold = 3;
int dryer_detailed_reporting = 0;
int washer_detailed_reporting = 0;
void setup()
{
Serial.begin(115200); // setup serial
// setup WiFi
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Wire.begin();
MPU_DRYER.initialize();
MPU_WASHER.initialize();
pins_init();
reconnect();
update_via_mqtt();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
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();
if (strcmp(topic, "sensor/washer/set/ay") == 0) {
payload[length] = '\0';
String s = String((char*)payload);
int i= s.toInt();
washer_ay_threshold = i;
Serial.print("washer ay set to ");
Serial.println(i);
}
else if (strcmp(topic, "sensor/dryer/set/ax") == 0) {
payload[length] = '\0';
String s = String((char*)payload);
int i= s.toInt();
dryer_ax_threshold = i;
Serial.print("dryer ax set to ");
Serial.println(i);
}
else if (strcmp(topic, "sensor/dryer/set/ay") == 0) {
payload[length] = '\0';
String s = String((char*)payload);
int i= s.toInt();
dryer_ay_threshold = i;
Serial.print("dryer ay set to ");
Serial.println(i);
}
else if (strcmp(topic, "sensor/dryer/set/az") == 0) {
payload[length] = '\0';
String s = String((char*)payload);
int i= s.toInt();
dryer_ax_threshold = i;
Serial.print("dryer az set to ");
Serial.println(i);
}
else if (strcmp(topic, "sensor/dryer/set/detected") == 0) {
payload[length] = '\0';
String s = String((char*)payload);
int i= s.toInt();
dryer_detected_threshold = i;
Serial.print("dryer detected threshold set to ");
Serial.println(i);
}
else if (strcmp(topic, "sensor/washer/set/detected") == 0) {
payload[length] = '\0';
String s = String((char*)payload);
int i= s.toInt();
washer_detected_threshold = i;
Serial.print("washer detected threshold set to ");
Serial.println(i);
}
else if (strcmp(topic, "sensor/washer/set/detail") == 0) {
payload[length] = '\0';
String s = String((char*)payload);
int i= s.toInt();
washer_detailed_reporting = i;
Serial.print("washer detailed reporting set to ");
Serial.println(i);
}
else if (strcmp(topic, "sensor/dryer/set/detail") == 0) {
payload[length] = '\0';
String s = String((char*)payload);
int i= s.toInt();
dryer_detailed_reporting = i;
Serial.print("dryer detailed reporting set to ");
Serial.println(i);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
// Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
// Serial.println("connected");
client.subscribe("sensor/washer/set/+");
client.subscribe("sensor/dryer/set/+");
} 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()
{
last_dryer_state = dryer_state;
last_washer_state = washer_state;
if (!client.connected()) {
reconnect();
}
client.loop();
//===================================================================
//dryer/washer door reed switches
// 1 = door open, 0 = door closed
dryer_door_status = digitalRead(DRYER_DOOR);
washer_door_status = digitalRead(WASHER_DOOR);
// if dryer door is opened and dryer cycle is complete
if (dryer_door_status == OPEN and dryer_state == COMPLETE)
{
dryer_state = EMPTY; // set dryer to empty
}
// if washer door is open and washer cycle is complete
if (washer_door_status == OPEN and washer_state == COMPLETE)
{
washer_state = EMPTY; //set washer to empty
}
MPU_DRYER.getMotion6(&dryer_ax, &dryer_ay, &dryer_az, &dryer_gx, &dryer_gy, &dryer_gz);
MPU_WASHER.getMotion6(&washer_ax, &washer_ay, &washer_az, &washer_gx, &washer_gy, &washer_gz);
trackMinMax(dryer_ax, &dryer_ax_min, &dryer_ax_max);
trackMinMax(dryer_ay, &dryer_ay_min, &dryer_ay_max);
trackMinMax(dryer_az, &dryer_az_min, &dryer_az_max);
trackMinMax(washer_ax, &washer_ax_min, &washer_ax_max);
trackMinMax(washer_ay, &washer_ay_min, &washer_ay_max);
trackMinMax(washer_az, &washer_az_min, &washer_az_max);
// deal with millis rollover
if (sample_time > millis())
{
sample_time = millis();
}
// samples every 5 sec
if ((millis() - sample_time) > 5000)
{
sample_time = millis(); //reset sample_time to wait for next Xms
// Calculate Range for each accelerometer direction.
dryer_ax_range = dryer_ax_max - dryer_ax_min;
dryer_ay_range = dryer_ay_max - dryer_ay_min;
dryer_az_range = dryer_az_max - dryer_az_min;
washer_ax_range = washer_ax_max - washer_ax_min;
washer_ay_range = washer_ay_max - washer_ay_min;
washer_az_range = washer_az_max - washer_az_min;
// Reset Range Counters
dryer_ax_min = 0, dryer_ax_max = 0;
dryer_ay_min = 0, dryer_ay_max = 0;
dryer_az_min = 0, dryer_az_max = 0;
washer_ax_min = 0, washer_ax_max = 0;
washer_ay_min = 0, washer_ay_max = 0;
washer_az_min = 0, washer_az_max = 0;
if (dryer_detailed_reporting == 1) {
sprintf(dryerString, "dryer %d, %d, %d, %d, %d", dryer_door_status, dryer_state, dryer_ax_range, dryer_ay_range, dryer_az_range);
client.publish(mqtt_topic_dryer_detail, dryerString);
Serial.println(dryerString);
}
if (washer_detailed_reporting == 1) {
sprintf(washerString, "washer %d, %d, %d, %d, %d", washer_door_status, washer_state, washer_ax_range, washer_ay_range, washer_az_range);
client.publish(mqtt_topic_washer_detail, washerString);
Serial.println(washerString);
}
if (abs(dryer_ax_range > dryer_ax_threshold) and abs(dryer_ay_range > dryer_ay_threshold) and abs(dryer_az_range > dryer_az_threshold))
{
dryer_reading = MOVEMENT_DETECTED;
}
if (abs(washer_ax_range) > washer_ay_threshold)
{
washer_reading = MOVEMENT_DETECTED;
}
dryer_detector_count = dryer_detector_count + 1; //count how many times we listened
if (dryer_reading == MOVEMENT_DETECTED) //count how many times detected movement
{
dryer_detected_count = dryer_detected_count + 1;
}
washer_detector_count = washer_detector_count + 1; //count how many times we listened
if (washer_reading == MOVEMENT_DETECTED) //count how many times detected movement
{
washer_detected_count = washer_detected_count + 1;
}
dryer_reading = MOVEMENT_NOT_DETECTED; //reset
washer_reading = MOVEMENT_NOT_DETECTED; //reset
}//end reading every 5 seconds
if (dryer_detector_count >= 15)
{
if (dryer_door_status == CLOSED)
{
if (dryer_detected_count >= dryer_detected_threshold) dryer_state = RUNNING;
else if (dryer_state == RUNNING) dryer_state = COMPLETE;
}
dryer_detector_count = 0;
dryer_detected_count = 0;
}
if (washer_detector_count >= 15)
{
if (washer_door_status == CLOSED)
{
if (washer_detected_count >= washer_detected_threshold) washer_state = RUNNING;
else if (washer_state == RUNNING and washer_door_status == CLOSED) washer_state = COMPLETE;
}
washer_detector_count = 0;
washer_detected_count = 0;
}
if (last_dryer_state != dryer_state or last_washer_state != washer_state)
{
update_via_mqtt();
}
}// end loop
void pins_init()
{
pinMode(DRYER_DOOR, INPUT);
pinMode(WASHER_DOOR, INPUT);
}
void update_via_mqtt()
{
if (dryer_state == RUNNING) {
client.publish(mqtt_topic_dryer, "Running", true);
}
else if (dryer_state == COMPLETE) {
client.publish(mqtt_topic_dryer, "Complete", true);
}
else {
client.publish(mqtt_topic_dryer, "Empty", true);
}
if (washer_state == RUNNING) {
client.publish(mqtt_topic_washer, "Running", true);
}
else if (washer_state == COMPLETE) {
client.publish(mqtt_topic_washer, "Complete", true);
}
else {
client.publish(mqtt_topic_washer, "Empty", true);
}
Serial.println("mqtt published!");
}
int16_t trackMinMax(int16_t current, int16_t *min, int16_t *max)
{
if (current > *max)
{
*max = current;
}
else if (current < *min)
{
*min = current;
}
}