cogito/esphome/devices/cogito-test-device-01.yaml

119 lines
3.5 KiB
YAML

esphome:
name: cogito-test-device-01
friendly_name: cogito_test_device_01
#on_boot:
# - priority: -100.0 # everything is initialized, system is online - if condition waits for connection
# then:
# if:
# condition:
# wifi.connected:
# then:
# - http_request.get: get_remaining_time
esp32:
board: esp32-c3-devkitm-1
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
#api:
# encryption:
# key: "9sTJ7NQXj5L72qFQZHTzmhFwl/0tzLMYsrsdt0BKSdE="
ota:
- platform: esphome
password: "eb51b9bdb426ad026bf49b69629eafd5"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Cogito-Test-Device-01"
password: "oTosL5R5DhES"
captive_portal:
# global variables
globals:
- id: remaining_time_secs
type: int
restore_value: no
initial_value: '0'
# UART for communication with STM32 device
uart:
rx_pin: GPIO20
tx_pin: GPIO21
baud_rate: 115200
# HTTP request component
http_request:
verify_ssl: False
# push button on GPIO10
binary_sensor:
- platform: gpio
pin:
number: GPIO10
mode:
input: true
pullup: true
inverted: true
name: "Test Push Button"
on_release:
then:
#- component.update: get_remaining_time
- http_request.get:
url: !secret get_remaining_time_endpoint # target URL
capture_response: true
on_response:
then:
- if:
condition:
lambda: return response->status_code == 200;
then:
# logs response on console
- logger.log:
format: 'Response status: %d, Duration: %u ms, Payload: %s'
args: ['response->status_code', 'response->duration_ms', 'body.c_str()']
# parses json payload and reads "secs" property
- lambda: |-
json::parse_json(body, [](JsonObject root) -> bool {
if (root["secs"]) {
id(remaining_time_secs) = root["secs"];
return true;
}
else {
ESP_LOGI(TAG,"[get_remaining_time] Error: missing or invalid 'secs' key in json payload");
id(remaining_time_secs) = 0;
return false;
}
});
# sends remaining time over UART
- uart.write: !lambda
char buf[64];
sprintf(buf, "\0x02{\"remaining_time_secs\":%d}", id(remaining_time_secs));
return buf;
else:
- logger.log:
format: "Error: Response status: %d, message %s"
args: [ 'response->status_code', 'body.c_str()' ]
#http_request:
# id: get_remaining_time
# url: !secret get_remaining_time_endpoint # Your target URL
# method: get # Specify the HTTP method
# headers: # Optional headers, e.g., for content type
# Content-Type: application/json
# on_response:
# then:
# - logger.log:
# format: 'Response status: %d, Duration: %u ms'
# args:
# - response->status_code
# - response->duration_ms