Finished ping support

Not immediately useful, but it is interesting to know how low an FDRS ping is (in ms). I won't ruin the surprise, but it's pretty low.

More importantly however, I laid out how SystemPackets will work. I might rename those.
This commit is contained in:
Timm Bogner 2022-07-22 01:37:57 -05:00
parent 3c3231e4f0
commit d04639a865
5 changed files with 69 additions and 3 deletions

View File

@ -729,9 +729,24 @@ void handleCommands() {
switch (theCmd.cmd) {
case cmd_ping:
DBG("Ping back to sender");
SystemPacket sys_packet;
sys_packet.cmd = cmd_ping;
#if defined(ESP32)
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, incMAC, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer");
return;
}
#endif
esp_now_send(incMAC, (uint8_t *) &sys_packet, sizeof(SystemPacket));
esp_now_del_peer(incMAC);
break;
case cmd_add:
DBG("Add sender to peer list");
DBG("Add sender to peer list (not completed)");
break;
}
theCmd.cmd = cmd_clear;

View File

@ -5,7 +5,7 @@
#include <fdrs_globals.h>
#define FDRS_DEBUG //Enable USB-Serial debugging
#define UNIT_MAC 0x01 // The address of this gateway
#define UNIT_MAC 0x04 // The address of this gateway
// Actions -- Define what happens when a packet arrives at each interface:
// Current function options are: sendESPNOW(MAC), sendSerial(), sendMQTT(),

View File

@ -17,6 +17,7 @@ void setup() {
beginFDRS();
}
void loop() {
pingFDRS(1000);
data1 = readHum();
loadFDRS(data1, HUMIDITY_T);
data2 = readTemp();

View File

@ -59,6 +59,27 @@ uint8_t LoRaAddress[] = {0x42, 0x00};
uint32_t wait_time = 0;
DataReading fdrsData[espnow_size];
uint8_t data_count = 0;
bool is_ping = false;
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
#if defined(ESP8266)
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
}
void OnDataRecv(uint8_t* mac, uint8_t *incomingData, uint8_t len) {
#elif defined(ESP32)
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
}
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
#endif
if (len < sizeof(DataReading)) {
SystemPacket command;
memcpy(&command, incomingData, sizeof(command));
if (command.cmd == cmd_ping) {
is_ping = true;
return;
}
}
}
void beginFDRS() {
#ifdef FDRS_DEBUG
@ -81,13 +102,18 @@ void beginFDRS() {
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_recv_cb(OnDataRecv);
// Register peers
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#elif defined(ESP32)
if (esp_now_init() != ESP_OK) {
DBG("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(OnDataRecv);
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
@ -166,3 +192,26 @@ void sleepFDRS(int sleep_time) {
DBG(" Delaying.");
delay(sleep_time * 1000);
}
void pingFDRS(int timeout) {
SystemPacket sys_packet;
sys_packet.cmd = cmd_ping;
#ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *) &sys_packet, sizeof(SystemPacket));
DBG(" ESP-NOW ping sent.");
uint32_t ping_start = millis();
is_ping = false;
while ((millis() - ping_start) <= timeout) {
yield(); //do I need to yield or does it automatically?
if (is_ping) {
DBG("Ping Returned:" + String(millis() - ping_start));
break;
}
}
#endif
#ifdef USE_LORA
//transmitLoRa(gtwyAddress, sys_packet, data_count); // TODO: Make this congruent to esp_now_send()
DBG(" LoRa ping not sent because it isn't implemented.");
#endif
}

View File

@ -5,12 +5,13 @@
#include <fdrs_globals.h>
#define READING_ID 1 //Unique ID for this sensor
#define GTWY_MAC 0x01 //Address of the nearest gateway
#define GTWY_MAC 0x04 //Address of the nearest gateway
#define USE_ESPNOW
//#define USE_LORA
#define DEEP_SLEEP
//#define POWER_CTRL 14
#define DYNAMIC_GTWY
#define FDRS_DEBUG
//SPI Configuration -- Needed only on chipsets with multiple SPI interfaces (ESP32)