简体   繁体   中英

Bash script to scan for iBeacons and use GPIO on Raspberry Pi

I followed a tutorial to illuminate an LED on a Raspberry Pi so that when an iBeacon detected an LED is turned on using the GPIO pins but I need to alter the script so that the LED goes off again when the iBeacon is no longer detected.

The script at the moment is:

#!/bin/bash
gpio mode 1 out
trap "gpio write 1 0" exit
while read line
do
    if [[ `echo $line | grep "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1" ` ]]; then
        gpio write 1 1
    fi     
done

Which is being called by:

$ beacon scan -b | ./scriptName

The out put of beacon scan is:

pi@pibeacon ~ $ sudo beacon scan
BLE Beacon Scan ...
iBeacon     UUID: 92AB49BE-4127-42F4-B532-90FAF1E26491   MAJOR: 1       MINOR: 1       POWER:    -59   RSSI: -62 
iBeacon     UUID: 92AB49BE-4127-42F4-B532-90FAF1E26491   MAJOR: 1       MINOR: 1       POWER: -59   RSSI: -65 
iBeacon     UUID: 92AB49BE-4127-42F4-B532-90FAF1E26491   MAJOR: 1       MINOR: 1       POWER: -59   RSSI: -65 

Continuously updating all the time the iBeacon is detected and just stops when the iBeacon is undetected.

The aim is to have a script run all the time and use the output of the beacon scan command to determine if the LED should be on or off - if the iBeacon is detected the LED should be on and if the iBeacon is then moved out of range the LED turn off again. The existing strip turns the LED on once and then the only way to reset the situation is to stop the script and start it again. Thanks

One way you could accomplish it with your existing code is to set a variable to a timestamp inside your if statement. Then, outside your if statement (but inside the while), you can compare the current time to the timestamp. If enough time has passed since the beacon was detected (say 5 seconds), you code can turn off the LED.

The disadvantage of this approach is that if no beacons are detected at all, your code will block on the read line statement. So this is only workable if you know for sure at least one beacon will always be around to keep your program running. This sort of programming is not ideally suited to a simple bash script, because you really need two threads to handle this. But if you want to keep your same basic toolset, this is a decent option.

I worked out a (bad?) solution and thought I'd share it here. It has the effect of when the beacon is detected the light flashes and then when the beacon goes out of range the light stops flashing. I set this code to run on startup of the Pi and has fulfilled the function I needed (a very rough proof of concept prototype!).

I used the very good Radius Networks Development Kit (which is where the original script is from) and would highly recommend that if anyone else is interested in messing about with iBeacons.

#!/bin/bash
gpio mode 1 out
trap "gpio write 1 0" exit
while read line
do
    if [[ $line = *"2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1"* ]]; then
       gpio write 1 1
    fi     
gpio write 1 0
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM