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

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

https://play.google.com/store/apps/details?id=com.thn.iotmqttdashboard

another Link


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

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

MQTT-Explorer for Windows


4 Likes