-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheBlackPearl.ino
477 lines (394 loc) · 11.4 KB
/
TheBlackPearl.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#define FASTLED_INTERNAL
#include <ESP8266WebServer.h>
#include <FS.h>
#include <FastLED.h>
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <WiFiManager.h>
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#define DEBUG 1
#define COLOR_ORDER RGB //GRB //灯的排列顺序,灯带 GRB,灯珠 RGB
#define LED_COUNT 30 //灯泡数量
#define LED_DT 4 //D2 PIN 接线位置,Arduino 位置4对应D2
CRGBArray<LED_COUNT> leds;
const char* ssid = "Xiloer'home"; //"Robin+";
const char* password = "Fhf838408";
IPAddress Ip(192,168,2,188);
IPAddress Gateway(192,168,2,1);
IPAddress Subnet(255,255,255,0);
uint8_t bright = 100; // 灯泡亮度 (0 - 255) 255 最大
ESP8266WebServer server(80);
//设置DFPlayer
SoftwareSerial mySoftwareSerial(D7,D5); // RX-D7, TX-D5
DFRobotDFPlayerMini soundPlayer;
void printDetail(uint8_t type, int value);
uint8_t rainbowhue = 200;
boolean rainbow = true;
uint8_t seaSound = 5; //soundPlayer.loopFolder(seaSound); 大海音效再05文件中, 可以循环播放
uint8_t canonSound = 5; //ADVERT文件夹中 0005 文件,播放方式: soundPlayer.advertise(canonSound);
uint8_t cickSound = 3; //ADVERT文件夹中 0003 文件,播放方式: soundPlayer.advertise(cickSound);
uint8_t resetTimeout = 6000;
uint8_t resetCount = 0;
#define WIFI_RESET D6
void setup() {
Serial.begin(9600);
Serial.print("OK");
//debugPin();
pinMode(LED_BUILTIN, OUTPUT);
//pinMode(D4, OUTPUT); //设置Blink灯
//pinMode(D1, OUTPUT); //设置Blink灯
//pinMode(WIFI_RESET, INPUT);
//wifiManager.resetSettings();
//setupWifi();
//initWifiServer();
//initDfPlayer();
initLights();
//setupWifi();
//启动文件系统功能
SPIFFS.begin();
}
void loop() {
// Blik 蓝灯,表示主循环在工作
digitalWrite(LED_BUILTIN, ! digitalRead(LED_BUILTIN)); // 主板 Blink灯
// handleResetWifiButton();
//if (soundPlayer.available()) {
// printDetail(soundPlayer.readType(), soundPlayer.read());
//}
//server.handleClient();
if(rainbow){
rainbowhue++;
if(rainbowhue==255){
rainbowhue=0;
}
leds.fill_rainbow(rainbowhue);
}
LEDS.show();
delay(20);
}
void handleResetWifiButton(){
if (digitalRead(WIFI_RESET) == HIGH) {
digitalWrite(D1, ! digitalRead(D1)); // 自己添加的 LED 灯
resetCount+=20;
if(resetCount > resetTimeout){
resetCount = 0;
setupWifi();
}
} else {
resetCount = 0;
}
}
void setupWifi(){
WiFiManager wifiManager;
wifiManager.resetSettings();
wifiManager.autoConnect("TheBlackPearlAP");
}
void debugPin(){
Serial.println();
Serial.print(F("D1:"));
Serial.println(D1);
Serial.print(F("D2:"));
Serial.println(D2);
Serial.print(F("D3:"));
Serial.println(D3);
Serial.print(F("D4:"));
Serial.println(D4);
Serial.print(F("D5:"));
Serial.println(D5);
Serial.print(F("D6:"));
Serial.println(D6);
Serial.print(F("D7:"));
Serial.println(D7);
Serial.print(F("D8:"));
Serial.println(D8);
}
void initDfPlayer(){
mySoftwareSerial.begin(9600);
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
while (!soundPlayer.begin(mySoftwareSerial)) {
Serial.print(F("."));
//digitalWrite(D4, ! digitalRead(D4));
digitalWrite(LED_BUILTIN, ! digitalRead(LED_BUILTIN));
soundPlayer.reset();
delay(1000); // Code to compatible with ESP8266 watch dog.
}
Serial.println(F("DFPlayer Mini online."));
soundPlayer.volume(10); //Set volume value. From 0 to 30
soundPlayer.enableLoop();
soundPlayer.loopFolder(seaSound);
soundPlayer.pause();
}
void initLights(){
LEDS.setBrightness(bright); //设置亮度
LEDS.addLeds<WS2812B, LED_DT, COLOR_ORDER>(leds, LED_COUNT); // настройки для вашей ленты (ленты на WS2811, WS2812, WS2812B)
leds.fill_solid(CRGB::White);
LEDS.show();
digitalWrite(LED_BUILTIN, ! digitalRead(LED_BUILTIN));
delay(200);
leds.fill_solid(CRGB::Black);
LEDS.show();
Serial.println("initLights()");
}
void initWifiServer(){
WiFi.config(Ip, Gateway, Subnet);
WiFi.begin(ssid, password);
Serial.print("WIFI");
while (WiFi.status() != WL_CONNECTED){
//digitalWrite(D1, ! digitalRead(D1));
digitalWrite(LED_BUILTIN, ! digitalRead(LED_BUILTIN));
delay(500);
Serial.print(".");
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.onNotFound([](){
if(!handleFileRead(server.uri()))
server.send(404, "text/plain", "FileNotFound");
});
//获取当前全部灯的状态
server.on("/lights", handleLights);
//处理一盏灯
server.on("/light", handleLight);
//处理一个房间的全部灯
server.on("/room", handleRoom);
//设置亮度
server.on("/bright", handleBright);
//设置彩虹效果
server.on("/rainbow", handleRainbow);
//设置色温
server.on("/temperature", handleTemperature);
//整体关闭
server.on("/off", handleOff);
//海浪声音开关
server.on("/sound/play", handlePlay);
//海浪声音开关
server.on("/sound/pause", handlePause);
//音量增加
server.on("/volume/up", handleVolumeUp);
//音量减少
server.on("/volume/down", handleVolumeDown);
//开炮
server.on("/sound/open-fire", handleOpenFire);
server.begin();
}
void handleOpenFire(){
soundPlayer.advertise(canonSound);
}
void handleVolumeUp(){
soundPlayer.volumeUp();
}
void handleVolumeDown(){
soundPlayer.volumeDown();
}
/**
* 播放/关闭大海的音效
*/
void handlePlay(){
soundPlayer.play();
}
void handlePause(){
soundPlayer.pause();
}
void handleOff(){
CRGB color = CRGB::Black;
//填充为黑色
leds.fill_solid(color);
//关闭彩虹效果
rainbow = false;
soundPlayer.advertise(cickSound);
String json = "{\"success\":true}";
server.send(200, "application/json", json);
}
void handleTemperature(){
int k = server.arg("temperature").toInt();
Serial.println(k);
CRGB color;
if(k==2600){
color = CRGB(ColorTemperature::Candle);
}
if(k==6000){
//color = CRGB(ColorTemperature::DirectSunlight);
color = CRGB::White;
}
//color = ColorTemperature::Tungsten40W;
leds.fill_solid(color);
rainbow = false;
soundPlayer.advertise(cickSound);
String json = "{\"success\":true}";
server.send(200, "application/json", json);
}
void handleBright(){
bright = server.arg("bright").toInt();
LEDS.setBrightness(bright);
String json = "{\"success\":true}";
soundPlayer.advertise(cickSound);
server.send(200, "application/json", json);
}
void handleRainbow(){
uint8_t initialhue = (uint8_t)(server.arg("initialhue").toInt());
rainbowhue = initialhue;
rainbow = true;
String json = "{\"success\":true}";
soundPlayer.advertise(cickSound);
server.send(200, "application/json", json);
}
void handleLights(){
//在栈中,创建JSON对象
StaticJsonDocument<2048> doc;
//转换为JSON数组
JsonArray list = doc.to<JsonArray>();
//将每个灯的状态添加到JSON中
for(int i=0; i<leds.size(); i++){
JsonObject light = list.createNestedObject();
light["id"]=i;
long r = leds[i].r;
long g = leds[i].g;
long b = leds[i].b;
long rgb = (r<<16)|(g<<8)|b;
String hex = String(rgb, HEX);
light["power"] = rgb==0 ? false : true;
light["color"] = hex;
}
//soundPlayer.reset();
//soundPlayer.advertise(cickSound);
//将JSON对象序列化为字符串
String json;
serializeJson(doc, json);
server.send(200, "application/json", json);
}
void handleRoom() {
boolean power = server.arg("power") == "true" ? true : false;
String hex = server.arg("color");
long rgb = (long) strtol( &hex[1], NULL, 16 );
rainbow = false;
String param=server.arg("lights");
int index = 0;
int startIndex = 0;
int id;
while((index = param.indexOf(',', startIndex)) !=-1){
id = param.substring(startIndex, index).toInt();
if(power){
leds[id].setColorCode(rgb);
}else{
leds[id].setRGB(0,0,0);
}
startIndex = index+1;
}
id = param.substring(startIndex).toInt();
if(power){
leds[id].setColorCode(rgb);
}else{
leds[id].setRGB(0,0,0);
}
soundPlayer.advertise(cickSound);
StaticJsonDocument<200> doc;
doc["power"] = power;
doc["success"] = true;
String json;
serializeJson(doc, json);
server.send(200, "application/json", json);
}
void handleLight() {
boolean power = server.arg("power") == "true" ? true : false;
String hex = server.arg("color");
long rgb = (long) strtol( &hex[1], NULL, 16 );
rainbow = false;
int id=server.arg("id").toInt();
if(power){
leds[id].setColorCode(rgb);
}else{
leds[id].setRGB(0,0,0);
}
soundPlayer.advertise(cickSound);
StaticJsonDocument<200> doc; // <- 200 bytes in the heap
doc["power"] = power;
doc["success"] = true;
String json;
serializeJson(doc, json);
server.send(200, "application/json", json);
}
bool handleFileRead(String path){
#ifdef DEBUG
Serial.println("handleFileRead: " + path);
#endif
if(path.endsWith("/")) path += "index.html";
if(SPIFFS.exists(path)){
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, getContentType(path));
file.close();
return true;
}
return false;
}
String getContentType(String filename){
if(server.hasArg("download")) return "application/octet-stream";
else if(filename.endsWith(".htm")) return "text/html";
else if(filename.endsWith(".html")) return "text/html";
else if(filename.endsWith(".css")) return "text/css";
else if(filename.endsWith(".js")) return "application/javascript";
else if(filename.endsWith(".png")) return "image/png";
else if(filename.endsWith(".gif")) return "image/gif";
else if(filename.endsWith(".jpg")) return "image/jpeg";
else if(filename.endsWith(".ico")) return "image/x-icon";
else if(filename.endsWith(".xml")) return "text/xml";
else if(filename.endsWith(".pdf")) return "application/x-pdf";
else if(filename.endsWith(".zip")) return "application/x-zip";
else if(filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
void printDetail(uint8_t type, int value) {
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}