switch from rf24 to espnow

This commit is contained in:
2023-04-08 20:28:34 +02:00
parent ede68db3df
commit e3c452436d
6 changed files with 101 additions and 42 deletions
+10
View File
@@ -15,9 +15,13 @@
#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
*
@@ -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;
+1
View File
@@ -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
+12 -33
View File
@@ -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;
}
}
+3 -6
View File
@@ -12,19 +12,17 @@
#pragma once
#include <menuControl.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>
// #include <printf.h>
#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();
+38 -3
View File
@@ -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;
}
+37
View File
@@ -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<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;