balena-labs-projects / connector

Auto-configured data connector block based on Telegraf
15 stars 8 forks source link

Issues with MQTT and localhost #33

Closed keenanjohnson closed 2 years ago

keenanjohnson commented 3 years ago

Hello! This issue is about your example configuration for a mqtt client.

The code you have states that you should be able to connect via the localhost keyword:

client = mqtt.Client("1") client.connect("localhost")

while(True): value = GetReading() # code omitted for brevity client.publish("sensors",json.dumps(value)) time.sleep(5)

However, when I try this, I get the following error:

14.06.21 19:15:52 (-0700) co2 Traceback (most recent call last): 14.06.21 19:15:52 (-0700) co2 File "/usr/src/co2.py", line 85, in 14.06.21 19:15:52 (-0700) co2 mqtt_client.connect("localhost", 1883, 60) 14.06.21 19:15:52 (-0700) co2 File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 941, in connect 14.06.21 19:15:52 (-0700) co2 return self.reconnect() 14.06.21 19:15:52 (-0700) co2 File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 1075, in reconnect 14.06.21 19:15:52 (-0700) co2 sock = self._create_socket_connection() 14.06.21 19:15:52 (-0700) co2 File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 3546, in _create_socket_connection 14.06.21 19:15:52 (-0700) co2 return socket.create_connection(addr, source_address=source, timeout=self._keepalive) 14.06.21 19:15:52 (-0700) co2 File "/usr/local/lib/python3.9/socket.py", line 843, in create_connection 14.06.21 19:15:52 (-0700) co2 raise err 14.06.21 19:15:52 (-0700) co2 File "/usr/local/lib/python3.9/socket.py", line 831, in create_connection 14.06.21 19:15:52 (-0700) co2 sock.connect(sa) 14.06.21 19:15:52 (-0700) co2 OSError: [Errno 99] Cannot assign requested address

phil-d-wilson commented 3 years ago

Hey Keenan,

If you're using a mosquito broker, then check the breaking change from version 2 onwards:

https://mosquitto.org/blog/2020/12/version-2-0-0-released/

Phil