-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedMatrixRestAPI.ino
More file actions
260 lines (222 loc) · 5.71 KB
/
LedMatrixRestAPI.ino
File metadata and controls
260 lines (222 loc) · 5.71 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
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
//#define FASTLED_ESP8266_RAW_PIN_ORDER
//#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
//#define FASTLED_ESP8266_D1_PIN_ORDER
#include <FastLED.h>
#include <stdio.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#define NUM_LEDS 64
#define DATA_PIN 13
CRGB leds[NUM_LEDS];
#define HTTP_REST_PORT 80
#define WIFI_RETRY_DELAY 500
#define MAX_WIFI_INIT_RETRY 50
struct Led
{
byte numLeds;
byte status;
} led_resource;
const char *wifi_ssid = "[wifi_ssid]";
const char *wifi_passwd = "[wifi_passwd]";
ESP8266WebServer http_rest_server(HTTP_REST_PORT);
void init_led_resource()
{
led_resource.numLeds = NUM_LEDS;
led_resource.status = LOW;
}
int init_wifi()
{
int retries = 0;
Serial.println("Connecting to WiFi AP..........");
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_passwd);
while ((WiFi.status() != WL_CONNECTED) && (retries < MAX_WIFI_INIT_RETRY))
{
retries++;
delay(WIFI_RETRY_DELAY);
Serial.print("#");
}
return WiFi.status();
}
void get_leds()
{
StaticJsonBuffer<200> jsonBuffer;
JsonObject &jsonObj = jsonBuffer.createObject();
char JSONmessageBuffer[200];
if (led_resource.numLeds == 0)
http_rest_server.send(204);
else
{
jsonObj["numLeds"] = led_resource.numLeds;
jsonObj["status"] = led_resource.status;
jsonObj.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
http_rest_server.send(200, "application/json", JSONmessageBuffer);
}
}
void json_to_resource(JsonObject &jsonBody)
{
int numLeds, status;
numLeds = jsonBody["numLeds"];
status = jsonBody["status"];
Serial.println(numLeds);
Serial.println(status);
led_resource.numLeds = numLeds;
led_resource.status = status;
}
void post_leds()
{
StaticJsonBuffer<500> jsonBuffer;
String post_body = http_rest_server.arg("plain");
Serial.println(post_body);
JsonObject &jsonBody = jsonBuffer.parseObject(http_rest_server.arg("plain"));
Serial.print("HTTP Method: ");
Serial.println(http_rest_server.method());
if (!jsonBody.success())
{
Serial.println("error in parsin json body");
http_rest_server.send(400);
}
else
{
json_to_resource(jsonBody);
http_rest_server.sendHeader("Location", "/leds/" + String(led_resource.status));
http_rest_server.send(201);
setFastLED(led_resource.status);
}
}
void setFastLED(byte status)
{
switch (status)
{
case 1:
draw_avaliable();
break;
case 2:
draw_busy();
break;
case 3:
draw_doNotDisturb();
break;
case 4:
draw_away();
break;
default:
clean();
break;
}
}
void clean(){
for(char index = 0; index < NUM_LEDS; index++)
leds[index] = CRGB::Black;
FastLED.show();
}
void draw_avaliable(){
char avaliable[] = {0x3c,0x7e,0xfb,0xf7,0xaf,0xdf,0x7e,0x3c};
int avaliable_color = 0x0A0000;
char count = 0;
char mask = 0x80;
for (char line = 0; line < 8; line++)
{
mask = 0x80;
for (char column = 0; column < 8; column++)
{
if (avaliable[line] & mask)
leds[count++] = avaliable_color;
else
leds[count++] = 0;
mask >>= 1;
}
}
FastLED.show();
}
void draw_doNotDisturb(){
char busy[] = {0x3c,0x7e,0xff,0x81,0x81,0xff,0x7e,0x3c};
int busy_color = 0x000C00;
char count = 0;
char mask = 0x80;
for (char line = 0; line < 8; line++)
{
mask = 0x80;
for (char column = 0; column < 8; column++)
{
if (busy[line] & mask)
leds[count++] = busy_color;
else
leds[count++] = 0;
mask >>= 1;
}
}
FastLED.show();
}
void draw_busy(){
char busy[] = {0x3c,0x7e,0xff,0xff,0xff,0xff,0x7e,0x3c};
int busy_color = 0x000C00;
char count = 0;
char mask = 0x80;
for (char line = 0; line < 8; line++)
{
mask = 0x80;
for (char column = 0; column < 8; column++)
{
if (busy[line] & mask)
leds[count++] = busy_color;
else
leds[count++] = 0;
mask >>= 1;
}
}
FastLED.show();
}
void draw_away(){
char away[] = {0x3c,0x6e,0xef,0xef,0xef,0xf7,0x7e,0x3c};
int away_color = 0x0A0C00;
char count = 0;
char mask = 0x80;
for (char line = 0; line < 8; line++)
{
mask = 0x80;
for (char column = 0; column < 8; column++)
{
if (away[line] & mask)
leds[count++] = away_color;
else
leds[count++] = 0;
mask >>= 1;
}
}
FastLED.show();
}
void config_rest_server_routing()
{
http_rest_server.on("/", HTTP_GET, []() {
http_rest_server.send(200, "text/html",
"Welcome to the ESP8266 REST Web Server");
});
http_rest_server.on("/leds", HTTP_GET, get_leds);
http_rest_server.on("/leds", HTTP_POST, post_leds);
}
void setup(void)
{
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
Serial.begin(115200);
init_led_resource();
if (init_wifi() == WL_CONNECTED)
{
Serial.print("Connected to ");
Serial.print(wifi_ssid);
Serial.print(" --- IP: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.print("Error connecting to: ");
Serial.println(wifi_ssid);
}
config_rest_server_routing();
http_rest_server.begin();
Serial.println("HTTP REST Server Started");
}
void loop(void)
{
http_rest_server.handleClient();
}