欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

ESP8266无线通信实现心跳包和断线重连功能(无需额外配置,可直接连接两个单片机的串口),默认数据包大小为66字节(不分帧读取)

最编程 2024-08-08 22:31:59
...

如果对速率没什么要求就当成无线串口用就行,烧录后就能互相配对了

代码主体来自网络一个前辈,待我找到链接了放出来,我改进了部分缺点。

服务器端代码

#include <ESP8266WiFi.h>
#include <Ticker.h>   

const char* ssid="ESP82662022";
const char* pswd="ESP82662022";

IPAddress local_IP(192,168,0,1);//手动设置的开启的网络的ip地址
IPAddress gateway(192,168,0,0);  //手动设置的网关IP地址
IPAddress subnet(255,255,255,0); //手动设置的子网掩码
//server esp8266
WiFiServer server(1234);
WiFiClient client;
uint64_t messageflag;
uint8_t reconnectflag;
Ticker timer1;
void TCPConfig()
{

  Serial.begin(256000);
  Serial.setTimeout(1);
  WiFi.mode(WIFI_AP); //设置WIFI模块为STA模式
  
  //配置接入点的IP,网关IP,子网掩码
  WiFi.softAPConfig(local_IP, gateway, subnet);
  
  WiFi.softAP(ssid, pswd,3,1);


  
  server.begin();
  timer1.attach(1, timer1_cb);

  //Serial.println("wifi created");
}
 

void setup() 
{
  
  TCPConfig();
}
 
void loop() 
{

  
  
  
  while(!(client.connected()&reconnectflag))
  {
    //检查客户端是否已连接
    client = server.available();
    if (!client) 
    {
      //Serial.println("wait for connected");
      delay(500);
      continue;
    }
    client.setTimeout(100); //默认为 1000
    reconnectflag=1;
    
  }


  //透传数据    
  while (client.available())//读取到的数据转发到到串口
  {
      uint8_t c = client.read();
      Serial.write(c);
      messageflag=0;
  }


  if (Serial.available())//串口读取到的转发到wifi,因为串口是一位一位的发送所以在这里缓存完再发送
  {
      delay(3);
      size_t counti = Serial.available();
      uint8_t sbuf[counti];
      Serial.readBytes(sbuf, counti);
      client.write(sbuf, counti);

  }

  
  }

void timer1_cb(void) 
{
messageflag++;
if(messageflag>10)
  reconnectflag=0;
}

客户端代码:

#include <ESP8266WiFi.h>

//wifi密码及ssid
const char* ssid="ESP82662022";
const char* pswd="ESP82662022";
//目标服务器ip即端口号
const char *host = "192.168.0.1";
const int tcpPort = 1234;
//连接成功信号
uint8_t flag_connect[66];
//tcpclient实例
WiFiClient client;


void setup() 
{
      //启动串口通信
      Serial.begin(256000);
      Serial.setTimeout(1);
      for( int i = 0 ; i < 66 ; i++)
      flag_connect[i] = 0; 

      flag_connect[0]=0xb5;
      flag_connect[1]=0x5b;
      flag_connect[65]=0xff;
      //数据头其余全为零
      //握手协议,十秒内没有数据传输时服务器端将重连

      
      //连接到WiFi
      WiFi.begin(ssid, pswd);
      
      //处理未连接异常
      while(WiFi.status() != WL_CONNECTED)
      {
        delay(100);
      }

   

      
      // Serial.println(WiFi.localIP());              // 通过串口监视器输出信息
}

void loop() 
{
      //处理未连接到服务器异常
      while(!client.connected())
      {
        if(WiFi.status() != WL_CONNECTED)
      {         WiFi.begin(ssid, pswd);
                while(WiFi.status() != WL_CONNECTED)
                {
                  delay(100);
                } 
      }
        if(!client.connect(host, tcpPort))
        {
          delay(100);
        }
        else
        { //连接成功信号
          delay(100);
          client.write(flag_connect, 66);
          Serial.write(flag_connect, 66);
        }
      }
      

      
      //透传数据
      while (client.available())//读取到的数据转发到到串口
      {
          uint8_t c = client.read();
          Serial.write(c);
      }
    
    
  if (Serial.available())//串口读取到的转发到wifi,因为串口是一位一位的发送所以在这里缓存完再发送
  {
      delay(3);//不加不行,暂未找到解决办法。目的是解决数据分帧读取的问题,每delay(1)增加27个字节
      size_t counti = Serial.available();
      uint8_t sbuf[counti];
      Serial.readBytes(sbuf, counti);
      client.write(sbuf, counti);

  }
}

基于c8t6和esp8266的无线烧录器我也制作了一对,不过没有画板打样,有兴趣可以看后续文章