jochocki / rtl2hass

19 stars 9 forks source link

MQTT connect error #1

Open joekerna opened 4 years ago

joekerna commented 4 years ago

I've created the following docker-compose.yml

version: '2'                                                                                                                                                                                                                               

services:                                                                                                                                                                                                                                  
 rtl2hass:                                                                                                                                                                                                                                 
   container_name: rtl2hass                                                                                                                                                                                                                

   image: jochocki/rtl2hass                                                                                                                                                                                                                

   devices:                                                                                                                                                                                                                                
    - "/dev/bus/usb/001/003"                                                                                                                                                                                                               
   environment:                                                                                                                                                                                                                            
    - MQTT_HOST=localhost                                                                                                                                                                                                                  
    - MQTT_PORT=1883                                                                                                                                                                                                                       
    - MQTT_USERNAME=rtl2mqtt                                                                                                                                                                                                               
    - MQTT_PASSWORD=mysecurepassword

I've triple checked the username, password and port. Mosquitto is running on localhost and receiving MQTT messages from another client successfully.

However I get the following error:

Starting rtl2hass ... done
Attaching to rtl2hass                                                                                                                                                                                                                      
rtl2hass    | rtl_433 version 19.08-233-ga552abc branch master at 202002141216 inputs file rtl_tcp RTL-SDR
rtl2hass    | Use -h for usage help and see https://triq.org/ for documentation.
rtl2hass    | Trying conf file at "rtl_433.conf"...
rtl2hass    | Trying conf file at "/root/.config/rtl_433/rtl_433.conf"...
rtl2hass    | Trying conf file at "/usr/local/etc/rtl_433/rtl_433.conf"...
rtl2hass    | Trying conf file at "/etc/rtl_433/rtl_433.conf"...
rtl2hass    | Publishing MQTT data to localhost port 1883
rtl2hass    | Publishing device info to MQTT topic "rtl_433[/model][/id][/channel:0]".
rtl2hass    | Publishing events info to MQTT topic "rtl_433/events".
rtl2hass    | Publishing states info to MQTT topic "rtl_433/states".
rtl2hass    | Registered 122 out of 149 device decoding protocols [ 1-4 8 11-12 15-17 19-21 23 25-26 29-36 38-60 63 67-71 73-100 102-105 108-116 119 121 124-128 130-149 ]
rtl2hass    | Found Rafael Micro R820T tuner
rtl2hass    | Exact sample rate is: 250000.000414 Hz
rtl2hass    | [R82XX] PLL not locked!
rtl2hass    | Sample rate set to 250000 S/s.
rtl2hass    | Tuner gain set to Auto.
rtl2hass    | Tuned to 433.920MHz.
rtl2hass    | Allocating 15 zero-copy buffers
rtl2hass    | Failed to allocate zero-copy buffer for transfer 0
rtl2hass    | Falling back to buffers in userspace
rtl2hass    | MQTT connect error: Connection refused

Is it possible to get more debug information as to why the connection is refused?

jochocki commented 4 years ago

I believe that if you use localhost as the MQTT_HOST, it resolves to the IP of the container itself, which there is no MQTT server there. I assume you are attempting to connect to Mosquitto running on the same machine in some other fashion.

How is your Mosquitto server setup? Is it running as a container on the same physical machine? And how is the working client setup (as a container, or a different method?)

joekerna commented 4 years ago

Mosquitto is running locally on the same machine (no docker). rtl2hass is setup using docker-compose

Will it help, if I change my docker-compose.yaml to MQTT_HOST=127.0.0.1

jochocki commented 4 years ago

I don't think so. If you do some reading regarding Docker, depending on your OS (Windows/Mac/Linux), there may be some DNS entries you can use to reference the actual localhost from within the container.

Otherwise, adding --net=host to your docker-compose file should work, although it's far less secure.

joekerna commented 4 years ago

How do you do it? Is there a recommended way?

jochocki commented 4 years ago

I run Mosquitto as a Home Assistant add-on (essentially a Docker container), and have a DNS record for mqtt.mylocaldomain.com, which points to my Home Assistant server. So anything needing to connect to MQTT uses that DNS name.

Really this comes down to a DNS issue. Are you running any sort of DNS server in your environment?

Another thing you could try is using the IP of the machine you have Mosquitto installed on, rather than a hostname.