-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Hello world support for UDP broadcasts over the LAN on ESP32 #5779
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #pragma once | ||
| #if HAS_UDP_MULTICAST | ||
| #include "configuration.h" | ||
| #include "main.h" | ||
| #include "mesh/Router.h" | ||
|
|
||
| #include <AsyncUDP.h> | ||
| #include <WiFi.h> | ||
|
|
||
| #define UDP_MULTICAST_DEFAUL_PORT 4403 // Default port for UDP multicast is same as TCP api server | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| #define UDP_MULTICAST_THREAD_INTERVAL_MS 15000 | ||
|
|
||
| class UdpMulticastThread : public concurrency::OSThread | ||
| { | ||
| public: | ||
| UdpMulticastThread() : OSThread("UdpMulticast") { udpIpAddress = IPAddress(224, 0, 0, 69); } | ||
|
|
||
| void start() | ||
| { | ||
| if (udp.listenMulticast(udpIpAddress, UDP_MULTICAST_DEFAUL_PORT)) { | ||
| LOG_DEBUG("UDP Listening on IP: %s", WiFi.localIP().toString().c_str()); | ||
| udp.onPacket([this](AsyncUDPPacket packet) { onReceive(packet); }); | ||
| } else { | ||
| LOG_DEBUG("Failed to listen on UDP"); | ||
| } | ||
| } | ||
|
|
||
| void onReceive(AsyncUDPPacket packet) | ||
| { | ||
| size_t packetLength = packet.length(); | ||
| LOG_DEBUG("UDP broadcast from: %s, len=%u", packet.remoteIP().toString().c_str(), packetLength); | ||
| meshtastic_MeshPacket mp; | ||
| uint8_t bytes[meshtastic_MeshPacket_size]; // Allocate buffer for the data | ||
| size_t packetSize = packet.readBytes(bytes, packet.length()); | ||
| LOG_DEBUG("Decoding MeshPacket from UDP len=%u", packetSize); | ||
| bool isPacketDecoded = pb_decode_from_bytes(bytes, packetLength, &meshtastic_MeshPacket_msg, &mp); | ||
| if (isPacketDecoded && router) { | ||
| UniquePacketPoolPacket p = packetPool.allocUniqueCopy(mp); | ||
| // Unset received SNR/RSSI | ||
| p->rx_snr = 0; | ||
| p->rx_rssi = 0; | ||
| router->enqueueReceivedMessage(p.release()); | ||
| } | ||
| } | ||
|
|
||
| bool onSend(const meshtastic_MeshPacket *mp) | ||
| { | ||
| if (!mp || WiFi.status() != WL_CONNECTED) { | ||
| return false; | ||
| } | ||
| LOG_DEBUG("Broadcasting packet over UDP (id=%u)", mp->id); | ||
| uint8_t buffer[meshtastic_MeshPacket_size]; | ||
| size_t encodedLength = pb_encode_to_bytes(buffer, sizeof(buffer), &meshtastic_MeshPacket_msg, mp); | ||
| udp.broadcastTo(buffer, encodedLength, UDP_MULTICAST_DEFAUL_PORT); | ||
| return true; | ||
| } | ||
|
|
||
| protected: | ||
| int32_t runOnce() override | ||
| { | ||
| canSleep = true; | ||
| // TODO: Implement nodeinfo broadcast | ||
| return UDP_MULTICAST_THREAD_INTERVAL_MS; | ||
| } | ||
|
|
||
| private: | ||
| IPAddress udpIpAddress; | ||
| AsyncUDP udp; | ||
| }; | ||
| #endif // ARCH_ESP32 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.