-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathArduino_FastLED.ino
More file actions
158 lines (148 loc) · 4.23 KB
/
Copy pathArduino_FastLED.ino
File metadata and controls
158 lines (148 loc) · 4.23 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
/*
实例视频 网址
程序功能:
1.实现一键三联动画效果
2.对接微信小程序实现对WS2812灯带的控制
作者:亮子力
时间:2020年4月6日
未经作者授权,禁止转载
*/
//#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WebSocketsServer.h>
#include <ESP8266mDNS.h>
//#include <Hash.h>
uint8_t command = 0;
WebSocketsServer webSocket = WebSocketsServer(80);
char service[] = "websocket";
char proto[] = "tcp";
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
switch (type) {
case WStype_DISCONNECTED: //如果连接失败
Serial.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED: //如果连接成功
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
webSocket.sendTXT(num, "连接成功"); //发送:返回信息到客户端
}
break;
case WStype_TEXT: //如果收到字符串
Serial.printf("[%u] get Text: %s\n", num, payload);
if (strcmp((char*)payload, "openlight") == 0)
{
//Serial.printf("openlight");
openlight();
command = 1;
}
if (strcmp((char*)payload, "closelight") == 0)
{
//Serial.printf("closelight");
closelight();
command = 0;
}
if (strcmp((char*)payload, "nightlight") == 0)
{
//nightlight();
command = 3;
}
if (strcmp((char*)payload, "colorslight") == 0)
{
//colorslight();
command = 4;
}
if (strcmp((char*)payload, "breathinglight") == 0)
{
//colorslight();
command = 5;
}
if (strcmp((char*)payload, "randomlight") == 0)
{
//colorslight();
command = 6;
}
//webSocket.sendTXT(num, payload);//发送信息到客户端
//webSocket.broadcastTXT(payload); // 发送数据到所有连接的客户端
break;
case WStype_BIN: //收到二进制
Serial.printf("[%u] get binary length: %u\n", num, length);
hexdump(payload, length);
webSocket.sendBIN(num, payload, length); //发送信息到客户端
break;
}
}
void setup() {
Serial.begin(115200);
smartConfig();//这里调用一键配网函数,替换上面的普通配网
Serial.setDebugOutput(true);
Serial.println();
for (uint8_t t = 4; t > 0; t--) { //等待t秒之后
Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
Serial.flush(); //等待串口数据发送结束
delay(1000);
}
if (MDNS.begin("esp8266")) { // void setup下编写
Serial.println("MDNS responder started");
}
MDNS.addService(service, proto, 80);
webSocket.begin();
webSocket.onEvent(webSocketEvent);
FastLEDsetup();
}
void loop() {
MDNS.update(); // void loop下编写
webSocket.loop();
while (Serial.available() > 0)
{
String data;
data = Serial.readString();
delay(5);
webSocket.broadcastTXT(data);// 发送数据到所有连接的客户端
Serial.println(data);
}
// BZC(BZCshow);//币赞藏渐变
FastLEDloop(command);//
}
//==========将下列代码添加到需要一键配网项目代码的最后==========
void smartConfig()
{
WiFi.begin();
for (int i = 0; i < 5; i++)
{
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("AutoConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
//WiFi.printDiag(Serial); //打印关键的Wi-Fi诊断信息,信息比较多
break;
}
else
{
Serial.print("AutoConfig Waiting......");
Serial.println(WiFi.status());
delay(1000);
}
}
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("AutoConfig Faild!" );
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig");
WiFi.beginSmartConfig();
while (1)
{
Serial.print(".");
if (WiFi.smartConfigDone())
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
WiFi.setAutoConnect(true); // 设置自动连接
break;
}
delay(1000);
}
}
}