This project uses an ESP32 to read the Joystick Control data sent via UDP. The system identifies the device using mDNS, starts a UDP Server, and receive the joystick control frames.
Libraries Used
- Async UDP: Provides asynchronous UDP support, this process runs on
Core 1
of ESP32 in parallel withCore 0
so this is non-blocking in nature. - ESPmDNS: For mDNS discovery to find the IP address of the transmitting device on the local network.
- HttpClient: For making HTTP request to the transmitting device to start or stop the joystick service.
- WiFi: To connect ESP32 to local WiFi network
- FreeRTOS: For managing queues,i.e., transferring data from
Core 1
toCore 0
Important
- ESP32 has 2 cores
Core 0
andCore 1
,Core 0
runs the code in thesetup()
andloop()
functions,Core 1
is dedicated for Networking related Tasks, likeWifi
andBluetooth
Callbacks. - There should be minimum processing in the
Core 1
and it should be only used to receive the data and then sent it toCore 0
for further processing. - For sending data form
Core 1
toCore 0
one should never use direct variables addressing as the two cores run asynchronously and this can result in random bits flipping. To send data between core one must usequeues
.
startWiFi()
: Initializes the WiFi connection.startmDNS()
: Sets up mDNS to discover the device and find its IP and port.startJoystick(IP)
: Asks the Joystick Device to begin communication.startAsyncUDPServer(port)
: Starts listening on the UDP port.
-
Setup
- The
Serial
connection starts at 115200 baud for debugging. - A FreeRTOS queue
udpQueue
is created to handle incoming UDP joystick packets (pkt
). - WiFi and mDNS are started using the
startWiFi()
andstartmDNS()
functions. - The joystick control service begins using
startJoystick()
with the IP found through mDNS. - The UDP server starts with
startAsyncUDPServer()
on the port obtained from mDNS.
- The
-
Loop
- Inside the loop(), the code waits to receive packets from the UDP queue using xQueueReceive().
- When a packet arrives, the channel data is read from and copied to a
struct
calledPacket
.
- The ExpressLRS pull request that inspired this implementation: ExpressLRS/ExpressLRS#2444
- Espresiff documentation, on how to use
queues
andFreeRTOS
. - This article on
UDP
Protocol