/** * @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 #include #include typedef void (*recieveCallbackPtr) (const uint8_t * mac, const uint8_t *incomingData, int len); typedef void (*sendCallbackPtr) (const uint8_t *mac_addr, esp_now_send_status_t status); struct NetworkAdresses { 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, NetworkAdresses adresses); ~Network(); bool activateEspNow(recieveCallbackPtr reci, sendCallbackPtr send); bool activateMqtt(const char *user = nullptr, const char *passphrase = nullptr); void printIPs(); bool isWifiConnected() const { return this->wifiConnected; } bool isMqttConnected() const { return this->mqttConnected; } const uint8_t* getBroadcastAddress() const { return this->broadcastAddress; } PubSubClient* getMqttClient() const { return this->mqttClient; } static uint8_t getCurrentChannel(); private: void run() override {}; void runAsChild() override; void init(const char *ssid, const char *passphrase); void checkWifi(); void checkMqtt(); bool connectMqtt(); NetworkAdresses adresses; WiFiClient wifiClient; PubSubClient* mqttClient = nullptr; bool initSucessful = false; bool wifiConnected = false; bool mqttConnected = false; const char *mqttUser; const char *mqttPassphrase; uint8_t broadcastAddress[6] = {0xC8, 0xC9, 0xA3, 0xC8, 0x57, 0x10}; uint32_t lastWifiRecoonectAttemp = 0; uint32_t lastMqttReconnectAttemp = 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