How to use the OpenFaas 'mqtt-publisher' with FaasD

73 Views Asked by At

For some basic background we are trying to develop an ADAS application using a serverless framework 'OpenFaas-Faasd'. We currently have a lidar sensor and a sunfounder rpi car as our vehicle. We are also communicating the lidar data to our serverless function through MQTT broker 'Mosquitto' using the OpenFaas 'mqtt-connector'.

We are using a Lidar to publish when objects are within 20cm to our serverless function using triggers with the mqtt-connector. Our serverless function receieves these messages, but it cannot communicate with the car because the 'picar_4wd' package import doesn't seem to work with OpenFaasd for some reason. So we decided another approach to have an 'external controller' to communicate with the car and publish commands for the rpi car through the serverless function. This is where we reached another problem as we were using paho to publish the lidar data locally to our MQTT broker 'Mosquitto' but I think our serverless function cannot communicate with our broker so we are running into errors trying to connect with the broker as it most likely has a different ip address. We looked into this a bit futher on how to publish from the serverless function to an external broker and came across an example 'Drone tracking project for Packet.com's session CES 2020' (https://github.com/packet-labs/iot link to example) github where we found some information regarding publishing however they are using the 'emitter' broker and are not running faasd any ideas on how we can get something similar for our case?

Github code example and link - https://github.com/equinix-labs/metal-iot/blob/50cb577271bc5162b56955b47a8cdfcb5e9ab711/openfaas/services/mqtt-publisher/publisher.js

const emitter = require('emitter-io')

class Publisher {
    constructor (options) {
        this.channels = options.channels
        this.client = emitter.connect({
            host: options.host,
            port: options.port,
            secure: !! options.secure
        })
        this.client.on('error', error => console.log(error.stack))
        this.client.on('connect', function () { console.log(arguments) })
        this.client.on('offline', function () { console.log('offline', arguments) })
    }

    publish (options) {
        console.log(this.channels, options)
        this.client.publish({
            channel: options.channel,
            key: this.channels[options.channel],
            message: JSON.stringify(options.message),
            me: false
        })
    }
}

module.exports = Publisher

When trying to connect to our MQTT broker Mosquitto on our RPI using local host ip address we got this error

Receieved message: {"angle": 340.58, "distance": 2210}
Traceback (most recent call last):
File "index.py", line 16, in <module>
ret = handler.handle(st)
File "/home/app/function/handler.py", line 14, in handle
client.connect(BROKER, PORT, 60)
File "/home/app/python/paho/mqtt/client.py", line 914, in connect
return self.reconnect()
File "/home/app/python/paho/mqtt/client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "/home/app/python/paho/mqtt/client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "/usr/local/lib/python2.7/socket.py", line 575, in create_connection
raise err
socket.error: [Errno 111] Connection refused

We tried again using the ip address of the RPI found using 'curl ifconfig.me' and we timed out as no connection was found.

Any response is extremely helpful. Thank you!

0

There are 0 best solutions below