network is now a component
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* @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_AP_STA))
|
||||
std::cout << "Network::connectWiFi failed WiFi.mode" << std::endl;
|
||||
|
||||
this->init(ssid, passphrase);
|
||||
}
|
||||
|
||||
Network::Network(const char *ssid, const char *passphrase, NetworkAdresses adresses) {
|
||||
if (!WiFi.mode(WIFI_AP_STA))
|
||||
std::cout << "Network::connectWiFi failed WiFi.mode" << std::endl;
|
||||
|
||||
if (!WiFi.config(this->adresses.localIP,
|
||||
this->adresses.gateway,
|
||||
this->adresses.subnet,
|
||||
this->adresses.dnsServer))
|
||||
{
|
||||
std::cout << "STA Failed to configure" << std::endl;
|
||||
}
|
||||
|
||||
this->init(ssid, passphrase);
|
||||
}
|
||||
|
||||
Network::~Network() {
|
||||
if (this->mqttClient)
|
||||
delete mqttClient;
|
||||
}
|
||||
|
||||
bool Network::activateEspNow(recieveCallbackPtr reci, sendCallbackPtr send) {
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
std::cout << "Network::activateEspNow - Error initializing ESP-NOW" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_now_register_send_cb(send);
|
||||
|
||||
memcpy(this->peerInfo.peer_addr, this->broadcastAddress, 6);
|
||||
this->peerInfo.channel = 0;
|
||||
this->peerInfo.encrypt = false;
|
||||
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
std::cout << "Network::connectEspNow - Failed to add peer" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_now_register_recv_cb(reci);
|
||||
return true;
|
||||
}
|
||||
|
||||
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->adresses.mqttServer, this->adresses.mqttPort);
|
||||
this->mqttClient->setSocketTimeout(1);
|
||||
|
||||
if (this->wifiConnected)
|
||||
return this->connectMqtt();
|
||||
return false;
|
||||
}
|
||||
|
||||
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 << this->adresses.localIP << std::endl;
|
||||
std::cout << "WiFi MAC Address: " << WiFi.macAddress() << std::endl << std::endl;
|
||||
}
|
||||
|
||||
uint8_t Network::getCurrentChannel() {
|
||||
uint8_t channel;
|
||||
wifi_second_chan_t secondChannel;
|
||||
if (esp_wifi_get_channel(&channel, &secondChannel) != ESP_OK) {
|
||||
std::cout << "Network::getCurrentChannel - Error!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
void Network::runAsChild() {
|
||||
if (!this->initSucessful)
|
||||
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->lastWifiRecoonectAttemp >= Network::wifiReconnectDelay))
|
||||
{
|
||||
std::cout << "Reconnecting to WiFi..." << std::endl;
|
||||
WiFi.disconnect();
|
||||
this->wifiConnected = WiFi.reconnect();
|
||||
this->lastWifiRecoonectAttemp = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void Network::checkMqtt() {
|
||||
if (!this->mqttClient->connected()
|
||||
&& millis() - this->lastMqttReconnectAttemp > Network::mqttReconnectDelay)
|
||||
{
|
||||
this->mqttConnected = this->connectMqtt();
|
||||
this->lastMqttReconnectAttemp = millis();
|
||||
}
|
||||
|
||||
if (this->mqttConnected)
|
||||
this->mqttClient->loop();
|
||||
}
|
||||
|
||||
bool Network::connectMqtt() {
|
||||
String clientId = "ESP32Rover-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
|
||||
if (this->mqttUser) {
|
||||
if (this->mqttClient->connect(clientId.c_str(), this->mqttUser, this->mqttPassphrase))
|
||||
this->mqttClient->publish("Rover/Info", "Connected to Mqtt-Broker");
|
||||
} else {
|
||||
if (this->mqttClient->connect(clientId.c_str()))
|
||||
this->mqttClient->publish("Rover/Info", "Connected to Mqtt-Broker");
|
||||
}
|
||||
|
||||
return this->mqttClient->connected();
|
||||
}
|
||||
Reference in New Issue
Block a user