Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4782

Python • Re: Button to restart Pi, button to restart program

$
0
0
On "button to restart program":
That doesn't appear to be what you want. It appears what you want is a button to trigger a manual update of the data.
You are absolutely correct. I wish to refresh data.
I can think of a few ways, but I'd put the code that does the updating into a function (without the sleep) . I'd then have the button press call that function. gpiozero's button class makes this easy to do.
I've been trying something like this. The sleep function is used to make the script repeat itself every 15 minutes. I have been trying to do this but the sleep function just hangs everything until it is done it's 15 minutes. I'm trying different things to get around it, but hit a snag with importing an installed module within a virtual environment (mentioned in another post on this topic).

You might want to take an event driven approach rather than a traditional linear one and instead of a sleep use a timer with a callback function.

An untested noddy example:

Code:

import gpiozeroimport signalimport threadingdef do_stuff():     print('doing some stuff')      def do_timed_stuff():     print('timer fired')     t.cancel()  # just to be sure     t.start()     do_stuff()  # after the timer restart to minimise driftbtn = gpiozero.Button(2)btn.when_pressed = do_stufft = threading.timer(15 * 60, do_timed_stuff)t.start()do_stuff()signal.pause()  # this pauses your main program so nothing will happen until one of the callbacks fires.
Timer documentation: https://docs.python.org/3/library/threa ... er-objects
*: Full disclosure - I wrote that.
I know you did, and it is fantastic and helpful. Thank you for it, and your help!

Thanks.

Statistics: Posted by thagrol — Sun Aug 25, 2024 5:24 pm



Viewing all articles
Browse latest Browse all 4782

Trending Articles