I am running a MQTT broker in a docker container on my home network. I am working on a project on a NodeMCU v3 which uploads 2 temperature, 1 voltage, 1 current and 1 battery state of charge number. I subscribe to 1 mqtt topic and read some values only once on startuo. My project is set up along the lines on the FullyFeatured-ESP8266.ino project.
I get way more network traffic to and from the NodeMCU than i expected. Every set of values i publish is around 120 bytes (determined with Wireshark). If the topics i am subsribed to do not chag=nge (no mqtt messages received on the MQTT client), and i upload 1 set of values every hour, i do see data consumption of around 100 bytes per hour, but my Unifi access point shows that stll around 15KB of data per hour is sent back to the NodeMCU where the mqtt client runs. Even if i remove all subscribe and publish commands form my code, still 15KB of data per hour is sent back to my NodeMCU (saysthe unifir controller). If that amount of data would also be transmitted via a mobile connection, i'm burning valuable mobile data.
Anyone knows whats causing this idle traffic? I can't detect it with Wireshark somehow.
See attachment, bytes sent by the NodeMCU are almost zero, but bytes replied keep hovering around 150KB per hour
edit: so this example code which does not publish anything at all still makes that aroud 150KB per hour is sent to the NodeMCU mqtt client.
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <AsyncMqttClient.h>
#define WIFI_SSID "xxxx"
#define WIFI_PASSWORD "xxxxxxxx"
#define SECRET_MQTTNAME "xxxxxx"
#define SECRET_MQTTPASSWORD "xxxxxxxxx"
#define MQTT_HOST IPAddress(10, 0, 0, 180)
#define MQTT_PORT 1883
AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;
float waarde = 0;
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
mqttClient.setCredentials(SECRET_MQTTNAME, SECRET_MQTTPASSWORD);
}
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
Serial.println("Connected to Wi-Fi.");
connectToMqtt();
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi.");
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
wifiReconnectTimer.once(2, connectToWifi);
}
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
uint16_t packetIdSub = mqttClient.subscribe("test2/lol", 2);
Serial.print("Subscribing at QoS 2, packetId: ");
Serial.println(packetIdSub);
mqttClient.publish("test/lol", 0, true, "test 1");
Serial.println("Publishing at QoS 0");
uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2");
Serial.print("Publishing at QoS 1, packetId: ");
Serial.println(packetIdPub1);
uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3");
Serial.print("Publishing at QoS 2, packetId: ");
Serial.println(packetIdPub2);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
mqttReconnectTimer.once(2, connectToMqtt);
}
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
Serial.println("Publish received.");
Serial.print(" topic: ");
Serial.println(topic);
Serial.print(" payload: ");
Serial.println(payload);
waarde = atof (payload);
Serial.print(" qos: ");
Serial.println(properties.qos);
Serial.print(" dup: ");
Serial.println(properties.dup);
Serial.print(" retain: ");
Serial.println(properties.retain);
Serial.print(" len: ");
Serial.println(len);
Serial.print(" index: ");
Serial.println(index);
Serial.print(" total: ");
Serial.println(total);
}
void onMqttPublish(uint16_t packetId) {
Serial.println("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println();
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
connectToWifi();
}
void loop() {
Serial.println(waarde);
delay (1000);
}