/
Arduino_Ethernet.ino
306 lines (247 loc) · 6.72 KB
/
Arduino_Ethernet.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
/*
CheerLights!
CheerLights Channel --> Arduino Ethernet --> FastLED
This sketch subscribes to the CheerLights ThingSpeak Channel and relays the
last color from the Internet to a FastLED compatible RGB Light like Neopixels
or RGB-123 light panels. This sketch uses DHCP to obtain an IP address
automatically and requires Arduino 1.0+.
Arduino Requirements:
* Arduino Ethernet (or Arduino + Ethernet Shield)
* Arduino 1.0+ IDE
* FastLED Library - https://github.com/FastLED/FastLED/releases
Network Requirements:
* Ethernet port on Router
* DHCP enabled on Router
* Unique MAC Address for Arduino
Created: December 19, 2014 by Hans Scharler - http://nothans.com
Additional Credits:
Example sketches from FastLED
ThingSpeak Tutorial:
http://community.thingspeak.com/tutorials/arduino/cheerlights-with-arduino-and-the-fastled-library/
To join the CheerLights project, visit http://www.cheerlights.com
*/
#include <SPI.h>
#include <Ethernet.h>
#include "FastLED.h"
// Define the number of LED in strip or on panel
#define NUM_LEDS 8
// For LED chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For LED chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 6
#define CLOCK_PIN 13
// Define the array of LEDs
CRGB leds[NUM_LEDS];
// Local Network Settings
byte mac[] = {
0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
// ThingSpeak / CheerLights Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String thingSpeakChannel = "1417"; // Channel number of the CheerLights Channel
String thingSpeakField = "1"; // Field number of the CheerLights Colors
const int thingSpeakInterval = 10 * 1000; // Time interval in milliseconds to get data from ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
String lastCommand = "";
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup() {
// Setup Serial
Serial.begin(9600);
delay(100);
Serial.flush();
delay(100);
// Start Ethernet on Arduino
startEthernet();
// Setup FastLED
FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS);
}
void loop() {
// Process CheerLights Commands
if(client.available() > 0)
{
delay(100);
String response;
char charIn;
do {
charIn = client.read(); // read a char from the buffer
response += charIn; // append that char to the string response
}
while (client.available() > 0);
// Send matching commands to LEDs
if (response == "white")
{
lastCommand = "white";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::White;
FastLED.show();
delay(30);
}
}
else if (response == "red")
{
lastCommand = "red";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Red;
FastLED.show();
delay(30);
}
}
else if (response == "green")
{
lastCommand = "green";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Green;
FastLED.show();
delay(30);
}
}
else if (response == "blue")
{
lastCommand = "blue";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Blue;
FastLED.show();
delay(30);
}
}
else if (response == "cyan")
{
lastCommand = "cyan";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Cyan;
FastLED.show();
delay(30);
}
}
else if (response == "magenta")
{
lastCommand = "magenta";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Magenta;
FastLED.show();
delay(30);
}
}
else if (response == "yellow")
{
lastCommand = "yellow";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Yellow;
FastLED.show();
delay(30);
}
}
else if (response == "purple")
{
lastCommand = "purple";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Purple;
FastLED.show();
delay(30);
}
}
else if (response == "orange")
{
lastCommand = "orange";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Orange;
FastLED.show();
delay(30);
}
}
else if (response == "warmwhite")
{
lastCommand = "warmwhite";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::OldLace;
FastLED.show();
delay(30);
}
}
else if (response == "oldlace")
{
lastCommand = "oldlace";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::OldLace;
FastLED.show();
delay(30);
}
}
else if (response == "pink")
{
lastCommand = "pink";
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Pink;
FastLED.show();
delay(30);
}
}
else
{
lastCommand = "(no match)";
}
// Echo command
delay(200);
Serial.println("CheerLight Command Received: "+lastCommand);
Serial.println();
delay(200);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Subscribe to ThingSpeak Channel and Field
if(!client.connected() && (millis() - lastConnectionTime > thingSpeakInterval))
{
subscribeToThingSpeak(thingSpeakChannel, thingSpeakField);
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3) {
startEthernet();
}
lastConnected = client.connected();
} // End loop
void subscribeToThingSpeak(String tsChannel, String tsField)
{
if (client.connect(thingSpeakAddress, 80))
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
client.println("GET /channels/"+tsChannel+"/field/"+tsField+"/last.txt");
client.println();
lastConnectionTime = millis();
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
delay(1000);
}