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