ESP Now Example

Example of communication between devices without a Wi-Fi router or Internet connection, using a direct local network.

Example of a master device that reads the state of a button and controls the turning on of an LED on the slave. The slave receives the command and turns on the LED, then waits for 2 seconds and responds to the master. The master receives the response and indicates it by turning on its own LED.

Master

esphome:
  name: test-com-offline-master
  friendly_name: test comunicazione offline - master

esp32:
  board: esp32dev
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "___"

ota:
  - platform: esphome
    password: "___"

wifi:
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Test-Com-Offline-Master"
    password: "12345678"

captive_portal:

espnow:
  peers:
    - "4C:11:AE:B3:96:9C"  #Slave mac 
  on_receive:
    address: "4C:11:AE:B3:96:9C"
    then:
      - if:
          condition:
            lambda: 'return size > 0 && data[0] == 0x01;'
          then:
            - output.turn_on: led
            - logger.log: "Ricevuto dato Accensione"
      - if:
          condition:
            lambda: 'return size > 0 && data[0] == 0x00;'
          then:
            - output.turn_off: led
            - logger.log: "Ricevuto dato Spegnimento"


binary_sensor:
  - platform: gpio
    pin: GPIO4
    name: "Button"
    on_press:
      then:
        - espnow.send:
            address: "4C:11:AE:B3:96:9C"  
            data: [0x01]  # Accende il LED sullo slave
        - logger.log: "Inviato dato Accensione"
        - output.turn_on: led
        - delay: 0.5s
        - espnow.send:
            address: "4C:11:AE:B3:96:9C"
            data: [0x00]  # Spegne il LED sullo slave
        - logger.log: "Inviato dato Spegnimento"
        - output.turn_off: led

output:
  - platform: gpio
    pin: GPIO2
    id: led
    inverted: False
    

Slave

esphome:
  name: test-com-offline-slave
  friendly_name: test comunicazione offline - slave

esp32:
  board: esp32dev
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "___"

ota:
  - platform: esphome
    password: "___"

wifi:
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Test-Com-Offline-Slave"
    password: "12345678"

captive_portal:

espnow:
  peers:
    - "F8:B3:B7:2A:0C:70" #Master mac 
  on_receive:
    address: "F8:B3:B7:2A:0C:70"
    then:
      - if:
          condition:
            lambda: 'return size > 0 && data[0] == 0x01;'
          then:
            - output.turn_on: led
            - logger.log: "Ricevuto dato Accensione"
            - delay: 2s
            - espnow.send:
                address: "F8:B3:B7:2A:0C:70"
                data: [0x01]
            - logger.log: "Inviato dato Accensione"
      - if:
          condition:
            lambda: 'return size > 0 && data[0] == 0x00;'
          then:
            - output.turn_off: led
            - logger.log: "Ricevuto dato Spegnimento"
            - delay: 2s
            - espnow.send:
                address: "F8:B3:B7:2A:0C:70"
                data: [0x00]
            - logger.log: "Inviato dato Spegnimento"

output:
  - platform: gpio
    pin: GPIO2
    id: led
    inverted: False
2 Likes

Thanks for this, it’ll definitely come in handy for a project I have started.

Rather than the lambda stuff and sending data packets, is it possible to send variables that relate directly to an entity that we can assign in each ESP? (looking at the docs I think the answer is NO)

eg:
I am planning on having multiple ESP’s in my car and want to be able to send fuel tank data from one ESP to the ESP monitoring vehicle speed and distance and via versa so that each ESP can then perform calculations on those entities to provide further info on displays.

Reading more on this and trying to understand it, is it even possible to send multiple different constantly updating data items from one ESP to another. ie: 3 variables that are always changing value?

The docs only seem to show sending data such as:

data: [0x00, 0x00, 0x34, 0x5d]

…which I guess could be used for multiple variables and then split out somehow by the receiving ESP…

I need to learn more.