-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashrgb_and_temperature_with_sms.ino
283 lines (246 loc) · 6.79 KB
/
dashrgb_and_temperature_with_sms.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
/* rgbleds.ino
*
* This is a sketch for the Hologram dash that receives a color name
* or list of names via SMS and then controls an RGB LED strip to
* display that color.
*
*
* 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.
*
* Test code for DHT 11 Temperature Module w/ Hologram Dash
* Modified by Hologram for the Hologram Dash
* This is based off of the Adafruit Arduino DHT example at:
* https://github.com/adafruit/DHT-sensor-library/tree/master/examples/DHTtester
* This modified Hologram version sends the humidity, temperature
* data back to the Hologram cloud
*
* 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.
*
*/
#include <DHT.h>
#define DHTPIN R04 // DHT Sensor output pin connected to Dash GPIO
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
int pins[3] = {L09, L08, L07}; //R, G, B
int curColVal[3] = {0,0,0};
int val;
int colorStep = 4;
int timeBetweenSteps = 100;
int nextColorStep = 0;
int curColors[10];
int curColorIdx = 0;
String tempBuffer = "";
String payload = "";
bool foundSMS = false;
#define NUMCOLORS 20
String colors[] =
{"BLACK", "RED", "GREEN", "BLUE", "WHITE",
"PURPLE", "YELLOW", "CYAN", "PINK", "BT",
"INDIGO", "NAVY", "TEAL", "TURQUOISE",
"CHARTREUSE", "GOLD", "ORANGE", "BROWN", "MAROON", "GRAY"};
int colorVals[][3] =
{ { 0, 0, 0 }, //Black
{ 255, 0, 0 }, //Red
{ 0, 255, 0 }, //Green
{ 0, 0, 255 }, //Blue
{ 255, 255, 255}, //White
{ 255, 0, 255}, //Purple
{ 255, 255, 0 }, //Yellow
{ 0, 255, 255}, // Cyan
{ 255, 181, 197}, // Pink
{ 0, 255, 204}, //bright Turquoise
{ 75, 0, 130}, //indigo
{ 0, 0, 128}, //navy
{ 0, 128, 128}, //teal
{ 64, 224, 208}, //turquoise
{ 127, 255, 0}, //chartreuse
{ 238, 201, 0}, //gold
{ 255, 128, 0}, //orange
{ 165, 42, 42}, //brown
{ 128, 0, 0}, //maroon
{ 128, 128, 128} //gray
};
void setup()
{
for(int i = 0; i < 3; ++i) {
pinMode(pins[i], OUTPUT);
}
resetColorList();
SerialUSB.begin(9600);
SerialCloud.begin(115200);
delay(4000);
Dash.begin();
dht.begin();
Dash.pulseLED(100,5000);
}
int findColor(String color)
{
SerialUSB.println(color);
int i;
for (i = 0; i < NUMCOLORS; ++i) {
if (color == colors[i]) {
SerialUSB.println("Color found");
return i;
}
}
return -1;
}
void resetColorList()
{
curColors[0] = 0;
for(int i = 1; i < 10; ++i) {
curColors[i] = -1;
}
curColorIdx = 0;
colorStep = 4;
timeBetweenSteps = 100;
}
void doTempSend() {
char buf[8];
dtostrf(dht.readTemperature(true),4,2,buf);
String ret = "&message=Temperature: ";
ret.concat(buf);
ret.concat(", ");
dtostrf(dht.readHumidity(),4,2,buf);
ret.concat("humidity: ");
ret.concat(buf);
SerialCloud.println(ret);
}
void doPartyMode()
{
SerialUSB.println("PARTY!");
for(int i = 0; i < 10; ++i) {
int randcolor = random(1, NUMCOLORS);
curColors[i] = randcolor;
}
curColorIdx = 0;
colorStep = 255;
timeBetweenSteps = 150;
}
void parseColorString(String sms)
{
resetColorList();
sms.toUpperCase();
int numcolors = 0;
int startidx = 0;
while(startidx < sms.length() && numcolors < 10) {
int endidx = sms.indexOf(' ', startidx);
if(endidx == -1) {
endidx = sms.length();
}
String colorStr = sms.substring(startidx, endidx);
SerialUSB.println(colorStr);
if(colorStr == "TEMPERATURE") {
doTempSend();
return;
}
if(colorStr == "PARTY") {
doPartyMode();
return;
} else if(colorStr == "BLINK") {
colorStep = 255;
timeBetweenSteps = 500;
} else {
int curColor = findColor(colorStr);
if(curColor >= 0) {
curColors[numcolors] = curColor;
++numcolors;
}
}
startidx = endidx + 1;
}
}
void loop()
{
char currChar;
while (SerialCloud.available()) {
currChar = (char)SerialCloud.read();
// check if the current buffer hits the SMSRCVD code.
if (!foundSMS) {
if (tempBuffer == "SMSRCVD") {
foundSMS = true;
}
}
// If it received the SMSRCVD code, the payload will get populated until
// we get a \n.
else if (currChar == '\n'){
SerialUSB.println("\nSMS received: ");
payload = stripOffLengthNumber(payload);
payload.trim();
SerialUSB.println(payload);
parseColorString(payload);
// reset foundSMS and the payload for the next iteration.
foundSMS = false;
payload = "";
}
else {
payload.concat(currChar);
}
// Only keep a sliding buffer length of size 7
// (need to only check for SMSRCVD).
if (tempBuffer.length() >= 7) {
tempBuffer.remove(0, 1);
}
// add latest char to our buffer.
tempBuffer.concat(currChar);
SerialUSB.write(currChar);
}
if(nextColorStep < millis()) {
nextColorStep = millis() + timeBetweenSteps;
int curColor = curColors[curColorIdx];
if(curColor >= 0) {
// Check if all colors are where they should be
bool colorsSet = true;
for(int i = 0; i < 3; ++i) {
if(curColVal[i] != colorVals[curColor][i]) {
colorsSet = false;
break;
}
}
if(colorsSet) {
++curColorIdx;
if(curColorIdx >= 10 || curColors[curColorIdx] == -1) {
curColorIdx = 0;
}
} else {
for(int i = 0; i < 3; ++i) {
if(curColVal[i] != colorVals[curColor][i]) {
int remaining = colorVals[curColor][i] - curColVal[i];
if(remaining < 0) {
curColVal[i] -= min((-1*remaining), colorStep);
} else {
curColVal[i] += min(remaining, colorStep);
}
analogWrite(pins[i], curColVal[i]);
}
}
}
}
}
delay(40);
}
String stripOffLengthNumber(String payload)
{
int index = 0;
while (payload[index] == ',') {
++index;
}
while (payload[index] != ',') {
++index;
}
++index;
payload.remove(0, index);
return payload;
}