Reputation: 172
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
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-
You can refer this LINK
Upvotes: 0