Back to the original project for the time being… Using the wiring I posted earlier, I was indeed able to successfully load a version of the Arduino sketch posted earlier with just a few modifications to match my network preferences and MQTT topics.
[code]#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Wifi Connection
const char* ssid = “ssid”;
const char* password = “pass”;
// MQTT Server address
const char* mqtt_server = “192.168.1.1”;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
// prepare GPIO4
pinMode(0, OUTPUT);
digitalWrite(0, 1);
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(2);
// We start by connecting to a WiFi network
Serial.println();
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print(“Message arrived [”);
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
int val;
if ((char)payload[0] == ‘1’) {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
val = 1;
digitalWrite(0, val);
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
val = 0;
digitalWrite(0, val);
}
}
void reconnect() {
// Loop until we’re reconnected
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
// Attempt to connect
if (client.connect(“ESP8266Client”)) {
Serial.println(“connected”);
// Once connected, publish an announcement…
client.publish(“home/bedroom/buttonswitch/set”, “connected”);
// … and resubscribe
client.subscribe(“home/bedroom/buttonswitch”);
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, “connected”, value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish(“home/bedroom/buttonswitch”, msg);
}
}[/code]
Then on the HASS side I added the switch to my config.yaml
switch:
platform: mqtt
name: "Bedroom Switch"
state_topic: "home/bedroom/buttonswitch"
command_topic: "home/bedroom/buttonswitch/set"
qos: 0
payload_on: "ON"
payload_off: "OFF"
optimistic: false
state_format:
I then created two new scenes of which one is the default “lights out” mode while we’re all sleeping and the other wherein the MQTT button will turn the lights on.
[code]- name: ‘Baby Change’
entities:
light.bedlamp:
state: on
brightness: 100
rgb_color: [255, 0, 0]
light.bedroom:
state: on
brightness: 100
rgb_color: [255, 0, 0]
light.bathroom:
state: on
brightness: 50
rgb_color: [20, 50, 200]
- name: ‘Sleep’
entities:
light.bedlamp:
state: off
brightness: 0
light.bedroom:
state: off
brightness: 0
light.bathroom:
state: on
brightness: 50
rgb_color: [20, 50, 200][/code]
Lastly, I created the following automation for the MQTT trigger itself-
[code]automation:
-
alias: ‘Baby Wake’
trigger:
- platform: mqtt
topic: home/bedroom/buttonswitch/setOptional
payload: ‘on’
action:
service: scene.turn_on
entity_id: scene.baby_change - platform: mqtt
automation:
-
alias: ‘Baby Wake’
trigger:
- platform: mqtt
topic: home/bedroom/buttonswitch/setOptional
payload: ‘off’
action:
service: scene.turn_off
entity_id: scene.sleep[/code] - platform: mqtt
While I was able to see the messages while connected to the Arduino Serial Monitor, the actions did not seem to be recognized by Home Assistant. So I’m definitely missing something. Is there any additional configuartion that needs to be done through the MQTT Broker (Mosquitto in my case?). Do I need to publish each topic and subtopic directly to Mosquitto or will it automatically publish from the config.yaml?
My current (scrubbed) config.yaml can be found at my pastebin if anybody would like to take a look- pastebin.com/hjeJm3xa