My lights are connected to my esp8266 which i have set up with mqtt to connect with my raspberry pi 3 b+. The raspberry pi is running home assistant. The light worked before but i unplugged my setup for a month or so, now i’ve downloaded the new version of hassio and connected everything again, the esp8266 connects to the raspberry pi without any problems but when i turn the light on using homeassistant it doesn’t work. The switch turns yellow for a sec but as soon as homeassistant realises the light doesn’t turn on, the switch goes gray again. the light never went on in this process.
My configuration.yalm:
light:
- platform: mqtt
_ name: ‘Bedroom Light1’_
_ state_topic: ‘Bedroom/light1/status’_
_ command_topic: ‘Bedroom/light1/switch’_
_ payload_on: “ON”_
_ payload_off: “OFF”_
_ _
- platform: mqtt
_ name: ‘Bedroom Light2’_
_ state_topic: ‘Bedroom/light2/status’_
_ command_topic: ‘Bedroom/light2/switch’_
_ qos: 0_
_ payload_on: “ON”_
_ payload_off: “OFF”_
_ optimistic: false_
My esp8266:
#define MQTT_VERSION MQTT_VERSION_3_1_1
// Wifi: SSID and password
const char* WIFI_SSID = “Michiel de Router”;
const char* WIFI_PASSWORD = “Een1Twee2Drie3”;
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = “Bedroomlight2”;
const PROGMEM char* MQTT_SERVER_IP = “192.168.178.20:8123”;
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = “XXXXXX”;
const PROGMEM char* MQTT_PASSWORD = “XXXXXX”;
// MQTT: topics
const char* MQTT_LIGHT_STATE_TOPIC = “Bedroom/light2/status”;
const char* MQTT_LIGHT_COMMAND_TOPIC = “Bedroom/light2/switch”;
// payloads by default (on/off)
const char* LIGHT_ON = “ON”;
const char* LIGHT_OFF = “OFF”;
const PROGMEM uint8_t LED_PIN = 5;
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();_
}