BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How to use TASMOTA inside BLYNK and HOME ASSISTANT [Work in Progress]

esp8266
home_automation

#21

Back to “MQTT”

Understanding MQTT: How Smart Home Devices Communicate


Add a door sensor to any door with a Sonoff basic.


Fix Random Switching in Tasmota by Adjusting Retain Settings

Effectively, he recommends :

  1. Make sure you have set the retain to false in Home Assistant Configuration.

  2. Restart the Home Assistant server .

  3. Send these commands to the Sonoff in the console menu.

    switchretain off
    buttonretain on
    buttonretain off
    poweronstate 3
    powerretain on

#22

IF you don’t have a Raspberry PI to install Mosquitto broker and need an MQTT that works with BLYNK here is the cheapest and easiest solution.

The “ESP uMQTT Broker”

MQTT Broker/Bridge on the ESP8266


the CODE works on any ESP8266 NodeMCU, Wemo D1 ,Sonoff Basic (Tested)

// uMQTTBroker for Sonoff
// 
// The program simply starts a broker and waits for any client to connect.
// maximal 10 Client possible
// 
///////////////////////////////////////////////////////////////////////////////

#include "ESP8266WiFi.h"
#include "uMQTTBroker.h"
#include "espconn.h"

// Your WiFi config here //////////////////////////////////////////////////////
char host[] = "ESP-MQTT";           // Hostname
char ssid[] = "ssid";               // your network SSID (name)
char pass[] = "password";           // your network password
bool STATIC = false;                // Static IP for STA Mode?   [false/true]
IPAddress ip(192,168,1,200);        // Static IP
IPAddress gw(192,168,1,1);          // Gateway
IPAddress su(255,255,255,0);        // Subnet
bool WiFiAP = false;                // Do yo want the ESP as AP? [false/true]
bool EXAMPL = true;                 // Example Progamm on/off?   [false/true]
// Example need rule in Test_1
// rule1 on power1#state=1 do publish stat/TEST_1/POWER TRUE endon on power1#state=0 do publish stat/TEST_1/POWER FALSE endon

// Declare Variable ///////////////////////////////////////////////////////////
String RECEIVE;                     // Received Topic for logical operations
String RECEVAL;	                    // Received Topic for logical operations
int GPIO0 = 0;                      // GPIO0  -> Sonoff Basic - Button
int GPIO12 = 12;                    // GPIO12 -> Sonoff Basic - Relais
int GPIO13 = 13;                    // GPIO13 -> Sonoff Basic - green LED
int GPIO14 = 14;                    // GPIO14 -> Sonoff Basic - Optional Sensor
int BSTAT1;                         // State Button GPIO0	
int RSTAT1;                         // State Relais GPIO12	
int TRIGG1 = LOW;

class myMQTTBroker: public uMQTTBroker
{
public:
    virtual bool onConnect(IPAddress addr, uint16_t client_count) {
      Serial.println(addr.toString()+" connected");
      return true;
    }
    
    virtual bool onAuth(String MQTTUSER, String MQTTPASS) {
      Serial.println("Username/Password: "+MQTTUSER+"/"+MQTTPASS);
      return true;
    }
    
    virtual void onData(String topic, const char *data, uint32_t length) {
      char data_chr[length+1];
      os_memcpy(data_chr, data, length);
      data_chr[length] = '\0';
      String data_str = (String)data_chr; 
      data_str.toUpperCase();
      
      RECEVAL = topic+" "+data_str;
      Serial.println("received: "+RECEVAL);
    }
};

void MQTT_Server_cleanupClientCons();

myMQTTBroker myBroker;

// WiFi init stuff ////////////////////////////////////////////////////////////
void startWiFiClient()
{
  if (STATIC) {
	WiFi.config(ip, gw, gw, su);
  }
  WiFi.softAPdisconnect(true);
  WiFi.hostname(host);
  Serial.println("Connecting to "+(String)ssid);
  WiFi.begin(ssid, pass);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  
  Serial.println("WiFi connected");
  Serial.println("IP address: " + WiFi.localIP().toString());
}

void startWiFiAP()
{
  WiFi.softAP(ssid, pass);
  Serial.println("AP started");
  Serial.println("IP address: " + WiFi.softAPIP().toString());
}

void setup()
{
  //  Configure GPIO //////////////////////////////////////////////////////////
  pinMode(GPIO0, INPUT_PULLUP);
  pinMode(GPIO12, OUTPUT);
  pinMode(GPIO13, OUTPUT);
  pinMode(GPIO14, OUTPUT);
  
  // Serial Interface  
  Serial.begin(115200);
  Serial.println();
  Serial.println();

  // Start WiFi
  espconn_tcp_set_max_con(10);
  if (WiFiAP) {
    startWiFiAP();
  } else {
    startWiFiClient();
  }

  // Start the broker
  Serial.println("Starting MQTT broker");
  myBroker.init();

  // Subscribe to anything ////////////////////////////////////////////////////
  myBroker.subscribe("#");
}

void loop()
{
  RECEIVE = RECEVAL;

  // Example Programm /////////////////////////////////////////////////////////  
  if (EXAMPL) {
  if (RECEIVE.equals("stat/TEST_1/POWER FALSE")) {
    myBroker.publish("cmnd/TEST_2/POWER", "ON");
  }
  if (RECEIVE.equals("stat/TEST_1/POWER TRUE")) {
    myBroker.publish("cmnd/TEST_2/POWER", "OFF");
  }
  }
  
  if (RECEIVE.equals("cmnd/"+(String)host+"/STATUS 1")) {
    myBroker.publish("cmnd/TEST_1/POWER", "(null)");
    myBroker.publish("cmnd/TEST_2/POWER", "(null)");
    if (RSTAT1 == LOW) {
      myBroker.publish("stat/"+(String)host+"/POWER", "OFF");
    } else {
      myBroker.publish("stat/"+(String)host+"/POWER", "ON");
    }
  }
  
  /////////////////////////////////////////////////////////////////////////////
  // Basic Function Button/MQTT ///////////////////////////////////////////////
  BSTAT1 = digitalRead(GPIO0);
  RSTAT1 = digitalRead(GPIO12);
  
  // Button event /////////////////////////////////////////////////////////////
  if (BSTAT1 == LOW and RSTAT1 == LOW and TRIGG1 == LOW) {
     digitalWrite(GPIO12, HIGH);
     TRIGG1 = HIGH;
     Serial.println("GPIO0 POWER ON");
     myBroker.publish("stat/"+(String)host+"/POWER", "ON");
  }
  if (BSTAT1 == LOW and RSTAT1 == HIGH and TRIGG1 == LOW) {
     digitalWrite(GPIO12, LOW);
     TRIGG1 = HIGH;
     Serial.println("GPIO0 POWER OFF");
     myBroker.publish("stat/"+(String)host+"/POWER", "OFF");
  }
  
  if (BSTAT1 == HIGH) { TRIGG1 = LOW; }
  
  // MQTT event ///////////////////////////////////////////////////////////////
  if (RECEIVE.equals("cmnd/"+(String)host+"/POWER ON")) {
    digitalWrite(GPIO12, HIGH);
    Serial.println("stat/"+(String)host+"/POWER ON");
    myBroker.publish("stat/"+(String)host+"/POWER", "ON");
    delay(50);
  }
  if (RECEIVE.equals("cmnd/"+(String)host+"/POWER OFF")) {
    digitalWrite(GPIO12, LOW);
    Serial.println("stat/"+(String)host+"/POWER OFF");
    myBroker.publish("stat/"+(String)host+"/POWER", "OFF");
    delay(50);
  }
  if (RECEIVE.equals("cmnd/"+(String)host+"/POWER ")) {
    if (RSTAT1 == LOW) {
      myBroker.publish("stat/"+(String)host+"/POWER", "OFF");
    } else {
      myBroker.publish("stat/"+(String)host+"/POWER", "ON");
    }
  }

  // LED STATUS ///////////////////////////////////////////////////////////////
  if (RSTAT1 == LOW) {	
   digitalWrite(GPIO13, HIGH);
  } else {
   digitalWrite(GPIO13, LOW);
  }
}

you can test it with

IoT MQTT Dashboard


I even test “uMQTT broker” with HA and it works OK

==============================================================================

MQTT-Explorer for Windows



#23

TASMOTA running on NodeMCU controlled by “BLYNK” and Home Assistant through MQTT




23:53:24 MQT: stairs/Distance = {"Distance":40.842}
23:53:24 MQT: stairs/tele/STATE = {"Time":"2019-03-19T23:53:24","Uptime":"0T01:11:57","Vcc":2.973,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":43,"POWER1":"ON","POWER2":"OFF","POWER3":"ON","Wifi":{"AP":1,"SSId":"BLYNK","BSSId":"48:F8:B3:84:AB:22","Channel":10,"RSSI":70,"LinkCount":1,"Downtime":"0T00:00:04"}}
23:53:24 MQT: stairs/tele/SENSOR = {"Time":"2019-03-19T23:53:24","Switch1":"OFF","DS18B20-1":{"Id":"000006F242B1","Temperature":21.5},"DS18B20-2":{"Id":"000006F2B80A","Temperature":21.6},"DS18B20-3":{"Id":"00000789E508","Temperature":21.5},"DS18B20-4":{"Id":"8000001F18D8","Temperature":21.5},"AM2301":{"Temperature":21.3,"Humidity":69.5},"SR04":{"Distance":40.404},"TempUnit":"C"}
23:53:24 MQT: stairs/tele/STATE = {"Time":"2019-03-19T23:53:24","Uptime":"0T01:11:57","Vcc":2.980,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":77,"POWER1":"ON","POWER2":"OFF","POWER3":"ON","Wifi":{"AP":1,"SSId":"BLYNK","BSSId":"48:F8:B3:84:AB:22","Channel":10,"RSSI":68,"LinkCount":1,"Downtime":"0T00:00:04"}}
23:53:24 MQT: stairs/stat/RESULT = {"POWER2":"OFF"}
23:53:24 MQT: stairs/stat/POWER2 = OFF
23:53:26 RUL: SR04#DISTANCE>%VAR2% performs "backlog  publish stairs/Distance {"Distance":40.456};power2 off"

00:03:06 MQT: stairs/stat/RESULT = {"POWER1":"ON"}
00:03:06 MQT: stairs/stat/POWER1 = ON
00:03:06 MQT: stairs/stat/RESULT = {"T1":30,"T2":0,"T3":0,"T4":0,"T5":0,"T6":0,"T7":0,"T8":0}
00:03:06 RUL: SR04#DISTANCE<%VAR2% performs "backlog  publish stairs/Distance {"Distance":17.298};power2 on"
00:03:06 MQT: stairs/Distance = {"Distance":17.298}