forked from Kegbot/kegboard-particle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
201 lines (165 loc) · 4.92 KB
/
main.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
/**
* kegboard-particle
*
* Copyright (c) 2016 Bevbot LLC.
*
* 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.1.0"
#define CLOUD_PUBLISH_INTERVAL_MILLIS 1000
#define TCP_PUBLISH_INTERVAL_MILLIS 250
#define CONSOLE_PUBLISH_INTERVAL_MILLIS 250
#define NUM_METERS 4
#define METER0_PIN D1
#define METER1_PIN D2
#define METER2_PIN D3
#define METER3_PIN D4
#define TCP_SERVER_PORT 8321
TCPServer server = TCPServer(TCP_SERVER_PORT);
TCPClient client;
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];
#define CREATE_METER_ISR(METER_NUM) \
void meter##METER_NUM##Interrupt(void) { \
detachInterrupt(METER##METER_NUM##_PIN); \
meters[METER_NUM].ticks++; \
cloudPending = consolePending = tcpPending = 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)
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 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);
Particle.function("resetMeter", publicResetMeter);
Particle.function("meterTicks", publicMeterTicks);
}
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 checkForTcpClient() {
if (!client.connected()) {
client = server.available();
if (client.connected()) {
client.print("info: kegboard-particle device_id=");
client.print(System.deviceID());
client.print(" version=");
client.print(VERSION);
client.println();
}
}
}
void publishCloudStatus() {
if (!cloudPending) {
return;
}
cloudPending = 0;
String statusMessage;
getStatus(&statusMessage);
bool published = Particle.publish("kb-status", statusMessage, PRIVATE);
if (published) {
lastCloudPublishMillis = millis();
}
}
void publishTcpStatus() {
if (!tcpPending) {
return;
}
tcpPending = 0;
if (client.connected()) {
String statusMessage;
getStatus(&statusMessage);
client.print("kb-status: ");
client.println(statusMessage);
}
lastTcpPublishMillis = millis();
}
void publishConsoleStatus() {
if (!consolePending) {
return;
}
consolePending = 0;
String statusMessage;
getStatus(&statusMessage);
Serial.print("kb-status: ");
Serial.println(statusMessage);
lastConsolePublishMillis = millis();
}
void loop() {
checkForTcpClient();
if (client.connected()) {
if ((millis() - lastTcpPublishMillis) >= TCP_PUBLISH_INTERVAL_MILLIS) {
publishTcpStatus();
}
} else {
if ((millis() - lastCloudPublishMillis) >= CLOUD_PUBLISH_INTERVAL_MILLIS) {
publishCloudStatus();
}
}
if ((millis() - lastConsolePublishMillis) >= CONSOLE_PUBLISH_INTERVAL_MILLIS) {
publishConsoleStatus();
}
}