![]() |
PIR help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: PIR help (/thread-33946.html) |
PIR help - duckredbeard - Jun-12-2021 I have a Raspberry Pi that monitors my keg fridge's temperature, pressure, and weight of the three kegs. It sends that information to an Android tablet that displays this data. I also have a PIR motion sensor that initiates a message to the tablet to "wake up" and show the home screen, the tablet also makes a short beep sound. Ideally, I would have this send this wakeup message no more than once per 2 minutes, the tablet's screen timeout is set to two minutes. This should keep the screen on as long as there is motion seen. The problem I am having with this code is that if it sees constant motion, it does not send the message. It does, however, send the message after I stop moving and then move again. I have done some tests and manually sent the wake message during the movement and the tablet correctly responds. Behold the code: from gpiozero import Button, MotionSensor, LED, PWMLED import requests from signal import pause from time import sleep import time start = time.time() enablepir = 1 from datetime import datetime now = datetime.now() current_time = now.strftime("%H:%M:%S") print("Service started at", current_time) kegpir = MotionSensor(10) def kegmotion(): global start if enablepir == 1: now = time.time() if now > start: r = requests.post("https://autoremotejoaomgcd.appspot.com/sendmessage?key=SuperSecretDeviceIDHiddenForPrivacy&message=wakee") # Message to tablet that motion has been detected near the fridge print("motion detected") start = time.time() start = start + 20 #seconds to wait before enabling motion scan again, set just under tablet timeout to keep tablet waking def kegmotionstop(): placeholder = 1 kegpir.when_motion = kegmotion kegpir.when_no_motion = kegmotionstop pause()The "def kegmotionstop()" is only there because I extracted this from another code I have, where in that code stopping motion triggers something. It is probably not needed in this script. Again, my end goal is to have the script do the requests.post as long as there is motion detected, but not every time it sees motion. I can change the tablet's screen timeout to 1, 2, 5, or 10 minutes, but I prefer 2 or 5. RE: PIR help - duckredbeard - Jun-12-2021 Really thinned it out and added a sleep step at line 14 for 100 seconds, the tablet would be set to time out at 120 seconds. As long as motion is detected between the end of the 100 seconds and the 120 second screen time out, this should work? Just theorizing now. from gpiozero import MotionSensor import requests from signal import pause from time import sleep import time from datetime import datetime kegpir = MotionSensor(10) def kegmotion(): r = requests.post("https://autoremotejoaomgcd.appspot.com/sendmessage?key=SuperSecretDeviceIDHiddenForPrivacy&message=wakee") # Message to tablet that motion has been detected near the fridge print("motion detected") sleep(100) def kegmotionstop(): placeholder = 1 kegpir.when_motion = kegmotion kegpir.when_no_motion = kegmotionstop RE: PIR help - duckredbeard - Jun-12-2021 Now that I am home and testing, here is what I have. The tablet is set to timeout at 2 minutes (120 seconds). I am running the code that I posted in my second post. The tablet gets the message and wakes & beeps when there is motion. Waking is working. And then I had a "EUREKA!" moment. I was overcomplicating things. Set tablet timeout to 30 minutes. Change line 17 to be a requests.post message to the tablet to "sleepee" similar to line 12 but a different keyword Have Tasker in the tablet respond to the message by turning the display off. Now, line 14 keeps the wake message from repeating constantly. Kind of like a "cooldown" function. When motion stops, the tablet gets the sleep message. Bingo. |