Ola a todos.
Estou começando agora o meu estudo sobre HA.
Com a ajuda que já recebi aqui no fórum, consegui editar os arquivos .yaml.
No entanto ainda preciso de ajuda para que o HA reconheça meus devices.
Tenho um sonoff que modifiquei o firmware dela com o seguinte código:
/*
MQTT Light for Home-Assistant - NodeMCU (ESP8266)
https://home-assistant.io/components/light.mqtt/
Libraries :
- ESP8266 core for Arduino : https://github.com/esp8266/Arduino
- PubSubClient : https://github.com/knolleary/pubsubclient
Sources :
- File > Examples > ES8266WiFi > WiFiClient
- File > Examples > PubSubClient > mqtt_auth
- File > Examples > PubSubClient > mqtt_esp8266
Schematic :
- https://github.com/mertenats/open-home-automation/blob/master/ha_mqtt_light/Schematic.png
- GND - LED - Resistor 220 Ohms - D1/GPIO5
Configuration (HA) :
light:
platform: mqtt
name: Office light'
state_topic: 'iead/light1/status'
command_topic: 'iead/light1/switch'
optimistic: false
Wilson S.N. - v1.1 - 11.2018
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define MQTT_VERSION MQTT_VERSION_3_1_1
// Wifi: SSID and password
const char* WIFI_SSID = "wifi";
const char* WIFI_PASSWORD = "minhasenha";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "gate_light";
const PROGMEM char* MQTT_SERVER_IP = "192.168.15.9";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "MQTT_wilson";
const PROGMEM char* MQTT_PASSWORD = "sennha_do_wilson";
// MQTT: topics
const char* MQTT_LIGHT_STATE_TOPIC = "iead/light1/status";
const char* MQTT_LIGHT_COMMAND_TOPIC = "iead/light1/switch";
// payloads by default (on/off)
const char* LIGHT_ON = "ON";
const char* LIGHT_OFF = "OFF";
const PROGMEM uint8_t LED_PIN = 12;
boolean m_light_state = false; // light is turned off by default
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// function called to publish the state of the light (on/off)
void publishLightState() {
if (m_light_state) {
client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_ON, true);
} else {
client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_OFF, true);
}
}
// function called to turn on/off the light
void setLightState() {
if (m_light_state) {
digitalWrite(LED_PIN, HIGH);
Serial.println("INFO: Turn light on...");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("INFO: Turn light off...");
}
}
// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
// concat the payload into a string
String payload;
for (uint8_t i = 0; i < p_length; i++) {
payload.concat((char)p_payload[i]);
}
// handle message topic
if (String(MQTT_LIGHT_COMMAND_TOPIC).equals(p_topic)) {
// test if the payload is equal to "ON" or "OFF"
if (payload.equals(String(LIGHT_ON))) {
if (m_light_state != true) {
m_light_state = true;
setLightState();
publishLightState();
}
} else if (payload.equals(String(LIGHT_OFF))) {
if (m_light_state != false) {
m_light_state = false;
setLightState();
publishLightState();
}
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("INFO: Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("INFO: connected");
// Once connected, publish an announcement...
publishLightState();
// ... and resubscribe
client.subscribe(MQTT_LIGHT_COMMAND_TOPIC);
} else {
Serial.print("ERROR: failed, rc=");
Serial.print(client.state());
Serial.println("DEBUG: try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// init the serial
Serial.begin(115200);
// init the led
pinMode(LED_PIN, OUTPUT);
analogWriteRange(255);
setLightState();
// init the WiFi connection
Serial.println();
Serial.println();
Serial.print("INFO: Connecting to ");
WiFi.mode(WIFI_STA);
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("INFO: WiFi connected");
Serial.print("INFO: IP address: ");
Serial.println(WiFi.localIP());
// init the MQTT connection
client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}/>
Consigo compilar e gravar no módulo sonoff, utilizei também as configurações para o HA conforme sugerido no próprio código fonte acima. Minha configuração de configuration.yaml. Ficou assim:
homeassistant:
# Name of the location where Home Assistant is running
name: Home
# Location required to calculate the time the sun rises and sets
latitude: 0
longitude: 0
# Impacts weather/sunrise data (altitude above sea level in meters)
elevation: 0
# metric for Metric, imperial for Imperial
unit_system: metric
# Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
time_zone: UTC
# Customization file
customize: !include customize.yaml
# Show links to resources in log and frontend
introduction:
# Enables the frontend
frontend:
# Enables configuration UI
config:
# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
# base_url: example.duckdns.org:8123
# Checks for available updates
# Note: This component will send some information about your system to
# the developers to assist with development of Home Assistant.
# For more information, please see:
# https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
updater:
# Optional, allows Home Assistant developers to focus on popular components.
# include_used_components: true
# Discover some devices automatically
discovery:
# Allows you to issue voice commands from the frontend in enabled browsers
conversation:
# Enables support for tracking state changes over time
history:
# View all events in a logbook
logbook:
# Enables a map showing the location of tracked devices
map:
# Track the sun
sun:
# Sensors
sensor:
# Weather prediction
- platform: yr
- platform: random
# Text to speech
tts:
- platform: google
# Cloud
cloud:
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
automation:
- alias: Check sensor value and show notification
trigger:
platform: numeric_state
entity_id: sensor.random_sensor
above: 10
action:
service: persistent_notification.create
data:
message: "Sensor value greater than 10"
light:
platform: mqtt
name: iead_light'
state_topic: 'iead/light1/status'
command_topic: 'iead/light1/switch'
optimistic: false
mqtt:
broker: localhost
port: 1883
client_id: gate_light
keepalive: 60
username: !secret username_mqtt
password: !secret password_mqtt
protocol: 3.1.1
discovery: true
panel_iframe:
configurator:
title: Configurator
icon: mdi:wrench
url: http://hassio.local:3218
O HA não acusa erro, mas o sonoff não aparece, e portanto não pode ser acessado. Como posso resolver isto? Será que não configurei corretamente o mqtt?
E sim estou lendo a documentação do HA, mas como estou no início, estou tendo dificuldades.
Obrigado pela paciência de todos.