106 lines
2.8 KiB
C++
106 lines
2.8 KiB
C++
/**
|
|
* @file network.h
|
|
* @author Alexander Klein (alex@kleiax.de)
|
|
* @brief Contains the Network class.
|
|
* @version 0.1
|
|
* @date 2021-12-14
|
|
*
|
|
* @copyright Copyright (c) 2021
|
|
*
|
|
*/
|
|
|
|
#ifndef NETWORK_H
|
|
#define NETWORK_H
|
|
|
|
#include <iostream>
|
|
#include <WiFi.h>
|
|
#include <PubSubClient.h>
|
|
#include <esp_now.h>
|
|
|
|
#include "networkConfig.h"
|
|
|
|
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);
|
|
|
|
/**
|
|
* @brief This class handles all network stuff
|
|
*
|
|
* This class only inherits static functions and members and use
|
|
* only the configuration data is given by networkConfig.h
|
|
*
|
|
*/
|
|
class Network {
|
|
public:
|
|
/**
|
|
* @brief Set the all IPs needed by this class
|
|
*/
|
|
static void setIps();
|
|
|
|
/**
|
|
* @brief Establish a connection to the WiFi.
|
|
*
|
|
* @return true Wifi is connected
|
|
* @return false Timeout
|
|
*/
|
|
static bool connectWifi();
|
|
static bool connectEspNow(recieveCallbackPtr reci, sendCallbackPtr send);
|
|
|
|
/**
|
|
* @brief Set all general settings to connect to a broker.
|
|
*/
|
|
static void setupMQTT();
|
|
|
|
/**
|
|
* @brief Checks if mqtt is still connected
|
|
*
|
|
* If mqtt is not connect, this function try a reconnect
|
|
* with the function connectMQTT. This function should be
|
|
* called every mainloop.
|
|
*
|
|
* @return true if connected
|
|
* @return false if not connected
|
|
*/
|
|
static bool checkMQTT();
|
|
static bool initMQTT();
|
|
|
|
/**
|
|
* @brief Checks if WiFi is still connected
|
|
*
|
|
* If WiFi is not connected, this function try a reconnect.
|
|
* This function should be called every mainloop.
|
|
*/
|
|
static void checkWiFi();
|
|
|
|
/**
|
|
* @brief Get the Mqtt Client object
|
|
*
|
|
* @return PubSubClient*
|
|
*/
|
|
static PubSubClient* getMqttClient();
|
|
static const uint8_t * getBroadcastAddress() { return broadcastAddress; }
|
|
|
|
private:
|
|
/**
|
|
* @brief Tries a connect to the broker
|
|
*
|
|
* @return true if the connect attemp was successful
|
|
* @return false if the connect attemp was unsuccessful
|
|
*/
|
|
static bool connectMQTT();
|
|
static int32_t getWiFiChannel(const char *ssid);
|
|
|
|
static esp_now_peer_info_t peerInfo;
|
|
static uint8_t broadcastAddress[6];
|
|
|
|
static IPAddress local_IP;
|
|
static IPAddress gateway;
|
|
static IPAddress subnet;
|
|
static IPAddress mqtt_server;
|
|
static IPAddress dnsServer;
|
|
|
|
static WiFiClient wifi_client;
|
|
static PubSubClient* mqtt_client;
|
|
};
|
|
|
|
#endif // NETWORK_H
|