Initial commit
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* @file network.cpp
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-09-18
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#include "network.h"
|
||||
|
||||
Network::Network(const char *ssid, const char *passphrase)
|
||||
{
|
||||
if (!WiFi.mode(WIFI_STA))
|
||||
std::cout << "Network::connectWiFi failed WiFi.mode" << std::endl;
|
||||
|
||||
this->init(ssid, passphrase);
|
||||
}
|
||||
|
||||
Network::Network(const char *ssid, const char *passphrase, NetworkAddresses addresses)
|
||||
{
|
||||
this->addresses = addresses;
|
||||
|
||||
if (!WiFi.mode(WIFI_STA))
|
||||
std::cout << "Network::connectWiFi failed WiFi.mode" << std::endl;
|
||||
|
||||
if (!WiFi.config(this->addresses.localIP,
|
||||
this->addresses.gateway,
|
||||
this->addresses.subnet,
|
||||
this->addresses.dnsServer))
|
||||
{
|
||||
std::cout << "STA Failed to configure" << std::endl;
|
||||
}
|
||||
|
||||
this->init(ssid, passphrase);
|
||||
}
|
||||
|
||||
Network::~Network()
|
||||
{
|
||||
if (this->mqttClient)
|
||||
delete mqttClient;
|
||||
}
|
||||
|
||||
bool Network::activateMqtt(const char *user, const char *passphrase)
|
||||
{
|
||||
this->mqttUser = user;
|
||||
this->mqttPassphrase = passphrase;
|
||||
|
||||
this->mqttClient = new PubSubClient(this->wifiClient);
|
||||
this->mqttClient->setServer(this->addresses.mqttServer, this->addresses.mqttPort);
|
||||
this->mqttClient->setSocketTimeout(1);
|
||||
|
||||
if (this->wifiConnected)
|
||||
return this->connectMqtt();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Network::activateMqtt(const char *user, const char *passphrase, IPAddress server, uint16_t port)
|
||||
{
|
||||
this->addresses.mqttServer = server;
|
||||
this->addresses.mqttPort = port;
|
||||
return this->activateMqtt(user, passphrase);
|
||||
}
|
||||
|
||||
void Network::printIPs()
|
||||
{
|
||||
std::cout << std::endl;
|
||||
if (!this->wifiConnected)
|
||||
{
|
||||
std::cout << "WiFi is not connected." << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << "WiFi is connected to" << std::endl;
|
||||
std::cout << "IP address: " << std::endl;
|
||||
std::cout << WiFi.localIP().toString().c_str() << std::endl;
|
||||
std::cout << "WiFi MAC Address: " << WiFi.macAddress().c_str() << std::endl
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void Network::runAsChild()
|
||||
{
|
||||
if (!this->initSuccessful)
|
||||
return;
|
||||
|
||||
this->checkWifi();
|
||||
|
||||
if (this->wifiConnected && this->mqttClient)
|
||||
this->checkMqtt();
|
||||
}
|
||||
|
||||
void Network::init(const char *ssid, const char *passphrase)
|
||||
{
|
||||
// Connect to Wi-Fi network with SSID and password
|
||||
std::cout << "Connecting to " << ssid << std::endl;
|
||||
WiFi.begin(ssid, passphrase);
|
||||
uint8_t timeout = Network::wifiConnectTimeout;
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(Network::wifiConnectLoopTime);
|
||||
std::cout << "." << std::flush;
|
||||
timeout--;
|
||||
if (timeout == 0)
|
||||
{
|
||||
std::cout << std::endl;
|
||||
std::cout << "WiFi NOT connected." << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this->wifiConnected = true;
|
||||
this->printIPs();
|
||||
}
|
||||
|
||||
void Network::checkWifi()
|
||||
{
|
||||
if ((WiFi.status() != WL_CONNECTED) && (millis() - this->lastWifiReconnectAttempt >= Network::wifiReconnectDelay))
|
||||
{
|
||||
std::cout << "Reconnecting to WiFi..." << std::endl;
|
||||
WiFi.disconnect();
|
||||
this->wifiConnected = WiFi.reconnect();
|
||||
this->lastWifiReconnectAttempt = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void Network::checkMqtt()
|
||||
{
|
||||
if (!this->mqttClient->connected() && millis() - this->lastMqttReconnectAttempt > Network::mqttReconnectDelay)
|
||||
{
|
||||
this->mqttConnected = this->connectMqtt();
|
||||
this->lastMqttReconnectAttempt = millis();
|
||||
}
|
||||
|
||||
if (this->mqttConnected)
|
||||
this->mqttClient->loop();
|
||||
}
|
||||
|
||||
bool Network::connectMqtt()
|
||||
{
|
||||
String clientId = "ESP32CrazyHead-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
|
||||
if (this->mqttUser)
|
||||
{
|
||||
if (this->mqttClient->connect(clientId.c_str(), this->mqttUser, this->mqttPassphrase))
|
||||
this->mqttClient->publish("CrazyHead/Info", "Connected to Mqtt-Broker");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (this->mqttClient->connect(clientId.c_str()))
|
||||
this->mqttClient->publish("CrazyHead/Info", "Connected to Mqtt-Broker");
|
||||
}
|
||||
|
||||
return this->mqttClient->connected();
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @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 <iostream>
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user