Difference between revisions of "BluetoothLE on a Raspberry Pi"
(→Hints) |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 49: | Line 49: | ||
The maximum characters that can be transmitted are 16, so you will want to look at a URL shortener like: | The maximum characters that can be transmitted are 16, so you will want to look at a URL shortener like: | ||
*https://bitly.com | *https://bitly.com | ||
− | |||
− | Please also read the following link to workout how .com is really encoded | + | Please also read the following link to workout how .com is really encoded as well as how https:// is encodede |
− | + | https://github.com/google/eddystone/tree/master/eddystone-url | |
*".com/" is actually encoded as "00" | *".com/" is actually encoded as "00" | ||
+ | *"https://" is actually encoded as 03 | ||
=== Beacons containing Sensor data === | === Beacons containing Sensor data === | ||
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 84: | Line 83: | ||
while True: | while True: | ||
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) |
− | time.sleep(3) | + | temp_float = float(temp) |
+ | temp_int = round(temp_float,0) | ||
+ | temp_int = int(temp_int) | ||
+ | temp_hex = hex(temp_int).split('x')[-1] | ||
+ | print(temp_int) | ||
+ | #line = line + str(temp_hex) | ||
+ | #print(line) | ||
+ | #time.sleep(3) | ||
+ | </pre> | ||
+ | |||
+ | Can you integrate these two pieces of code? Can you integrate them? | ||
+ | |||
+ | <pre> | ||
+ | #!/usr/bin/python3 | ||
+ | |||
+ | import os | ||
+ | |||
+ | line = "sudo hcitool -i hci0 cmd 0x08 0x0008 1c 02 01 06 03 03 aa fe 14 16 aa fe 20 00 03 01 " | ||
+ | os.system(line) | ||
</pre> | </pre> | ||
Line 98: | Line 115: | ||
A different way of doing this is with Python. Get python-pip and a supporting library | A different way of doing this is with Python. Get python-pip and a supporting library | ||
− | sudo apt-get install | + | sudo apt-get install libglib2.0-dev |
Then you can run: | Then you can run: | ||
− | sudo | + | sudo pip3 install bluepy |
You can then run: | You can then run: |
Latest revision as of 02:21, 19 October 2022
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
Please also read the following link to workout how .com is really encoded as well as how https:// is encodede https://github.com/google/eddystone/tree/master/eddystone-url
- ".com/" is actually encoded as "00"
- "https://" is actually encoded as 03
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: 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) temp_hex = hex(temp_int).split('x')[-1] print(temp_int) #line = line + str(temp_hex) #print(line) #time.sleep(3)
Can you integrate these two pieces of code? Can you integrate them?
#!/usr/bin/python3 import os line = "sudo hcitool -i hci0 cmd 0x08 0x0008 1c 02 01 06 03 03 aa fe 14 16 aa fe 20 00 03 01 " os.system(line)
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 libglib2.0-dev
Then you can run:
sudo pip3 install bluepy
You can then run:
sudo blescan
- What is the difference between a -34dBm device and an -97dBm device which one is closer?