-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbeebotte.h
265 lines (236 loc) · 8.55 KB
/
beebotte.h
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
/*
Copyright (C) 2019 Jeremy Spencer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <PubSubClient.h>
#include <ArduinoJson.h>
#define BBT "mqtt.beebotte.com" // Domain name of Beebotte MQTT service
#define CHANNEL "Hearts" // Replace with your device name
#define LED_RESOURCE "ga"
WiFiClient espClient;
PubSubClient client(espClient);
void nextPattern(bool);
void nextPalette(bool);
// to track delay since last reconnection
long lastReconnectAttempt = 0;
const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
char id[17];
const char * generateID()
{
randomSeed(analogRead(0));
int i = 0;
for (i = 0; i < sizeof(id) - 1; i++) {
id[i] = chars[random(sizeof(chars))];
}
id[sizeof(id) - 1] = '\0';
return id;
}
int find_text(String needle, String haystack) {
int foundpos = -1;
if (needle.length() <= haystack.length()) {
for (int i = 0; i <= haystack.length() - needle.length(); i++) {
if (haystack.substring(i, needle.length() + i) == needle) {
foundpos = i;
}
}
}
return foundpos;
}
void find_commands(String data) {
/* power on off
turn on off
pattern up down autocyle on off
pallete up down autocyle on off
speed up down maximum minimum
brightness up down maximum minimum
*/
if ( find_text("power", data) > -1) {
if (find_text("on", data) > -1) {
Serial.println("Command power on");
power = 1;
} else if (find_text("off", data) > -1) {
Serial.println("Command power off");
power = 0;
}
} else if ( find_text("pattern", data) > -1) {
if (find_text("next", data) > -1) {
Serial.println("Command next pattern");
nextPattern(true);
writeFieldsToEEPROM(fields, fieldCount);
} else if (find_text("previous", data) > -1) {
Serial.println("Command previous pattern");
nextPattern(false);
writeFieldsToEEPROM(fields, fieldCount);
} else if (find_text("auto cycle", data) > -1) {
if (find_text("on", data) > -1) {
Serial.println("Command auto cycle patterns on");
autoplay = 1;
autoPlayTimeout = millis() + (autoplayDuration * 1000);
writeFieldsToEEPROM(fields, fieldCount);
} else if (find_text("off", data) > -1) {
Serial.println("Command auto cylce patterns off");
autoplay = 0;
writeFieldsToEEPROM(fields, fieldCount);
}
}
} else if ( find_text("pallet", data) > -1 || find_text("palette", data) > -1) {
if (find_text("next", data) > -1) {
Serial.println("Command next palette");
nextPalette(true);
writeFieldsToEEPROM(fields, fieldCount);
} else if (find_text("previous", data) > -1) {
Serial.println("Command previous palette");
nextPalette(false);
writeFieldsToEEPROM(fields, fieldCount);
} else if (find_text("auto cycle", data) > -1) {
if (find_text("on", data) > -1) {
Serial.println("Command auto cycle palettes on");
cyclePalettes = 1;
paletteTimeout = millis() + (paletteDuration * 1000);
writeFieldsToEEPROM(fields, fieldCount);
} else if (find_text("off", data) > -1) {
Serial.println("Command auto cylce palettes off");
cyclePalettes = 0;
writeFieldsToEEPROM(fields, fieldCount);
}
}
} else if ( find_text("speed", data) > -1) {
if (find_text("up", data) > -1 || find_text("increase", data) > -1) {
Serial.println("Command speed up");
adjustSpeed (true);
} else if (find_text("down", data) > -1 || find_text("decrease", data) > -1) {
Serial.println("Command speed down");
adjustSpeed (false);
} else if (find_text("max", data) > -1) {
Serial.println("Command max speed");
speed = 10;
adjustSpeed (true);
} else if (find_text("min", data) > -1) {
Serial.println("Command min speed");
speed = 1;
adjustSpeed (false);
}
} else if ( find_text("slow", data) > -1) {
if (find_text("down", data) > -1) {
Serial.println("Command slow down");
adjustSpeed (false);
}
} else if ( find_text("brightness", data) > -1) {
if (find_text("up", data) > -1 || find_text("increase", data) > -1) {
Serial.println("Command brightness up");
adjustBrightness(true);
} else if (find_text("down", data) > -1 || find_text("decrease", data) > -1) {
Serial.println("Command brightness down");
adjustBrightness(false);
} else if (find_text("max", data) > -1) {
Serial.println("Command max brightness");
brightnessIndex = brightnessCount;
adjustBrightness(true);
} else if (find_text("min", data) > -1) {
Serial.println("Command min brightness");
brightnessIndex = 1;
adjustBrightness(false);
}
} else if ( find_text("brighter", data) > -1) {
Serial.println("Command brighter");
adjustBrightness(true);
} else if ( find_text("dimmer", data) > -1) {
Serial.println("Command dimmer");
adjustBrightness(false);
} else if ( find_text("white", data) > -1) {
Serial.println("Command white");
power = 1;
solidColor = CRGB::White;
currentPatternIndex = patternCount - 1; // set the current pattern to Solid Color
autoplay = 0; // turn off pattern auto cycle
writeFieldsToEEPROM(fields, fieldCount);
} else if ( find_text("red", data) > -1) {
Serial.println("Command red");
power = 1;
solidColor = CRGB::Red;
currentPatternIndex = patternCount - 1; // set the current pattern to Solid Color
autoplay = 0; // turn off pattern auto cycle
writeFieldsToEEPROM(fields, fieldCount);
} else if ( find_text("green", data) > -1) {
Serial.println("Command green");
power = 1;
solidColor = CRGB::Green;
currentPatternIndex = patternCount - 1; // set the current pattern to Solid Color
autoplay = 0; // turn off pattern auto cycle
writeFieldsToEEPROM(fields, fieldCount);
} else if ( find_text("blue", data) > -1) {
Serial.println("Command blue");
power = 1;
solidColor = CRGB::Blue;
currentPatternIndex = patternCount - 1; // set the current pattern to Solid Color
autoplay = 0; // turn off pattern auto cycle
writeFieldsToEEPROM(fields, fieldCount);
} else {
Serial.println("Command not found");
}
}
// will be called every time a message is received
void onMessage(char* topic, byte* payload, unsigned int length) {
// decode the JSON payload
StaticJsonBuffer<128> jsonInBuffer;
JsonObject& root = jsonInBuffer.parseObject(payload);
// Test if parsing succeeded
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
// led resource is a boolean read it accordingly
String data = root["data"];
data.toLowerCase();
// Print the received value to serial monitor for debugging
Serial.print("Received message of length ");
Serial.print(length);
Serial.println();
Serial.print("data ");
Serial.print(data);
Serial.println();
find_commands(data);
// Set the led pin to high or low
// digitalWrite(LEDPIN, data ? HIGH : LOW);
}
// reconnects to Beebotte MQTT server
boolean reconnect() {
if (client.connect(generateID(), TOKEN, "")) {
char topic[64];
sprintf(topic, "%s/%s", CHANNEL, LED_RESOURCE);
client.subscribe(topic);
Serial.println("Connected to Beebotte MQTT");
}
return client.connected();
}
void setupBeebotte() {
randomSeed(micros());
client.setServer(BBT, 1883);
client.setCallback(onMessage);
lastReconnectAttempt = 0;
}
void loopBeebotte()
{
if (!client.connected()) {
long now = millis();
if (now - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now;
// Attempt to reconnect
if (reconnect()) {
lastReconnectAttempt = 0;
}
}
} else {
// Client connected
client.loop();
}
}