/** * @file network.h * @author Alexander Klein (alex@kleiax.de) * @brief * @version 0.1 * @date 2023-09-18 * * @copyright Copyright (c) 2023 * */ #ifndef NETWORK_H #define NETWORK_H #include "component.h" #include #include #include struct NetworkAddresses { IPAddress localIP; IPAddress gateway; IPAddress subnet; IPAddress dnsServer; IPAddress mqttServer; uint16_t mqttPort = 1883; }; class Network : public Component { public: Network(const char *ssid, const char *passphrase); Network(const char *ssid, const char *passphrase, NetworkAddresses addresses); ~Network(); bool activateMqtt(const char *user = nullptr, const char *passphrase = nullptr); bool activateMqtt(const char *user, const char *passphrase, IPAddress server, uint16_t port); void printIPs(); bool isWifiConnected() const { return this->wifiConnected; } bool isMqttConnected() const { return this->mqttConnected; } PubSubClient* getMqttClient() const { return this->mqttClient; } private: void run() override {}; void runAsChild() override; void init(const char *ssid, const char *passphrase); void checkWifi(); void checkMqtt(); bool connectMqtt(); NetworkAddresses addresses; WiFiClient wifiClient; PubSubClient* mqttClient = nullptr; bool initSuccessful = false; bool wifiConnected = false; bool mqttConnected = false; const char *mqttUser; const char *mqttPassphrase; uint32_t lastWifiReconnectAttempt = 0; uint32_t lastMqttReconnectAttempt = 0; static constexpr uint8_t wifiConnectTimeout = 20; static constexpr uint16_t wifiConnectLoopTime = 500; static constexpr uint16_t wifiReconnectDelay = 5000; static constexpr uint16_t mqttReconnectDelay = 2500; }; #endif //NETWORK_H