prampec / IotWebConf

ESP8266/ESP32 non-blocking WiFi/AP web configuration Arduino library
MIT License
528 stars 141 forks source link

Conflict between IotWebConf and Painless mesh #207

Open hesama110 opened 3 years ago

hesama110 commented 3 years ago

I am trying to have a mesh network which one of them is Gateway and it need to be connect to WIFI and internet to transfer data by MQTT. I am using PainlessMesh to create the mesh. Issue I have When I am adding IotWebConf setting as minimal version to start with, some times it isn't showing any access point, some times showing ESP-xxxx, if AP name show up in wifi list in PC. I couldn't understand why it is not connecting to WIFI My simplified code is here, please let me know what can be wrong

`

include

include

include

include

include

include

define MESH_PREFIX "whateverYouLike"

define MESH_PASSWORD "somethingSneaky"

define MESH_PORT 5555

define STATION_SSID "stTest"

define STATION_PASSWORD "StTestPass"

define HOSTNAME "MQTT_Bridge"

define STATUS_PIN LED_BUILTIN

// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point. const char thingName[] = "testThing";

// -- Initial password to connect to the Thing, when it creates an own Access Point. const char wifiInitialApPassword[] = "123456";

// Prototypes void handleRoot(); void receivedCallback( const uint32_t &from, const String &msg ); void newConnectionCallback(uint32_t nodeId); void changedConnectionCallback(); void nodeTimeAdjustedCallback(int32_t offset);

void mqttCallback(char topic, byte payload, unsigned int length); String splitStringByIndex(String data, char separator, int index); int countAttributes(String data, char separator);

IPAddress getlocalIP();

IPAddress myIP(0,0,0,0);

const char* mqtt_server = "MQTT_Server_Address"; //mqtt server

painlessMesh mesh; WiFiClient wifiClient; PubSubClient mqttClient(wifiClient);

DNSServer dnsServer; WebServer server(80);

IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword);

void setup() { Serial.begin(115200); Serial.println(); Serial.println("Starting up...");

// -- Initializing the configuration. //iotWebConf.setStatusPin(STATUS_PIN); iotWebConf.init();

mqttClient.setServer(mqtt_server, 1883);//connecting to mqtt server mqttClient.setCallback(mqttCallback);

mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION ); // set before init() so that you can see startup messages

// Channel set to 6. Make sure to use the same channel for your mesh and for you other // network (STATION_SSID) mesh.init( MESH_PREFIX, MESH_PASSWORD, MESH_PORT, WIFI_STA, 6 ); mesh.onReceive(&receivedCallback); mesh.onNewConnection(&newConnectionCallback); mesh.onChangedConnections(&changedConnectionCallback); mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback); mesh.onDroppedConnection([](auto nodeId) { // Do something with the event Serial.println("------Droped node "+String(nodeId)); Serial.printf("--> Droped: \n", mesh.subConnectionJson().c_str());

});

// mesh.stationManual(STATION_SSID, STATION_PASSWORD); // mesh.setHostname(HOSTNAME);

// Bridge node, should (in most cases) be a root node. See the wiki for some background mesh.setRoot(true); // This node and all other nodes should ideally know the mesh contains a root, so call this on all nodes mesh.setContainsRoot(true);

// -- Set up required URL handlers on the web server. server.on("/", handleRoot); server.on("/config", []{ iotWebConf.handleConfig(); }); server.onNotFound([](){ iotWebConf.handleNotFound(); });

Serial.println("Ready.!!!!!!!!!!!!!!!!!!!!!");

}

void loop() {

// -- doLoop should be called as frequently as possible. iotWebConf.doLoop();

mesh.update(); mqttClient.loop();

// if(myIP != getlocalIP()){ // myIP = getlocalIP(); // Serial.println("My IP is " + myIP.toString());

// if (mqttClient.connect("helix")) { // Serial.println("########## mqttClient Connected ############"); // //mqttClient.publish("/iot/Mesh/attrs/","Ready!"); // //mqttClient.subscribe("/iot/Mesh/attrs/#"); // } // } }

/**

void receivedCallback( const uint32_t &from, const String &msg ) {

Serial.println(" "); Serial.println("##############"); Serial.printf("bridge: Received from %u msg=%s\n", from, msg.c_str());

String stationNumber = splitStringByIndex(msg, ';', 0); Serial.printf("stationNumber %s \n", stationNumber.c_str());

//Quantidade de parâmetros a serem enviados int count = countAttributes(msg, ';');

String topic = "/iot/station"; topic += stationNumber; topic += "/attrs/";

String mensageToSend = "";

//Realiza a leitura de parametros a serem enviados de forma dinâmica, formato recebido - "tag|valor;tag2|valor2" for(int i=1; i < count; i++){ mensageToSend = splitStringByIndex(msg, ';', i);

String debug = "mensageToSend " + String(i) + " mqtt " + mensageToSend.c_str();
Serial.println(debug);

//Realiza a publicação no servidor MQTT de forma dinâmica.
mqttClient.publish(topic.c_str(), mensageToSend.c_str());

}

}

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

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

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

void mqttCallback(char topic, uint8_t payload, unsigned int length) {

}

IPAddress getlocalIP() { return IPAddress(mesh.getStationIP()); }

String splitStringByIndex(String data, char separator, int index) { int found = 0; int strIndex[] = {0, -1}; int maxIndex = data.length()-1;

for(int i=0; i<=maxIndex && found<=index; i++){ if(data.charAt(i)==separator || i==maxIndex){ found++; strIndex[0] = strIndex[1]+1; strIndex[1] = (i == maxIndex) ? i+1 : i; } }

return found>index ? data.substring(strIndex[0], strIndex[1]) : ""; }

int countAttributes(String data, char separator) { int found = 0; int maxIndex = data.length()-1;

for(int i=0; i<=maxIndex; i++){ if(data.charAt(i)==separator || i==maxIndex){ found++; } }

return found; }

`

prampec commented 3 years ago

I'm not sure about this, but mesh network might also tries to manage the WiFi connection as IotWebConf does. In this case you are not able to use both of them. (I wanted to separate the config section from the WiFi-management section, but it will not happen in the near future.)

As a general rule you might want to use standard the WiFi.begin() and WiFi.softAP() methods to test whether these work properly with the corresponding library, as IotWebConf uses these basic methods to operate the WiFi radio.