network is now a component
This commit is contained in:
@@ -44,7 +44,7 @@ void MenuSensorData::printPage() const {
|
||||
lineOne = "Calc Azimuth:";
|
||||
lineTwo.concat(this->sensorData->getCalcAzimuth());
|
||||
lineTwo.concat(" ");
|
||||
lineTwo.concat(CalcAzimuth::stateToString(this->sensorData->getCalcAzimuthState));
|
||||
lineTwo.concat(CalcAzimuth::stateToString(this->sensorData->getCalcAzimuthState()));
|
||||
break;
|
||||
|
||||
case 3: {
|
||||
|
||||
+31
-32
@@ -29,6 +29,7 @@
|
||||
#include "LcdWrapper.h"
|
||||
#include "moveControl.h"
|
||||
#include "network.h"
|
||||
#include "networkConfig.h"
|
||||
#include "debugMqtt.h"
|
||||
#include "battery.h"
|
||||
#include "sensorData.h"
|
||||
@@ -45,10 +46,10 @@
|
||||
#include "SpecialMenus/Systeminformation/menuSysteminformation.h"
|
||||
#include "SpecialMenus/PID/menuPidSettings.h"
|
||||
#include "SpecialMenus/Route/menuRoute.h"
|
||||
#include "SpecialMenus/GPS/menuGPS.h"
|
||||
#include "SpecialMenus/SensorData/menuSensorData.h"
|
||||
|
||||
MoveControl moveController;
|
||||
Network* network;
|
||||
DriveManager* driveManager;
|
||||
Menu* main_m;
|
||||
LiquidCrystal_I2C* lcd;
|
||||
@@ -68,17 +69,13 @@ void restart(void);
|
||||
void receiveCallback (const uint8_t * mac, const uint8_t *incomingData, int len);
|
||||
void sendCallback (const uint8_t *mac_addr, esp_now_send_status_t status);
|
||||
void lcdWrapperCallback (const char data[][LcdWrapper::totalRows], uint8_t lines, uint8_t rows);
|
||||
NetworkAdresses setIPs(void);
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
// DebugTimes::setConsolOutput(true);
|
||||
DebugTimes setupTime;
|
||||
char wifiIndicator = 'X';
|
||||
|
||||
WiFi.mode(WIFI_MODE_STA);
|
||||
Serial.print("MAC Address: ");
|
||||
Serial.println(WiFi.macAddress());
|
||||
|
||||
spiPort = new SPIClass(HSPI);
|
||||
spiPort->begin(PinNumbers::spiSck, PinNumbers::spiCipo, PinNumbers::spiCopi, PinNumbers::gnssSpiCs);
|
||||
@@ -94,25 +91,21 @@ void setup() {
|
||||
lcd->clear();
|
||||
lcd->noBacklight();
|
||||
|
||||
Network::setIps();
|
||||
Network::setupMQTT();
|
||||
wifiIsActive = Network::connectWifi();
|
||||
if (wifiIsActive) {
|
||||
Network::initMQTT();
|
||||
if (Network::checkMQTT()) {
|
||||
DebugMqtt::init(Network::getMqttClient(), Loglevel::debug);
|
||||
std::cout << "Activate additional output via MQTT..." << std::endl;
|
||||
network = new Network(NetworkConfig::ssid, NetworkConfig::password, setIPs());
|
||||
network->activateEspNow(receiveCallback, sendCallback);
|
||||
if (NetworkConfig::mqtt) {
|
||||
if (network->activateMqtt(MqttConfig::user, MqttConfig::password)) {
|
||||
DebugMqtt::init(network->getMqttClient(), Loglevel::debug);
|
||||
debugMqtt = new DebugMqtt("Console");
|
||||
outputBuf = new OutputBuf(debugMqtt);
|
||||
} else
|
||||
outputBuf->activateMqtt(true);
|
||||
} else {
|
||||
outputBuf = new OutputBuf();
|
||||
wifiIndicator = '-';
|
||||
}
|
||||
} else
|
||||
outputBuf = new OutputBuf();
|
||||
std::cout.rdbuf(outputBuf);
|
||||
|
||||
Network::connectEspNow(receiveCallback, sendCallback);
|
||||
|
||||
std::cout << "Welcome to Kleiax-Rover" << std::endl;
|
||||
std::cout << "Project verion: " << VERSION << std::endl;
|
||||
std::cout << "Build timestamp: " << BUILD_TIMESTAMP << std::endl;
|
||||
@@ -121,11 +114,12 @@ void setup() {
|
||||
sensorData = new SensorData();
|
||||
sensorData->enableGnss(spiPort, PinNumbers::gnssSpiCs);
|
||||
sensorData->enableRealCompass();
|
||||
sensorData->enableGyroskop();
|
||||
// sensorData->enableGyroskop();
|
||||
driveManager = new DriveManager(&moveController, sensorData, controlPad->getControlPadDataPtr());
|
||||
|
||||
outputBuf->activateMqtt(true);
|
||||
|
||||
char wifiIndicator = 'X';
|
||||
if (network->isWifiConnected())
|
||||
wifiIndicator = '-';
|
||||
lcd->backlight();
|
||||
lcd->printf("%c Kleiax-Rover %c", wifiIndicator, wifiIndicator);
|
||||
lcd->setCursor(0, 1);
|
||||
@@ -139,16 +133,7 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (wifiIsActive) {
|
||||
DebugTimes wifiTime;
|
||||
Network::checkWiFi();
|
||||
#ifdef MQTT
|
||||
bool err = Network::checkMQTT();
|
||||
if (!err)
|
||||
std::cout << "loop(): checkMQTT returns false" << std::endl;
|
||||
#endif //MQTT
|
||||
wifiTime.stopConsol("WiFi-Time", 10);
|
||||
}
|
||||
network->loop();
|
||||
sensorData->loop();
|
||||
driveManager->loop();
|
||||
controlPad->loop();
|
||||
@@ -297,8 +282,22 @@ void lcdWrapperCallback (const char data[][LcdWrapper::totalRows], uint8_t lines
|
||||
if (!controlPad->isControlPadConnected())
|
||||
return;
|
||||
|
||||
esp_err_t result = esp_now_send(Network::getBroadcastAddress(), (uint8_t *) data, lines * rows);
|
||||
esp_err_t result = esp_now_send(network->getBroadcastAddress(), (uint8_t *) data, lines * rows);
|
||||
|
||||
if (result != ESP_OK)
|
||||
Serial.println("Error sending the data");
|
||||
}
|
||||
|
||||
NetworkAdresses setIPs() {
|
||||
NetworkAdresses adresses;
|
||||
adresses.localIP.fromString(NetworkConfig::ip);
|
||||
adresses.subnet.fromString(NetworkConfig::subnet);
|
||||
adresses.gateway.fromString(NetworkConfig::gateway);
|
||||
adresses.dnsServer.fromString(NetworkConfig::dns);
|
||||
if (NetworkConfig::mqtt) {
|
||||
adresses.mqttServer.fromString(MqttConfig::server);
|
||||
adresses.mqttPort = MqttConfig::port;
|
||||
}
|
||||
|
||||
return adresses;
|
||||
}
|
||||
|
||||
-174
@@ -1,174 +0,0 @@
|
||||
/**
|
||||
* @file network.cpp
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief Contains the implementation of the ststic class Network
|
||||
* @version 0.1
|
||||
* @date 2021-12-14
|
||||
*
|
||||
* @copyright Copyright (c) 2021
|
||||
*
|
||||
*/
|
||||
|
||||
#include "network.h"
|
||||
|
||||
IPAddress Network::local_IP;
|
||||
IPAddress Network::gateway;
|
||||
IPAddress Network::subnet;
|
||||
IPAddress Network::mqtt_server;
|
||||
IPAddress Network::dnsServer;
|
||||
|
||||
WiFiClient Network::wifi_client;
|
||||
PubSubClient* Network::mqtt_client;
|
||||
|
||||
esp_now_peer_info_t Network::peerInfo;
|
||||
uint8_t Network::broadcastAddress[6] = ESP_NOW_CONTROLER_MAC;
|
||||
|
||||
void Network::setIps() {
|
||||
local_IP.fromString(WLAN_IP);
|
||||
gateway.fromString(WLAN_GATEWAY);
|
||||
subnet.fromString(WLAN_SUBNETMASK);
|
||||
mqtt_server.fromString(MQTT_SERVER);
|
||||
dnsServer.fromString(WLAN_DNS_SERVER);
|
||||
}
|
||||
|
||||
void Network::setupMQTT() {
|
||||
Network::mqtt_client = new PubSubClient(wifi_client);
|
||||
Network::mqtt_client->setServer(MQTT_SERVER, MQTT_PORT);
|
||||
Network::mqtt_client->setSocketTimeout(1);
|
||||
}
|
||||
|
||||
bool Network::connectMQTT() {
|
||||
#ifndef MQTT
|
||||
return false;
|
||||
#endif
|
||||
|
||||
// Create a random client ID
|
||||
String clientId = "ESP32Rover-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
|
||||
#ifdef MQTT_AUTH
|
||||
if (mqtt_client->connect(clientId.c_str(), MQTT_USER, MQTT_PASSWORD)) {
|
||||
#endif //MQTT_AUTH
|
||||
|
||||
#ifndef MQTT_AUTH
|
||||
if (mqtt_client->connect(clientId.c_str())) {
|
||||
#endif //MQTT_AUTH
|
||||
|
||||
// Once connected, publish an announcement...
|
||||
mqtt_client->publish("Rover/Info", "Connected to Mqtt-Broker");
|
||||
}
|
||||
return mqtt_client->connected();
|
||||
}
|
||||
bool Network::connectWifi() {
|
||||
// Configures static IP address
|
||||
|
||||
if (!WiFi.mode(WIFI_AP_STA))
|
||||
std::cout << "Network::connectWiFi failed WiFi.mode" << std::endl;
|
||||
|
||||
if (!WiFi.config(local_IP, gateway, subnet, dnsServer))
|
||||
std::cout << "STA Failed to configure" << std::endl;
|
||||
|
||||
// Connect to Wi-Fi network with SSID and password
|
||||
std::cout << "Connecting to " << WLAN_SSID << std::endl;
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASSWORD);
|
||||
uint8_t timeout = WLAN_CONNECT_TIMEOUT;
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(WLAN_CONNECT_LOOP_TIME);
|
||||
std::cout << "." << std::flush;
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
std::cout << std::endl;
|
||||
std::cout << "WiFi NOT connected." << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Print local IP address and start web server
|
||||
std::cout << std::endl;
|
||||
std::cout << "WiFi connected." << std::endl;
|
||||
std::cout << "IP address: " << std::endl;
|
||||
std::cout << WLAN_IP << std::endl;
|
||||
// std::cout << "WiFi MAC Address: " << WiFi.macAddress() << std::endl << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Network::connectEspNow(recieveCallbackPtr reci, sendCallbackPtr send) {
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
std::cout << "Network::connectEspNow - Error initializing ESP-NOW" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_now_register_send_cb(send);
|
||||
|
||||
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
|
||||
peerInfo.channel = 0;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
// std::cout << "Network::getCurrentChannel - Current WiFi channel: "
|
||||
// << (int) channel << " second channel: " << (int) secondChannel
|
||||
// << std::endl;
|
||||
return channel;
|
||||
}
|
||||
|
||||
bool Network::checkMQTT() {
|
||||
static uint64_t lastReconnectAttempt = 0;
|
||||
if (!mqtt_client->connected()) {
|
||||
long now = millis();
|
||||
if (now - lastReconnectAttempt > MQTT_TIME_RECONNECT) {
|
||||
lastReconnectAttempt = now;
|
||||
// Attempt to reconnect
|
||||
if (connectMQTT())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
mqtt_client->loop();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Network::initMQTT() {
|
||||
if (connectMQTT())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t Network::getWiFiChannel(const char *ssid) {
|
||||
if (int32_t n = WiFi.scanNetworks())
|
||||
for (uint8_t i=0; i<n; i++)
|
||||
if (!strcmp(ssid, WiFi.SSID(i).c_str()))
|
||||
return WiFi.channel(i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Network::checkWiFi() {
|
||||
static uint64_t previousMillis = 0;
|
||||
static uint16_t delay = 5000;
|
||||
uint64_t currentMillis = millis();
|
||||
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >= delay)) {
|
||||
std::cout << "Reconnecting to WiFi..." << std::endl;
|
||||
WiFi.disconnect();
|
||||
WiFi.reconnect();
|
||||
previousMillis = currentMillis;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
PubSubClient* Network::getMqttClient() {
|
||||
return mqtt_client;
|
||||
}
|
||||
Reference in New Issue
Block a user