Skip to content

Latest commit

 

History

History

mqtt-connector

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

MQTT-connector for OpenFaaS

The MQTT-Connector is used to trigger functions and services in response to messages generated by the event source. It runs inside the Kubernetes cluster and is private with no ingress.

The topic is drone-position.

git clone https://github.com/openfaas-incubator/mqtt-connector
cd mqtt-connector

Add the connector for the Drone positions

Now install the chart, but edit the command-line as follows

  • Set topic=$DRONE_POSITION_CHANNEL_KEY with the channel key generated by the Emitter keygen page, for example: njPofPVkJJq7GEoQUFHN49jSbtw4DoX_/drone-position/
cd mqtt-connector
cd chart

export PATH=$PATH:~/.k3sup/bin/

export DRONE_POSITION_CHANNEL_KEY="njPofPVkJJq7GEoQUFHN49jSbtw4DoX_/drone-position/" # Set with the value you got from installing the Emitter
echo "Topic: $DRONE_POSITION_CHANNEL_KEY"
helm template --name drone-position --namespace openfaas mqtt-connector/ --values mqtt-connector/values.yaml  \
  --set trimChannelKey=false \
  --set topic=$DRONE_POSITION_CHANNEL_KEY \
  --set broker=tcp://emitter:8080 \
| kubectl apply -f -
kubectl logs -n openfaas deploy/drone-position-mqtt-connector -f

2019/12/13 17:05:43 Topic: i-Xrxb7mpm-bs5LVY3jDU_kcE2XAgnyY/drone-position/     Broker: tcp://emitter:8080

Add the connector for the Drone events

Now install the chart, but edit the command-line as follows

  • Set topic=$DRONE_EVENT_CHANNEL_KEY with the channel key generated by the Emitter keygen page, for example: 5F7ofPVkJJq7GEoQUFHN459jLeo4DoX_/drone-event/
cd mqtt-connector
cd chart

export PATH=$PATH:~/.k3sup/bin/
export DRONE_EVENT_CHANNEL_KEY="5F7ofPVkJJq7GEoQUFHN459jLeo4DoX_/drone-event/" # Set with the value you got from installing the Emitter
echo "Topic: $DRONE_EVENT_CHANNEL_KEY"

helm template --name drone-event --namespace openfaas mqtt-connector/ --values mqtt-connector/values.yaml  \
  --set trimChannelKey=false \
  --set topic=$DRONE_EVENT_CHANNEL_KEY \
  --set broker=tcp://emitter:8080 \
| kubectl apply -f -

Check the logs:

kubectl logs -n openfaas deploy/drone-event-mqtt-connector -f

2019/12/13 17:05:43 Topic: RMPCeXiw2r5WGmi31ef6b1H86SYI8CAq/drone-event/     Broker: tcp://emitter:8080

Test the Drone positions topic (drone-position/)

Feel free to try publishing a message:

  • Make use of ./openfaas/services/publisher.py

  • Make sure the emitter is port-forwarded:

kubectl port-forward -n openfaas svc/emitter 8081:8080
  • Run the publisher
cd ./openfaas/services/

# Install the MQTT broker library
sudo pip3 install paho-mqtt  # Alternatively use `pip`

export CHANNEL_KEY="D6vCeXiw2r5WGmi31ef6bxZNqhAI8CAq/drone-position/" # as per values.yaml

python3 ./publisher.py       # Alternatively use `python`

You should see this message:

2019/12/13 17:07:16 Invoking (http://gateway.openfaas:8080) on topic: "drone-position/", value: "{\"name\": \"Carpark-watch\", \"tempCelsius\": 3.5, \"location\": {\"lat\": 52.5740072, \"lon\": -0.2399354}, \"batteryPercent\": 80}"
2019/12/13 17:07:16 Invoke function: db-inserter.openfaas-fn
[200] drone-position/ => db-inserter.openfaas-fn
{"status":"OK"}
2019/12/13 17:07:16 connector-sdk got result: [200] drone-position/ => db-inserter.openfaas-fn (15) bytes
2019/12/13 17:07:16 tester got result: [200] drone-position/ => db-inserter.openfaas-fn (15) bytes