使用esptool 重启 开发板
spooking 归属分类: 开发运维 评论数: 0 个

之所以会有这篇博文,是因为用esp32模块实现了一个温度/湿度实时上传的小功能,但是 不知道是买的开发板不行,还是自己的代码有bug,运行3-5天,板子就死机了,需要重启才能继续工作,💦

没找到具体原因,好在是这个小东西是通过usb链接在电脑上供电的。那就定时重启呗。

python -m esptool -p COM3 -c esp32 -b 11520 -a hard_reset run

怎么知道它的程序卡死没?使用串口就可以了。

运行串口终端,配置中确认的串口:波特率 = 115200(如有需要,请更改为使用芯片的默认波特率),数据位 = 8,停止位 = 1,奇偶校验 = N,流控制 = XON/XOFF,编码选utf-8.

附上 监控脚本(nodejs)

const {SerialPort} = require('serialport');
const { ReadlineParser } = require('@serialport/parser-readline')
const { spawn } = require('child_process');

// 配置串口参数
const portConfig = {
  path: 'COM3', // Linux/macOS 示例路径,Windows 可能是 COM3 等
  baudRate: 115200,
  dataBits: 8,
  parity: 'none',
  stopBits: 1,
  //rtscts: true, // 禁用硬件流控制
  xon: true,     // 启用 XON/XOFF 软件流控制
  xoff: true,
  xany: false,
  encoding: 'utf8'
};

function time(dstr) {
  if (dstr) {
    return new Date(dstr.replace(' ','T')).getTime();
  } else {
    return new Date().getTime();
  }
}

function time2str(format = 'YYYY-MM-DD HH:mm:ss', timestamp) {
  if (typeof format != 'string') format = 'YYYY-MM-DD HH:mm:ss';
  timestamp = timestamp || time();
  const date = new Date(timestamp);

  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的
  const day = date.getDate().toString().padStart(2, '0');
  const hours = date.getHours().toString().padStart(2, '0');
  const minutes = date.getMinutes().toString().padStart(2, '0');
  const seconds = date.getSeconds().toString().padStart(2, '0');

  // 替换格式化字符串中的年月日时分秒
  const formattedDateTime = format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds);

  return formattedDateTime;
}

let lsTime = time();
let state = 0;

// 创建串口实例
const port = new SerialPort(portConfig);

// 创建解析器,按行解析数据
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }));

// 监听串口数据
parser.on('data', (data) => {
    lsTime = time();
    data = `${data}`;
    let pos = data.indexOf(']')
    if(pos!=-1){
        data = data.substring(pos+1)
    }
  console.log(`[ `+time2str()+` ] ${data}`);
});

// 监听串口打开事件
port.on('open', () => {
    state = 1;
  console.log(`已打开串口: ${portConfig.path}`);
  console.log('开始监听温控芯片数据...');
});

// 监听错误事件
port.on('error', (err) => {
  console.error(`串口错误: ${err.message}`);
});

// 监听关闭事件
port.on('close', () => {
    state = 0;
  console.log('串口已关闭');
  const pythonProcess = spawn("python.exe", ["-m","esptool","-p","COM3","run"], {
      cwd: "D:\\Soft\\thonny", // 设置工作目录
      shell: false, // 是否通过 shell 执行
    });
  // 监听进程关闭事件
    pythonProcess.on('close', (code) => {
      console.log(`Python 脚本退出,退出码: ${code}`);
      port.open();
    });
});

// 优雅处理程序退出
process.on('SIGINT', () => {
  port.close((err) => {
    if (err) {
      console.error(`关闭串口时出错: ${err.message}`);
    }
    console.log('程序已退出');
    process.exit();
  });
});  


setInterval(()=>{
    if(time()-lsTime>60000){
        //两轮未更新,重启esp32
        if(state==1){
            lsTime = time();
            console.log("esp32 60秒内无反应,尝试重启")
            port.close();
        }
    }
},5000);