Multiple MQTT sensors in raspberrypi

Hi,

I need some input in the finishing stage of my project. I am using Cayenne on a raspberrypi for a solar powered / off-grid cabin automation project. The device will have the following features:

  • Inside temperature
  • Outside temperature
  • Battery voltage on the solar cell battery bank
  • Diesel tank level (using ultrasonic sensor)
  • Run rpitx script to turn on the diesel heater
  • Run rpitx script to start the generator in case of low battery

The idea is to preheat the cabin a few hours before arrival using the diesel heater, as the regular fireplace uses a couple of hours to bring the cabin up to comfort temperature.

The voltage sensor and the ultrasonic diesel tank level sensor are not supported by cayenne out of the box, so I have used MQTT to implement these. This goes also for running the rpitx scripts to start the diesel heater and the generator (system commands, launched when the button is pressed, which in turn launches rpitx to replicate 433 mHZ remote control signals)

I have managed to set everything up, and it is working. However, I have some questions on the “putting into production” of the python code to do the MQTT part.

I have created one python script, that includes all the code:

  • Import the modules I need
  • Set variables
  • Define some functions for reading sensor values
  • Listen for messages from MQTT (start diesel heater / generator)
  • Loop through the sensors every 5 seconds, and send the values to Cayenne

The script is included in the crontab of the raspberrypi to be run at boottime, and due to the loop it will continue to run continuously.

One issue I have encountered is that if I disconnect one sensor, the script will stop. I have also encountered some stability issues, where the messages sent from Cayenne dashboard does not trigger the devices. I am considering to split the script into one python script per sensor / message listener, to avoid a “full breakdown” in case one sensor does not return a value. Any thoughts on this? I guess better error handling in the script would be one way to go, but my programming skills are at the “self-learned”/“google to see what I find” level.

Maybe there are other ways to implement MQTT on rasberrypi than to launch python script from crontab?

Thanks for any input!

Best regards,
Anders
Norway

you would just use a Python Exceptions: An Introduction – Real Python so that even if one sensor is removed, others still work.

there are many factors that can affect this case, one being the code. can you share the code you are using?

what do you mean by this?