Attempting to communicate to WLED using UDP

Hello, I am trying to get UDP stuff to work with an ESP32.
This is the code I currently have:

import socket

# Set the IP address and port of the WLED controller
controller_ip = "" (my ESP IP)
controller_port = 21324

# Create a socket to send data to the controller
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Define the LED data
led_data = bytearray([0, 0, 255, 0, 0])  # LED 0, red color (255, 0, 0)

# Send the LED data to the controller
sock.sendto(led_data, (controller_ip, controller_port))

In my mind, this should turn the first LED red. however nothing happens. Am I not understanding this code correctly?

I got the UDP port # in the sync tab, next to UDP port.

ESP32 is running 0.14.0-b1

I’m not an expert in this area by any means, but from my POV you may be conflating a few communication techniques:

From the KB: Websocket comms which can give you live control via the JSON API.

From a GitHub search: UDP-Realtime-Control which may be closer to what you’re trying to achieve. From what I can tell, the output format is slightly more complicated than your example.

Sadly the JSON API is too slow for my needs, so perhaps I should restate my question;
Could someone assist me by giving me a piece of code that would change the color of the first LED in WLED to red. I dont need any of the backend stuff, but I just cannot understand how UDP connections work with WLED.

I have made some progress since my first post! After spending a lot of today working on trying to understand this. But I still have some questions for any dev reading this.

So, I want to use the DNRGB protocol, which means my byte stream needs to start with a 4 (in binary 00000100),

My second value is how long I want the LEDs to stay on, in my case this doesn’t matter since im hopping to ping em like 30 times a second, so lets say 1. (00000001).

My third and fourth value are a mystery to me, in the docs they are simply referred to as “start index low byte and start index high byte”. My assumption is this is 16 bits allowing you to set an index between 1 and 2^16. (please do confirm if this is correct).

My fifth, sixth, and seventh values are the RGB values of the next pixel, and this repeats until my packet ends after 489 LEDs.

So for example, if I wanted to simply turn the first LED to red, I would send the following data as far as I understand;
00000100 00000001 00000000 00000000 11111111 00000000 00000000

    4               1                       0                       255         0               0               

Protocol 4 | 1 second | Index 0 | R = 255 | B=0 | G= 0

2 questions then:

  1. how does the third and fourth byte need to be formatted (technically 2nd and 3rd if we start counting from 0)?
  2. Does the byte stream need to be terminated. IE; DMX has these flags bytes they specify at the end of packages to tell the driver software to “display everything now”, or are my last 3 bytes just the RGB value of the 489th led in that packet.

Hopefully my question makes sense, and thanks to anyone who takes the time to answer this.

Why don’t you check udp.cpp and read from the UDP code?

Where could I find this sorry?

That’s a reference to the source code files in WLED GitHub Source code

If I’m using curl with Json API then the command would be

curl -H "Content-Type: application/json" -X POST -d '{"seg": {"id":1,"i": [0,1,[255,0,0]]}}'

Why don’t you try this payload with your program. This turns on 0th LED.

I think what you are missing is the segment.

I have gotten the JSON API stuff to work, enough to realize I cant get 45 FPS with 1764 LEDs unfortunately. the advantage of UDP is it s lower level. I will take a look at the source code and see if I can figure out the combination of bytes to get this to work. Thanks!