sampazzer
Posts: 30
Joined: Wed Nov 13, 2013 7:57 pm

Automation and Control

Thu Jul 26, 2018 2:13 pm

Hi all.

I have a requirement to temperature control a greenhouse. I have other sensors but for simplicity sake I'll keep it at that.

I need to be able to view the temperature and change the required set point. I may also want to upload this data to a database so I can trend the values.

I've attempted this before on a single Pi and got so caught up and stuck using multithreading or multiprocessing that I threw the towel in.

The main problem is that I have a while true control loop that runs continuously e.g. monitors temp, compares against setpoint, opens window or starts heater dependant on the result. This takes up all processor power and means there's no chance of running any GUI / IoT code without using multiple threads. I also then struggled to share variables between threads using queues ect.

Having re-thought this. Would it be easier to have two Pi's communicating over serial. One to record the sensors and perform the control loop and the other to deal with sending setpoint data and recieving sensor data to display on the GUI and database control?

Anyone that can shed some light on this... It would be greatly appreciated.

scotty101
Posts: 3753
Joined: Fri Jun 08, 2012 6:03 pm

Re: Automation and Control

Thu Jul 26, 2018 2:26 pm

The Pi is more than capable of doing both tasks and more at the same time without you having to deal with threading/multiprocessing.

You are running on Linux which is very capable of managing/sharing resources between programs. It might just be that the way you've written your program leaves no time for other programs to take over.

For example, if you have a very large infinite while loop, put a sleep in at the end. During the sleep the processor is free to do other things.

Using a database might be a good way for you to share data. Log the temperature in to a SQLite database periodically and then the GUI/IoT program can read this data when it needs it.

Many people try to update GUIs or read temperature far too quickly. On a GUI do you really need to update the screen more than once a second? With temperature, does the temperature really change that much over 1-2 seconds. If it is a greenhouse that you are measuring the temperature of, will the temperature change dramatically over the space of 5 minutes? If not, you only need to read the temperature every 5 minutes.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
The Traveler
Posts: 363
Joined: Sat Oct 21, 2017 3:48 pm

Re: Automation and Control

Thu Jul 26, 2018 4:14 pm

The main problem is that I have a while true control loop that runs continuously e.g. monitors temp, compares against setpoint, opens window or starts heater dependant on the result
Set interval sampling, say 15 minute intervals. There's no need to sample continuously because the temperature or humidity takes time to change. Constant controlling of an environment that can take hours to fluctuate is wasted effort. I've programmed commercial greenhouse systems and never sampled below 15 minutes, even with plants like roses and dahlias which can be more sensitive to heat and moisture fluctuations.

In a simple application, using a db like Sqlite3 has low CPU overhead and is easily integrated.
Retired IT professional, C programmer and "beardie weirdie".
RPi interests: Developing an Infinite Improbability Drive
“Thinking outside of the box allows you to get rewards outside of your reach.” Matshona Dhliwayo

sampazzer
Posts: 30
Joined: Wed Nov 13, 2013 7:57 pm

Re: Automation and Control

Thu Jul 26, 2018 7:12 pm

Thank you for your input. Now for some questions!

My background is in industrial automation so I work with Siemens PLC's a lot. I guess I might be trying to fit the project into a box that compares too much to an industrial PLC and not playing to the Pi's strengths.
For example, if you have a very large infinite while loop, put a sleep in at the end. During the sleep the processor is free to do other things.
I always thought that if you sleep during a while true loop that it just hangs the whole processor for that sleep time and nothing else can be processed unless its in another thread?
If it is a greenhouse that you are measuring the temperature of, will the temperature change dramatically over the space of 5 minutes? If not, you only need to read the temperature every 5 minutes.
Set interval sampling, say 15 minute intervals.
I guess this is true. How would you guys go about setting a sample time for my control/sensing and GUI/IoT loops? Do i need to run some scheduling software or can I hard code it?
Using a database might be a good way for you to share data. Log the temperature in to a SQLite database periodically and then the GUI/IoT program can read this data when it needs it.
This is the kind of idea that I had in mind. Is it difficult to ensure that data has finished being written/read in one program before the other. Example: A user has just changed the control set point to 25 Degrees in the GUI part of the program. This is written to the database, but as this is being written, the control part of the program attempts to read that database field and reads 2Degrees before the whole value. This would cause problematic control!

Again I appreciate your input. Thanks very much!

PhatFil
Posts: 1440
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Automation and Control

Thu Jul 26, 2018 10:02 pm

Check out home automation software, what your wanting to do falls well within the scope of all the options i have seen

personally i use nodered..

check out this project for a wireless sensor roadmap https://github.com/ThomDietrich/miflora-mqtt-daemon


to control your heater and window remotely from your PI i would suggest the itead/sonoff range of wifi enabled controllers reflashed with the tasmota project, that way you can turn remote devices on/off by publishing the correct mqtt topic

https://github.com/arendst/Sonoff-Tasmota/wiki

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Automation and Control

Fri Jul 27, 2018 1:32 am

Instead of looping and sleeping, how about using interrupts?
Without knowing why you are deleting my postings, I will not know how...

User avatar
The Traveler
Posts: 363
Joined: Sat Oct 21, 2017 3:48 pm

Re: Automation and Control

Fri Jul 27, 2018 2:08 am

Instead of looping and sleeping, how about using interrupts?
Not sure why you'd want to do this. Adding the necessary masking and detection adds more complexity to the programming. I mean, sure, you could, but your problem domain is simpler. Interrupts I consider better for events that are asynchronous and/or random. You current design uses a time interval and samples along the time line. Loop and sleep is fine. If you want to take action on an event-based system, ie, temperature drops below a predetermined point and then closes a switch generating an event, then interrupts make sense.

As Albert Einstein put it “Make things as simple as possible, but not simpler." I'd stick with loop and sleep.
Retired IT professional, C programmer and "beardie weirdie".
RPi interests: Developing an Infinite Improbability Drive
“Thinking outside of the box allows you to get rewards outside of your reach.” Matshona Dhliwayo

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Automation and Control

Fri Jul 27, 2018 3:08 am

sampazzer wrote:
Thu Jul 26, 2018 7:12 pm

I guess this is true. How would you guys go about setting a sample time for my control/sensing and GUI/IoT loops? Do i need to run some scheduling software or can I hard code it?
An RTC and using alarms, that you configure through code, is one way. Setting the RTC to cause an interrupt once every Xminutes where the program could then take action; based upon the alarm receipt.

An, inexpensive, DS1307 would do the trick.

I have my RTC set to send my program a 1PPS interrupt. Upon receipt of the 1PPS signal, counters are incremented for several threads. The threads are held in a wait by a threading.event. When a particular counter reaches a predetermined count the threading.event is released, the thread does its ThingDo, and the ThingDo is reset so the whole show can start all over.
Without knowing why you are deleting my postings, I will not know how...

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

Re: Automation and Control

Fri Jul 27, 2018 7:12 am

Linux is a preemptive non-realtime multitasking operating system. Linux has complete control over processes and can kill them if they misbehave (eg. OOM killer). It also schedules processes as it sees fit. This is not different from Windows, by the way. When a process sleeps, Linux is free to schedule other processes in the meantime. Because Linux is not realtime, sleep only puts a lower bound on how long your process sleeps in real life.

Linux also manages all interrupts. You're not supposed to meddle with them. Use poll()
or epoll() instead. Most GPIO libraries offer a custom "callback" mechanism which is similiar.

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

scotty101
Posts: 3753
Joined: Fri Jun 08, 2012 6:03 pm

Re: Automation and Control

Fri Jul 27, 2018 8:55 am

sampazzer wrote:
Thu Jul 26, 2018 7:12 pm
For example, if you have a very large infinite while loop, put a sleep in at the end. During the sleep the processor is free to do other things.
I always thought that if you sleep during a while true loop that it just hangs the whole processor for that sleep time and nothing else can be processed unless its in another thread?
This would be the case on a microcontroller or PLC but not so with Python and Linux (or another OS). The sleep function uses the OS's own sleep function. When you call time.sleep(15) in a python program you are basically saying to the OS "Hi Linux, I'm process 12345 and I don't need any processor time for another 15 seconds, come and see me again in 15 seconds.". This leaves the processor free to schedule other tasks.
sampazzer wrote:
Thu Jul 26, 2018 7:12 pm
If it is a greenhouse that you are measuring the temperature of, will the temperature change dramatically over the space of 5 minutes? If not, you only need to read the temperature every 5 minutes.
Set interval sampling, say 15 minute intervals.
I guess this is true. How would you guys go about setting a sample time for my control/sensing and GUI/IoT loops? Do i need to run some scheduling software or can I hard code it?
There are many ways to do this. You can use the python schedule module or if you just have one great big main loop in your program simple use of the time.time() function can be used.

Code: Select all

#Psuedo code only
start_time = time.time()
while True:
    if time.time() > start_time + 15:
        #15 seconds has elapsed, do this now.
        read_temperature()
        #Restart the timer
        start_time = time.time()
sampazzer wrote:
Thu Jul 26, 2018 7:12 pm
Using a database might be a good way for you to share data. Log the temperature in to a SQLite database periodically and then the GUI/IoT program can read this data when it needs it.
This is the kind of idea that I had in mind. Is it difficult to ensure that data has finished being written/read in one program before the other. Example: A user has just changed the control set point to 25 Degrees in the GUI part of the program. This is written to the database, but as this is being written, the control part of the program attempts to read that database field and reads 2Degrees before the whole value. This would cause problematic control!
The trick is to get in and get out quickly. Open the database for only as long as you need to so the other program can access it. There is no need to worry about partial entries, this won't happen as data isn't stored until you commit it to the database.
Also if you are storing every 15 mintutes and updating the display every 1 minute for example and each read/write operation only takes half a second (even this is exaggerated), what are the odds of the two programs trying to operate simultaneously.
Idahowalker wrote:An RTC and using alarms, that you configure through code, is one way. Setting the RTC to cause an interrupt once every Xminutes where the program could then take action; based upon the alarm receipt.
This, in my opinion, is total overkill. Unless you need very precise timing over a very long period of time, there is no need for a real time clock. Especially if your Pi is connected to the internet and can sync with NTP servers.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

sampazzer
Posts: 30
Joined: Wed Nov 13, 2013 7:57 pm

Re: Automation and Control

Fri Jul 27, 2018 1:42 pm

Thank you all for your information. Some more quick questions. In the past ive tried to run flask as the iot bit. It has a function that starts it's own app. Its always confused me how I get my control to run side by side with a web server. Your saying that if I put enough sleep at the end of my control loop that when someone accesses the site then flask will be able to serve the user?

I got told in the past that the only way to achieve this was to run the loop in another thread so the server can deal with the incoming traffic?

I need the control loop to be running even when noone is connected to the server also.

Also the likes of guizero starts its own app loop which would surely conflict with my control loop?

Hope this makes sense. Thanks again

Return to “Automation, sensing and robotics”