Difference between revisions of "BluetoothLE on a Raspberry Pi"
Line 70: | Line 70: | ||
#!/usr/bin/python3 | #!/usr/bin/python3 | ||
− | import RPi. | + | import RPi.GPIO |
import time | import time | ||
import os | import os | ||
import sys | import sys | ||
import signal | import signal | ||
− | |||
temp = os.popen('vcgencmd measure_temp').readline() | temp = os.popen('vcgencmd measure_temp').readline() | ||
Line 83: | Line 82: | ||
while True: | while True: | ||
+ | line = "sudo hcitool -i hci0 cmd 0x08 0x0008 1c 02 01 06 03 03 aa fe 14 16 aa fe 20 00 03 01 " | ||
temp = os.popen('vcgencmd measure_temp').readline() | temp = os.popen('vcgencmd measure_temp').readline() | ||
− | print(temp) | + | #print(temp) |
temp= temp.replace("temp=","").replace("'C\n","") | temp= temp.replace("temp=","").replace("'C\n","") | ||
− | print(temp) | + | #print(temp) |
+ | temp_float = float(temp) | ||
+ | temp_int = round(temp_float,0) | ||
+ | temp_int = int(temp_int) | ||
+ | print(temp_int) | ||
+ | line = line + str(temp_int) | ||
+ | print(line) | ||
+ | #line = line + str(t) | ||
+ | #print(temp) | ||
+ | #line = line + temp | ||
+ | #print(line) | ||
+ | os.system(line) | ||
time.sleep(3) | time.sleep(3) | ||
+ | |||
</pre> | </pre> | ||
Revision as of 04:49, 12 October 2020
Contents
Sending Bluetooth LE Beacons
In this first activity, we will show you how you can craft a Bluetooth LE beacon using the Raspberry Pi.
Start by logging into your Raspberry Pi and then check that Bluez is installed:
sudo apt install bluez
We are going to use the Raspberry Pi to transmit an Eddystone beacon.
Lets look at the available commands:
hciconfig -h
Lets bring up the Bluetooth device on our Raspberry Pi using below command:
sudo hciconfig hci0 up
Then, lets set the Bluetooth interface to advertise but not be connectable:
sudo hciconfig hci0 leadv 3
Beacons containing URLs
Now we will construct and send the Bluetooth LE beacon:
sudo hcitool -i hci0 cmd 0x08 0x0008 1c 02 01 06 03 03 aa fe 14 16 aa fe 10 00 03 6D 75 72 64 6F 63 68 2E 65 64 75 2E 61 75 00 00 00
[Linux commands ] OFG OCF [Len] [Flag+L] [UUID + ? ] [TypeIF] [URL+TX] [This is the URL based Payload ]
This will create a Bluetooth low energy beacon that points to murdoch.edu.au
We will read this BTLE becon on our smartphone. Download and open the Bluetooth app linked to below based on your phone:
- https://apps.apple.com/au/app/ble-scanner-4-0/id1221763603
- https://play.google.com/store/apps/details?id=com.bridou_n.beaconscanner
Pickup the Eddystone beacon and visit the URL, where does it take you?
Challenge
Go here https://www.rapidtables.com/convert/number/ascii-hex-bin-dec-converter.html and then reverse engineer the string above. You know it points to murdoch.edu.au.
- Can you change the hcitool string to point to your own site, or your favourite website?
- Can you point it to Little Salmon Bay on Rotnest (https://www.google.com/maps/place/Little+Salmon+Bay/@-32.0333166,115.5377011,14.65z/data=!4m5!3m4!1s0x2a2d5a5442996ffd:0x9032cfa0949b90f8!8m2!3d-32.0246369!4d115.5250543)
Hints
The maximum characters that can be transmitted are 16, so you will want to look at a URL shortener like:
*https://bitly.com *https://tinyurl.com/
Please also read the following link to workout how .com is really encoded.
- https://github.com/google/eddystone/tree/master/eddystone-url
- ".com/" is actually encoded as "00"
Beacons containing Sensor data
Try the following:
sudo hcitool -i hci0 cmd 0x08 0x0008 1c 02 01 06 03 03 aa fe 14 16 aa fe 20 00 03 01 02 03 04
Scan on your phone, play with and try changing the values while reading: https://github.com/google/eddystone/blob/master/eddystone-tlm/tlm-plain.md
Challenge
The following python code will report the temp of the CPU. Can you get this temperature value into the beacon:
#!/usr/bin/python3 import RPi.GPIO import time import os import sys import signal temp = os.popen('vcgencmd measure_temp').readline() print(temp) temp= temp.replace("temp=","").replace("'C\n","") print(temp) while True: line = "sudo hcitool -i hci0 cmd 0x08 0x0008 1c 02 01 06 03 03 aa fe 14 16 aa fe 20 00 03 01 " temp = os.popen('vcgencmd measure_temp').readline() #print(temp) temp= temp.replace("temp=","").replace("'C\n","") #print(temp) temp_float = float(temp) temp_int = round(temp_float,0) temp_int = int(temp_int) print(temp_int) line = line + str(temp_int) print(line) #line = line + str(t) #print(temp) #line = line + temp #print(line) os.system(line) time.sleep(3)
You can use the code below to use bash from Python
import subprocess subprocess.check_call(['whoami'])
Scanning for Bluetooth LE Devices
Use Bluetooth Low Energy to scan the environment around you with:
hcitool -i hci0 lescan
A different way of doing this is with Python. Get python-pip and a supporting library
sudo apt-get install python-pip libglib2.0-dev
Then you can run:
sudo pip install bluepy
You can then run:
sudo blescan
- What is the difference between a -34dBm device and an -97dBm device which one is closer?