'python workaround gpiozero when_motion with multiple sensors
i'm trying to listen on 3 motion sensors, each one triggering its own sound
i'm using when_motion because it's creating a thread, so i can do other thing while the sensors doing their thing
but!
you can assign a function to it as long as it has no parameter.
well, not what says the documentation:
"This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that activated it will be passed as that parameter."
not sure of what it means because when i do that
MotionSensor(17).when_motion = myfunction(parameter)
i get this error:
/usr/lib/python3/dist-packages/gpiozero/mixins.py:191: CallbackSetToNone: The callback was set to None. This may have been unintentional e.g. btn.when_pressed = pressed() instead of btn.when_pressed = pressed
warnings.warn(CallbackSetToNone(callback_warning))
while this works fine:
MotionSensor(17).when_motion = myfunction
so i'm trying to create a class to workaround that
but i don't know how to pass a variable (call element) from the class to the function without create a parameter for the function
how will you do that?
my non working code so far:
from gpiozero import MotionSensor
import vlc
from time import sleep
player = vlc.MediaPlayer()
state = 0
class PIR:
def __init__(self, pin, element):
self.pir = MotionSensor(pin=pin)
self.pir.when_motion = self.motion
self.element = element
def motion(self):
if state == 0:
print ("{}!".format(element))
player.set_media(vlc.Media("{}.wav".format(element)))
player.play()
Pir1 = PIR(17,"feu")
Pir2 = PIR(22,"eau")
Pir3 = PIR(27,"vent")
while True:
print("listening")
sleep(1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
