From e3c452436de97bc6dbe86f352c015a67fbe2b185 Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Sat, 8 Apr 2023 20:28:34 +0200 Subject: [PATCH] switch from rf24 to espnow --- include/network.h | 10 ++++++++ include/networkConfig.h | 1 + lib/ControllPad/controlPad.cpp | 45 +++++++++------------------------- lib/ControllPad/controlPad.h | 9 +++---- src/main.cpp | 41 ++++++++++++++++++++++++++++--- src/network.cpp | 37 ++++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 42 deletions(-) diff --git a/include/network.h b/include/network.h index 0a8f0b8..b2f6460 100644 --- a/include/network.h +++ b/include/network.h @@ -15,9 +15,13 @@ #include #include #include +#include #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 * @@ -39,6 +43,7 @@ class Network { * @return false Timeout */ static bool connectWifi(); + static bool connectEspNow(recieveCallbackPtr reci, sendCallbackPtr send); /** * @brief Set all general settings to connect to a broker. @@ -71,6 +76,7 @@ class Network { * @return PubSubClient* */ static PubSubClient* getMqttClient(); + static const uint8_t * getBroadcastAddress() { return broadcastAddress; } private: /** @@ -80,6 +86,10 @@ class Network { * @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; diff --git a/include/networkConfig.h b/include/networkConfig.h index 3e3064e..8e06181 100644 --- a/include/networkConfig.h +++ b/include/networkConfig.h @@ -30,6 +30,7 @@ */ #define WLAN_CONNECT_LOOP_TIME 500 #define WLAN_DNS_SERVER "8.8.8.8" +#define ESP_NOW_CONTROLER_MAC {0xC8, 0xC9, 0xA3, 0xC8, 0x57, 0x10} #define NTRIP_RTK2GO diff --git a/lib/ControllPad/controlPad.cpp b/lib/ControllPad/controlPad.cpp index 377b96a..52b4d21 100644 --- a/lib/ControllPad/controlPad.cpp +++ b/lib/ControllPad/controlPad.cpp @@ -11,34 +11,8 @@ #include "controlPad.h" -ControlPad::ControlPad(SPIClass *spiPort, uint8_t cePin, uint8_t csnPin) { - this->radio = new RF24(cePin, csnPin); +ControlPad::ControlPad() { - std::cout << "ControlPad::ControlPad: sizeof ControlPadInput in byte: " << sizeof(ControlPadInput) << std::endl; - - - if (!this->radio->begin(spiPort)) { - std::cout << "ControlPad::ControlPad: Error radio->begin!" << std::endl; - while(true); - } - - // this->radio->setAddressWidth(5); - // this->radio->setChannel(7); - // this->radio->setDataRate(RF24_1MBPS); - // this->radio->setAutoAck(true); - // this->radio->enableDynamicPayloads(); - // this->radio->enableAckPayload(); - // this->radio->setCRCLength(RF24_CRC_16); - - this->radio->setPALevel(RF24_PA_LOW); - - // this->radio->openWritingPipe(this->address[1]); - this->radio->openReadingPipe(0, this->address[0]); - - this->radio->startListening(); - - // this->radio->printPrettyDetails(); - this->radio->printPrettyDetails(); } bool ControlPad::loop() { @@ -78,6 +52,16 @@ bool ControlPad::loop() { return true; } +void ControlPad::insertData(const uint8_t *data) { + uint8_t lastCount = this->controlInput.counter + 1; + memcpy(&(this->controlInput), data, sizeof(this->controlInput)); + if (lastCount != this->controlInput.counter) + std::cout << "ControlPad::insertData counter wrong value" << std::endl; + + this->lastMessageReceive = millis(); + std::cout << "data: " << (int) this->controlInput.counter << std::endl; +} + const ControlPadInput * ControlPad::getControlPadDataPtr(uint16_t maxElapsedTime) const { if (maxElapsedTime && millis() - this->lastMessageReceive > maxElapsedTime) return nullptr; @@ -85,10 +69,5 @@ const ControlPadInput * ControlPad::getControlPadDataPtr(uint16_t maxElapsedTime } void ControlPad::receiveData() { - this->radio->startListening(); - if (this->radio->available()) { - ControlPadInput lastInput = this->controlInput; - this->radio->read(&this->controlInput, sizeof(ControlPadInput)); - std::cout << "Data RF24: " << this->controlInput.counter << std::endl; - } + } diff --git a/lib/ControllPad/controlPad.h b/lib/ControllPad/controlPad.h index 24a57ec..b81eef7 100644 --- a/lib/ControllPad/controlPad.h +++ b/lib/ControllPad/controlPad.h @@ -12,19 +12,17 @@ #pragma once #include -#include -#include -#include -// #include #include "controlPadInput.h" class ControlPad { public: - ControlPad(SPIClass *spiPort, uint8_t cePin, uint8_t csnPin); + ControlPad(); bool loop(); + void insertData(const uint8_t *data); + void setMenuControl(MenuControl* menuControl) { this->menuControl = menuControl; } const ControlPadInput * getControlPadDataPtr(uint16_t maxElapsedTime = 0) const; @@ -33,7 +31,6 @@ class ControlPad { private: MenuControl* menuControl = nullptr; - RF24* radio; ControlPadInput controlInput; void receiveData(); diff --git a/src/main.cpp b/src/main.cpp index dc91c48..fdc5abb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,6 +59,8 @@ bool wifiIsActive; void i2cScanner(void); void makeMenu(void); void restart(void); +void recieveCallback (const uint8_t * mac, const uint8_t *incomingData, int len); +void sendCallback (const uint8_t *mac_addr, esp_now_send_status_t status); void setup() { @@ -67,11 +69,15 @@ void setup() { DebugTimes setupTime; char wifiIndicator = 'X'; + WiFi.mode(WIFI_MODE_STA); + Serial.print("MAC Address: "); + Serial.println(WiFi.macAddress()); + spiPort = new SPIClass(HSPI); spiPort->begin(UBLOX_GNSS_SPI_SCK, UBLOX_GNSS_SPI_CIPO, UBLOX_GNSS_SPI_COPI, UBLOX_GNSS_SPI_CS); mainBattery = new Battery(35, 692000, 218500); - controlPad = new ControlPad(spiPort, RF24_CE_PIN, RF24_CSN_PIN); + controlPad = new ControlPad(); controlPad->setMenuControl(main_m); Wire.begin(21, 19); @@ -94,11 +100,12 @@ void setup() { } else outputBuf = new OutputBuf(); wifiIndicator = '-'; - } else { + } else outputBuf = new OutputBuf(); - } std::cout.rdbuf(outputBuf); + Network::connectEspNow(recieveCallback, sendCallback); + std::cout << "Welcome to Kleiax-Rover" << std::endl; std::cout << "All actions from the main program run on Core -> " << xPortGetCoreID() << std::endl; @@ -118,6 +125,23 @@ void setup() { } void loop() { + static uint32_t lastMillis = 0; + if (millis() - lastMillis > 5000) { + lastMillis = millis(); + char blub[] = {"Du dumme Bla"}; + esp_err_t result = esp_now_send(Network::getBroadcastAddress(), (uint8_t *) blub, sizeof(blub)); + + if (result == ESP_OK) { + Serial.println("Sent with success"); + } + else { + Serial.println("Error sending the data"); + } + // debugMqtt->sendMsg(Loglevel::info, "Alive"); + + } + + if (wifiIsActive) { DebugTimes wifiTime; Network::checkWiFi(); @@ -260,3 +284,14 @@ void restart(void) { lcdWrapper->print("Rebooting ..."); ESP.restart(); } + +void recieveCallback (const uint8_t * mac, const uint8_t *incomingData, int len) { + if (len != sizeof(ControlPadInput)) + return; + controlPad->insertData(incomingData); +} + +void sendCallback (const uint8_t *mac_addr, esp_now_send_status_t status) { + if (status != ESP_NOW_SEND_SUCCESS) + std::cout << "sendCallback - Delivery Fail" << std::endl; +} diff --git a/src/network.cpp b/src/network.cpp index 7520df7..11d97f9 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -20,6 +20,9 @@ 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); @@ -58,6 +61,10 @@ bool Network::connectMQTT() { } 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; @@ -81,6 +88,28 @@ bool Network::connectWifi() { 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; } @@ -100,6 +129,14 @@ bool Network::checkMQTT() { return true; } +int32_t Network::getWiFiChannel(const char *ssid) { + if (int32_t n = WiFi.scanNetworks()) + for (uint8_t i=0; i