question

matt1309 avatar image
matt1309 asked

Arduino PubSubClient cannot connect to MQTT

Hi, I'm struggling to connect to Victron MQTT (local) broker using PubSubClient.h arduino library. MQTT explorer is fine, as is mosquitto_pub

I'm trying to connect openmqttgateway device to victron MQTT. OpenMQTTGateway uses PubSubClient.h

(my end goal of modifying the openmqttgateway to capture heating oil ultrasonic depth sensor, perform the data extraction and calculations of turning depth to volume forward on to dummy tank driver).

I have no issue connecting to "normal" MQTT server (0 config mosquitto server) and i can view mqtt via mqtt explorer. As you can see here, the server is receiving data from esp32 device:

1714695568656.png



However it fails to connect on victron mqtt server. I thought maybe the initial message structure was causing the issue or maybe even mqtt version being too high on victron server but i tested using the command:

mosquitto_pub -h 192.168.3.104 -t home/T/RFtoMQTT -m '{"active":3,"frequency":433.92,"rssithreshold":-105,"rssi":-113,"avgrssi":-114,"count":0,"ookthreshold":11}' -V mqttv31


1714695966178.pngAnd the topic posted. Yet esp32 device that uses PubSubClient.h for connections is unable to.

Is there any specific settings in mqtt broker that could be causing weirdness? Im using venus os 3.30 so think that means broker is started via dbus-flashmq.


I had a quick scan but couldnt spot anything obvious. Any tips greatly appreciated as i know this is probably very Niche setup but would be nice to have openmqttgateway integration, would open the door for so many sensors.


MQTT
1714695568656.png (61.4 KiB)
1714695966178.png (30.0 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.

2 Answers
matt1309 avatar image
matt1309 answered ·

Logs show: (log locaiton: /var/log/flashmq/current)

[NOTICE] Accepting connection from: address='192.168.3.171', transport='TCP/Non-SSL', fd=20
@400000006634a27f14660db4 [2024-05-03 08:38:13.341] [ERROR] Unspecified or non-MQTT protocol error: Username flagged as present, but it's 0 bytes.. Removing client.
@400000006634a27f14662524 [2024-05-03 08:38:13.341] [NOTICE] Removing client '[ClientID='', username='', fd=20, keepalive=10s, transport='TCP/Non-SSL', address='192.168.3.171', prot=3.1.1, clean=0]'. Reason(s): Username flagged as present, but it's 0 bytes.


I suspect mosquitto/the other broker servers I've tested on treat empty string username's as indicator to disable username flag but flashmq doesnt?

I'll tweak the openmqtt code to confirm this but leaving here just in case someone stumbles upon this post and has the same issues

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 ·
Also an empty Client id may cause issues. Client id should be unique.
0 Likes 0 ·
matt1309 avatar image matt1309 dognose commented ·

Thanks for the tip. It was username that was causing the issue for me.


For anyone else using openmqttgateway in the future. THis is the line that needs changed.

if(client.connect(gateway_name, mqtt_user, mqtt_pass, topic, will_QoS, will_Retain, will_Message)) {...


I changed it to:

 bool conn=false;

  if((mqtt_user == "" && mqtt_pass == "") || ((mqtt_user[0] == '\0' && mqtt_pass[0] == '\0') || (strlen(mqtt_user) == 0 && strlen(mqtt_pass) == 0))){

    conn = client.connect(gateway_name, NULL, NULL, topic, will_QoS, will_Retain, will_Message);
    Log.warning(F("NULL case" CR));
  }else{

    conn = client.connect(gateway_name, mqtt_user, mqtt_pass, topic, will_QoS, will_Retain, will_Message);
  Log.warning(F("default case" CR));
  Log.warning(F("mqttUser %s, mqttPass %s" CR), mqtt_user, mqtt_user);
  }




 
  if (conn) { ...
1 Like 1 ·
kurs270 avatar image
kurs270 answered ·

Another fix might have been updating your GX device to the latest firmware. This is a known issue that has been fixed with V3.31

1 comment
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 commented ·

hahaha typical, spent ages trying to work that out! thank you though!

0 Likes 0 ·

Related Resources