I've successfully made communication with MQTT broker (HiveMQ cloud) using the Arduino library PubSubClient.h in an esp8266 client.
However, I get a lot of annoying things in the serial console as shown below
12:10:36.856 - :rdi 31, 5
12:10:36.902 - :rd 26, 31, 5
12:10:36.902 - :rdi 26, 26
12:10:36.902 - :c0 26, 31
12:10:36.944 - :wr 126 0
12:10:36.944 - :wrc 126 126 0
12:10:36.977 - :ack 126
12:10:36.977 - Message published [esp8266_data]: {"deviceId":"NodeMCU","siteId":"My Demo Lab","humidity":21.21,"temperature":2151}
How do I stop receiving those wrc ack ....?
Here is my code
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <ArduinoJson.h>
#include <>
#include <WiFPubSubClient.hiClientSecure.h>
/**** LED Settings *******/
const int led = 5; //Set LED pin as GPIO5
/****** WiFi Connection Details *******/
const char* ssid = "xxxxx";
const char* password = "xxxxxx";
/******* MQTT Broker Connection Details *******/
const char* mqtt_server = "xxxxxxxxx.s2.eu.hivemq.cloud";
const char* mqtt_username = "xxxxxx";
const char* mqtt_password = "xxxxxx";
/**** Secure WiFi Connectivity Initialisation *****/
WiFiClientSecure espClient;
/**** MQTT Client Initialisation Using WiFi Connection *****/
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
const int mqtt_port =8883;
/****** root certificate *********/
static const char *root_ca PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
more
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
-----END CERTIFICATE-----
)EOF";
/************* Connect to WiFi ***********/
void setup_wifi() {
delay(10);
Serial.print("\nConnecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("\nWiFi connected\nIP address: ");
Serial.println(WiFi.localIP());
}
/************* Connect to MQTT Broker ***********/
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP8266Client-"; // Create a random client ID
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
Serial.println("connected");
client.subscribe("led_state"); // subscribe the topics here
} else {
//Serial.print("failed, rc=");
//Serial.print(client.state());
//Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying
delay(1000);
}
}
}
/***** Call back Method for Receiving MQTT messages and Switching LED ****/
void callback(char* topic, byte* payload, unsigned int length) {
String incommingMessage = "";
for (int i = 0; i < length; i++) incommingMessage+=(char)payload[i];
Serial.println("Message arrived ["+String(topic)+"]"+incommingMessage);
//--- check the incomming message
if( strcmp(topic,"led_state") == 0){
if (incommingMessage.equals("1")) digitalWrite(led, HIGH); // Turn the LED on
else digitalWrite(led, LOW); // Turn the LED off
}
}
/**** Method for Publishing MQTT Messages **********/
void publishMessage(const char* topic, String payload , boolean retained){
if (client.publish(topic, payload.c_str(), false))
Serial.println("Message published ["+String(topic)+"]: "+payload);
}
/**** Application Initialisation Function******/
void setup() {
pinMode(led, OUTPUT); //set up LED
Serial.begin(9600);
while (!Serial) delay(1);
setup_wifi();
#ifdef ESP8266
espClient.setInsecure();
#else
espClient.setCACert(root_ca); // enable this line and the the "certificate" code for secure connection
#endif
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
/******** Main Function *************/
void loop() {
if (!client.connected()) reconnect(); // check if client is connected
client.loop();
DynamicJsonDocument doc(1024);
doc["deviceId"] = "NodeMCU";
doc["siteId"] = "My Demo Lab";
doc["humidity"] = 81.21;
doc["temperature"] = 18.51;
char mqtt_message[128];
serializeJson(doc, mqtt_message);
publishMessage("esp8266_data", mqtt_message, false);
delay(10000);
}
thanks in advance
Ricardo