Blynk and MQTT run concurrently - esp8266 issue Heart beat time out

I see problem in the MQTT.loop() function


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <PubSubClient.h>
WiFiClient c;
const char* mqtt_server;
int mqtt_port;
const char* mqtt_user;
const char* mqtt_pass;
PubSubClient MQTT(mqtt_server,mqtt_port,[](char * tp, byte * nd,unsigned int l ){},c);

void setup()
{

Serial.begin(9600);
mqtt_server = "postman.cloudmqtt.com";
mqtt_port = 13033;
mqtt_user = "xxxx";
mqtt_pass = "xxxx";

WiFi.begin("test2","xxxx");
while(WiFi.status()!=WL_CONNECTED)
{
Serial.print(".");
delay(100);
}
MQTT.setServer(mqtt_server,mqtt_port);
MQTT.connect("test",mqtt_user,mqtt_pass);
MQTT.subscribe("thongbao");



Blynk.config("xxxxxx","abc.com" ,8080);
Blynk.connect();
}

void loop()
{
Blynk.run();
float t = millis()/1000;
Serial.print("Millis: ");
Serial.println(t);
MQTT.publish("ml",String(t).c_str());
MQTT.loop();
Blynk.virtualWrite(V1, t);
delay(1000);
}

Your void loop is a problem, read this:

Personally, I don’t use MQTT and Blynk code on the same device (except in some very unusual circumstances). I prefer to use Node-Red with the Blynk.ws contrib plugin, and just run MQTT code on my ESP devices.

Pete.

1 Like

Thank you very much !
I edited again code, it run pefect

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <PubSubClient.h>
WiFiClient c;
const char* mqtt_server;
int mqtt_port;
const char* mqtt_user;
const char* mqtt_pass;
//PubSubClient MQTT(mqtt_server,mqtt_port,[](char * tp, byte * nd,unsigned int l ){},c);
BlynkTimer timer;

void nhandl(char* tp, byte* nd, unsigned int length) 
{
  String topic(tp);
  String noidung= String((char*)nd);
  noidung.remove(length);
  Serial.print("Have new msg from topic "); Serial.print(topic);
  Serial.print(": ");
  Serial.println(noidung);
}
PubSubClient MQTT(mqtt_server,mqtt_port,nhandl,c);

void setup()
{
 
  Serial.begin(9600);
  mqtt_server = "postman.cloudmqtt.com";
  mqtt_port = 13033;
  mqtt_user = "xxx";
  mqtt_pass = "xxx";

  timer.setInterval(2000L, senddata);


  WiFi.begin("test2","xxxx");
  while(WiFi.status()!=WL_CONNECTED)
{
  Serial.print(".");
  delay(100);
}
  MQTT.setServer(mqtt_server,mqtt_port);
  MQTT.connect("Pzem004t",mqtt_user,mqtt_pass);
  MQTT.subscribe("nd");

  
  
  Blynk.config("you_auth","abc.com" ,8080);
  Blynk.connect();
}

void senddata()
{
  float t = millis()/1000;
  Serial.print("Millis: ");
  Serial.println(t,0);
  MQTT.publish("ml",String(t,0).c_str());
  Blynk.virtualWrite(V1, t);
  if (!MQTT.connected()) 
  {
    if (MQTT.connect("Pzem004t",mqtt_user,mqtt_pass))
    {
      Serial.println("Connected");
      MQTT.publish("tb",String("Connected").c_str());
      MQTT.subscribe("nd");
    }
    else 
    {
      Serial.print("Error:, rc=");
      Serial.print(MQTT.state());
      Serial.println(" try again in 3 seconds");
    }
   }
}

void loop()
{
  Blynk.run();
  timer.run();
  MQTT.loop();
}

1 Like