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=/test/open
pin_a=2
pin_b=3

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

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

while true
do
  rm -f /tmp/cmds
  mosquitto_sub --remove-retained -i 10.0.0.254 -C 1 -h $host -u $user -P $pass -t $topic >/tmp/cmds

  if grep -q "employee" /tmp/cmds; then
  echo "0" > /sys/class/gpio/gpio$pin_a/value
  fi

  if grep -q "corporate" /tmp/cmds; then
  echo "0" > /sys/class/gpio/gpio$pin_a/value
  echo "0" > /sys/class/gpio/gpio$pin_b/value
  fi

  date >>/var/log/open.log
  sleep 2
  echo "1" > /sys/class/gpio/gpio$pin_a/value
  echo "1" > /sys/class/gpio/gpio$pin_b/value
  sleep 4
done
mqtt.txt · Last modified: 2022/02/15 14:57 by Jan Forman