-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
334 lines (286 loc) · 8.3 KB
/
main.cpp
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
#include <espduino.h>
#include <mqtt.h>
#include "main.h"
#include "credentials.h"
#include "MqttClient.h"
#include "Leds.h"
// global flag to turn off the leds
static bool ledsOff = false;
SoftwareSerial debugSerial(12,13); // Rx, Tx
ESP esp(&Serial, &debugSerial, ESP_CHIP_ENABLE);
MqttClient mqttClient(&esp);
volatile bool wifiConnected = false;
// Forward declaration
bool sendCmd(Cmd cmd);
void wifiCb(void* response)
{
uint32_t status;
RESPONSE res(response);
if(res.getArgc() == 1) {
res.popArgs((uint8_t*)&status, 4);
if(status == STATION_GOT_IP) {
debugSerial.println("ARDUINO: WIFI CONNECTED");
wifiConnected = true;
}
else {
debugSerial.println("ARDUINO: WIFI failed to connect");
wifiConnected = false;
MqttClient::connected = false;
}
}
else
{
debugSerial.println("ARDUINO: Wifi is doing something unusual...");
}
}
void setup(void)
{
// Set button pins (14-17) as inputs. Bits(0-3)
DDRC &= 0xF0;
// Set reset pin as output
pinMode(ESP_CHIP_RESET, OUTPUT);
// Keep reset pin held high for regular operation
digitalWrite(ESP_CHIP_RESET, HIGH);
initLeds();
Serial.begin(19200);
debugSerial.begin(19200);
esp.wifiCb.attach(&wifiCb);
debugSerial.println("ARDUINO: Initial Setup Complete");
}
void espMultipleProcess(short numTimes)
{
for(short processCount = 0; processCount<numTimes; ++processCount)
{
esp.process();
delay(20);
}
}
void hardResetEspChip()
{
digitalWrite(ESP_CHIP_RESET, LOW);
delay(20);
digitalWrite(ESP_CHIP_RESET, HIGH);
delay(20);
}
bool sendCmd(Cmd cmd)
{
static const char* itemCurtains = "curtains";
static const char* itemLights = "overHeadLights";
static const char* itemPixels = "pixelWall";
static const char* itemFan = "fan";
// figure out which itemName we are talking to
const char* itemName = 0;
switch(cmd)
{
case e_cmdButton1:
itemName = itemCurtains;
break;
case e_cmdButton2:
itemName = itemLights;
break;
case e_cmdButton3:
itemName = itemPixels;
break;
case e_cmdButton4:
itemName = itemFan;
break;
default:
debugSerial.println("ARDUINO: Bad cmd passed to sendCmd()\n");
itemName = "";
return false;
}
char topic[32] = {0};
sprintf(topic, "/%s/%s", "bedroom1", itemName);
char mesg[64] = {0};
sprintf(mesg, "{\"command\":\"toggle\"}");
debugSerial.println("");
debugSerial.println(topic);
debugSerial.println(mesg);
// Send out command over mqtt protocol
mqttClient.publish(topic, mesg);
return true;
}
void bringUpWifi()
{
unsigned triesBeforeChipReset = 2000;
unsigned count = 0;
while(!wifiConnected)
{
// Start by putting chip in known state
esp.enable();
delay(500);
hardResetEspChip();
delay(1000);
esp.reset();
delay(500);
while(!esp.ready())
{
digitalWrite(HEART_BEAT_LED, HIGH);
delay(1000);
digitalWrite(HEART_BEAT_LED, LOW);
delay(1000);
if(count >= 3)
{
debugSerial.println("ARDUINO: Resetting ESP chip");
esp.enable();
delay(500);
hardResetEspChip();
delay(1000);
esp.reset();
delay(500);
count = 0;
}
count++;
}
/*setup wifi*/
debugSerial.println("ARDUINO: Setting up wifi");
esp.wifiConnect(SSID,PASSWORD);
// We should chill here until we get a wifi connection
while(!wifiConnected && (count < triesBeforeChipReset) )
{
esp.process();
delay(100);
++count;
}
count = 0;
}
}
void bringUpMqtt()
{
unsigned brokerConnectionTryLimit = 10;
unsigned count = 0;
// if we lost wifiConnectivity, we need to break out and
// go back to bringing up wifi
debugSerial.println("ARDUINO: Inside bringUpMqtt()");
if( !mqttClient.init() )
{
debugSerial.println("ARDUINO: Bad news, mqtt client failed to initialize correctly");
return;
}
while(!MqttClient::connected && (count < brokerConnectionTryLimit) )
{
debugSerial.println("ARDUINO: Connecting mqttClient");
mqttClient.connect(SERVER_NAME, SERVER_PORT, false);
// process 10 times
espMultipleProcess(10);
delay(100);
espMultipleProcess(10);
++count;
}
debugSerial.println("ARDUINO: Leaving bringUpMqtt()");
return;
}
const unsigned debounceDelay = 20;
int index = 0;
unsigned cyclesCount;
unsigned char buttonPressed = 0x00;
unsigned char lastButtonPressed = 0x00;
unsigned lastButtonPressExpireTimer = 0;
const unsigned lastButtonPressTimeout = 5000;
bool forceLedUpdate = true;
void loop(void)
{
if(!wifiConnected)
{
debugSerial.println("ARDUINO: Wifi down, bringing it up");
bringUpWifi();
espMultipleProcess(10);
delay(1000);
}
if(!MqttClient::connected && wifiConnected)
{
debugSerial.println("ARDUINO: Mqtt down, bringing it up");
bringUpMqtt();
espMultipleProcess(10);
delay(1000);
}
if(MqttClient::connected && !wifiConnected)
{
debugSerial.println("ARDUINO: Bad state. mqtt somehow connected and wifi isn't");
debugSerial.println("ARDUINO: restarting state machine");
wifiConnected = false;
delay(1000);
}
// subscriptions!!!!
if(wifiConnected && MqttClient::connected)
{
debugSerial.println("ARDUINO: All systems online!");
// set subscriptions
mqttClient.subscribe();
espMultipleProcess(10);
delay(1000);
}
while(wifiConnected && MqttClient::connected)
{
// Throw some clock cycles at the esp
esp.process();
buttonPressed = (PINC & 0x0F);
if( buttonPressed )
{
// lastButtonPressed will be cleared after a time
if( buttonPressed!=lastButtonPressed )
{
if( buttonPressed == BTN_1)
{
ledsOff = !ledsOff;
forceLedUpdate = true;
debugSerial.println("Turning Button Leds");
debugSerial.println(ledsOff?"OFF":"ON");
//if( !sendCmd(e_cmdButton1) )
//{
// debugSerial.println("Error sending e_cmdButton1");
//}
}
else if( buttonPressed == BTN_2 )
{
if( !sendCmd(e_cmdButton2) )
{
debugSerial.println("ARDUINO: Error sending e_cmdButton2");
}
}
else if( buttonPressed == BTN_3 )
{
if( !sendCmd(e_cmdButton3) )
{
debugSerial.println("ARDUINO: Error sending e_cmdButton3");
}
}
else if( buttonPressed == BTN_4 )
{
if( !sendCmd(e_cmdButton4) )
{
debugSerial.println("ARDUINO: Error sending e_cmdButton4");
}
}
espMultipleProcess(5);
delay(debounceDelay); //debounce delay
// save lastButtonPressed to check against next time
lastButtonPressed = buttonPressed;
buttonPressed = 0x00;
}
else
{
// They pressed the same button again, restart the counter
lastButtonPressExpireTimer = 0;
}
}
if(lastButtonPressed)
{
++lastButtonPressExpireTimer;
}
// After a while, we clear the lastButtonPressed
// The point is to prevent a user holding a button
if(lastButtonPressExpireTimer > lastButtonPressTimeout)
{
lastButtonPressed = 0x00;
lastButtonPressExpireTimer = 0;
}
++cyclesCount;
if( (cyclesCount >= ledUpdateWaitCycles) || forceLedUpdate )
{
updateButtonLeds(ledsOff);
cyclesCount = 0;
forceLedUpdate = false;
}
}
}