ESP32自组网


title: esp32自组网


ESP32自组网

通过plainlessmesh来完成esp32直接的自组网

参考了国外大神的文章

ESP-MESH with ESP32 and ESP8266: Getting Started | Random Nerd Tutorials

基本设置

#include "painlessMesh.h"
	//库文件肯定要包含
//mesh组网
#define MESH_PREFIX "whateverYouLike"
	//组网名字
#define MESH_PASSWORD "somethingSneaky"
	//组网密码
#define MESH_PORT 5555
	//组网端口
#define mesh_who 1 
	//1为接收端 0为发送端 方便后续开发
//初始化
// mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
    mesh.setDebugMsgTypes(ERROR | STARTUP); 

    mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);

直接使用的函数

发送函数

void meshSendMessage(String msg)
{
    msg = "Hi from node";
    msg += mesh.getNodeId();
    mesh.sendBroadcast(msg);
}

发送并说明来自哪个节点

后续要采用回调函数去使用的函数

	mesh.onReceive(&meshReceivedCallback);
    mesh.onNewConnection(&meshNewConnectionCallback);
    mesh.onChangedConnections(&meshChangedConnectionCallback);
    mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);

为以下函数的回调

接受函数

void meshReceivedCallback(uint32_t from, String &msg)
{
    Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
}

接受

新的连接节点

void meshNewConnectionCallback(uint32_t nodeId)
{
    Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}

更改连接

void meshChangedConnectionCallback()
{
    Serial.printf("Changed connections\n");
}

调整节点时间

void nodeTimeAdjustedCallback(int32_t offset)
{
    Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
}

总的示例

#include <你要的环境>
#include "painlessMesh.h"

painlessMesh mesh;



void meshSendMessage(String msg)
{
    msg = "Hi from node";
    msg += mesh.getNodeId();
    mesh.sendBroadcast(msg);
}


void meshReceivedCallback(uint32_t from, String &msg)
{
    Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
}

void meshNewConnectionCallback(uint32_t nodeId)
{
    Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}

void meshChangedConnectionCallback()
{
    Serial.printf("Changed connections\n");
}

void nodeTimeAdjustedCallback(int32_t offset)
{
    Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
}

void meshdo()
{
    

    // mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
    mesh.setDebugMsgTypes(ERROR | STARTUP); 

    mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
    mesh.onReceive(&meshReceivedCallback);
    mesh.onNewConnection(&meshNewConnectionCallback);
    mesh.onChangedConnections(&meshChangedConnectionCallback);
    mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);

    
    
}


void setup()
{

  // 串口
  Serial.begin(115200);

  //mesh初始
  meshdo();

}

void loop()
{
  

  //组网
  // if(!mesh_who)
  // {
  //   meshSendMessage("hello???");
  // }

  mesh.update();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发愤图强想做全栈的小陈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值