-
Notifications
You must be signed in to change notification settings - Fork 1
/
lightstring.ino
146 lines (117 loc) · 3.19 KB
/
lightstring.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
#include <ESP8266WiFi.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 4
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 20
#define BUFSIZE 32768
const char* ssid = "SSID";
const char* password = "PASSWORD";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(10000);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
byte buf[BUFSIZE];
void setup() {
Serial.begin(115200);
delay(10);
pixels.begin();
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
bool parseCommand(byte* buf, byte** currentCmd, byte** nextCmd) {
int length = (((int)(buf[0])) << 8) | buf[1];
if (length == 0) {
*currentCmd = NULL;
*nextCmd = NULL;
return false;
}
*currentCmd = buf;
*nextCmd = buf + length + 2;
return true;
}
void runCommand(byte *cmd) {
int length = (((int)(buf[0])) << 8) | buf[1];
if (length < 1) {
return;
}
byte which = cmd[2];
if (which == 'w') {
if (length < 4)
return;
int delayb = cmd[3];
int startIndex = cmd[4];
int numPixels = cmd[5];
if (length != numPixels * 3 + 4)
return;
byte* pixelData = cmd + 6;
for (int i = 0; i < numPixels; i++) {
pixels.setPixelColor(i + startIndex, pixelData[3 * i], pixelData[3 * i + 1], pixelData[3 * i +2]);
}
pixels.show();
delay(delayb);
}
}
void printCommand(byte *cmd) {
int length = (((int)(buf[0])) << 8) | buf[1];
byte which = cmd[2];
Serial.printf("l=%d w=%c", length, which);
if (which == 'w')
Serial.printf(" delay=%d idx=%d pix=%d", cmd[3], cmd[4], cmd[5]);
Serial.println("");
}
WiFiClient client;
void loop() {
/*if(!client) {
// Check if a client has connected
WiFiClient newClient = server.available();
if (!newClient) {
return;
}
Serial.println("new client");
client = newClient;
}*/
// Check if a client has connected
WiFiClient newClient = server.available();
if (newClient) {
Serial.println("new client");
//WiFiClient::stopAllExcept(&newClient);
client = newClient;
}
if (!client || !client.connected() || !client.available()) {
delay(1);
return;
}
int bytesRead = client.read(buf, BUFSIZE);
Serial.print("Bytes read: ");
Serial.println(bytesRead);
byte* nextCmd;
byte* currentCmd;
nextCmd = buf;
while(parseCommand(nextCmd, ¤tCmd, &nextCmd)) {
printCommand(currentCmd);
runCommand(currentCmd);
}
client.flush();
// Send the response to the client
client.print("ok\r\n");
}