esp8266运行几天后就死机,想问问各位大佬应该怎么解决
下面贴上源码,#include "config.h"
#include <ESP8266WiFi.h>
WiFiClient client;
String num = HOSTNAME;
const char* ssid = WiFiSSID;
const char* password = WiFiPSW;
const char* host = ServerIP;
const uint16_t port = PORT;
unsigned long lastOutActivity;
unsigned long lastInActivity;
byte indata = "";
byte outdata = "";
bool mynum(byte numbyte[]) {
int i;
for (i = 0; i < numlen; i++)
{
if (numbyte != (byte)num)
return false;
}
if (i == numlen)
return true;
}
void(* resetFunc) (void) = 0;
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.hostname(HOSTNAME);
WiFi.begin(ssid, password);
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
client.connect(host, port);
client.setNoDelay(true);
lastInActivity = lastOutActivity = millis();
}
void loop() {
if (client.connected()==true) {
unsigned long t = millis();
if ((t - lastInActivity > 60000) || (t - lastOutActivity > 60000))
{
t = lastInActivity = lastOutActivity = millis();
client.print(num);
}
while (Serial.available() > 0)
{
if (Serial.readBytes(indata, readlen) == readlen && indata == 0x47 && indata == 0x45 && indata == 0x54)
{
client.write(indata, readlen);
while (Serial.read() >= 0);
memset(indata, 0, readlen * sizeof(byte));
}
else
{
while (Serial.read() >= 0);
memset(indata, 0, readlen * sizeof(byte));
}
}
while (client.available() > 0)
{
if (client.read(outdata, readlen) == readlen && outdata == 0x4C && outdata == 0x59 && outdata == 0x4A && mynum(outdata))
{
Serial.write(outdata, readlen);
while (client.read() >= 0);
lastOutActivity = millis();
memset(outdata, 0, readlen * sizeof(byte));
}
else
{
while (client.read() >= 0);
memset(outdata, 0, readlen * sizeof(byte));
}
}
}
else if(client.connected()==false)
{
if (WiFi.status() != WL_CONNECTED)
{
WiFi.reconnect();
delay(500);
}
else if (WiFi.isConnected())
{
int con;
for (con = 0; con < 5;)
{
if (client.connect(host, port) == true)
{
return;
}
else
{
client.stop();
con++;
}
}
if (con >= 5)
{
ESP.reset();
}
}
}
}
业界难题。路由器跑个个把月的也是卡机 1.串口一直连接,可以debug,也许可以捕获到原因
2.软件硬件问题都可能造成死机
3.应该考虑下意外死机后如何重启 往年重启大法 用ESPflashdownloadtool擦除一下flash 做个看门狗 死机自己重启 硬重启 本帖最后由 114144640 于 2021-2-12 19:49 编辑
建议多拿几个esp8266,源码中设定,死机重启期间换下一个esp8266发送,并发送错误数据,同时检测网络:lol
页:
[1]