WiFi disconnect after mesh.init
Im getting an error with wifi connection when mesh is initialized. I want to connect the root node (that is elected automatically) to mqtt broker, so i initialize the WIFI connection but then i initialize the mesh and the WiFi connection drops and i cannot connect to mqtt broker.
Am i doing something wrong?
void setup() {
Serial.begin(115200); // initialize serial communication
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
// mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( ERROR | STARTUP ); // Set the types of debug messages that should be printed to the serial monitor
mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT ); // Initialize the mesh network with the given prefix, password, scheduler, and port
//delay(5000); // Wait 5 seconds for nodes to join the mesh
// Register callback functions for various events in the mesh network
mesh.onReceive(&receivedCallback);
mesh.onNewConnection(&newConnectionCallback);
mesh.onChangedConnections(&changedConnectionCallback);
mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
mesh.setContainsRoot(true);
// Verify if this node is the root node
verifyRoot();
Serial.println("Setup complete");
// Schedule the task to send messages
userScheduler.addTask( taskSendMessage );
taskSendMessage.enable();
//set connection to a mqtt broker
client.setServer(server, mqtt_port);
client.setCallback(callback);
}
void connect_mqtt(){
while (!client.connected()) {
String client_id = "esp8266-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to mosquitto mqtt broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public emqx mqtt broker connected");
// publish and subscribe
//client.publish(topic, "Hello From ESP8266!");
client.subscribe(topic);
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
Edited by Nelson Silva