switch from rf24 to espnow
This commit is contained in:
+38
-3
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user