Skip to content

Commit

Permalink
Use ESP-NOW broadcast by default, while no paired radios.
Browse files Browse the repository at this point in the history
Remove old espnowBroadcast setting.
  • Loading branch information
nseidle committed Aug 26, 2024
1 parent 786012d commit 5921fcc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 88 deletions.
11 changes: 0 additions & 11 deletions Firmware/RTK_Everywhere/AP-Config/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1636,17 +1636,6 @@
</div>
</div>

<div class="form-check">
<div class="form-check mt-3">
<label class="form-check-label" for="espnowBroadcast">Broadcast Override</label>
<input class="form-check-input" type="checkbox" value="" id="espnowBroadcast" unchecked>
<span class="tt" data-bs-placement="right"
title="When true, overrides peers and sends all data via broadcast to all ESP32s in range that have ESP-Now enabled. Default: Disabled">
<span class="icon-info-circle text-primary ms-2"></span>
</span>
</div>
</div>

</div>
</div>
</div>
Expand Down
114 changes: 56 additions & 58 deletions Firmware/RTK_Everywhere/ESPNOW.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ void updateEspnow()
#ifdef COMPILE_ESPNOW
if (settings.enableEspNow == true)
{
if (espnowState == ESPNOW_PAIRED)
if (espnowState == ESPNOW_PAIRED || espnowState == ESPNOW_BROADCASTING)
{
// If it's been longer than a few ms since we last added a byte to the buffer
// then we've reached the end of the RTCM stream. Send partial buffer.
if (espnowOutgoingSpot > 0 && (millis() - espnowLastAdd) > 50)
{
if (settings.espnowBroadcast == false)
if (espnowState == ESPNOW_PAIRED)
esp_now_send(0, (uint8_t *)&espnowOutgoing, espnowOutgoingSpot); // Send partial packet to all peers
else
else // if (espnowState == ESPNOW_BROADCASTING)
{
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_send(broadcastMac, (uint8_t *)&espnowOutgoing,
Expand Down Expand Up @@ -189,8 +189,8 @@ void espnowStart()
// If WiFi is on but ESP NOW is off, then enable LR protocol
else if (wifiState > WIFI_STATE_OFF && espnowState == ESPNOW_OFF)
{
// // Enable WiFi + ESP-Now
// // Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
// Enable WiFi + ESP-Now
// Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
if (protocols != (WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR))
{
response = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N |
Expand Down Expand Up @@ -244,11 +244,35 @@ void espnowStart()

if (settings.espnowPeerCount == 0)
{
espnowSetState(ESPNOW_ON);
// Enter broadcast mode

uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

if (esp_now_is_peer_exist(broadcastMac) == true)
{
if (settings.debugEspNow == true)
systemPrintln("Broadcast peer already exists");
}
else
{
esp_err_t result = espnowAddPeer(broadcastMac, false); // Encryption not support for broadcast MAC
if (result != ESP_OK)
{
if (settings.debugEspNow == true)
systemPrintln("Failed to add broadcast peer");
}
else
{
if (settings.debugEspNow == true)
systemPrintln("Broadcast peer added");
}
}

espnowSetState(ESPNOW_BROADCASTING);
}
else
{
// If we already have peers, move to paired state
// If we have peers, move to paired state
espnowSetState(ESPNOW_PAIRED);

if (settings.debugEspNow == true)
Expand All @@ -273,32 +297,6 @@ void espnowStart()
}
}

if (settings.espnowBroadcast == true)
{
// Add broadcast peer if override is turned on
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

if (esp_now_is_peer_exist(broadcastMac) == true)
{
if (settings.debugEspNow == true)
systemPrintln("Broadcast peer already exists");
}
else
{
esp_err_t result = espnowAddPeer(broadcastMac, false); // Encryption not support for broadcast MAC
if (result != ESP_OK)
{
if (settings.debugEspNow == true)
systemPrintln("Failed to add broadcast peer");
}
else
{
if (settings.debugEspNow == true)
systemPrintln("Broadcast peer added");
}
}
}

systemPrintln("ESP-Now Started");
#endif // COMPILE_ESPNOW
}
Expand Down Expand Up @@ -401,13 +399,9 @@ bool espnowIsPaired()
#ifdef COMPILE_ESPNOW
if (espnowState == ESPNOW_MAC_RECEIVED)
{

if (settings.espnowBroadcast == false)
{
// Remove broadcast peer
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
espnowRemovePeer(broadcastMac);
}
// Remove broadcast peer
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
espnowRemovePeer(broadcastMac);

if (esp_now_is_peer_exist(receivedMAC) == true)
{
Expand Down Expand Up @@ -525,8 +519,8 @@ void espnowSetState(ESPNOWState newState)
case ESPNOW_OFF:
systemPrintln("ESPNOW_OFF");
break;
case ESPNOW_ON:
systemPrintln("ESPNOW_ON");
case ESPNOW_BROADCASTING:
systemPrintln("ESPNOW_BROADCASTING");
break;
case ESPNOW_PAIRING:
systemPrintln("ESPNOW_PAIRING");
Expand All @@ -547,31 +541,35 @@ void espnowSetState(ESPNOWState newState)
void espnowProcessRTCM(byte incoming)
{
#ifdef COMPILE_ESPNOW
if (espnowState == ESPNOW_PAIRED)
// If we are paired,
// Or if the radio is broadcasting
// Then add bytes to the outgoing buffer
if (espnowState == ESPNOW_PAIRED || espnowState == ESPNOW_BROADCASTING)
{
// Move this byte into ESP NOW to send buffer
espnowOutgoing[espnowOutgoingSpot++] = incoming;
espnowLastAdd = millis();
}

if (espnowOutgoingSpot == sizeof(espnowOutgoing))
{
espnowOutgoingSpot = 0; // Wrap
// Send buffer when full
if (espnowOutgoingSpot == sizeof(espnowOutgoing))
{
espnowOutgoingSpot = 0; // Wrap

if (settings.espnowBroadcast == false)
esp_now_send(0, (uint8_t *)&espnowOutgoing, sizeof(espnowOutgoing)); // Send packet to all peers
else
{
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_send(broadcastMac, (uint8_t *)&espnowOutgoing,
sizeof(espnowOutgoing)); // Send packet via broadcast
}
if (espnowState == ESPNOW_PAIRED)
esp_now_send(0, (uint8_t *)&espnowOutgoing, sizeof(espnowOutgoing)); // Send packet to all peers
else // if (espnowState == ESPNOW_BROADCASTING)
{
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_send(broadcastMac, (uint8_t *)&espnowOutgoing,
sizeof(espnowOutgoing)); // Send packet via broadcast
}

delay(10); // We need a small delay between sending multiple packets
delay(10); // We need a small delay between sending multiple packets

espnowBytesSent += sizeof(espnowOutgoing);
espnowBytesSent += sizeof(espnowOutgoing);

espnowOutgoingRTCM = true;
}
espnowOutgoingRTCM = true;
}
#endif // COMPILE_ESPNOW
}
Expand Down
19 changes: 9 additions & 10 deletions Firmware/RTK_Everywhere/menuMain.ino
Original file line number Diff line number Diff line change
Expand Up @@ -557,17 +557,15 @@ void menuRadio()
systemPrintln();
systemPrintln("Menu: Radios");

#ifdef COMPILE_ESPNOW
systemPrint("1) ESP-NOW Radio: ");
systemPrintf("%s\r\n", settings.enableEspNow ? "Enabled" : "Disabled");
#else // COMPILE_ESPNOW
#ifndef COMPILE_ESPNOW
systemPrintln("1) **ESP-Now Not Compiled**");
#endif // COMPILE_ESPNOW
#else // COMPILE_ESPNOW
if (settings.enableEspNow == false)
systemPrintln("1) ESP-NOW Radio: Disabled");

if (settings.enableEspNow == true)
else // ESP-NOW enabled
{
// Pretty print the MAC of all radios
systemPrint(" Radio MAC: ");
systemPrint("1) ESP-NOW Radio: Enabled - MAC ");
for (int x = 0; x < 5; x++)
systemPrintf("%02X:", wifiMACAddress[x]);
systemPrintf("%02X\r\n", wifiMACAddress[5]);
Expand All @@ -584,7 +582,7 @@ void menuRadio()
}
}
else
systemPrintln(" No Paired Radios");
systemPrintln(" No Paired Radios - Broadcast Enabled");

systemPrintln("2) Pair radios");
systemPrintln("3) Forget all radios");
Expand All @@ -598,6 +596,7 @@ void menuRadio()
systemPrintln("7) Broadcast dummy data");
}
}
#endif // COMPILE_ESPNOW

if (present.radio_lora == true)
{
Expand Down Expand Up @@ -717,7 +716,7 @@ void menuRadio()
"nearing 200 characters but needs to be near 250.";
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
#ifdef COMPILE_ESPNOW
esp_now_send(broadcastMac, (uint8_t *)&espnowData, sizeof(espnowData)); // Send packet to all peers
esp_now_send(broadcastMac, (uint8_t *)&espnowData, sizeof(espnowData)); // Send packet over broadcast
#endif
}

Expand Down
6 changes: 0 additions & 6 deletions Firmware/RTK_Everywhere/menuSystem.ino
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,6 @@ void menuDebugNetwork()
systemPrint("1) Print Ethernet diagnostics: ");
systemPrintf("%s\r\n", settings.enablePrintEthernetDiag ? "Enabled" : "Disabled");

// ESP-Now
systemPrint("2) ESP-Now Broadcast Override: ");
systemPrintf("%s\r\n", settings.espnowBroadcast ? "Enabled" : "Disabled");

// WiFi
systemPrint("3) Debug WiFi state: ");
systemPrintf("%s\r\n", settings.debugWifiState ? "Enabled" : "Disabled");
Expand Down Expand Up @@ -665,8 +661,6 @@ void menuDebugNetwork()

if (incoming == 1)
settings.enablePrintEthernetDiag ^= 1;
else if (incoming == 2)
settings.espnowBroadcast ^= 1;
else if (incoming == 3)
settings.debugWifiState ^= 1;
else if (incoming == 4)
Expand Down
4 changes: 1 addition & 3 deletions Firmware/RTK_Everywhere/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ typedef struct _NTRIP_SERVER_DATA
typedef enum
{
ESPNOW_OFF,
ESPNOW_ON,
ESPNOW_BROADCASTING,
ESPNOW_PAIRING,
ESPNOW_MAC_RECEIVED,
ESPNOW_PAIRED,
Expand Down Expand Up @@ -1059,7 +1059,6 @@ struct Settings
// ESP Now
bool debugEspNow = false;
bool enableEspNow = false;
bool espnowBroadcast = false; // When true, overrides peers and sends all data via broadcast
uint8_t espnowPeerCount = 0;
uint8_t espnowPeers[ESPNOW_MAX_PEERS][6] = {0}; // Contains the MAC addresses (6 bytes) of paired units

Expand Down Expand Up @@ -1553,7 +1552,6 @@ const RTK_Settings_Entry rtkSettingsEntries[] =
// ESP Now
{ 0, 0, 0, 0, 1, 1, 1, 1, _bool, 0, & settings.debugEspNow, "debugEspNow", },
{ 0, 1, 1, 0, 1, 1, 1, 1, _bool, 0, & settings.enableEspNow, "enableEspNow", },
{ 0, 1, 1, 0, 1, 1, 1, 1, _bool, 0, & settings.espnowBroadcast, "espnowBroadcast", },
{ 0, 1, 1, 0, 1, 1, 1, 1, _uint8_t, 0, & settings.espnowPeerCount, "espnowPeerCount", },
{ 0, 1, 1, 1, 1, 1, 1, 1, tEspNowPr, ESPNOW_MAX_PEERS, & settings.espnowPeers[0][0], "espnowPeer_", },

Expand Down

0 comments on commit 5921fcc

Please sign in to comment.