首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ESP8266网格网络

ESP8266网格网络
EN

Stack Overflow用户
提问于 2018-10-11 07:11:16
回答 1查看 2.9K关注 0票数 2

我正在使用ESP8266创建一个mesh网络,这里我使用两个ESP-01作为客户端,nodemcu作为服务器。

我也在使用painlessMesh库。它工作得很好,我把这些值作为JSON发送给服务器。但问题是,现在我必须连接到本地WiFi连接,才能将这些数据发布到nodered。无痛网格可以创建一个mqtt网桥来将这些数据传输到mqtt客户端,但是它必须与WiFi路由器在同一个通道上。我试过了,但似乎有点复杂。

是否有任何方法可以终止上述mesh任务,并使用Arduino的WiFi库连接到internet。

代码语言:javascript
运行
复制
 // Prototype for the msg received from other nodes
 void receivedCallback( uint32_t from, String &msg );


// create a task instance to send msg to client nodes
Task logServerTask(10000, TASK_FOREVER, []() {
   DynamicJsonBuffer jsonBuffer;
   JsonObject& msg = jsonBuffer.createObject();
   msg["server"] = "logServer";
   msg["nodeId"] = mesh.getNodeId();

   String str;
   msg.printTo(str);
   mesh.sendBroadcast(str);

   Serial.printf("\n");
   });

void setup() {
   Serial.begin(115200);

     mesh.setDebugMsgTypes( ERROR | CONNECTION | S_TIME );  // set before 
      init() 
     //initialise the mesh
      mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT, 
      WIFI_AP_STA, 6 );
      mesh.onReceive(&receivedCallback);
      mesh.stationManual(STATION_SSID, STATION_PASSWORD);
      mesh.setHostname(HOSTNAME);
      client.setClient(wifiClient);
      client.setServer(mqtt_server,1883);
      mesh.onNewConnection([](size_t nodeId) {
      Serial.printf("New Connection %u\n", nodeId);
    });

     mesh.onDroppedConnection([](size_t nodeId) {
     Serial.printf("Dropped Connection %u\n", nodeId);
    });

     // Add the task to the your scheduler
       userScheduler.addTask(logServerTask);
       logServerTask.enable();
     }

  void loop() {
      userScheduler.execute(); // it will run mesh scheduler as well
      mesh.update();
      client.loop();

      while(!client.connected()){
          if(client.connect("ESP8266Client123456789")){
          client.subscribe("thermalValues");
          client.subscribe("thermalValues1");
    }
      else{
       Serial.print("failed,rc=");
       Serial.println(client.state());
       delay(500);
    }   
  }
 }

//callback to the received messages from different nodes
void receivedCallback( uint32_t from, String &msg ) {
  //callback received
 }
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52754285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档