-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeSensors.ino
471 lines (369 loc) · 9.37 KB
/
HomeSensors.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
464
465
466
467
468
469
470
471
//AVR
#include <avr/sleep.h>
#include <avr/wdt.h>
//Arduino
#include <Arduino.h>
#include <EEPROM.h>
//Third party
#include <SerialCommands.h>
#include <U8g2lib.h> // Display
#include <ClosedCube_HDC1080.h> // Temperature, humidity
#include <RFM69.h> // Wireless
#include <Adafruit_TSL2561_U.h> // Light Sensor
#include "secrets.h" // ENCRYPTKEY
#include <MemoryUsage.h>
#define SERIAL_BAUD 9600
#define BUTTON_INTERRUPT_PIN 3
#define LED_PIN 4
volatile bool powerDownEnabled = true;
volatile bool wdtInterrupted = true; // To send data after power up
uint8_t lastSource = 0;
int16_t lastRssi = 0;
uint8_t packetId = 0;
struct Settings
{
uint8_t networkid;
uint8_t nodeid;
};
Settings settings;
enum PacketHeaderType {
SENSOR_DATA,
PING_REQUEST,
PING_RESPONSE,
};
struct PacketHeader
{
uint8_t type;
uint8_t id;
};
struct PacketSensorData
{
struct PacketHeader ph;
uint8_t radioTemperature;
double temperature;
double humidity;
uint32_t light;
};
struct Hal
{
bool radio;
bool tsl;
bool hdc;
};
Hal hal;
char serial_command_buffer_[32];
void cmdSleep(SerialCommands *);
void cmdGetNetowrkId(SerialCommands *);
void cmdSetNetworkId(SerialCommands *);
void cmdGetNodeId(SerialCommands *);
void cmdSetNodeId(SerialCommands *);
SerialCommands SCmd(&Serial, serial_command_buffer_, sizeof(serial_command_buffer_), "\n", " ");
SerialCommand scmdSleep("sleep", cmdSleep);
SerialCommand scmdGetNetworkId("gNetId", cmdGetNetowrkId);
SerialCommand scmdSetNetworkId("sNetId", cmdSetNetworkId);
SerialCommand scmdGetNodeId("gNodeId", cmdGetNodeId);
SerialCommand scmdSetNodeId("sNodeId", cmdSetNodeId);
// Temperature and humidity sensor
ClosedCube_HDC1080 hdc;
// RF module
RFM69 radio;
// Oled display
U8X8_SSD1306_128X64_NONAME_HW_I2C oled(U8G2_R0);
// Light sensor
Adafruit_TSL2561_Unified tsl(TSL2561_ADDR_FLOAT, 12345);
void setup(void)
{
Serial.begin(SERIAL_BAUD);
Serial.println(F("[CMD]"));
SCmd.SetDefaultHandler(&cmdUnrecognized);
SCmd.AddCommand(&scmdSleep);
SCmd.AddCommand(&scmdGetNetworkId);
SCmd.AddCommand(&scmdSetNetworkId);
SCmd.AddCommand(&scmdGetNodeId);
SCmd.AddCommand(&scmdSetNodeId);
Serial.print(F("[EEPROM] "));
EEPROM.get(0, settings);
Serial.print(F("Network id: "));
Serial.print(settings.networkid);
Serial.print(F(" Node id: "));
Serial.println(settings.nodeid);
Serial.println(F("[PINS]"));
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
pinMode(BUTTON_INTERRUPT_PIN, INPUT_PULLUP);
Serial.println(F("[OLED]"));
oled.begin();
oled.setFont(u8x8_font_chroma48medium8_r);
oled.draw2x2String(0, 0, "Booting");
Serial.print(F("[HDC1080] "));
hdc.begin(0x40);
uint16_t manId = hdc.readManufacturerId();
uint16_t devId = hdc.readDeviceId();
hal.hdc = (manId != 0xFFFF) && (devId != 0xFFFF);
if ( hal.hdc )
{
Serial.print(F("Manufacture: "));
Serial.print(manId, HEX);
Serial.print(F(" Device: "));
Serial.println(devId, HEX);
}
else
{
Serial.println(F(" Failed"));
}
Serial.print(F("[TSL2561]"));
hal.tsl = tsl.begin();
if ( hal.tsl )
{
tsl.enableAutoRange(true);
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
sensor_t sensor;
tsl.getSensor(&sensor);
Serial.print(F(" Sensor: "));
Serial.print(sensor.name);
Serial.print(F(" Ver: "));
Serial.print(sensor.version);
Serial.print(F(" ID: "));
Serial.println(sensor.sensor_id);
}
else
{
Serial.println(F(" Failed"));
}
Serial.print(F("[RFM69] "));
hal.radio = radio.initialize(RF69_868MHZ, settings.nodeid, settings.networkid);
if ( hal.radio )
{
radio.setHighPower(true);
radio.setPowerLevel(2); // 0-31
radio.encrypt(ENCRYPTKEY);
uint8_t temp = radio.readTemperature();
Serial.print(F("Temperature: "));
Serial.println(temp);
}
else
{
Serial.println(F(" Failed"));
}
}
void loop(void)
{
SCmd.ReadSerial();
if (radio.receiveDone())
{
receivePacket();
}
// If timed out set new interval
if ( wdtInterrupted )
{
FREERAM_PRINT;
nodeLoop();
// Set new wdt timer
wdtInterrupted = false;
wdt_enable(WDTO_8S);
WDTCSR |= (1 << WDIE);
}
// Come up with another solution - dont call it too often
attachInterrupt(digitalPinToInterrupt(BUTTON_INTERRUPT_PIN), ButtonInterrupt, LOW);
if ( powerDownEnabled )
{
powerDown();
}
}
// Receive
void receivePacket()
{
lastSource = radio.SENDERID;
lastRssi = radio.RSSI;
PacketHeader *p = (PacketHeader*)radio.DATA;
Serial.print(F("[RFM69] Incoming packet from: "));
Serial.print(lastSource, DEC);
Serial.print(F(" LEN: "));
Serial.print(radio.DATALEN);
Serial.print(F(" RSSI: "));
Serial.println(lastRssi);
switch (p->type)
{
case SENSOR_DATA:
publishPacketSensorData(lastSource, lastRssi, (PacketSensorData*)p);
break;
case PING_REQUEST:
Serial.print(F("[RFM69] Ping request"));
break;
case PING_RESPONSE:
Serial.print(F("[RFM69] Ping reponse"));
break;
default:
Serial.print(F("[RFM69] Invalid packet type "));
Serial.println(p->type);
break;
}
}
void nodeLoop()
{
PacketSensorData p = {SENSOR_DATA, packetId, 0, 0.0, 0.0, 0};
Serial.print(F("[SENSORS] Reading"));
p.radioTemperature = radio.readTemperature();
if ( hal.hdc )
{
p.temperature = hdc.readTemperature();
p.humidity = hdc.readHumidity();
//hdc.heatUp(30);
Serial.print(F(" hdc1080"));
}
if ( hal.tsl )
{
sensors_event_t event;
tsl.getEvent(&event);
p.light = event.light;
Serial.print(F(" tsl2561"));
}
Serial.println();
// Publish data
// Send to master (nodeid == 0)
// 255 = broadcast
if (settings.nodeid) // if not master
{
Serial.println(F("[RFM69] Sending packet"));
radio.send(0, &p, sizeof(PacketSensorData));
++packetId;
}
else // Publish on master node
{
publishPacketSensorData(settings.nodeid, 0, &p);
}
updateDisplay(p.radioTemperature, p.temperature, p.humidity, p.light, lastSource, lastRssi);
}
void publishPacketSensorData(uint8_t id, int16_t rssi , const struct PacketSensorData *p)
{
Serial.print(F("# "));
Serial.print(F("ID: "));
Serial.print(id, DEC);
Serial.print(F(" RSSI: "));
Serial.print(rssi, DEC);
Serial.print(F(" RT: "));
Serial.print(p->radioTemperature);
Serial.print(F(" T: "));
Serial.print(p->temperature);
Serial.print(F(" H: "));
Serial.print(p->humidity);
Serial.print(F(" L: "));
Serial.print(p->light);
Serial.println();
}
void updateDisplay(uint8_t rt, double t, double h, uint32_t l, uint8_t source, int16_t rssi)
{
Serial.println(F("[OLED] Updating"));
const int MAX_LEN = 17;
char line[MAX_LEN];
// 1. zluty radek
snprintf(line, MAX_LEN, "Net:%3d SIG:%4d", settings.networkid, rssi);
oled.drawString(0, 0, line);
// 2. zluty radek
snprintf(line, MAX_LEN, "Node:%3d Src:%3d", settings.nodeid, source);
oled.drawString(0, 1, line);
// 3. modry radek
dtostrf(t, 4, 2, line);
snprintf(line, MAX_LEN, "%s C", line);
//oled.draw2x2String(0, 2, line);
oled.drawString(0, 2, line);
// 4. modry radek
dtostrf(h, 4, 2, line);
snprintf(line, MAX_LEN, "%s %%", line);
//oled.draw2x2String(0, 4, line);
oled.drawString(0, 3, line);
// 5. modry radek
dtostrf(l, 4, 0, line);
snprintf(line, MAX_LEN, "%s lux", line);
oled.drawString(0, 4, line);
// 6. blue line
snprintf(line, MAX_LEN, "%4d C", rt);
oled.drawString(0, 5, line);
// 8. Posledni radek
snprintf(line, MAX_LEN, "%d x", packetId);
oled.drawString(0, 7, line);
}
void cmdSleep(SerialCommands *sender)
{
powerDownEnabled = true;
}
void cmdGetNetowrkId(SerialCommands *sender)
{
Serial.print(F("Network ID: "));
Serial.println(settings.networkid);
}
void cmdSetNetworkId(SerialCommands *sender)
{
char *arg = sender->Next();
if (arg)
{
uint8_t nid = constrain(atoi(arg), 0, 255);
Serial.print(F("new Network ID: "));
Serial.println(nid);
settings.networkid = nid;
EEPROM.put(0, settings);
radio.setNetwork(nid);
}
else
{
Serial.println(F("[CMD] Missing argument"));
}
}
void cmdGetNodeId(SerialCommands *sender)
{
Serial.print(F("Node ID: "));
Serial.println(settings.nodeid);
}
void cmdSetNodeId(SerialCommands *sender)
{
char *arg = sender->Next();
if (arg)
{
uint8_t nid = constrain(atoi(arg), 0, 255);
Serial.print(F("new Node ID: "));
Serial.println(nid);
settings.nodeid = nid;
EEPROM.put(0, settings);
radio.setAddress(nid);
}
else
{
Serial.println(F("[CMD] Missing argument"));
}
}
void cmdUnrecognized(SerialCommands *sender, const char *arg)
{
Serial.print(F("[CMD] Uncrecognized: "));
Serial.println(arg);
}
void ButtonInterrupt()
{
detachInterrupt(digitalPinToInterrupt(BUTTON_INTERRUPT_PIN));
powerDownEnabled = false;
}
ISR(WDT_vect)
{
wdt_disable();
wdtInterrupted = true;
}
void powerDown()
{
digitalWrite(LED_PIN, LOW);
Serial.flush();
radio.receiveDone(); // Must be here to have working waking interrupt when packet is received
//attachInterrupt(digitalPinToInterrupt(BUTTON_INTERRUPT_PIN), ButtonInterrupt, LOW);
//Turn off ADC
ADCSRA &= ~(1 << ADEN);
//Do the sleep according to documentation
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli();
sleep_enable();
sleep_bod_disable();
sei();
sleep_cpu();
sleep_disable();
sei();
//Turn on ADC
ADCSRA |= (1 << ADEN);
digitalWrite(LED_PIN, HIGH);
}