Mosquitto Support in DEV


 

CParrott

New member
I had some time to play around with the mosquitto_pub in the latest dev. Had some issues with rc.local running before WIFI was up (and thinking it was an issue with rc.local but it wasnt) so I made this script. Maybe it will help someone.

start_mosquitto_pub.sh:
Code:
#!/bin/sh

MQTT_HOST=192.168.0.123
MQTT_PORT=1883
MQTT_USER="somemosquittouser"
MQTT_PWD="somemosquittopassword"
MQTT_CLIENT="heatermeter"
MQTT_TOPIC="heatermeter/hmstatus"

#TESTING...
#echo "HOST:     " $MQTT_HOST
#echo "PORT:     " $MQTT_PORT
#echo "USER:     " $MQTT_USER
#echo "PASSWORD: " $MQTT_PWD
#echo "CLIENT:   " $MQTT_CLIENT
#echo "TOPIC:    " $MQTT_TOPIC

#Wait for ping return of the MQTT server before trying to connect
while ! ping -c 1 -W 1 $MQTT_HOST; do
    sleep 1
done

#Connect to MQTT and publish HeaterMeter data...
/usr/bin/lmclient @LMSS,1 | /usr/bin/mosquitto_pub -h $MQTT_HOST -p $MQTT_PORT -u $MQTT_USER -P $MQTT_PWD -i $MQTT_CLIENT -t $MQTT_TOPIC -l

Seems to work the few times I have rebooted the HeaterMeter. Id like to add something to have it try to reconnect if there's an error in mosquitto_pub or it disconnects. May be best to make an init.d script but I dont know how to do this yet.

If I get anywhere with NodeRed flow I'll post it here if anyone is interested.

EDIT: Added '/root/start_mosquitto_pub.sh' to /etc/rc.local before the 'exit 0' BTW...
 
Last edited:
I'm interested. I'm working on converting a lot of my smart devices to MQTT control to help with HA/NodeRed integration. I'll give this a shot next time I have my HM hooked up.
Thank you for the work.
 
EDIT: Added '/root/start_mosquitto_pub.sh' to /etc/rc.local before the 'exit 0' BTW...
Looks good except you'll want to add a & to the end of the command in rc.local. The script blocks so system startup never completes, which means it also won't reboot if you ask. It should be:
Code:
/root/start_mosquitto_pub.sh &
exit 0

I'd also recommend against putting the script in /root, since that is blasted away when the firmware is upgraded. /mnt/mmcblk0p4 is used for archives, so putting the script there will persist it across firmware upgrades. /etc/rc.local is already persisted by the system backup operation that takes place before an upgrade.
 
Thanks for the help! I have made the changes you suggested and it is working great!

Code:
/mnt/mmcblk0p4/start_mosquitto_pub.sh &

exit 0

I appriciate the project and all the work that went into it! Sorry to stumble my way through Linux 101 on here...
 
Sorry to stumble my way through Linux 101 on here...
No worries, you're doing great! Ideally this should all be done through the webui but I just haven't had time to properly integrate it and just wanted to have something that was usable as quickly as possible. Making it user friendly comes a little later.
 

 

Back
Top