-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeoPixelArduinoWebServer.ino
More file actions
213 lines (126 loc) · 4.72 KB
/
NeoPixelArduinoWebServer.ino
File metadata and controls
213 lines (126 loc) · 4.72 KB
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
#include <Ethernet.h>
#include <Adafruit_NeoPixel.h>
#define PIN 7
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(110, PIN, NEO_GRB + NEO_KHZ800);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Sets MAC Address (can be anything valid)
IPAddress ip(192, 168, 1, 130); // Sets static Arduino IP
EthernetServer server(80);
//Make variables to be used later
String reqData; // Request from Smartphone
String header;
int contentSize = -1;
String CONTENT_LENGTH_TXT = "Content-Length: ";
/// SET UP
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// Pin mode
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Serial.print("Ready...");
Ethernet.begin(mac, ip);
server.begin();
Serial.println("Begin...");
} //End setup
void loop() {
EthernetClient client = server.available(); // Checks for a client (phone)
if (client) {
// Let's start reading
boolean isLastLine = true;
boolean isBody = false;
header = "";
reqData = "";
int contentLen = 0;
Serial.print("Client connected!");
while (client.connected()) {
if (client.available()) {
// Read data
char c = client.read();
// Serial.print(c);
if (contentSize == contentLen) {
//Serial.println("Body ["+reqData+"]");
Serial.println(reqData);
// Extract the JSON string like [r,g,b]
int pos1 = reqData.indexOf("[");
int pos2 = reqData.lastIndexOf("]");
// Parse the string
String colors = reqData.substring(pos1 + 1, pos2);
Serial.println("Colors [" + colors + "]");
int idx1 = colors.indexOf(',');
int idx2 = colors.indexOf(',', idx1 + 1);
int idx3 = colors.indexOf(',', idx2 + 1);
int idx4 = colors.indexOf(',', idx3 + 1);
String sRed = colors.substring(0, idx1);
String sGreen = colors.substring(idx1 + 1, idx2);
String sBlue = colors.substring(idx2 + 1, idx3);
String sBright = colors.substring(idx3 + 1, idx4);
// Convert the Red, Green and Blue string values to int
int red = sRed.toInt();
int green = sGreen.toInt();
int blue = sBlue.toInt();
//int brightness = sBright.toInt();
int brightness = 255; //hard coding at full brightness for now
uint32_t colour = strip.Color(red, green, blue, brightness);
// Set the RGB led color according to the values sent by the Android client
Serial.print("Pre-Set: ");
Serial.println(colour);
setColor(colour, brightness); // Does the actual changing of colors
// Create the response to client
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
client.println("<!DOCTYPE html>");
client.println("<html>");
delay(1);
break;
}
if (c == '\n' && isLastLine) {
isBody = true;
int pos = header.indexOf(CONTENT_LENGTH_TXT);
String tmp = header.substring(pos, header.length());
//Serial.println("Tmp ["+tmp+"]");
int pos1 = tmp.indexOf("\r\n");
String size = tmp.substring(CONTENT_LENGTH_TXT.length(), pos1);
Serial.println("Size [" + size + "]");
contentSize = size.toInt();
}
if (isBody) {
reqData += c;
contentLen++;
}
else {
header += c;
}
if (c == '\n' ) {
isLastLine = true;
}
else if (c != '\r' ) {
isLastLine = false;
}
} // end if available
} //end while(connected)
// Close connection
Serial.println("Stop..");
client.stop();
} // end if(client)
} // end loop
//Sets the color of all LEDs to the one passed in, and displays them
void setColor(uint32_t colour, int br) {
Serial.println(strip.numPixels());
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, colour);
Serial.println(i);
}
strip.setBrightness(br);
strip.show();
}