|
楼主 |
发表于 2022-5-21 17:40
|
显示全部楼层
- #include <WiFi.h>
- #include "esp_system.h"
- #include <EEPROM.h> //导入Flash库文件
- const int conn_button = 0;
- String ssid;
- String psw;
- uint8_t def_server_name[16] = {0x01, 'f', 'i', 'n', 'g', 'e', 'r', '-', '8', '2', '6', '6', '-', '0', '0', '1'}; //定义设备ID 第一个为server type
- uint8_t def_massage_psw[4] = {0x00, 0x00, 0x00, 0x00}; //报文密码
- #define MAX_SRV_CLIENTS 10 //定义TCP服务器最大允许4个连接数
- #define MAX_MASSAGE_LENGH 200 //定义socket消息最大长度
- #define DEF_MSG_HEAD_LEN 11 //报文头消息长度
- #define DEF_MASSAGE_HEAD {0xF1,0x1F,0xE2,0x2E,0xB6,0x6B,0xA8,0x8A} //报文头
- #define DEF_MASSAGE_COMMAND_GET_SERVER_NAME {0XFF,0XFF} //获取设备ID
- #define DEF_MASSAGE_REG_FINGER {0x01,0x11}
- #define DEF_WIFI_MSG_TYPE_GET_SERVER_NAME 1 //获取设备ID参数
- #define DEF_WIFI_MSG_BEGIN_REG_FINGER 2 //开始录入指纹
- uint8_t wifimessage[MAX_MASSAGE_LENGH];
- //自制看门狗
- hw_timer_t *timer = NULL;
- void IRAM_ATTR resetModule() {
- esp_restart();
- }
- //开启看门狗
- void wdtEnable(int wdtTimeout)
- {
- timer = timerBegin(0, 80, true); //初始化定时器 定时器编号0,分频数 80,是否是累加模式:true
- timerAttachInterrupt(timer, &resetModule, true); //附加回调
- timerAlarmWrite(timer, wdtTimeout * 1000, false); //设置定时器
- timerAlarmEnable(timer); //开启中断
- }
- //喂狗
- void wdtFeed() {
- timerWrite(timer, 0);
- }
- //创建server 端口号是58266
- WiFiServer server(58266);
- WiFiClient serverClients[MAX_SRV_CLIENTS];
- struct config_type
- {
- char stassid[32];//定义配网得到的WIFI名长度(最大32字节)
- char stapsw[64];//定义配网得到的WIFI密码长度(最大64字节)
- };
- config_type config;//声明定义内容
- void saveConfig()//保存函数
- {
- EEPROM.begin(1024);//向系统申请ROM
- //开始写入
- uint8_t *p = (uint8_t*)(&config);
- for (int i = 0; i < sizeof(config); i++)
- {
- EEPROM.write(i, *(p + i)); //在闪存内模拟写入
- }
- EEPROM.commit();//执行写入ROM
- }
- void loadConfig()//读取函数
- {
- EEPROM.begin(1024);
- uint8_t *p = (uint8_t*)(&config);
- for (int i = 0; i < sizeof(config); i++)
- {
- *(p + i) = EEPROM.read(i);
- }
- EEPROM.commit();
- ssid = config.stassid;
- psw = config.stapsw;
- }
- void smartConfig()//配网函数
- {
- // 等待配网
- WiFi.beginSmartConfig();
- while (1)
- {
- // wdtFeed();//喂狗
- digitalWrite(2, HIGH);
- delay(200);
- digitalWrite(2, LOW); //加个LED快闪,确认配网是否成功!成功就不闪了。
- delay(200);
- if (WiFi.smartConfigDone())
- {
- strcpy(config.stassid, WiFi.SSID().c_str()); //名称复制
- strcpy(config.stapsw, WiFi.psk().c_str()); //密码复制
- saveConfig();//调用保存函数
- WiFi.setAutoConnect(true); // 设置自动连接
- break;
- }
- //启动server
- server.begin();
- //关闭小包合并包功能,不会延时发送数据
- server.setNoDelay(true);
- }
- }
- //数据和校验方法
- uint8_t FP_protocol_get_checksum(uint8_t *data, int len)
- {
- uint8_t sum = 0;
- for (int i = 0; i < len; i++) {
- sum = sum + data[i];
- }
- return (~sum) + 1;
- }
- int check_message_head(uint8_t *data)
- {
- uint8_t msg_head[8] = DEF_MASSAGE_HEAD;
- int i;
- for (i = 0; i < 8; i++)
- {
- if (msg_head[i] != data[i])
- return 0;
- }
- if (data[DEF_MSG_HEAD_LEN - 1] != FP_protocol_get_checksum(data, DEF_MSG_HEAD_LEN - 1)) return 0;
- uint8_t uint_len[] = {data[9], data[10]};
- uint8_t *l_p = uint_len;
- unsigned short *plen = (unsigned short *)l_p;
- int ilen = (int) * plen;
- return ilen;
- }
- bool check_message_content(uint8_t *data, int len)
- {
- int i;
- for(i=0;i<4;i++)
- {
- if (def_massage_psw[i] != data[i])
- return false;
- }
- int lenc = len - 1;
- uint8_t sum_i = FP_protocol_get_checksum(data, lenc);
- return data[lenc] == sum_i;
- }
- int get_msg_command(uint8_t *command_data)
- {
- uint8_t comd_type[2] = DEF_MASSAGE_COMMAND_GET_SERVER_NAME;
- if (command_data[DEF_MSG_HEAD_LEN + 4] == comd_type[0] && command_data[DEF_MSG_HEAD_LEN + 5] == comd_type[1])
- return DEF_WIFI_MSG_TYPE_GET_SERVER_NAME;
- /*
- uint8_t temp1[2] = DEF_MASSAGE_REG_FINGER;
- memcpy (comd_type, temp1, 2);
- if (command_data[DEF_MSG_HEAD_LEN + 4] == comd_type[0] && command_data[DEF_MSG_HEAD_LEN + 5] == comd_type[1])
- return DEF_WIFI_MSG_BEGIN_REG_FINGER;
- */
- return 0;
- }
- void wifi_message_get_server_name( WiFiClient *client)
- {
- uint8_t msg_head[8] = DEF_MASSAGE_HEAD;
- uint8_t msg_cmd[2] = DEF_MASSAGE_COMMAND_GET_SERVER_NAME;
- uint16_t content_len_short = sizeof(def_server_name) / sizeof(uint8_t);
- content_len_short = 4 + 2 + content_len_short + 1;
- uint8_t content_len[2];
- content_len[0] = (uint8_t)(content_len_short >> 8);
- content_len[1] = (uint8_t)(content_len_short);
- for (int i = 0; i < 8; i++)
- wifimessage[i] = msg_head[i];
- for (int i = 0; i < 2; i++)
- wifimessage[i + 8] = content_len[i];
- wifimessage[10] = FP_protocol_get_checksum(wifimessage, 10);
- for (int i = 0; i < 4; i++)
- wifimessage[DEF_MSG_HEAD_LEN + i] = def_massage_psw[i];
- for (int i = 0; i < 2; i++)
- wifimessage[i + 15] = msg_cmd[i];
- for (int i = 0; i < content_len_short; i++)
- wifimessage[i + 17] = def_server_name[i];
- wifimessage[DEF_MSG_HEAD_LEN + content_len_short - 1] = FP_protocol_get_checksum(&wifimessage[DEF_MSG_HEAD_LEN], content_len_short - 1);
- if (*client && client->connected())
- client->write(wifimessage, DEF_MSG_HEAD_LEN + content_len_short);
- }
- void wifi_message_analyse(uint8_t *data, int lenght, WiFiClient *client)
- {
- // wdtFeed();//喂狗
- int i;
- for(i=0;i<lenght;i++)
- {
- Serial.write(data[i]);
- }
-
- int msg_len1 = check_message_head(data);
- if ( msg_len1 == 0 ) return;
- Serial.write(msg_len1);
- uint8_t msg_data[msg_len1];
-
- for(int j=0;j<msg_len1;j++){
- Serial.write( data[11+j]);
- Serial.write(j);
- }
-
- if (!check_message_content(msg_data, msg_len1)) return;
- Serial.write(0x99);
- int wifi_msg_type = get_msg_command(data);
- if (wifi_msg_type == DEF_WIFI_MSG_TYPE_GET_SERVER_NAME)
- {
- wifi_message_get_server_name(client);
- return;
- }
- /*
- if (wifi_msg_type == DEF_WIFI_MSG_BEGIN_REG_FINGER)
- {
- wifi_message_reg_finger(data, lenght, client);
- return;
- }
- */
- }
- void setup() {
- digitalWrite(2, HIGH);
- delay(1000);
- digitalWrite(2, LOW); //重启或者开机亮蓝灯1秒
- Serial.begin(57600);
- WiFi.mode(WIFI_STA);
- delay(500);
- // wdtEnable(10000);//设定看门狗
- loadConfig();//读取ROM是否包含密码
- //判断ROM是否有密码
- if (ssid != 0 && psw != 0) {
- WiFi.begin(ssid.c_str(), psw.c_str()); //如果有密码则自动连接
- while (WiFi.status() != WL_CONNECTED) {
- if (digitalRead(0) == LOW) {
- smartConfig();//如果配网按钮被按下则停止当前连接开始配网
- break;//跳出所有循环进入主程序
- }
- digitalWrite(2, HIGH);
- delay(1000);
- digitalWrite(2, LOW); //加个LED慢闪,确认联网是否成功!成功就不闪了。
- delay(1000);
- }
- //启动server
- server.begin();
- //关闭小包合并包功能,不会延时发送数据
- server.setNoDelay(true);
- } else {
- smartConfig();//如果ROM没有密码则自动进入配网模式
- }
- }
- void loop() {
- // put your main code here, to run repeatedly:
- // wdtFeed();//先喂狗释放资源
- if (digitalRead(conn_button) == 0) delay(2000);
- if (digitalRead(conn_button) == 0) Serial.println("smartconfig will begin.");
- if (digitalRead(conn_button) == 0) smartConfig(); //如果配网按钮被按下则停止所有任务开始重新配网
- //检测是否有新的client请求进来
- // wdtFeed();//再喂狗释放资源
- uint8_t i;
- if (server.hasClient()) {
- for (i = 0; i < MAX_SRV_CLIENTS; i++) {
- //释放旧无效或者断开的client
- if (!serverClients[i] || !serverClients[i].connected()) {
- if (serverClients[i]) {
- serverClients[i].stop();
- }
- //分配最新的client
- serverClients[i] = server.available();
- break;
- }
- }
- //当达到最大连接数 无法释放无效的client,需要拒绝连接
- if (i == MAX_SRV_CLIENTS) {
- WiFiClient serverClient = server.available();
- serverClient.stop();
- }
- }
- //WIFI通信
- for (i = 0; i < MAX_SRV_CLIENTS; i++) {
- if (serverClients[i] && serverClients[i].connected()) {
- if (serverClients[i].available()) {
- int j = 0;
- uint8_t client_msg[MAX_MASSAGE_LENGH];
- while (serverClients[i].available()) {
- //发送到串口调试器
- client_msg[j] = serverClients[i].read();
- j++;
- }
- wifi_message_analyse(client_msg, j, &serverClients[i]);
- }
- }
- }
- //串口通信
- if (Serial.available()) {
- //把串口调试器发过来的数据 发送给client
- size_t len = Serial.available();
- uint8_t sbuf[len];
- Serial.readBytes(sbuf, len);
- for (i = 0; i < MAX_SRV_CLIENTS; i++) {
- if (serverClients[i] && serverClients[i].connected()) {
- serverClients[i].write(sbuf, len);
- delay(1);
- }
- }
- }
- }
复制代码 能否帮我看下整体的代码,我也挺奇怪的,我是通过tcp发送过来的报文。感觉这个问题出现的挺突然的,之前运行的好好的,突然就这样了。 |
|