#include <Arduino.h>
#include "WiFi.h"
#include "WiFiMulti.h"
// put function declarations here:
WiFiMulti WiFiMulti;
WiFiClient client;
WiFiGenericClass WIFI_CONFIG;
#define ssid "ESP32_AP"//连接的WiFi名称
#define password "syscan002" //连接的WiFi密码
unsigned char ipdata,upCheck1 = 0,upCheck2 = 0;
unsigned long gu32CheckErrorNum=0,gu32data_Recv_Number=0,gu32data_send_Number=0;
uint8_t test[6]={0x12,0x34,0x56,0x78,0x0c,0x0b};//6 个字节的测试数据
char pRx_Buf[10]={0};
char u8cmd;
int value = 0;
WiFiServer ServerPort(12329);
IPAddress LocalIP(192,168,4,22);
IPAddress Gateway(192,168,4,22);
IPAddress SubNet(255,255,255,0);
void LED_RUN();
/// @brief
void setup() {
// put your setup code here, to run once:
pinMode(16, OUTPUT);
Serial.begin(115200); //启动串口通信并设置波特率为115200
WiFi.mode(WIFI_AP); // 设置为AP模式
WiFi.setSleep(false);//关闭休眠模式
WIFI_CONFIG.setTxPower(WIFI_POWER_19_5dBm);
WiFi.softAPConfig(LocalIP,Gateway,SubNet);
delay(1000);
WiFi.softAP(ssid); //设置AP模式热点的名称和密码,密码可不填则发出的热点为无密码热点
//停止小包合并发送
ServerPort.setNoDelay(true);
Serial.print("\n wifi name:");
Serial.print(ssid); //串口输出ESP32建立的wifi的名称
Serial.print("\n wifi password:");
Serial.print(password);
Serial.print("\n wifi IP:");
Serial.print(WiFi.softAPIP()); //串口输出热点的IP地址
Serial.print("\n wifi serverport:");
Serial.print("12329");
// 启动TCP服务器
ServerPort.begin();
}
void loop() {
// put your main code here, to run repeatedly:
WiFiClient client =ServerPort.available(); // 监听客户接入
if (client) { // if you get a client,
Serial.print("New Client."); // print a message out the serial port
while(client.connected())
{
// 检查是否有数据可供读取
if(client.available()!=0)//打印报错数量和收发总数据量
{
// 读取客户端发送的数据
client.readBytes(pRx_Buf,10);
if(pRx_Buf[0]==0x62)
{
client.print("the ErrorNum is:");
client.print(gu32CheckErrorNum);
client.print("the Sendtotal is:");
client.print(gu32data_send_Number);
client.print("the Recvtotal is:");
client.print(gu32data_Recv_Number);
}
else
{
upCheck2 = 0;
for(ipdata=0;ipdata<9;ipdata++)
{
upCheck2 ^= pRx_Buf[ipdata];
}
if(upCheck2!=pRx_Buf[9])
{
gu32CheckErrorNum++;
}
gu32data_Recv_Number++;
// Serial.write(pRx_Buf,10);//打印客户端发来的数据
client.write(pRx_Buf,10);// 发送响应到客户端
gu32data_send_Number++;
}
}
}
}
//LED_RUN();//code run status
}
使用上代码连续收发数据两三天就会导致程序崩溃了出现了一堆报警信息。不知道哪里导致错误
报错:Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x4008c180 PS : 0x00060e30 A0 : 0x800d3d75 A1 : 0x3ffcb550
A2 : 0x06050403 A3 : 0x3ffcb59c A4 : 0xffffffff A5 : 0x00000000
A6 : 0x3ffcb680 A7 : 0x00000000 A8 : 0x8008c16b A9 : 0x3ffcb530
A10 : 0x00000002 A11 : 0x3ffd7418 A12 : 0x00060a20 A13 : 0x00060a23
A14 : 0x00060a23 A15 : 0x00000001 SAR : 0x00000016 EXCCAUSE: 0x0000001c
EXCVADDR: 0x06050403 LBEG : 0x400891f4 LEND : 0x4008920a LCOUNT : 0x00000000
Backtrace: 0x4008c17d:0x3ffcb550 0x400d3d72:0x3ffcb590 0x400d3de1:0x3ffcb5c0 0x40152f31:0x3ffcb680 0x40153004:0x3ffcb6d0
ELF file SHA256: eb6a928b52068462
网上是这样解释的当应用程序尝试读取或写入无效的内存位置时,会发生此 CPU 异常. 写入/读取的地址可在寄存器转储中的 EXCVADDR 寄存器中找到. 如果此地址为零,则通常表示应用程序尝试取消引用 NULL 指针. 如果此地址接近于零,则通常意味着应用程序尝试访问结构的成员,但指向该结构的指针为 NULL. 如果该地址是别的(垃圾值,不在 0x3fxxxxxx - 0x6xxxxxxx 范围内),则可能意味着用于访问数据的指针未初始化或已损坏.