vaibhav sharma
vaibhav sharma

Reputation: 172

ESP8266 Mesh Network

I am creating a mesh network using ESP8266, here I am using two ESP-01 as clients and nodemcu as server.

I am using painlessMesh library for the same. It is working fine I am getting the values to the server as a JSON . But the problem is now I have to connect to local WiFi connection to post this data to nodered. painless mesh can create a mqtt bridge to transfer this data to mqtt client but it has to be in same channel as the WiFi router. I have tried it but it seems a bit complicated.

Is there any way by which we can terminate the above mesh task and connect to internet using Arduino's WiFi library.

 // 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
 }

Upvotes: 2

Views: 3437

Answers (1)

vaibhav sharma
vaibhav sharma

Reputation: 172

And at last, I did it without using any serial UART. All wireless. So, My architecture consists of 3 ESP-01 and 1 NodeMcu ESP 12E. where 2 ESP-01 acts as a client. One ESP01 acts as a moderator and NodeMcu as a server. I have made the following approach-

  1. The clients will first send a message containing sensor data to the moderator as a JSON
  2. The moderator will receive these message form clients. Thers can be n number of clients. The moderator will then send these message to NodeMcu which functions as a server
  3. The server will only receive the messages that were broadcast by the moderator. The server will then parse the JSON received from the moderator and extract all the sensor data
  4. you can do this operation in one loop.and the following part is done on another loop
  5. connect to wiFi and post data to cloud

You can refer this LINK

Upvotes: 0

Related Questions