question

petr-slepicka avatar image
petr-slepicka asked

Unable to connecto to MQTT on LAN

I am trying to use MQTT data via home LAN on my Arduino Uno device.

Sometimes the client connects on the first try, sometimes it doesn't even connect after an hour. It's completely unpredictable.

Nothing helps: Opening the web site at https://vrm.victronenergy.com/ (the data is updated), via the mobile app; turning off and then on Modbus TCP via remote console doesn't help either.

Neither MQTT-Explorer nor my .NET application will connect.

What could it be?


Next day, the same code works like a charm... :-(


#include <UIPEthernet.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

// nastavení MAC adresy
byte mac[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
IPAddress localIP(192, 168, 1, 110);
IPAddress mqttIPAddress(192, 168, 1, 111);
EthernetClient ethernetClient;
PubSubClient mqttClient(ethernetClient);
long nextKeepAlive = 
char* topics = "#";
float batterySOC = 0;
float fvePower[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, localIP);
  Serial.println("Starting... ----------------------------");
  Serial.println(Ethernet.localIP());
  Serial.println(Ethernet.gatewayIP());
  Serial.println(Ethernet.dnsServerIP());
  Serial.println("");

  mqttClient.setServer(mqttIPAddress, 1883);
  mqttClient.setCallback(ReceiveMQTT);
  connectToMQTT();
}

void loop() {
  if (millis() >= nextKeepAlive) {
    sendKeepAlive();
  }
  mqttClient.loop();
}

void connectToMQTT() {
  Serial.println("Connecting to MQTT broker...");

  while (!mqttClient.connect("victronMQTT")) {
    Serial.println("Connecting to MQTT broker...");
    delay(1000);
  }
  Serial.println("Connected to MQTT broker.");
  mqttClient.subscribe(topics);
  Serial.print("Subscribed to topic: ");
  Serial.println(topics);

  nextKeepAlive = millis() + 50000;
}

void sendKeepAlive() {
  if (mqttClient.state() == 0) {
    Serial.println("Sending keepAlive...");
    mqttClient.publish("R/d<secretID>/keepalive", "");
    nextKeepAlive = millis() + 10000;
  } else {
    connectToMQTT();
  }
}

void ReceiveMQTT(char* topic, byte* bytePayload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  String payload = "";
  for (int i = 0; i < length; i++) {
    payload.concat((char)bytePayload[i]);
  }
  Serial.println(payload);
  ProcessMessage(topic, payload);
}

void ProcessMessage(String topic, String payload) {
  if (topic == "N/<secretID>/system/0/Dc/Pv/Power") {
    float value = readJsonValue(payload);
    int index = (int)((millis() / 60000) % 10);
    Serial.print("Updating FVE power. Index = ");
    Serial.print(index);
    Serial.print("; value = ");
    Serial.println(value);
    fvePower[index] = value;
  }
  if (topic == "N/<secretID>/battery/512/Soc") {
    batterySOC = readJsonValue(payload);
  }
}

float readJsonValue(String payload) {
  DynamicJsonDocument doc(1024);
  deserializeJson(doc, payload);
  return doc["value"];
}
MQTT
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

7 Answers
gerard-van-seventer avatar image
gerard-van-seventer answered ·

Using MQTT is independent from Modbus TCP. If MQTT-Explorer is unable to connect then you have a network issue that should be solved before doing something else.

Next to that I doubt is the Uno is fast enough to process the huge amount of data and maintaining network connectivity at the same time.

These setting should be enough to use mqtt.

victron-mqtt.png


victron-mqtt.png (29.6 KiB)
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

petr-slepicka avatar image
petr-slepicka answered ·

Thanks for answer.

It won't be a network problem. I would never connect. It looks more like the MQTT broker on the Victron side has a limited number of client slots. By not disconnecting the Arduino on reboot, the slot stays occupied and it takes some time for the broker to free it up so another client can connect... ?

Is there anyone here who can confirm/disprove this theory?

On Arduino performance: I only want to monitor SoC battery and PV panel performance - so two topics. Perhaps the Arduino could handle that? Or do you think it couldn't?

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

tom w avatar image
tom w answered ·

You problem sounds very familiar with something I experienced when starting programming an ESP8266.

Are you using an exclusive network for the Ardunio Uno, or is shared with other stuff in your home like streaming music?

In my case I was too lazy to setup a separate WiFi and the ESP got overflown by traffic from streaming and other services in my home network. The Uno has even less performance than an ESP, so it's very likely that it is too busy filtering packets that are not MQTT messages.

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

petr-slepicka avatar image
petr-slepicka answered ·

Your idea is interesting, I'll definitely read up on it. I am working with Ethernet Shield ENC28J60, connected by cable to LAN.

However, it doesn't explain why MQTT Explorer running on my laptop won't connect either. :-(

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

tom w avatar image
tom w answered ·

I'm not familiar with MQTT Explore, so I used 'mosquitto-sub -t "#" -v ' on LINUX to show the MQTT messages from Venus-OS running on R-Pi3. When I connect I can see only two messages:

N/b827xxx0b0cc /keepalive {"value": 1}

N/b827xxx0b0cc /system/0/Serial {"value": "b827xxx0b0cc"}

The rest is silence, although I'm running Node-RED that constantly spitting out messages about my low battery state.

Do you get a connection error or don't you see incoming messages?

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

leppenraub avatar image
leppenraub answered ·

Hmn, same for me currently. Just rebooted my Victron system for connecting with ACout. Since then I cannot connect MQTT on LAN neither with my script nor with MQTT explorer.

So the solution is to wait a few hours, a day?

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

matt1309 avatar image
matt1309 answered ·

I'm having same issue using same library.

I can access via MQTT explorer but not via arduino. Any ideas?

2 comments
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

dognose avatar image dognose commented ·
If one device works, but the other, it always indicates a configuration issue.


make sure to use the IP of the gx device, unless you have a properly functioning DNS-Service running. Don't rely on Netbios-Name-Resolution. (The original Post seems to use a netbios name, hence i'm mentioning that)

0 Likes 0 ·
Show more comments

Related Resources