birojanos
Posts: 4
Joined: Sat Jan 17, 2015 4:58 am

Paralell?

Thu Jan 24, 2019 8:36 pm

I want to read out the DS18B20 with Rasperry Pi and I want to control a lamp that heats up a small box. I want to read out Ds18b20 every 10 sec with a while loop but how i can wait in parallel that a button is pressed with GPIO.add_event_detect (17, GPIO.FALLING, callback = interrupt_routine1, bouncetime = 200)

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Paralell?

Thu Jan 24, 2019 10:41 pm

I generally wouldm't use call back threading for such a simple task.

It can be easily achieved like this

Code: Select all

import time

#add code to setup the pin  for button pressed

start_time = time.now()

While True:
	
	#if 10 seconds has passed then read sensor
	if time.now() - start_time > 10:		
		#code to read sensor goes here		
		start_time = time.now()
	
	#if button pressed then execute the code for button pressed
	if button_pressed :
		#code to run of button pressed
		
	#small sleep to rest the CPU
	time.sleep(0.01)

User avatar
rpdom
Posts: 14766
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Paralell?

Thu Jan 24, 2019 11:20 pm

OutoftheBOTS wrote:
Thu Jan 24, 2019 10:41 pm
It can be easily achieved like this
The problem with that code is that unless you do use a callback or something like that, if the button is pressed while the sensor is being read, it won't be detected. The DS18B20 can take a second or so to read.

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Paralell?

Fri Jan 25, 2019 8:50 am

rpdom wrote:
Thu Jan 24, 2019 11:20 pm
OutoftheBOTS wrote:
Thu Jan 24, 2019 10:41 pm
It can be easily achieved like this
The problem with that code is that unless you do use a callback or something like that, if the button is pressed while the sensor is being read, it won't be detected. The DS18B20 can take a second or so to read.
I had no idea that reading that temp sensor was so slow. I have use the sht31-d in the past and it reads instantaneously with very high accuracy.

pootle
Posts: 323
Joined: Wed Sep 04, 2013 10:20 am
Location: Staffordshire
Contact: Website

Re: Paralell?

Sat Jan 26, 2019 9:11 am

Time to bring out the threading then - it sounds hairy but for simple cases where the information all flows in one direction between threads it can be pretty simple.

You don't explain how the 2 inputs interact, so I assume that what you want is something like:
  • * read the temperature every few seconds and remember the result
    * when button is pressed, check the temperature and do something that depends on temperature
so start a simple thread that just

Code: Select all

def readtempforever():
	global lasttemp
	while True:
		lasttemp = readtempsensor()
		time.sleep(10)
and the button handler

Code: Select all

def interrupt_routine1():
    if lasttemp > 21:    // or whatever you want to do
        print("Its too hot - get me our of here!")
     elif lasttemp < 17:
         print("I'm cold where's my woolly underwear?)
     else:
         print('so comfy")
and of course the main code needs to sleep forever or the whole thing will exit

Code: Select all

lasttemp=None
* start the thread to read the temperature
time.sleep(5) // to make sure we've got a first reading
* setup interrupt handler
try:
    while True:
        time.sleep(100)
except KeyboardInterrupt:  // or kill signal if running freestanding
    pass
print("bye bye")

Return to “Python”