Skip to content

Instantly share code, notes, and snippets.

@ah-cog
Last active March 16, 2024 15:19
Show Gist options
  • Save ah-cog/3f4d2f074305d4d84344 to your computer and use it in GitHub Desktop.
Save ah-cog/3f4d2f074305d4d84344 to your computer and use it in GitHub Desktop.
ESP8266 AT command sequence to start a UDP server with Espressif AT firmware v0.22.

Create a UDP Server to Listen for Incoming Broadcasts

The following sequence of commands will (1) start a TCP server listening for traffic on port 80 and (2) start a UDP server and listen for incoming UDP packets from all addresses.

AT

OK
AT+CWMODE_CUR=3

OK
AT+CWJAP="AWS","Codehappy123"

OK
AT+CIFSR
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"1a:fe:34:f4:ef:b2"
+CIFSR:STAIP,"192.168.19.30"
+CIFSR:STAMAC,"18:fe:34:f4:ef:b2"

OK
AT+CIPMUX=1

OK
AT+CIPSERVER=1,80

OK
AT+CIPSTART=0,"UDP","0.0.0.0",4445,4445,2
0,CONNECT

OK

+IPD,0,15:turn light 2 on
OK

+IPD,0,16:turn light 1 off
OK

+IPD,0,13:connect servo
OK
AT+CIPCLOSE=0

0,CLOSED

OK

The generic sequence of commands is the following:

AT+CWMODE=3

OK
AT+CWJAP="AP_SSID","AP_PASSWORD"

OK
AT+CIPMUX=1

OK
AT+CIPSTART=0,"UDP","192.168.0.140",4445,4445,2
0,CONNECT

OK
AT+CIPSEND=0,7,"192.168.0.140",4445

OK
> hello\r\n
SEND OK
AT+CIPCLOSE=0
0,CLOSED

OK

Create a UDP Server (Listen to Specific Address)

Log for Creating Server:

AT+CWMODE=3

OK
AT+CWJAP="AP_SSID","AP_PASSWORD"

OK
AT+CIPMUX=1

OK
AT+CIPSTART=0,"UDP","192.168.0.140",4445,4445,2
0,CONNECT

OK

+IPD,0,13:test from nc

OK
AT+CIPCLOSE=0
0,CLOSED

OK

The AT commands used above are defined as follows:

AT+CIPSTART=<id>,<type>,<remote address>,<remote port>[,(<local port>),(<mode>)]
AT+CIPSEND=[<id>,]<length>[,<ip>,<port>]
AT+CIPCLOSE=<id>

Send test UDP Packet

Run the following command (tested on Mac OS X):

echo "test from nc" | nc -4u -w0 192.168.0.140 4445

Note that the -w0 argument can be replaced with -w1.

Notes:

  • This sequence connects to an access point (AP) with SSID "AP_SSID". This could also be done on the ESP8266 when set up as a "soft AP".
  • This sets AT+CWMODE=3. This could be different.
  • Likewise, this sets AT+CIPMUX=1. This could be AT+CIPMUX=0, but that case is not covered here.
  • Here, the UDP local port is 4445. The remote IP and port are irrespective until sending data.
@Myk014
Copy link

Myk014 commented Jul 7, 2023

I used your exemple (Create a UDP Server (Listen to Specific Address)) for work with ESP07 it is work, great!
But if change last parameter of IP adress to 255 in this exemple, it work great for broadcast datagrams listerning in local network.
Thanks your so much!

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