/
led_board.ino
259 lines (228 loc) · 6.75 KB
/
led_board.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
#include <Adafruit_NeoPixel.h>
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <functional>
#include <WiFiManager.h>
#include <WiFiUdp.h>
// Some UDP / WeMo specific variables:
String device_name = "pixel board"; // Name of device
float power = true; // Pin to toggle
WiFiUDP UDP;
IPAddress ipMulti(239, 255, 255, 250);
unsigned int portMulti = 1900; // local port to listen on
ESP8266WebServer HTTP(80);
String serial; // Where we save the string of the UUID
String persistent_uuid; // Where we save some socket info with the UUID
// Buffer to save incoming packets:
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
// Define the connections and instantiate the NeoPixels
#define PIN1 D5
#define PIN2 D6
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(50, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(50, PIN2, NEO_GRB + NEO_KHZ800);
// Define the size of the arrays
int top[5][10][3];
int bottom[5][10][3];
// Variables to store time counts
int lightTimer = 0;
int webTimer = 0;
void setup() {
// Set the UUIDs and socket information:
prepareIds();
// Get settings from WiFi Manager:
WiFiManager wifiManager;
// wifiManager.resetSettings(); // Uncomment this to test WiFi Manager function
wifiManager.setAPCallback(configModeCallback);
wifiManager.autoConnect();
// Connect to UDP:
bool udpConnected = connectUDP();
if (udpConnected){
startHttpServer(); // Start the HTTP Server
}
// Initialize the display
strip1.begin();
strip2.begin();
// Initialize the random seed
randomSeed(1989);
}
void loop() {
if (lightTimer == 0) {
lightTimer = millis();
}
if (millis() - lightTimer >= 500) {
randomFill(power);
displayBoard1();
displayBoard2();
lightTimer = 0;
}
HTTP.handleClient();
if (webTimer == 0) {
webTimer = millis();
}
if (millis() - webTimer >= 1) {
// If there are packets, we parse them:
int packetSize = UDP.parsePacket();
if(packetSize) {
int len = UDP.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
String request = packetBuffer;
if(request.indexOf('M-SEARCH') > 0) {
if(request.indexOf("urn:Belkin:device:**") > 0) {
respondToSearch();
}
}
}
webTimer = 0;
}
}
void randomFill(bool fill) {
for(int x=0; x<5; x++){
for(int y=0; y < 10; y++){
for(int z=0; z<3; z++){
if(fill == true){
top[x][y][z] = random(3);
bottom[x][y][z] = random(3);
}
else {
top[x][y][z] = 0;
bottom[x][y][z] = 0;
}
}
}
}
}
void displayBoard1() {
for(int x=0; x<5; x++){
for(int y=0; y < 10; y++){
strip1.setPixelColor(x*10 + y, strip1.Color(top[x][y][0], top[x][y][1], top[x][y][2]));
}
}
strip1.show();
}
void displayBoard2() {
for(int x=0; x<5; x++){
for(int y=0; y < 10; y++){
strip2.setPixelColor(x*10 + y, strip2.Color(top[x][y][0], bottom[x][y][1], bottom[x][y][2]));
}
}
strip2.show();
}
void prepareIds() {
uint32_t chipId = ESP.getChipId();
char uuid[64];
sprintf_P(uuid, PSTR("38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
(uint16_t) ((chipId >> 16) & 0xff),
(uint16_t) ((chipId >> 8) & 0xff),
(uint16_t) chipId & 0xff);
serial = String(uuid);
persistent_uuid = "Socket-1_0-" + serial;
}
bool connectUDP(){
boolean state = false;
if(UDP.beginMulticast(WiFi.localIP(), ipMulti, portMulti)) {
state = true;
}
return state;
}
void startHttpServer() {
HTTP.on("/index.html", HTTP_GET, [](){
HTTP.send(200, "text/plain", "Hello World!");
});
HTTP.on("/upnp/control/basicevent1", HTTP_POST, []() {
String request = HTTP.arg(0);
if(request.indexOf("<BinaryState>1</BinaryState>") > 0) {
power = true;
}
if(request.indexOf("<BinaryState>0</BinaryState>") > 0) {
power = false;
}
HTTP.send(200, "text/plain", "");
});
HTTP.on("/eventservice.xml", HTTP_GET, [](){
String eventservice_xml = "<?scpd xmlns=\"urn:Belkin:service-1-0\"?>"
"<actionList>"
"<action>"
"<name>SetBinaryState</name>"
"<argumentList>"
"<argument>"
"<retval/>"
"<name>BinaryState</name>"
"<relatedStateVariable>BinaryState</relatedStateVariable>"
"<direction>in</direction>"
"</argument>"
"</argumentList>"
"<serviceStateTable>"
"<stateVariable sendEvents=\"yes\">"
"<name>BinaryState</name>"
"<dataType>Boolean</dataType>"
"<defaultValue>0</defaultValue>"
"</stateVariable>"
"<stateVariable sendEvents=\"yes\">"
"<name>level</name>"
"<dataType>string</dataType>"
"<defaultValue>0</defaultValue>"
"</stateVariable>"
"</serviceStateTable>"
"</action>"
"</scpd>\r\n"
"\r\n";
HTTP.send(200, "text/plain", eventservice_xml.c_str());
});
HTTP.on("/setup.xml", HTTP_GET, [](){
IPAddress localIP = WiFi.localIP();
char s[16];
sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
String setup_xml = "<?xml version=\"1.0\"?>"
"<root>"
"<device>"
"<deviceType>urn:Belkin:device:controllee:1</deviceType>"
"<friendlyName>"+ device_name +"</friendlyName>"
"<manufacturer>Belkin International Inc.</manufacturer>"
"<modelName>Emulated Socket</modelName>"
"<modelNumber>3.1415</modelNumber>"
"<UDN>uuid:"+ persistent_uuid +"</UDN>"
"<serialNumber>221517K0101769</serialNumber>"
"<binaryState>0</binaryState>"
"<serviceList>"
"<service>"
"<serviceType>urn:Belkin:service:basicevent:1</serviceType>"
"<serviceId>urn:Belkin:serviceId:basicevent1</serviceId>"
"<controlURL>/upnp/control/basicevent1</controlURL>"
"<eventSubURL>/upnp/event/basicevent1</eventSubURL>"
"<SCPDURL>/eventservice.xml</SCPDURL>"
"</service>"
"</serviceList>"
"</device>"
"</root>\r\n"
"\r\n";
HTTP.send(200, "text/xml", setup_xml.c_str());
});
HTTP.begin();
}
void respondToSearch() {
IPAddress localIP = WiFi.localIP();
char s[16];
sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
String response =
"HTTP/1.1 200 OK\r\n"
"CACHE-CONTROL: max-age=86400\r\n"
"DATE: Wed, 8 Feb 2017 02:30:00 GMT\r\n"
"EXT:\r\n"
"LOCATION: http://" + String(s) + ":80/setup.xml\r\n"
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"
"01-NLS: b9200ebb-736d-4b93-bf03-835149d13983\r\n"
"SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
"ST: urn:Belkin:device:**\r\n"
"USN: uuid:" + persistent_uuid + "::urn:Belkin:device:**\r\n"
"X-User-Agent: redsonic\r\n\r\n";
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
UDP.write(response.c_str());
UDP.endPacket();
}
void configModeCallback(WiFiManager *myWiFiManager) {
WiFi.softAPIP();
myWiFiManager->getConfigPortalSSID();
}