switch from rf24 to espnow
This commit is contained in:
@@ -15,9 +15,13 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
#include <esp_now.h>
|
||||||
|
|
||||||
#include "networkConfig.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
|
* @brief This class handles all network stuff
|
||||||
*
|
*
|
||||||
@@ -39,6 +43,7 @@ class Network {
|
|||||||
* @return false Timeout
|
* @return false Timeout
|
||||||
*/
|
*/
|
||||||
static bool connectWifi();
|
static bool connectWifi();
|
||||||
|
static bool connectEspNow(recieveCallbackPtr reci, sendCallbackPtr send);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set all general settings to connect to a broker.
|
* @brief Set all general settings to connect to a broker.
|
||||||
@@ -71,6 +76,7 @@ class Network {
|
|||||||
* @return PubSubClient*
|
* @return PubSubClient*
|
||||||
*/
|
*/
|
||||||
static PubSubClient* getMqttClient();
|
static PubSubClient* getMqttClient();
|
||||||
|
static const uint8_t * getBroadcastAddress() { return broadcastAddress; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
@@ -80,6 +86,10 @@ class Network {
|
|||||||
* @return false if the connect attemp was unsuccessful
|
* @return false if the connect attemp was unsuccessful
|
||||||
*/
|
*/
|
||||||
static bool connectMQTT();
|
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 local_IP;
|
||||||
static IPAddress gateway;
|
static IPAddress gateway;
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
*/
|
*/
|
||||||
#define WLAN_CONNECT_LOOP_TIME 500
|
#define WLAN_CONNECT_LOOP_TIME 500
|
||||||
#define WLAN_DNS_SERVER "8.8.8.8"
|
#define WLAN_DNS_SERVER "8.8.8.8"
|
||||||
|
#define ESP_NOW_CONTROLER_MAC {0xC8, 0xC9, 0xA3, 0xC8, 0x57, 0x10}
|
||||||
|
|
||||||
#define NTRIP_RTK2GO
|
#define NTRIP_RTK2GO
|
||||||
|
|
||||||
|
|||||||
@@ -11,34 +11,8 @@
|
|||||||
|
|
||||||
#include "controlPad.h"
|
#include "controlPad.h"
|
||||||
|
|
||||||
ControlPad::ControlPad(SPIClass *spiPort, uint8_t cePin, uint8_t csnPin) {
|
ControlPad::ControlPad() {
|
||||||
this->radio = new RF24(cePin, csnPin);
|
|
||||||
|
|
||||||
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() {
|
bool ControlPad::loop() {
|
||||||
@@ -78,6 +52,16 @@ bool ControlPad::loop() {
|
|||||||
return true;
|
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 {
|
const ControlPadInput * ControlPad::getControlPadDataPtr(uint16_t maxElapsedTime) const {
|
||||||
if (maxElapsedTime && millis() - this->lastMessageReceive > maxElapsedTime)
|
if (maxElapsedTime && millis() - this->lastMessageReceive > maxElapsedTime)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -85,10 +69,5 @@ const ControlPadInput * ControlPad::getControlPadDataPtr(uint16_t maxElapsedTime
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ControlPad::receiveData() {
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,19 +12,17 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <menuControl.h>
|
#include <menuControl.h>
|
||||||
#include <nRF24L01.h>
|
|
||||||
#include <RF24.h>
|
|
||||||
#include <SPI.h>
|
|
||||||
// #include <printf.h>
|
|
||||||
|
|
||||||
#include "controlPadInput.h"
|
#include "controlPadInput.h"
|
||||||
|
|
||||||
class ControlPad {
|
class ControlPad {
|
||||||
public:
|
public:
|
||||||
ControlPad(SPIClass *spiPort, uint8_t cePin, uint8_t csnPin);
|
ControlPad();
|
||||||
|
|
||||||
bool loop();
|
bool loop();
|
||||||
|
|
||||||
|
void insertData(const uint8_t *data);
|
||||||
|
|
||||||
void setMenuControl(MenuControl* menuControl) { this->menuControl = menuControl; }
|
void setMenuControl(MenuControl* menuControl) { this->menuControl = menuControl; }
|
||||||
|
|
||||||
const ControlPadInput * getControlPadDataPtr(uint16_t maxElapsedTime = 0) const;
|
const ControlPadInput * getControlPadDataPtr(uint16_t maxElapsedTime = 0) const;
|
||||||
@@ -33,7 +31,6 @@ class ControlPad {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
MenuControl* menuControl = nullptr;
|
MenuControl* menuControl = nullptr;
|
||||||
RF24* radio;
|
|
||||||
ControlPadInput controlInput;
|
ControlPadInput controlInput;
|
||||||
|
|
||||||
void receiveData();
|
void receiveData();
|
||||||
|
|||||||
+38
-3
@@ -59,6 +59,8 @@ bool wifiIsActive;
|
|||||||
void i2cScanner(void);
|
void i2cScanner(void);
|
||||||
void makeMenu(void);
|
void makeMenu(void);
|
||||||
void restart(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() {
|
void setup() {
|
||||||
@@ -67,11 +69,15 @@ void setup() {
|
|||||||
DebugTimes setupTime;
|
DebugTimes setupTime;
|
||||||
char wifiIndicator = 'X';
|
char wifiIndicator = 'X';
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_MODE_STA);
|
||||||
|
Serial.print("MAC Address: ");
|
||||||
|
Serial.println(WiFi.macAddress());
|
||||||
|
|
||||||
spiPort = new SPIClass(HSPI);
|
spiPort = new SPIClass(HSPI);
|
||||||
spiPort->begin(UBLOX_GNSS_SPI_SCK, UBLOX_GNSS_SPI_CIPO, UBLOX_GNSS_SPI_COPI, UBLOX_GNSS_SPI_CS);
|
spiPort->begin(UBLOX_GNSS_SPI_SCK, UBLOX_GNSS_SPI_CIPO, UBLOX_GNSS_SPI_COPI, UBLOX_GNSS_SPI_CS);
|
||||||
|
|
||||||
mainBattery = new Battery(35, 692000, 218500);
|
mainBattery = new Battery(35, 692000, 218500);
|
||||||
controlPad = new ControlPad(spiPort, RF24_CE_PIN, RF24_CSN_PIN);
|
controlPad = new ControlPad();
|
||||||
controlPad->setMenuControl(main_m);
|
controlPad->setMenuControl(main_m);
|
||||||
|
|
||||||
Wire.begin(21, 19);
|
Wire.begin(21, 19);
|
||||||
@@ -94,11 +100,12 @@ void setup() {
|
|||||||
} else
|
} else
|
||||||
outputBuf = new OutputBuf();
|
outputBuf = new OutputBuf();
|
||||||
wifiIndicator = '-';
|
wifiIndicator = '-';
|
||||||
} else {
|
} else
|
||||||
outputBuf = new OutputBuf();
|
outputBuf = new OutputBuf();
|
||||||
}
|
|
||||||
std::cout.rdbuf(outputBuf);
|
std::cout.rdbuf(outputBuf);
|
||||||
|
|
||||||
|
Network::connectEspNow(recieveCallback, sendCallback);
|
||||||
|
|
||||||
std::cout << "Welcome to Kleiax-Rover" << std::endl;
|
std::cout << "Welcome to Kleiax-Rover" << std::endl;
|
||||||
std::cout << "All actions from the main program run on Core -> " << xPortGetCoreID() << std::endl;
|
std::cout << "All actions from the main program run on Core -> " << xPortGetCoreID() << std::endl;
|
||||||
|
|
||||||
@@ -118,6 +125,23 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
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) {
|
if (wifiIsActive) {
|
||||||
DebugTimes wifiTime;
|
DebugTimes wifiTime;
|
||||||
Network::checkWiFi();
|
Network::checkWiFi();
|
||||||
@@ -260,3 +284,14 @@ void restart(void) {
|
|||||||
lcdWrapper->print("Rebooting ...");
|
lcdWrapper->print("Rebooting ...");
|
||||||
ESP.restart();
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ IPAddress Network::dnsServer;
|
|||||||
WiFiClient Network::wifi_client;
|
WiFiClient Network::wifi_client;
|
||||||
PubSubClient* Network::mqtt_client;
|
PubSubClient* Network::mqtt_client;
|
||||||
|
|
||||||
|
esp_now_peer_info_t Network::peerInfo;
|
||||||
|
uint8_t Network::broadcastAddress[6] = ESP_NOW_CONTROLER_MAC;
|
||||||
|
|
||||||
void Network::setIps() {
|
void Network::setIps() {
|
||||||
local_IP.fromString(WLAN_IP);
|
local_IP.fromString(WLAN_IP);
|
||||||
gateway.fromString(WLAN_GATEWAY);
|
gateway.fromString(WLAN_GATEWAY);
|
||||||
@@ -58,6 +61,10 @@ bool Network::connectMQTT() {
|
|||||||
}
|
}
|
||||||
bool Network::connectWifi() {
|
bool Network::connectWifi() {
|
||||||
// Configures static IP address
|
// 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))
|
if (!WiFi.config(local_IP, gateway, subnet, dnsServer))
|
||||||
std::cout << "STA Failed to configure" << std::endl;
|
std::cout << "STA Failed to configure" << std::endl;
|
||||||
|
|
||||||
@@ -81,6 +88,28 @@ bool Network::connectWifi() {
|
|||||||
std::cout << "WiFi connected." << std::endl;
|
std::cout << "WiFi connected." << std::endl;
|
||||||
std::cout << "IP address: " << std::endl;
|
std::cout << "IP address: " << std::endl;
|
||||||
std::cout << WLAN_IP << 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,6 +129,14 @@ bool Network::checkMQTT() {
|
|||||||
return true;
|
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() {
|
void Network::checkWiFi() {
|
||||||
static uint64_t previousMillis = 0;
|
static uint64_t previousMillis = 0;
|
||||||
static uint16_t delay = 5000;
|
static uint16_t delay = 5000;
|
||||||
|
|||||||
Reference in New Issue
Block a user