How to configure and use MQTT from AutoPI as a client to an external Mosquitto server?

Hi,
I have a 2nd Gen DIY Autopi on a Raspberry Pi Zero WH.
I want to detect the starting and stopping of the engine and to send this information to an external Mosquitto server.
How to configure AutoPi to use the Mosquitto server and where and how to trigger the publication of engine start or stop events?
How to choose Topic for publishing ?

My actual settings
The external Mosquitto server is listening at IP address : 192.169.0.156 port 1883
AutoPi is on the same local network with WiFi connection.


Thanks for your help
cordially
Grebe

Hi,
UP
nobody to help me?
Grebe

Hej

I also couldn’t get it to configure an external MQTT broker. I think this has something to do with the IP tables. But I don’t know anything about that.

Therefore I use the script from Topeju

This works! You can also edit the Main Topic.

For the SystemEvents like Vehicle/engine you can look at this

3 Likes

Hi
Thank you very much for this reply. I’ll take a look at this possible solution.
Grebe

1 Like

Note: I’ve stopped using my own custom MQTT service that dasTholo linked above and switched to the built-in MQTT support. That did require installing Mosquitto through the Settings → Mqtt page, and adding -A INPUT -s 192.168.4.0/24 -i uap0 -p tcp --dport 1883 -j ACCEPT as an entry in Settings → System → Firewall to allow the second computer in my car to access the broker.

Note also that the built-in MQTT support doesn’t use the exact same format for the messages as mine does: there is no topic root used, so everything starts at the base topic, but different message types are split in different topic hierarchies like obd/speed. Also, the messages do not include time stamps (except track/pos and events/#), and those that do, use UTC whereas mine used the local time zone.

1 Like

I also can’t succeed to connect to an external MQTT broker using built-in returner.
I’ve contacted support by email and will try to work this out with them.

1 Like

Hi,
It would be very nice if you could give us a little feedback on the solution provided by the support.
Best regards

Support helped so far by:

This second advice seems to trigger something because now I see errors in my device logs:

Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]: [ERROR   ] Error when calling returner hook for message: {'filter': 'alternating_
readout', 'handler': 'query', 'returner': 'cloud,mqtt', 'args': ['AMBIANT_AIR_TEMP'], 'kwargs': {'force': True}}
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]: Traceback (most recent call last):
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:   File "/var/cache/salt/minion/extmods/utils/messaging.py", line 452, in _call_ho
oks_for
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:     func(*args, **kwargs)
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:   File "/var/cache/salt/minion/extmods/utils/messaging.py", line 645, in returner
_wrapper
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:     return returner_func(result, *args, **kwargs)
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:   File "/var/cache/salt/minion/extmods/returners/mqtt_returner.py", line 242, in 
returner_data
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:     client = _get_client_for(kwargs)
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:   File "/var/cache/salt/minion/extmods/returners/mqtt_returner.py", line 150, in 
_get_client_for
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:     bind_address=options["bind_address"])
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:   File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 937, in
 connect
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:     return self.reconnect()
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:   File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1071, i
n reconnect
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:     sock = self._create_socket_connection()
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:   File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 3522, i
n _create_socket_connection
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:     return socket.create_connection(addr, source_address=source, timeout=self._ke
epalive)
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:   File "/usr/lib/python2.7/socket.py", line 575, in create_connection
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]:     raise err
Sep  6 19:08:25 autopi-f9bf700a5581 salt-minion[1101]: error: [Errno 97] Address family not supported by protocol

I had no time to try to understand it yet but it could be linked to my configuration not being correct, by MQTT server not being configured correctly, the autoPI not being able to access 4g network (it has access to wifi so it should not be a problem) or a bug in the mqtt returner. I’ll provide more info when I’ll have this issue troubleshooted.

TL;DR; For anyone encountering issue with MQTT, the most useful advice for now is to manually enable MQTT returner (despite documentation stating hooks will be added automatically).

By any chance, would someone have access to the source code of this? (Having to ssh on the autopi while being in the car is not ideal from a debugging comfort point of view).

Hi,

You can find the source code for the function that is being executed here.

I just want to clarify that the hooks themselves are created, if you take a look under the obd_manager, acc_manager services for example, under hooks, you’ll see the MQTT returner being created there.

However, the returners of the workers aren’t changed automatically, so maybe it is a good idea to have a toggleable button in the MQTT returner settings that will replace all returners with the MQTT returner instead of having to do it manually.

Best,
Nikola