Smart Cities - Programming the ESP-01 using YAML

From Sensors in Schools
Revision as of 20:47, 5 October 2024 by EdmondLascaris (talk | contribs) (Created page with "= temp.yaml code = <syntaxhighlight lang="yaml"> #This ESPHome temp.yaml file configures a black ESP-01 board fitted with a Dallas DS18B20 temperature sensor #The temperature sensor is connected via the one-wire bus to GPIO02 #One sensor is for Inside Air Temperatire (IAT) and the other is for Outside Air Temperature (OAT) #To upload: esphome run temp.yaml #To upload set the name, zone and IP address: esphome -s name iat -s zone 15 -s ip_address 192.168.2.17 run temp.y...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

temp.yaml code

#This ESPHome temp.yaml file configures a black ESP-01 board fitted with a Dallas DS18B20 temperature sensor
#The temperature sensor is connected via the one-wire bus to GPIO02
#One sensor is for Inside Air Temperatire (IAT) and the other is for Outside Air Temperature (OAT)
#To upload: esphome run temp.yaml
#To upload set the name, zone and IP address: esphome -s name iat -s zone 15 -s ip_address 192.168.2.17 run temp.yaml
#To upload set the name, zone and IP address: esphome -s name oat -s zone 16 -s ip_address 192.168.2.18 run temp.yaml


#Set up constants
substitutions:
  name:          'iat'            #The name of this device
  zone:          '15'             #IAT topic
  ip_address:    '192.168.2.17'   #Static IP address of this device
#  name:          'oat'            #The name of this device
#  ip_address:    '192.168.2.18'   #Static IP address of this device
#  zone:          '16'             #OAT topic
  from_server:    'p${zone}'       #The topic for receiving messages from the server
  to_server:      's${zone}'       #The topic for sending messages to the server
  state_message:  '?'

#Set up core functionality
esphome:
  name: ${name}                   #The name of this device
  compile_process_limit: 2        #Reduce the number of cores to use. Necessary for compiling on the RPi 3.

#Set up processor and board
esp8266:                          #Use the ESP8266 platform
  board: esp01_1m                 #The Black ESP-01 board with 1MB of ram

#Set up WiFi
wifi:
  ssid:        A                  #This network is isolated from the Internet, but has an MQTT broker
  password:    <SECRET>
  manual_ip:
    static_ip: ${ip_address}
    gateway:   192.168.2.1
    subnet:    255.255.255.0

#Enable a fallback access point in case the WiFi connection fails
  ap:
    ssid: ${name}                 #Use the device name as the SSID

#Enable the captive portal for the WiFi access point
captive_portal:

#Enable logging
logger:
  logs:
    mqtt.component: DEBUG

#Enable programming Over The Air
ota:
  - platform: esphome

#Enable Message Queuing Telemetry Transport
mqtt:
  broker: 192.168.2.2             #The IP Address of the MQTT broker
  port: 1883                      #The port number of the MQTT broker
  on_message:                     #When a message is received from the MQTT broker
    - topic: ${from_server}       #When a message is received from the server
      payload: ${state_message}   #When a state message is received
      then:
        mqtt.publish:             #Publish an MQTT message
          topic: ${to_server}     #Publish a message to the server
          payload: !lambda |-
            return to_string(id(temp).state);

one_wire:
  - platform: gpio
    pin: GPIO02

sensor:                   
  - platform: dallas_temp
    id: temp
    address: 0x0200000042495a28
    update_interval: 60s
    on_value:
      then:
        mqtt.publish:             #Publish an MQTT message
          topic: ${to_server}     #Publish a message to the server
          payload: !lambda |-
            return to_string(id(temp).state);

Explanation of temp.yaml file

This temp.yaml file configures a black ESP-01 board fitted with two Dallas DS18B20 temperature sensors (one for Inside Air Temperature (IAT) and one for Outside Air Temperature (OAT)) and integrates it with MQTT to publish and receive data. Here's an interpretation of its key sections:

1. Substitutions:

The substitutions block sets up constants used throughout the file. It allows flexibility when deploying this configuration for multiple sensors.


substitutions:
  name:          'iat'            # The name of the device, 'iat' for Inside Air Temperature.
  zone:          '15'             # MQTT topic ID for this sensor (IAT).
  ip_address:    '192.168.2.17'   # Static IP address for the device.
  from_server:   'p${zone}'       # Topic to receive messages from the server, dynamically set as 'p15'.
  to_server:     's${zone}'       # Topic to send messages to the server, dynamically set as 's15'.
  state_message: '?'


  • Flexibility: You can modify this to deploy different sensors by adjusting the name, zone, and ip_address values, e.g., for oat (Outside Air Temperature).
  • from_server and to_server: These are dynamic MQTT topics to handle inbound (from_server) and outbound (to_server) messages for this specific device.

2. ESPHome Core Configuration:

esphome:
  name: ${name}                   # The device name, substituted from the 'substitutions' block.
  compile_process_limit: 2        # Limits the number of cores used during compilation (for performance reasons on RPi).
  • Device Name: Set using the substitutions variable name, so it's easy to deploy with different names for different sensors.


3. ESP8266 Platform and Board Configuration:

esp8266:
  board: esp01_1m                 # Specifies that the ESP-01 board with 1MB flash is being used.
  • Board Type: Specifically for the black ESP-01 board with 1MB of flash memory.

4. WiFi Setup:

wifi:
  ssid:        A                  # WiFi network SSID.
  password:    <SECRET>            # WiFi password.
  manual_ip:
    static_ip: ${ip_address}       # Assigns the static IP address from the 'substitutions' block.
    gateway:   192.168.2.1         # The gateway IP address.
    subnet:    255.255.255.0       # Subnet mask.
  • Static IP: The static IP address is assigned using the substitutions field, ensuring the device always uses a fixed IP address (192.168.2.17 in this case).
  • Fallback AP: If the device fails to connect to the specified WiFi network, it will fall back to creating its own AP.

5. MQTT Configuration:

mqtt:
  broker: 192.168.2.2             # The IP address of the MQTT broker on your isolated network.
  port: 1883                      # Default MQTT port (1883).
  on_message:
    - topic: ${from_server}       # Listens for messages from the server on the 'from_server' topic.
      payload: ${state_message}   # If a message matches the state_message ('?'), it triggers the next action.
      then:
        mqtt.publish:             # Publishes a response back to the server.
          topic: ${to_server}     # Publishes the current temperature to the 'to_server' topic.
          payload: !lambda |-
            return to_string(id(temp).state);  # Sends the current temperature value.
  • Broker and Port: The device connects to the MQTT broker at 192.168.2.2 (on the experimental network) on port 1883.

on_message: When a message is received on the from_server topic (e.g., 'p15'), it checks if the payload matches state_message (which is set to '?'). If so, it publishes the current temperature to the to_server topic (e.g., 's15').

  • Publishing Temperature: Uses a lambda function to publish the current temperature read from the sensor.


6. Sensor Configuration:

one_wire:
  - platform: gpio
    pin: GPIO02                   # The one-wire bus is connected to GPIO02.

sensor:                   
  - platform: dallas_temp
    id: temp
    address: 0x0200000042495a28    # The unique address of the Dallas sensor (for IAT).
    update_interval: 60s           # The sensor updates every 60 seconds.
    on_value:
      then:
        mqtt.publish:             # When the temperature is updated, publish the new value via MQTT.
          topic: ${to_server}     # Publishes to the 'to_server' topic.
          payload: !lambda |-
            return to_string(id(temp).state);
  • Dallas DS18B20: Configures the temperature sensor on the one-wire bus connected to GPIO02.
  • Sensor Address: The unique address of the DS18B20 sensor is specified to identify the exact sensor.
  • Temperature Updates: The sensor updates every 60 seconds. When it updates, the temperature value is automatically published via MQTT to the to_server topic (e.g., 's15').

Key Features:

  • Static IP Setup: The device uses a static IP address (192.168.2.17) to ensure a stable connection to the experimental router.
  • MQTT Communication: The ESP-01 device communicates with an MQTT broker (192.168.2.2). It subscribes to messages from the server and publishes temperature data back to the server.
  • Flexible Substitutions: The use of substitutions makes it easy to switch between sensors (IAT and OAT) by simply changing the name, zone, and ip_address.

How It Works:

  • Startup: The ESP-01 connects to your WiFi network using the static IP address defined.
  • MQTT Communication: It connects to the MQTT broker and listens for messages on the topic (e.g., 'p15' for IAT).
  • Sensor Updates: Every 60 seconds, the DS18B20 sensor reads the temperature, and this data is published to the MQTT broker on the corresponding topic (e.g., 's15').