You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
3.9 KiB
161 lines
3.9 KiB
#include <ESP8266WiFi.h> |
|
#include <algorithm> |
|
#include <ESP8266WiFiMulti.h> |
|
|
|
#ifndef STASSID |
|
#define STASSID "esp8266" //WIFI名 |
|
#define STAPSK "12345678" //WIFI密码 |
|
#endif |
|
|
|
#define BAUD_SERIAL 115200 // 串口波特率115200 |
|
|
|
#define RXBUFFERSIZE 1024 //设置串口接收的字节大小 |
|
|
|
//最多的连接个数 |
|
#define MAX_SRV_CLIENTS 4 |
|
|
|
const char* ssid = STASSID; //wifi名字 |
|
const char* password = STAPSK;//wifi密码 |
|
|
|
const int port = 30; //服务器端口30 |
|
|
|
WiFiServer server(port); //创建服务器对象 |
|
WiFiClient serverClients[MAX_SRV_CLIENTS]; //连接对象数组 |
|
|
|
ESP8266WiFiMulti WiFiMulti; |
|
|
|
struct UserInfo{ |
|
char humi_value; |
|
char temp_value; |
|
char temp_pig; |
|
char temp_threshold; |
|
char humi_threshold; |
|
char cleanFlag; |
|
char feedFlag; |
|
}; |
|
|
|
int getSum(char *data, int length){ |
|
int sum = 0, i = 0; |
|
for(i = 0; i < length; i++){ |
|
sum += data[i]; |
|
} |
|
return sum; |
|
} |
|
|
|
char deserialization(struct UserInfo *usr, char* data, int length){ |
|
if(length != 13) return -1; |
|
if(data[length - 1] != 0x0D || data[0] != 0xFD) return -1; |
|
if(getSum(data, length - 3) != (data[length - 3] * 256 + data[length - 2])) return -1; |
|
usr->temp_value = data[3]; |
|
usr->humi_value = data[4]; |
|
usr->temp_pig = data[5]; |
|
usr->temp_threshold = data[6]; |
|
usr->humi_threshold = data[7]; |
|
usr->cleanFlag = data[8]; |
|
usr->feedFlag = data[9]; |
|
return 0; |
|
} |
|
|
|
char serialize(struct UserInfo *usr, char* data, int length){ |
|
int sum = 0; |
|
if(length != 13) return -1; |
|
data[0] = 0xFD; |
|
data[1] = 0x00; |
|
data[2] = 0x01; |
|
data[3] = usr->temp_value; |
|
data[4] = usr->humi_value; |
|
data[5] = usr->temp_pig; |
|
data[6] = usr->temp_threshold; |
|
data[7] = usr->humi_threshold; |
|
data[8] = usr->cleanFlag; |
|
data[9] = usr->feedFlag; |
|
sum = getSum(data, 10); |
|
data[10] = sum >> 8; |
|
data[11] = sum & 0x00FF; |
|
data[12] = 0x0D; |
|
return 0; |
|
} |
|
|
|
// 初始化函数 |
|
void setup() { |
|
Serial.begin(BAUD_SERIAL); // 串口初始化 |
|
Serial.setRxBufferSize(RXBUFFERSIZE); //串口接收的初始化 |
|
|
|
pinMode(LED_BUILTIN, OUTPUT); //配置LED灯 |
|
digitalWrite(LED_BUILTIN, HIGH); //LED置高 |
|
|
|
//设置客户端模式 |
|
WiFi.mode(WIFI_STA); |
|
//连接网络 |
|
WiFiMulti.addAP(ssid, password); |
|
Serial.println(ssid); |
|
Serial.println(password); |
|
// 等待连接WIFI |
|
while (WiFiMulti.run() != WL_CONNECTED) { |
|
Serial.print("."); |
|
delay(500); |
|
} |
|
Serial.println(""); |
|
Serial.println("WiFi connected"); |
|
Serial.println("IP address: "); |
|
Serial.println(WiFi.localIP()); //获取自身IP地址 |
|
delay(500); |
|
//启动服务器 |
|
server.begin(); |
|
server.setNoDelay(true); //关闭小包合并包模式并去除延时 |
|
} |
|
|
|
// loop函数 |
|
void loop() { |
|
//如果新的连接请求 |
|
if (server.hasClient()) |
|
{ |
|
int i = 0; |
|
for (i = 0; i < MAX_SRV_CLIENTS; i++) |
|
{ |
|
if (!serverClients[i]) //遍历连接 |
|
{ |
|
serverClients[i] = server.available();//建立新的连接 |
|
Serial.print("New:"); |
|
Serial.print(i+1); |
|
break; |
|
} |
|
} |
|
//如果连接数量已经达到最大 |
|
if (i == MAX_SRV_CLIENTS) |
|
{ |
|
WiFiClient client = server.available(); |
|
client.stop(); |
|
Serial.print("stop\n"); |
|
} |
|
} |
|
//检测客户端发送上来的信息 |
|
for (int i = 0; i < MAX_SRV_CLIENTS; i++) |
|
{ |
|
//如果该客户端处于连接状态 |
|
if(serverClients[i] && serverClients[i].connected()) |
|
{ |
|
while(serverClients[i].available()) |
|
{ |
|
uint8_t c = serverClients[i].read(); |
|
Serial.write(c); |
|
} |
|
} |
|
} |
|
// 如果串口接收到数据 |
|
if(Serial.available()) |
|
{ |
|
delay(10); |
|
size_t counti = Serial.available(); |
|
uint8_t sbuf[counti]; |
|
Serial.readBytes(sbuf, counti); |
|
for (int i = 0; i < MAX_SRV_CLIENTS; i++) |
|
{ |
|
// 如果客户端还处于连接状态 |
|
if(serverClients[i] && serverClients[i].connected()) |
|
{ |
|
serverClients[i].write(sbuf,counti); //将串口接收到的数据发送到各个客户端 |
|
} |
|
} |
|
} |
|
}
|
|
|