ESP8266 透传程序 点对点串口tcp透传

两个 esp8266 01s 点对点tcp串口透传

带自动重连 看门狗防死机 服务端和客户端 串口波特率115200

客户端要设置正确服务端的ip地址和端口

 

实现两个esp8266 01s点对点tcp串口透传可以按照以下步骤进行:

1. 准备工作
在两个ESP8266 01S模块上分别烧录TCP转串口透传程序,并将波特率设置为115200。如果您使用Arduino IDE进行编程,可以参考以下链接:https://github.com/markszabo/IRremoteESP8266/wiki/TCP-Serial-Bridge。
另外,我们还需要对代码进行修改以实现自动重连和看门狗防死机的功能。

2. 实现自动重连
当ESP8266模块掉线后,我们需要让其自动重连。我们可以通过在代码中添加一个定时器来实现。具体做法是,在setup()函数中初始化一个定时器,然后在loop()函数中不断检测连接状态,如果掉线则启动定时器,在定时器回调函数中尝试重新连接。

3. 实现看门狗防死机
看门狗是一种硬件或软件机制,用于监视程序运行状态并在程序死机时进行重启。可以通过向WDT(watchdog timer)写入特定值来重置看门狗计数器。每隔一段时间内程序需要向WDT写入该特定值,以避免WDT超时引起的系统复位。
在ESP8266上使用看门狗需要先对timer1寄存器进行配置,然后在loop()函数中不断向WDT写入值。

4. 实现TCP串口透传
在ESP8266上实现TCP串口透传的关键是使用WiFiServer和WiFiClient库。在服务端代码中创建一个WiFiServer并监听指定端口,客户端代码中创建一个WiFiClient并连接服务端。一旦连接建立成功,就可以使用Serial和WiFiClient之间的通信来实现串口透传功能。

以下是参考代码,请按照需要进行修改:

服务端代码:
```
#include <ESP8266WiFi.h>

#define SERIAL_BAUD 115200
#define SERVER_PORT 8080
#define RECONNECT_INTERVAL 10000 // 自动重连时间间隔

// Wifi参数
char* ssid = "your_ssid";
char* password = "your_password";

// 创建server对象
WiFiServer server(SERVER_PORT);

// 定时器对象
os_timer_t reconnect_timer;

void setup() {
  delay(1000);
  Serial.begin(SERIAL_BAUD);
  
  // 连接wifi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  
  // 开启看门狗
  timer1_enable(TIM_DIV256, TIM_EDGE, TIM_SINGLE);
  timer1_write(5000000); // 设置看门狗定时器周期
  
  // 开启server监听
  server.begin();
}

void loop() {
  
  // 检查是否有client连接,并将数据转发到串口
  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (Serial.available() > 0) {
        client.write(Serial.read());
      }
      if (client.available() > 0) {
        Serial.write(client.read());
      }
    }

    // 断开连接
    client.stop();
  }

  // 检查wifi连接状态
  if (WiFi.status() != WL_CONNECTED) {
    // 开启自动重连定时器
    os_timer_disarm(&reconnect_timer);
    os_timer_setfn(&reconnect_timer, (os_timer_func_t *) reconnect_wifi, NULL);
    os_timer_arm(&reconnect_timer, RECONNECT_INTERVAL, true);
  }

  // 喂狗
  timer1_write(5000000); 
}

// 自动重连函数
void reconnect_wifi() {
  Serial.println("Reconnecting to WiFi...");
  
  WiFi.disconnect();
  delay(500);
  
  WiFi.begin(ssid, password);
}
```

客户端代码:
```
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

#define SERIAL_BAUD 115200
#define SERVER_ADDRESS "192.168.1.100" // 替换为服务端IP
#define SERVER_PORT 8080
#define RECONNECT_INTERVAL 10000 // 自动重连时间间隔

// Wifi参数
char* ssid = "your_ssid";
char* password = "your_password";

// 定时器对象
os_timer_t reconnect_timer;

void setup() {
  delay(1000);
  
  Serial.begin(SERIAL_BAUD);

  // 连接wifi
  WiFi.begin(ssid, password);
  
   while (WiFi.status() != WL_CONNECTED) {
     delay(1000);
     Serial.println("Connecting to WiFi...");
   }

   // 开启看门狗
   timer1_enable(TIM_DIV256, TIM_EDGE, TIM_SINGLE);
   timer1_write(5000000); // 设置看门狗定时器周期
}

void loop() {
  static WiFiClient client;
  
  // 检查wifi连接状态
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Disconnected from WiFi");
    
    // 开启自动重连定时器
    os_timer_disarm(&reconnect_timer);
    os_timer_setfn(&reconnect_timer, (os_timer_func_t *) reconnect_wifi, NULL);
    os_timer_arm(&reconnect_timer, RECONNECT_INTERVAL, true);
    
    return;
  }

  // 当前没有连接,则尝试连接server
  if (!client.connected()) {
    Serial.println("Reconnecting to server...");
    client.connect(SERVER_ADDRESS, SERVER_PORT);
  }
  
  // 连接成功,则将数据转发到串口
  while (client.connected()) {
     if (Serial.available() > 0) {
       client.write(Serial.read());
     }
     if (client.available() > 0) {
       Serial.write(client.read());
     }
   }

   // 连接失败,则停止client并关闭与server的连接
   Serial.println("Lost connection to server");
   client.stop();
   
   delay(1000);

   return; 

   // 喂狗
  timer1_write(5000000); 
}

// 自动重连函数
void reconnect_wifi() {
  Serial.println("Reconnecting to WiFi...");

   WiFi.disconnect();
   delay(500);

   WiFi.begin(ssid, password);
}
```

请先按照代码中的提示替换WiFi参数和服务端IP地址。这个程序使用了定时器来启用自动重连和看门狗防死机功能。请注意:当两个ESP8266进行通信时,它们之间的距离不能太远,否则会导致信号干扰。

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值