-
Notifications
You must be signed in to change notification settings - Fork 8
/
kegboard.ino
469 lines (398 loc) · 11.3 KB
/
kegboard.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
/**
* Copyright 2016-2020 The Kegbot Project contributors <https://kegbot.org/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define VERSION "0.3.0"
#define KEGBOARD_DEBUG 0
#define CLOUD_PUBLISH_INTERVAL_MILLIS 1000
#define TCP_PUBLISH_INTERVAL_MILLIS 250
#define CONSOLE_PUBLISH_INTERVAL_MILLIS 250
#define CLIENT_WATCHDOG_TIMEOUT_MILLIS 30000
#define COMMAND_WATCHDOG_ON "watchdog on"
#define COMMAND_WATCHDOG_OFF "watchdog off"
#define COMMAND_WATCHDOG_KICK "watchdog kick"
#define NUM_METERS 4
#define METER0_PIN D1
#define METER1_PIN D2
#define METER2_PIN D3
#define METER3_PIN D4
#define ONEWIRE_PIN D5
#define TCP_SERVER_PORT 8321
#define TCP_CLIENT_INCOMING_BUFSIZE 256
#include "MDNS.h"
#include "OneWire.h"
#include "ds1820.h"
#if KEGBOARD_DEBUG
SerialLogHandler logHandler;
#endif
TCPServer server = TCPServer(TCP_SERVER_PORT);
TCPClient client;
char clientBuffer[TCP_CLIENT_INCOMING_BUFSIZE] = { '\0' };
unsigned int clientBufferPos = 0;
mdns::MDNS mdnsImpl;
bool mdnsRunning = false;
volatile unsigned int watchdogEnabled = 0;
unsigned long lastWatchdogKick;
volatile unsigned int meterPending;
volatile unsigned int thermoPending;
volatile unsigned int cloudPending;
unsigned long lastCloudPublishMillis;
volatile unsigned int consolePending;
unsigned long lastConsolePublishMillis;
volatile unsigned int tcpPending;
unsigned long lastTcpPublishMillis;
typedef struct {
volatile unsigned int ticks;
} meter_t;
meter_t meters[NUM_METERS];
OneWire onewire = OneWire(ONEWIRE_PIN);
DS1820Sensor thermoSensor = DS1820Sensor();
typedef struct {
float temp;
char probe[17];
} temp_t;
temp_t temps[NUM_METERS];
#define CREATE_METER_ISR(METER_NUM) \
void meter##METER_NUM##Interrupt(void) { \
detachInterrupt(METER##METER_NUM##_PIN); \
meters[METER_NUM].ticks++; \
cloudPending = consolePending = tcpPending = meterPending = 1; \
attachInterrupt(METER##METER_NUM##_PIN, meter##METER_NUM##Interrupt, FALLING); \
}
#define SETUP_METER(METER_NUM) \
do { \
memset(&meters[METER_NUM], 0, sizeof(meter_t)); \
pinMode(METER##METER_NUM##_PIN, INPUT_PULLUP); \
attachInterrupt(METER##METER_NUM##_PIN, meter##METER_NUM##Interrupt, FALLING); \
} while (false)
#define SETUP_TEMP(TEMP_NUM) \
do { \
memset(&temps[TEMP_NUM], 0, sizeof(temp_t)); \
} while (false)
CREATE_METER_ISR(0);
CREATE_METER_ISR(1);
CREATE_METER_ISR(2);
CREATE_METER_ISR(3);
//
// Main program
//
int resetMeter(int meterNum) {
if (meterNum < 0 || meterNum >= NUM_METERS) {
return -1;
}
memset(&meters[meterNum], 0, sizeof(meter_t));
return 0;
}
int publicResetMeter(String extra) {
int meterNum = atoi(extra);
return resetMeter(meterNum);
}
int publicMeterTicks(String extra) {
int meterNum = atoi(extra);
if (meterNum < 0 || meterNum >= NUM_METERS) {
return -1;
}
return (int) meters[meterNum].ticks;
}
void enableClientWatchdog() {
lastWatchdogKick = millis();
watchdogEnabled = 1;
}
void disableClientWatchdog() {
watchdogEnabled = 0;
}
void kickClientWatchdog() {
if (!watchdogEnabled) {
enableClientWatchdog();
}
lastWatchdogKick = millis();
}
void checkClientWatchdog() {
if (!watchdogEnabled) {
return;
}
if (!client.connected()) {
return;
}
if ((millis() - lastWatchdogKick) >= CLIENT_WATCHDOG_TIMEOUT_MILLIS) {
Log.info("Watchdog expired");
client.println("error: watchdog expired");
client.stop();
watchdogEnabled = 0;
}
}
bool setupMdns() {
std::vector<String> subServices;
subServices.push_back("kegboard");
bool success = mdnsImpl.setHostname("kegboard") &&
mdnsImpl.addService("tcp", "kegboard", TCP_SERVER_PORT, "Kegboard") &&
mdnsImpl.begin(true);
return success;
}
void addTemperature(char* probe, float temp) {
for (int i = 0; i < NUM_METERS; i++) {
if (strcmp(temps[i].probe, probe) == 0) {
temps[i].temp = temp;
return;
}
}
// New probe ID; Store it.
for (int i = 0; i < NUM_METERS; i++) {
if (temps[i].probe[0] == '\0') {
std::copy(probe, probe+17, temps[i].probe);
temps[i].temp = temp;
return;
}
}
}
int stepOnewireThermoBus() {
uint8_t addr[8];
unsigned long now = millis();
// Are we already working on a sensor? service it, possibly emitting a a
// thermo packet.
if (thermoSensor.Initialized() || thermoSensor.Busy()) {
if (thermoSensor.Update(now)) {
char buf[64];
char nameBuf[17];
// Just finished conversion
//writeThermoPacket(&gThermoSensor);
thermoSensor.GetTempC(buf);
thermoSensor.GetName(nameBuf);
if (buf[0] != '\0') {
addTemperature(nameBuf,atof(buf));
cloudPending = consolePending = tcpPending = thermoPending = 1;
}
thermoSensor.Reset();
} else if (thermoSensor.Busy()) {
// More cycles needed on this sensor
return 1;
} else {
// finished or not started
}
// First time, or finished with last sensor; clean up, and look more more
// devices.
int more_search = onewire.search(addr);
if (!more_search) {
// Bus exhausted; start over
onewire.reset_search();
return 0;
}
// New sensor. Initialize and start work.
thermoSensor.Initialize(&onewire, addr);
thermoSensor.Update(now);
return 1;
}
// First time, or finished with last sensor; clean up, and look more more
// devices.
int more_search = onewire.search(addr);
if (!more_search) {
// Bus exhausted; start over
onewire.reset_search();
return 0;
}
// New sensor. Initialize and start work.
thermoSensor.Initialize(&onewire, addr);
thermoSensor.Update(now);
return 1;
}
void setup() {
Log.info("Starting setup ...");
Serial.begin(115200);
Serial.print("start: kegboard-particle online, ip: ");
Serial.println(WiFi.localIP());
server.begin();
SETUP_METER(0);
SETUP_METER(1);
SETUP_METER(2);
SETUP_METER(3);
SETUP_TEMP(0);
SETUP_TEMP(1);
SETUP_TEMP(2);
SETUP_TEMP(3);
Particle.function("resetMeter", publicResetMeter);
Particle.function("meterTicks", publicMeterTicks);
mdnsRunning = setupMdns();
}
void getStatus(String* statusMessage) {
for (int i = 0; i < NUM_METERS; i++) {
meter_t *meter = &meters[i];
unsigned int ticks = meter->ticks;
statusMessage->concat(String::format("meter%i.ticks=%u ", i, ticks));
}
}
void getThermoStatus(String* statusMessage) {
for (int i = 0; i < NUM_METERS; i++) {
if (temps[i].probe[0] != '\0') {
statusMessage->concat(String::format("temp_%s.temp=%f ", temps[i].probe, temps[i].temp));
}
}
}
void handleCommand(char *command) {
Log.info("Handling command: \"%s\" ...", command);
String commandString = String(command);
if (commandString.equals(COMMAND_WATCHDOG_ON)) {
Log.info("Enabling watchdog.");
enableClientWatchdog();
} else if (commandString.equals(COMMAND_WATCHDOG_OFF)) {
Log.info("Disabling watchdog.");
disableClientWatchdog();
} else if (commandString.equals(COMMAND_WATCHDOG_KICK)) {
Log.info("Watchdog kicked.");
kickClientWatchdog();
} else {
Log.info("Unknown command");
}
}
void serviceCurrentClient() {
if (!client.connected()) {
return;
}
while (client.available()) {
char c = client.read();
if ((int)c == -1) {
break;
}
// Always ignore CRs.
if (c == '\r') {
continue;
}
// Consume and ignore leading whitespace.
if (clientBufferPos == 0) {
if (c == '\n' || c == ' ') {
continue;
}
}
clientBuffer[clientBufferPos++] = c;
// We've got a complete command.
if (c == '\n') {
break;
}
// Haven't received a complete command & about to overflow.
if (clientBufferPos == (TCP_CLIENT_INCOMING_BUFSIZE - 1)) {
Log.info("Incoming buffer overflow");
clientBufferPos = 0;
}
}
if (clientBufferPos > 0 && clientBuffer[clientBufferPos - 1] == '\n') {
clientBuffer[clientBufferPos - 1] = '\0';
handleCommand(clientBuffer);
clientBufferPos = 0;
clientBuffer[0] = '\0';
}
}
void checkAndServiceTcp() {
// Check for a new client.
if (!client.connected()) {
client = server.available();
if (client.connected()) {
Log.info("TCP client connectd");
watchdogEnabled = 0;
client.print("info: kegboard-particle device_id=");
client.print(System.deviceID());
client.print(" version=");
client.print(VERSION);
client.println();
}
}
if (client.connected()) {
serviceCurrentClient();
}
}
void publishCloudStatus() {
if (!cloudPending) {
return;
}
cloudPending = 0;
String statusMessage;
if (meterPending) {
getStatus(&statusMessage);
bool published = Particle.publish("kb-status", statusMessage, PRIVATE);
if (published) {
lastCloudPublishMillis = millis();
}
}
if (thermoPending) {
statusMessage = "";
getThermoStatus(&statusMessage);
bool published = Particle.publish("kb-thermo", statusMessage, PRIVATE);
if (published) {
lastCloudPublishMillis = millis();
}
}
}
void publishTcpStatus() {
if (!tcpPending) {
return;
}
tcpPending = 0;
if (client.connected()) {
String statusMessage;
if (meterPending) {
getStatus(&statusMessage);
client.print("kb-status: ");
client.println(statusMessage);
}
if (thermoPending) {
getThermoStatus(&statusMessage);
client.print("kb-thermo: ");
client.println(statusMessage);
}
}
lastTcpPublishMillis = millis();
}
void publishConsoleStatus() {
if (!consolePending) {
return;
}
consolePending = 0;
String statusMessage;
if (meterPending) {
getStatus(&statusMessage);
Serial.print("kb-status: ");
Serial.println(statusMessage);
}
if (thermoPending) {
statusMessage = "";
getThermoStatus(&statusMessage);
Serial.print("kb-thermo: ");
Serial.println(statusMessage);
}
lastConsolePublishMillis = millis();
}
void loop() {
checkClientWatchdog();
checkAndServiceTcp();
stepOnewireThermoBus();
if (mdnsRunning) {
mdnsImpl.processQueries();
}
if (client.connected()) {
if ((millis() - lastTcpPublishMillis) >= TCP_PUBLISH_INTERVAL_MILLIS) {
publishTcpStatus();
}
}
if ((millis() - lastCloudPublishMillis) >= CLOUD_PUBLISH_INTERVAL_MILLIS) {
publishCloudStatus();
}
if ((millis() - lastConsolePublishMillis) >= CONSOLE_PUBLISH_INTERVAL_MILLIS) {
publishConsoleStatus();
}
meterPending = thermoPending = 0;
}