Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more neighbors to ESP-NOW and LoRa #226

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/fdrs_datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ enum
{
event_clear,
event_espnowg,
event_espnow0,
event_espnow1,
event_espnow2,
event_espnow3,
event_serial,
event_mqtt,
event_lorag,
event_lora0,
event_lora1,
event_lora2,
event_lora3,
event_internal
};

Expand Down
26 changes: 25 additions & 1 deletion src/fdrs_gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
#ifndef ESPNOWG_ACT
#define ESPNOWG_ACT
#endif
#ifndef ESPNOW0_ACT
#define ESPNOW0_ACT
#endif
#ifndef ESPNOW1_ACT
#define ESPNOW1_ACT
#endif
#ifndef ESPNOW2_ACT
#define ESPNOW2_ACT
#endif
#ifndef ESPNOW3_ACT
#define ESPNOW3_ACT
#endif
#ifndef SERIAL_ACT
#define SERIAL_ACT
#endif
Expand All @@ -25,12 +31,18 @@
#ifndef LORAG_ACT
#define LORAG_ACT
#endif
#ifndef LORA0_ACT
#define LORA0_ACT
#endif
#ifndef LORA1_ACT
#define LORA1_ACT
#endif
#ifndef LORA2_ACT
#define LORA2_ACT
#endif
#ifndef LORA3_ACT
#define LORA3_ACT
#endif
#ifndef INTERNAL_ACT
#define INTERNAL_ACT
#endif
Expand Down Expand Up @@ -239,12 +251,18 @@ void handleActions() {
case event_espnowg:
ESPNOWG_ACT
break;
case event_espnow0:
ESPNOW0_ACT
break;
case event_espnow1:
ESPNOW1_ACT
break;
case event_espnow2:
ESPNOW2_ACT
break;
case event_espnow3:
ESPNOW3_ACT
break;
case event_serial:
SERIAL_ACT
break;
Expand All @@ -254,12 +272,18 @@ void handleActions() {
case event_lorag:
LORAG_ACT
break;
case event_lora0:
LORA0_ACT
break;
case event_lora1:
LORA1_ACT
break;
case event_lora2:
LORA2_ACT
break;
case event_lora3:
LORA3_ACT
break;
case event_internal:
INTERNAL_ACT
break;
Expand Down Expand Up @@ -287,7 +311,7 @@ void loopFDRS()
// "Skeleton Functions related to FDRS Actions"
#ifndef USE_LORA
void broadcastLoRa() {}
void sendLoRaNbr(uint8_t address) {}
void sendLoRa_Nbr(uint8_t address) {}
void timeFDRSLoRa(uint8_t *address) {} // fdrs_gateway_lora.h
void sendTimeLoRa() { return; } // fdrs_gateway_time.h
void handleLoRa() { return; } // fdrs_gateway_lora.h
Expand Down
127 changes: 52 additions & 75 deletions src/fdrs_gateway_espnow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ const uint8_t mac_prefix[] = {MAC_PREFIX};
uint8_t selfAddress[] = {MAC_PREFIX, UNIT_MAC};
uint8_t incMAC[6];

uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW_NEIGHBOR_1};
uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW_NEIGHBOR_2};
// uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW_NEIGHBOR_1};
// uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW_NEIGHBOR_2};
// uint8_t ESPNOW3[] = {MAC_PREFIX, ESPNOW_NEIGHBOR_3};
// uint8_t ESPNOW4[] = {MAC_PREFIX, ESPNOW_NEIGHBOR_4};


uint8_t ESPNOW_Nbr[4][6] = {{MAC_PREFIX, ESPNOW_NEIGHBOR_0},
{MAC_PREFIX, ESPNOW_NEIGHBOR_1},
{MAC_PREFIX, ESPNOW_NEIGHBOR_2},
{MAC_PREFIX, ESPNOW_NEIGHBOR_3}};
extern time_t now;
// JL - pingFlagEspNow var probably to be removed
bool pingFlagEspNow = false;
Expand Down Expand Up @@ -57,16 +65,26 @@ void OnDataRecv(const esp_now_recv_info *pkt_info, const uint8_t *incomingData,
memcpy(&theData, incomingData, sizeof(theData));
DBG("Incoming ESP-NOW DataReading from 0x" + String(incMAC[5], HEX));
ln = len / sizeof(DataReading);
if (memcmp(&incMAC, &ESPNOW1, 6) == 0)
if (memcmp(&incMAC, &ESPNOW_Nbr[0], 6) == 0)
{
newData = event_espnow0;
return;
}
if (memcmp(&incMAC, &ESPNOW_Nbr[1], 6) == 0)
{
newData = event_espnow1;
return;
}
if (memcmp(&incMAC, &ESPNOW2, 6) == 0)
if (memcmp(&incMAC, &ESPNOW_Nbr[2], 6) == 0)
{
newData = event_espnow2;
return;
}
if (memcmp(&incMAC, &ESPNOW_Nbr[3], 6) == 0)
{
newData = event_espnow3;
return;
}
newData = event_espnowg;
}
}
Expand Down Expand Up @@ -236,81 +254,41 @@ void pingback_espnow()
}
}

void sendESPNowNbr(uint8_t interface)
void sendESPNowNbr(uint8_t neighbor)
{
switch (interface)
{
case 1:
{ // These brackets are required!
DBG("Sending DR to ESP-NOW Neighbor #1");
DBG("Sending data to ESP-NOW Neighbor #" + String(neighbor));
#if defined(ESP32)
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, ESPNOW1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
DBG("Failed to add peer");
return;
}
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, ESPNOW_Nbr[neighbor], 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
DBG("Failed to add peer");
return;
}
#endif // ESP32
DataReading thePacket[ln];
int j = 0;

for (int i = 0; i < ln; i++)
{
if (j > espnow_size)
{
j = 0;
esp_now_send(ESPNOW1, (uint8_t *)&thePacket, sizeof(thePacket));
}
thePacket[j] = theData[i];
j++;
}
esp_now_send(ESPNOW1, (uint8_t *)&thePacket, j * sizeof(DataReading));
esp_now_del_peer(ESPNOW1);
DataReading thePacket[ln];
int j = 0;

break;
} // These brackets are required!
case 2:
for (int i = 0; i < ln; i++)
{
if (j > espnow_size)
{
DBG("Sending DR to ESP-NOW Neighbor #2");
#if defined(ESP32)
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, ESPNOW2, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
DBG("Failed to add peer");
return;
}
#endif // ESP32
DataReading thePacket[ln];
int j = 0;
for (int i = 0; i < ln; i++)
{
if (j > espnow_size)
{
j = 0;
esp_now_send(ESPNOW2, (uint8_t *)&thePacket, sizeof(thePacket));
}
thePacket[j] = theData[i];
j++;
}
esp_now_send(ESPNOW2, (uint8_t *)&thePacket, j * sizeof(DataReading));
esp_now_del_peer(ESPNOW2);

break;
j = 0;
esp_now_send(ESPNOW_Nbr[neighbor], (uint8_t *)&thePacket, sizeof(thePacket));
}
thePacket[j] = theData[i];
j++;
}
esp_now_send(ESPNOW_Nbr[neighbor], (uint8_t *)&thePacket, j * sizeof(DataReading));
esp_now_del_peer(ESPNOW_Nbr[neighbor]);
}

void sendESPNowPeers()
{
DBG("Sending DR to ESP-NOW peers.");
DBG("Sending data to ESP-NOW peers.");
DataReading thePacket[ln];
int j = 0;
for (int i = 0; i < ln; i++)
Expand Down Expand Up @@ -500,14 +478,13 @@ esp_err_t sendTimeESPNow() {
esp_err_t result1 = ESP_OK, result2 = ESP_OK, result3 = ESP_OK;
SystemPacket sys_packet = { .cmd = cmd_time, .param = now };

if((timeSource.tmAddress != (ESPNOW1[4] << 8 | ESPNOW1[5])) && ESPNOW1[5] != 0x00) {
DBG1("Sending time to ESP-NOW Peer 1");
result1 = sendESPNow(ESPNOW1, &sys_packet);
}
if((timeSource.tmAddress != (ESPNOW2[4] << 8 | ESPNOW2[5])) && ESPNOW2[5] != 0x00) {
DBG1("Sending time to ESP-NOW Peer 2");
result2 = sendESPNow(ESPNOW2, &sys_packet);
for (uint8_t i = 0; i < 4; i++){
if((timeSource.tmAddress != (ESPNOW_Nbr[i][4] << 8 | ESPNOW_Nbr[i][5])) && ESPNOW_Nbr[i][5] != 0x00) {
DBG1("Sending time to ESP-NOW Neighbor #" + String(i));
result1 = sendESPNow(ESPNOW_Nbr[i], &sys_packet);
}
}

DBG1("Sending time to ESP-NOW registered peers");
result3 = sendESPNow(nullptr, &sys_packet);

Expand Down
79 changes: 37 additions & 42 deletions src/fdrs_lora.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,15 @@ commstate_t loraAckState = stReady;
const uint8_t mac_prefix[] = {MAC_PREFIX};
const uint8_t selfAddress[] = {MAC_PREFIX, UNIT_MAC};
#endif
uint16_t LoRa1 = ((mac_prefix[4] << 8) | LORA_NEIGHBOR_1); // Use 2 bytes for LoRa addressing instead of previous 3 bytes
uint16_t LoRa2 = ((mac_prefix[4] << 8) | LORA_NEIGHBOR_2);
uint16_t LoRa_Nbr[] = {((mac_prefix[4] << 8) | LORA_NEIGHBOR_0),
((mac_prefix[4] << 8) | LORA_NEIGHBOR_1),
((mac_prefix[4] << 8) | LORA_NEIGHBOR_2),
((mac_prefix[4] << 8) | LORA_NEIGHBOR_3)};
uint16_t loraBroadcast = 0xFFFF;
uint16_t gtwyAddress = ((mac_prefix[4] << 8) | UNIT_MAC); // for a gateway this is our own address
#elif defined(FDRS_NODE)
uint8_t selfAddress[6] = {0};
uint16_t LoRa1 = 0;
uint16_t LoRa2 = 0;
uint16_t LoRa_Nbr[4] = {0};
uint16_t loraBroadcast = 0;
const uint8_t mac_prefix[] = {MAC_PREFIX};
uint16_t gtwyAddress = ((mac_prefix[4] << 8) | GTWY_MAC); // for a node, this is our gateway
Expand Down Expand Up @@ -423,28 +424,27 @@ uint transmitSameAddrLoRa() {

// Send time to LoRa broadcast and peers
// Only used in gateways
void sendTimeLoRa() {
void sendTimeLoRa()
{
// Check for node or gateway. Do not send if we are a node.
if(selfAddress[0] != 0) {
DBG1("Sending time via LoRa");
SystemPacket spTimeLoRa = {.cmd = cmd_time, .param = now};
DBG1("Sending time to LoRa broadcast");
transmitLoRaAsync(&loraBroadcast, &spTimeLoRa, 1);
// Do not send to LoRa peers if their address is 0x..00
if(((LoRa1 & 0x00FF) != 0x0000) && (LoRa1 != timeSource.tmAddress)) {
DBG1("Sending time to LoRa Neighbor 1");
spTimeLoRa.param = now;
// add LoRa neighbor 1
transmitLoRaAsync(&LoRa1, &spTimeLoRa, 1);
}
if(((LoRa2 & 0x00FF) != 0x0000) && (LoRa2 != timeSource.tmAddress)) {
DBG1("Sending time to LoRa Neighbor 2");
spTimeLoRa.param = now;
// add LoRa neighbor 2
transmitLoRaAsync(&LoRa2, &spTimeLoRa, 1);
if (selfAddress[0] != 0)
{
DBG1("Sending time via LoRa");
SystemPacket spTimeLoRa = {.cmd = cmd_time, .param = now};
DBG1("Sending time to LoRa broadcast");
transmitLoRaAsync(&loraBroadcast, &spTimeLoRa, 1);
// Do not send to LoRa peers if their address is 0x..00
for (int i = 0; i < 4; i++)
{
if (((LoRa_Nbr[i] & 0x00FF) != 0x0000) && (LoRa_Nbr[i] != timeSource.tmAddress))
{
DBG1("Sending time to LoRa Neighbor " + String(i));
spTimeLoRa.param = now;
transmitLoRaAsync(&LoRa_Nbr[i], &spTimeLoRa, 1);
}
}
}
}
return;
return;
}

// Send time to LoRa node at specific address - gateway
Expand Down Expand Up @@ -543,14 +543,22 @@ crcResult receiveLoRa()
}
memcpy(&theData, &packet[4], packetSize - 6); // Split off data portion of packet (N - 6 bytes (6 bytes for headers and CRC))
ln = (packetSize - 6) / sizeof(DataReading);
if (memcmp(&sourceMAC, &LoRa1, 2) == 0)
{ // Check if it is from a registered sender
if (memcmp(&sourceMAC, &LoRa_Nbr[0], 2) == 0) // Check if it is from a registered sender
{
newData = event_lora0;
}
else if (memcmp(&sourceMAC, &LoRa_Nbr[1], 2) == 0)
{
newData = event_lora1;
}
else if (memcmp(&sourceMAC, &LoRa2, 2) == 0)
else if (memcmp(&sourceMAC, &LoRa_Nbr[2], 2) == 0)
{
newData = event_lora2;
}
else if (memcmp(&sourceMAC, &LoRa_Nbr[3], 2) == 0)
{
newData = event_lora3;
}
else {
newData = event_lorag;
}
Expand Down Expand Up @@ -763,22 +771,9 @@ void broadcastLoRa()
}

// Sends packet to neighbor gateways
void sendLoRaNbr(uint8_t interface)
void sendLoRaNbr(uint8_t neighbor)
{
DBG("Sending to LoRa neighbor buffer");
switch (interface)
{
case 1:
{
transmitLoRaAsync(&LoRa1,theData,ln);
break;
}
case 2:
{
transmitLoRaAsync(&LoRa2,theData,ln);
break;
}
}
transmitLoRaAsync(&LoRa_Nbr[neighbor],theData,ln);
}

// Handles completion of Asynchronous LoRa processes
Expand Down