#include "Network.h" #include "WiFi.h" #include // https://github.com/tzapu/WiFiManager #include "Arduino.h" #include "MqttTopics.h" #include "PreferencesKeys.h" Network* nwInst; Network::Network(Preferences* preferences) : _mqttClient(_wifiClient), _preferences(preferences) { nwInst = this; } void Network::initialize() { WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP // it is a good practice to make sure your code sets wifi mode how you want it. // put your setup code here, to run once: Serial.begin(115200); //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around WiFiManager wm; // reset settings - wipe stored credentials for testing // these are stored by the esp library //wm.resetSettings(); bool res = wm.autoConnect(); // password protected ap if(!res) { Serial.println("Failed to connect"); return; // ESP.restart(); } else { //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); } const char* brokerAddr = _preferences->getString(preference_mqtt_broker).c_str(); strcpy(_mqttBrokerAddr, brokerAddr); int port = _preferences->getInt(preference_mqtt_broker_port); if(port == 0) { port = 1883; _preferences->putInt(preference_mqtt_broker_port, port); } Serial.print(F("MQTT Broker: ")); Serial.print(_mqttBrokerAddr); Serial.print(F(":")); Serial.println(port); _mqttClient.setServer(_mqttBrokerAddr, port); _mqttClient.setCallback(Network::onMqttDataReceivedCallback); } bool Network::reconnect() { while (!_mqttClient.connected() && millis() > _nextReconnect) { Serial.println("Attempting MQTT connection"); // Attempt to connect if (_mqttClient.connect("nukiHub")) { Serial.println(F("MQTT connected")); // ... and resubscribe _mqttClient.subscribe(mqtt_topic_lockstate_action); } else { Serial.print(F("MQTT connect failed, rc=")); Serial.println(_mqttClient.state()); _nextReconnect = millis() + 5000; } } } void Network::update() { if(!WiFi.isConnected()) { Serial.println(F("WiFi not connected")); vTaskDelay( 1000 / portTICK_PERIOD_MS); } if(!_mqttClient.connected()) { bool success = reconnect(); if(!success) { return; } } _mqttClient.loop(); vTaskDelay( 100 / portTICK_PERIOD_MS); } void Network::onMqttDataReceivedCallback(char *topic, byte *payload, unsigned int length) { nwInst->onMqttDataReceived(topic, payload, length); } void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &length) { char value[50]; size_t l = min(length, sizeof(value)-1); for(int i=0; i