wjmnelis
Posts: 12
Joined: Thu Oct 26, 2017 5:03 pm

Python for three independent control functions?

Sun Jan 21, 2018 6:52 pm

Being new to (using a) Raspberry Pi and python, I am wondering if the following functions can be performed by one RPI 0 W, running raspbian and python. There will be two and possibly three independent functions I like to do with this RPi0:

(A) Using time and weather forecast, control a home ventilation system. The ventilation system is controlled using the (hardware) PWM GPIO output. The frequency of changes is very low, the minimum time between two changes is two minutes. For this function a few sensors will be added in a later stage, which will be connected using I2C. These sensors will be polled, with a low frequency.
(B) Receive multiple messages per second via a serial port and send a summary via Wifi to a server once every 5 minutes
(C) Query a power meter, connected to the converter of solar cells, using bluetooth and send a summary via Wifi to a server once every 5 minutes. As many details about the bluetooth connection are still unknown to me, it is not certain that this function will be implemented.

One objective is to have the CPU utilisation as low as possible. Besides the above mentioned functions, the RPI0 will also need to support a few SSH sessions (via Wifi) for maintenance, monitoring of the log files and periodic backups.

I think a handful of independent watchdog timers are needed, each with a different function to perform if the timer expires. For GPIO there seems to be methods to handle events through call-back functions, however I did not find something similar for serial I/O (handling of a BREAK is in this case important) or bluetooth.

Any comments on the feasibility of implementing these functions using python, or on alternative approaches will be appreciated.

ghans
Posts: 7770
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Python for three independent control functions?

Sun Jan 21, 2018 8:34 pm

It seems to me that you should look into "systemd timers" or "cron jobs".
Of course Raspbian supports and expects multiple Python scripts running in parallel.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

bertlea
Posts: 151
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: Python for three independent control functions?

Fri Jan 26, 2018 9:49 am

If you don’t want to touch the OS configurations (e.g. cron jobs) and have a pure Python solution, you can just Threading in Python. You should make each worker thread doing one independent task. Your threads can communicate to the main thread and the other worker threads via thread-safe queue. I prefer this kind of pure Python solution as it is more portable (easy to deploy to another Pi).

I used that method to implement a web API (using Flask) and communicate to a worker thread controlling a blinkt (a set of RGB LED) on a Pi Zero W. So, other programs within the Pi or on the network can send request to that Pi Zero to show a sequence of LED display. Because the sequence need some time to complete, so it is on a separate worker thread and the request from Web API is queued up waiting to be “served” if the blinkt worker thread still displaying a LED sequence.

ejolson
Posts: 1738
Joined: Tue Mar 18, 2014 11:47 am

Re: Python for three independent control functions?

Fri Jan 26, 2018 9:12 pm

bertlea wrote:
Fri Jan 26, 2018 9:49 am
If you don’t want to touch the OS configurations (e.g. cron jobs) and have a pure Python solution, you can just Threading in Python.
While there is a system crontab for OS stuff, each user also has an individual crontab that is specifically intended for non-OS user-level stuff. For the five minute updates it may be lower impact and easier to add a few lines to your crontab using

$ crontab -e

Cron has been optimized over the years to handle many scheduled events for many users with very low overhead.

bertlea
Posts: 151
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: Python for three independent control functions?

Wed Jan 31, 2018 6:40 am

Seems its time for me to update myself on cron job. Good to know it is consistent and easy to use now.

Return to “General programming discussion”

Who is online

Users browsing this forum: No registered users and 5 guests