Difference between revisions of "MQTT/Mosquitto"
From Teknologisk videncenter
< MQTT
(Created page with "=Example on publish/subscribe= ==Publish script== '''NOTE:''' No security in this example <source lang=bash> #!/bin/bash # Publish: # Robot/stand4/motor/x/rpm # Robot/stand4/...") |
(No difference)
|
Latest revision as of 06:44, 27 October 2023
Example on publish/subscribe
Publish script
NOTE: No security in this example
#!/bin/bash
# Publish:
# Robot/stand4/motor/x/rpm
# Robot/stand4/motor/x/temperatur
# Robot/stand4/motor/x/position
# Robot/stand8/motor/y/rpm
# Robot/stand8/motor/y/temperatur
# Robot/stand8/motor/y/position
# henrik/cputemp/fahrenheit
publish() {
mosquitto_pub -h 93.166.84.21 -m $1 -t $2
}
robot() {
XRPM=`echo "$RANDOM % 500 + 1000" | bc`
YRPM=`echo "$RANDOM % 500 + 1000" | bc`
XPOS=`echo "$RANDOM % 360" | bc`
YPOS=`echo "$RANDOM % 360" | bc`
XTMP=`echo "$RANDOM % 50 + 150" | bc`
YTMP=`echo "$RANDOM % 50 + 150" | bc`
publish $XRPM "robot/${1}/motor/x/rpm"
publish $YRPM "robot/${1}/motor/y/rpm"
publish $XPOS "robot/${1}/motor/x/position"
publish $YPOS "robot/${1}/motor/y/position"
publish $XTMP "robot/${1}/motor/x/temperatur"
publish $YTMP "robot/${1}/motor/y/temperatur"
}
while :
do
TEMPERATUR=`echo "$RANDOM % 50 + 150" | bc`
publish $TEMPERATUR "henrik/cputemp/fahrenheit"
echo -en "."
sleep 2
robot stand4
sleep 1
robot stand8
sleep 1
done
Subscribe
Example 1:
mosquitto_sub -v -h 93.166.84.21 -t '#'
Example 2:
mosquitto_sub -v -h 93.166.84.21 -t 'robot/+/motor/+/rpm'
Mosquitto on Ubuntu
To use mosquitto on all interfaces and anonymous. Add these lines to /etc/mosqquitto/mosquitto.conf
allow_anonymous true
listener 1883 0.0.0.0