In this example, w e're gonna to listen on the trigger of the Grove PIR sensor, in Python code.
Connect the Grove PIR sensor's
D1
pin to the ReSpeaker Core v2's header pin
0
w ith jumper. Don't f orget to w ire the VCC and GND at the same time.
Then copy the code below into a new f ile and save it as a python f ile, name as
m raa_pir.py
. Copy this f ile into your ReSpeaker Core v2.
import mraa
def on_trigger(gpio):
print("pin " + repr(gpio.getPin(True)) + " = " + repr(gpio.read()))
pin = 0
try:
x = mraa.Gpio(pin)
print("Starting ISR for pin " + repr(pin))
x.dir(mraa.DIR_IN)
# respeaker v2 only support EDGE_BOTH
x.isr(mraa.EDGE_BOTH, on_trigger, x)
var = raw_input("Press ENTER to stop")
x.isrExit()
except ValueError as e:
print(e)
Then run the code w ith the command below .(make sure you have located in the f older w hich contains the mraa_pir.py you've just saved)
sudo python mraa_pir.py
The result w ill be like
$ sudo python mraa_pir.py
Starting ISR for pin 0
Press ENTER to stoppin 1091 = 0
pin 1091 = 0
pin 1091 = 1
...
B. Us e UPM Library
The UPM project implements sensors' driver based on the MRAA library, so w e no longer need to care about the GPIO programming or w hat the I2C address of a
sensor, all the def ault inf ormations and logics f or a particular sensor has been w rapped into a UPM library. UPM has supported bunch of sensors.
https://iotdk.intel.com/docs/master/upm/modules.html
. But please note that w e didnt conf irm every sensor w orks on the ReSpeaker Core V2.
Exam ple for Grove Digital Light Se ns or
Materials
Re Spe ak e r Core v2
Grove - PIR Motion Se ns or