In new Hassbian 0.77.2 version, it requests a new user account and password. The mqtt switch based on ESP8266 cannot login with neither legacy API password nor new username and password.
if (mqtt_client.connect("NodeClient")) { // Work
// if (mqtt_client.connect("NodeClient","homeassistant","Legacy_API_password")) { // Not work 0.77.2, worked in 0.70.xx
// if (mqtt_client.connect("NodeClient","new_username","new_password")) { // Not work
}
The mqtt switch can only login (connect) and work without api_password in “configuration.yaml”:
http:
# Secrets are defined in the file secrets.yaml
# api_password: !secret http_password
But there is another problem with ifttt when removing api_password
https://your_duckdns_domain_name.duckdns.org:8123/api/services/switch/turn_on?api_password=your_api_password
ifttt does not work withou api_password and I do not know how to use ifttt with new username/password
Code for ESP8266 using Arduino IDE
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
const char* ssid = "abc";
const char* password = "12345678";
const char* mqtt_server = "192.168.0.100";
int SwitchedPin = 2;
WiFiClient espClient;
PubSubClient mqtt_client(espClient);
void setup() {
pinMode(SwitchedPin, OUTPUT);
digitalWrite(SwitchedPin, HIGH);
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
mqtt_client.setServer(mqtt_server, 1883);
mqtt_client.setCallback(callback);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
//turn the switch off if the payload is 'OFF' and publish to the MQTT server a confirmation message
payload[length] = '\0';
if (strcmp((char *)payload,"ON") == 0)
{
mqtt_client.publish("homeassistant/bedroom/switch1", "Relay is ON");
digitalWrite(SwitchedPin, LOW);
}
if (strcmp((char *)payload,"OFF") == 0)
{
mqtt_client.publish("homeassistant/bedroom/switch1", "Relay is OFF");
digitalWrite(SwitchedPin, HIGH);
}
}
void loop() {
if (!mqtt_client.connected()) {
Serial.println("reconnecting ... ");
reconnect();
}
mqtt_client.loop();
}
void reconnect() {
// Loop until we're reconnected
while (!mqtt_client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqtt_client.connect("NodeClient")) {
// if (mqtt_client.connect("NodeClient","homeassistant","Legacy_API_password")) {
Serial.println("connected");
// Once connected, publish an announcement...
mqtt_client.subscribe("homeassistant/bedroom/#");
} else {
Serial.print("failed, rc=");
Serial.print(mqtt_client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
I need either ifttt and mqtt work. Thank you