ESPHome ESP8266 UDP Usage?

Hi,
I would like to use UDP on platform ESP8266. But I was running into trouble …
The built-in UDP Component

has a udp.write action and a on_receive trigger.
But if I try to compile I always get errors like “Unable to find action with the name …”.

I looked into the API-Documentation
https://api-docs.esphome.io/classesphome_1_1udp_1_1_u_d_p_component
and found the public member function send_packet but not udp.write

Now I’m confused …

  • is udp not implemented on ESP8266 (only implemented on ESP32?)
  • maybe it’s necessary to include a library

Could please anybody point me into the right direction?
Thanks!
Paule

Hi, please post your yaml, using formatted text tags (</> on the posting toolbar) so it looks something like:

esphome:
  name: sunroom-sensor

esp8266:
  board: nodemcuv2

wifi:
  ssid: toggle
  password: !secret wifi_password
  output_power: 8.5db

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

etc...

Udp is implemented for both, but there are some differences.
Anyway, udp.write is valid syntax, you will find it if you dig into the library files.
So you likely have some other error on your code.

Hi Daryl, thanks for your response

esphome:
  platform: ESP8266
  board: nodemcuv2
  name: udptest
  friendly_name: UDPTest

api:
  encryption:
    key: !secret encryptionkey

ota:
  - platform: esphome
    password: !secret passwordota

wifi: 
  ssid: !secret ssid
  password: !secret passwordwlan
  manual_ip: 
    static_ip: 192.168.178.231
    gateway: 192.168.178.1
    subnet: 255.255.255.0
    dns1: 192.168.178.1
    dns2: 8.8.8.8
    
logger:
  baud_rate: 0       # 9600   0 schaltet loggen ueber UART ab
                     # notwendig, da die Standard-UART belegt ist         
                     # via netzwerk wird weiterhin geloggt
  level: DEBUG       # level: VERBOSE
  
udp:
  - id: udp_sender
    addresses: 192.168.178.242
    port: 3333
    on_receive:
      then:
        - logger.log:
            format: Received %s
            args:
              - format_hex_pretty(data).c_str()

#script:    #Source: Magic Lambda
#  - id: send_udp
#    parameters:
#      msg: string
#      host: string
#      port: int
#    then:
#      - lambda: |-
#          int sock = ::socket(AF_INET, SOCK_DGRAM, 0);
#          struct sockaddr_in destination, source;
#
#          destination.sin_family = AF_INET;
#          destination.sin_port = htons(port);
#          destination.sin_addr.s_addr = inet_addr(host.c_str());
#
#          // you can remove the next 4 lines if you don't want to set the source port for outgoing packets
#          source.sin_family = AF_INET;
#          source.sin_addr.s_addr = htonl(INADDR_ANY);
#          source.sin_port = htons(64998);  // the source port number
#          bind(sock, (struct sockaddr*)&source, sizeof(source));
#
#          int n_bytes = ::sendto(sock, msg.c_str(), msg.length(), 0, reinterpret_cast<sockaddr*>(&destination), sizeof(destination));
#          ESP_LOGD("lambda", "Sent %s to %s:%d in %d bytes", msg.c_str(), host.c_str(), port, n_bytes);
#          ::close(sock);
            
interval:
  - interval: 5s
    then:
      - udp_sender.send_packet:
          data: "Hello World!"
#      - script.execute:
#          id: send_udp
#          msg: "Hello World!"
#          host: "192.168.1.10"
#          port: 4444

… and the output:
INFO ESPHome 2024.12.2

INFO Reading configuration ESPHome_UDP_Test.yaml...
Failed config

udp: [source ESPHome_UDP_Test.yaml:32]
  - id: udp_sender
    addresses: 192.168.178.242
    port: 3333

    [on_receive] is an invalid option for [udp]. Please check the indentation.
    on_receive:
      then:
        - logger.log:
            format: Received %s
            args:
              - format_hex_pretty(data).c_str()

You appear to be using an older version of ESPHome, the format of this block is no longer valid and if you are using a recent version would have given you an error.

Maybe you are using a version without UDP support (or at least an earlier version of the UDP platform).

Hi,
I’ve deinstalled and reinstalled python, pip and esphome. After some small changes in the source-code …
All works fine!
Thanks for your help!

During installation I’ve got an error:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
gflanguages 0.7.1 requires protobuf<4,>=3.7.0, but you have protobuf 7.34.1 which is incompatible.
Successfully installed MarkupSafe-3.0.3 PyYAML-6.0.3 aioesphomeapi-44.6.2 annotated-types-0.7.0 bleak-2.1.1 cbor2-5.9.0 cffi-2.0.0 click-8.3.1 crcmod-1.7 cryptography-46.0.5 esphome-2026.3.3 esphome-dashboard-20260210.0 esphome-glyphsets-0.2.0 esptool-5.2.0 jinja2-3.1.6 markdown-it-py-4.0.0 mdurl-0.1.2 pillow-12.1.1 platformio-6.1.19 protobuf-7.34.1 puremagic-1.30 pydantic-2.12.5 pydantic-core-2.41.5 pygments-2.20.0 requests-2.32.5 resvg-py-0.2.6 rich-14.3.3 rich_click-1.9.7 ruamel.yaml-0.19.1 ruamel.yaml.clib-0.2.15 smp-4.0.2 smpclient-6.0.0 tornado-6.5.5 typing-extensions-4.15.0 typing-inspection-0.4.2 tzlocal-5.3.1 voluptuous-0.16.0 winrt-Windows.Devices.Bluetooth-3.2.1 winrt-Windows.Devices.Bluetooth.Advertisement-3.2.1 winrt-Windows.Devices.Bluetooth.GenericAttributeProfile-3.2.1 winrt-Windows.Devices.Enumeration-3.2.1 winrt-Windows.Devices.Radios-3.2.1 winrt-Windows.Foundation-3.2.1 winrt-Windows.Foundation.Collections-3.2.1 winrt-Windows.Storage.Streams-3.2.1 winrt-runtime-3.2.1 zeroconf-0.148.0

No idea on that one but if it works then :roll_eyes:

Hi Daryl, I’am also not affraid about the dependencies …

Now my program works well with one UDP-sender (this is a industrial produced part, no chance for any change).
I receive the UDP-message and send the correct answer.

But I need to handle many of these parts. The only difference between the UDP-senders is the IP-address. But unfortunatelly there is no access from the UDP-component in ESPHome to get the address …

I see only three options:

  • play Man-In-The-Middle with another µC and change the content of the UDP-messages (e.g. add an information about the IP-address)
  • dive into the source-code to get the IP-address inside ESPHome from anywhere
  • feature request

Does anybody has an idea???
Paule