Cannot connect to MQTT broker on local network

Hey everybody,

I currently have node-red running on a Raspberry Pi Zero and so far it's been great, but I've been running into some issues connecting to an MQTT broker on my lan. I have Mosquitto v1.6.7 running on a Freebsd jail (hostname: mosquitto) on a server in my home network. A sensor (BME280 on a NodeMCU) is publishing data to this MQTT broker, and I can see the data being published successfully using MQTT Explorer running on a separate PC. For whatever reason, I cannot get the mqtt in node to connect to this broker. It just hangs with a yellow icon and connecting. Here are my settings:

Screenshot from 2020-12-19 14-29-12

I can ping mosquitto.lan from the RPi running node-red. I can also ssh into the RPi and subscribe to the messages from the command line i.e. mosquitto_sub -h mosquitto.lan -t esp/bme280/temperature and it connects successfully and displays the published temperature values. Also, if I run Mosquitto on the RPi, publish sensor data to that instead, and point the mqtt in node at it, everything seems to work. I can also connect to the test.mosquitto.org test broker from node-red. There is no security on the broker and it is set up to accept anonymous connections.

Does anybody have any suggestions?

May I suggest you enter the IP number rather than the name.

Probably not important, but for the sake of eliminating this as a possible cause.

How did you install node red? Are you using Docker?
Stop node red and then start it in a terminal and see what it shows in the log. Paste the log here from the start. When pasting use the </> button at the top of the forum entry window.
Also look at the mosquito log on the server and see what it shows when the pi tries to connect.

A good suggestion, but I've tried that as well.

If you are using other devices (eg: you mentioned esp/bme280) I am not sure how they are going to resolve the name.

I would again suggest you use the IP number on all devices.

What is the command you use from a terminal to send the messages? You have only shown how you are subscribing, but not publishing.

Could you copy/paste the terminal stuff you are doing?
Or screen show with the two terminals open: one publish one subscribed.

The issue is that it won't connect, so whether it can publish is not relevant.

Also try with the legacy support checkbox selected.

Yes, ok. But I would like to see what is being sent as well and what is (not) being received.

So from a terminal seeing what is being sent would/may help.

But.... I'm not expert.

Hey Colin, I installed it on a RPi running Raspbian GNU/Linux 10 using this guide, no Docker containers involved. Here's the log when starting it from a terminal using the node-red-pi command

Welcome to Node-RED
===================

19 Dec 11:18:59 - [info] Node-RED version: v1.2.3
19 Dec 11:18:59 - [info] Node.js  version: v12.19.0
19 Dec 11:18:59 - [info] Linux 5.4.79+ arm LE
19 Dec 11:19:04 - [info] Loading palette nodes
19 Dec 11:19:18 - [info] Dashboard version 2.24.0 started at /ui
19 Dec 11:19:21 - [info] Settings file  : /home/pi/.node-red/settings.js
19 Dec 11:19:21 - [info] Context store  : 'default' [module=memory]
19 Dec 11:19:21 - [info] User directory : /home/pi/.node-red
19 Dec 11:19:21 - [warn] Projects disabled : editorTheme.projects.enabled=false
19 Dec 11:19:21 - [info] Flows file     : /home/pi/.node-red/flows_raspberrypi.json
19 Dec 11:19:21 - [info] Server now running at http://127.0.0.1:1880/
19 Dec 11:19:21 - [warn] 

---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.

If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.

You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------
19 Dec 11:19:22 - [info] Starting flows
19 Dec 11:19:26 - [info] Started flows
19 Dec 11:19:28 - [info] [mqtt-broker:237e7ff3.a6434] Connection failed to broker: mqtt://192.168.1.10:1883
19 Dec 11:19:44 - [info] [mqtt-broker:237e7ff3.a6434] Connection failed to broker: mqtt://192.168.1.10:1883
19 Dec 11:19:59 - [info] [mqtt-broker:237e7ff3.a6434] Connection failed to broker: mqtt://192.168.1.10:1883

The mosquitto log is showing the following error (192.168.1.8 is the ip address of the RPi)

1608379082: New connection from 192.168.1.8 on port 1883.
1608379082: Socket error on client <unknown>, disconnecting.

I tried this. No dice.

Hey @Trying_to_learn, I am publishing messages to the MQTT broker from an ESP8266, similar to what is described here. I am using the IP address of the MQTT broker (192.168.1.10) on the ESP8266, rather than the hostname. Publishing seems to work fine, but I cannot subscribe to those messages from the node-red instance running on the RPi.

And how does the pub request arrive inside the jail? Do you need some kind of port forwarding?

Run the install script again to upgrade node-red to the latest, there were some issues with 1.2.3, though I am not sure whether mqtt was involved. Always a good idea to do the easy things first though.

If I remember right, 1.2.3 targets a version of MQTT that had issues. So updating to latest node-red should fix this.

Upgraded to Node-RED v1.2.6 and everything's working now. Let that be a lesson to me to always upgrade to the latest version... or at least check the patch notes.

Thanks for your help everybody!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.