Skip to content

Instantly share code, notes, and snippets.

@santolucito
Last active December 17, 2024 22:03
Show Gist options
  • Save santolucito/4016405f54850f7a216e9e453fe81803 to your computer and use it in GitHub Desktop.
Save santolucito/4016405f54850f7a216e9e453fe81803 to your computer and use it in GitHub Desktop.
Sending UDP on ESP32 in Station Mode
#include <WebServer.h>
#include <WiFi.h>
#include <WiFiUdp.h>
//set up to connect to an existing network (e.g. mobile hotspot from laptop that will run the python code)
const char* ssid = "ssid";
const char* password = "password";
WiFiUDP Udp;
unsigned int localUdpPort = 4210; // port to listen on
char incomingPacket[255]; // buffer for incoming packets
void setup()
{
int status = WL_IDLE_STATUS;
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to wifi");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
// we recv one packet from the remote so we can know its IP and port
bool readPacket = false;
while (!readPacket) {
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
readPacket = true;
}
}
}
void loop()
{
// once we know where we got the inital packet from, send data back to that IP address and port
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// Just test touch pin - Touch0 is T0 which is on GPIO 4.
Udp.printf(String(touchRead(T0)).c_str(),2);
Udp.endPacket();
delay(1000);
}
# a python program to send an initial packet, then listen for packets from the ESP32
# the laptop/rasp pi that this code runs on can still be connected to the internet, but should also "share" its connection by creating its own mobile hotspot
# this version of the code allows your laptop to remain connected to the internet (which is a postive)
# but requires configuring your laptop to share its internet connection (which can be a negative because it is tricky to set up depending on your OS)
# for version that does not require sharing an internet connection, see https://gist.github.com/santolucito/70ecb94ce297eb1b8b8034f78683447b
import socket
UDP_IP = "192.168.137.96" # The IP that is printed in the serial monitor from the ESP32
SHARED_UDP_PORT = 4210
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Internet # UDP
sock.connect((UDP_IP, SHARED_UDP_PORT))
def loop():
while True:
data = sock.recv(2048)
print(data)
if __name__ == "__main__":
sock.send('Hello ESP32'.encode())
loop()
@Alexandros444
Copy link

The beginPacket returns error code 1 (socket already Open)
Do you have to close the openend UDP after every message?

@santolucito
Copy link
Author

not unless one of libraries changed since i last ran this. my guess is that you may have multiple instances of the python code running (forgot to kill one in another terminal or something)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment