User Tools

Site Tools


mqtt

MQTT

Mosquitto

mosquitto_passwd -c /etc/mosquitto/pwdfile user
allow_anonymous false
password_file /etc/mosquitto/pwdfile


configure websocket

listener 8083
protocol websockets
certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem

QoS

Level
QoS 0 - at most once There is no guarantee of delivery
QoS 1 - at least once Message is delivered at least one time
QoS 2 - exactly once Safest and slowest quality of service level

Bash MQTT RaspberryPI

Install client libraries

apt install mosquitto-clients
#!/bin/bash
##########################
cd /tmp
host=10.0.0.1
user=user
pass=pass
topic=zavora
p=/tmp/pipe
pin=2
rm $p

if [ ! -f /sys/class/gpio/gpio$pin/value ]; then
echo "$pin" > /sys/class/gpio/export
fi
echo "out" > /sys/class/gpio/gpio$pin/direction
echo "1" > /sys/class/gpio/gpio$pin/value

mkfifo $p
(mosquitto_sub -h $host -u $user -P $pass -t $topic >$p 2>/dev/null) &
while read line <$p
do
  echo $line > /tmp/cmds
  if grep -q "ON" /tmp/cmds; then
  echo "0" > /sys/class/gpio/gpio$pin/value
  sleep 0.7
  echo "1" > /sys/class/gpio/gpio$pin/value
  date >>/var/log/open.log
  fi
done
mqtt.txt · Last modified: 2020/06/16 11:39 by Jan Forman