-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMicrobit_ReadTemp.py
More file actions
166 lines (148 loc) · 5.91 KB
/
Microbit_ReadTemp.py
File metadata and controls
166 lines (148 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python3
#
# Connects to a specified device and reads the temperature characteristic of a BBC microbit.
# Run from the command line with a bluetooth device address argument
# Bluetooth Technology for Linux Developers Study Guide
# Bluetooth SIG and Martin Wooley
# Includes sample bluetooth code for Microbit and Raspberry Pi
# https://www.bluetooth.com/blog/the-bluetooth-for-linux-developers-study-guide/
# Microbit v2 Hardware (uses the NRF52833 processor)
# Pimoroni Stock # MBIT0007
# https://shop.pimoroni.com
import bluetooth_constants
import bluetooth_utils
import dbus
import dbus.mainloop.glib
import sys
import time
from gi.repository import GLib
sys.path.insert(0, '.')
bus = None
device_interface = None
device_path = None
found_ts = False
found_tc = False
ts_path = None
tc_path = None
def read_temperature():
global tc_path
char_proxy = bus.get_object(bluetooth_constants.BLUEZ_SERVICE_NAME,tc_path)
char_interface = dbus.Interface(char_proxy, bluetooth_constants.GATT_CHARACTERISTIC_INTERFACE)
try:
value = char_interface.ReadValue({})
except Exception as e:
print("Failed to read temperature")
print(e.get_dbus_name())
print(e.get_dbus_message())
return bluetooth_constants.RESULT_EXCEPTION
else:
temperature = bluetooth_utils.dbus_to_python(value[0])
print("Temperature="+str(temperature)+"C")
return bluetooth_constants.RESULT_OK
def service_discovery_completed():
global found_ts
global found_tc
global ts_path
global tc_path
global bus
if found_ts and found_tc:
print("Required service and characteristic found - device is OK")
print("Temperature service path: ",ts_path)
print("Temperature characteristic path: ",tc_path)
read_temperature()
else:
print("Required service and characteristic were not found - device is NOK")
print("Temperature service found: ",str(found_ts))
print("Temperature characteristic found: ",str(found_tc))
bus.remove_signal_receiver(interfaces_added,"InterfacesAdded")
bus.remove_signal_receiver(properties_changed,"PropertiesChanged")
mainloop.quit()
def properties_changed(interface, changed, invalidated, path):
global device_path
if path != device_path:
return
if 'ServicesResolved' in changed:
sr = bluetooth_utils.dbus_to_python(changed['ServicesResolved'])
print("ServicesResolved : ", sr)
if sr == True:
service_discovery_completed()
def interfaces_added(path, interfaces):
global found_ts
global found_tc
global ts_path
global tc_path
if bluetooth_constants.GATT_SERVICE_INTERFACE in interfaces:
properties = interfaces[bluetooth_constants.GATT_SERVICE_INTERFACE]
print("--------------------------------------------------------------------------------")
print("SVC path :", path)
if 'UUID' in properties:
uuid = properties['UUID']
if uuid == bluetooth_constants.TEMPERATURE_SVC_UUID:
found_ts = True
ts_path = path
print("SVC UUID : ", bluetooth_utils.dbus_to_python(uuid))
print("SVC name : ", bluetooth_utils.get_name_from_uuid(uuid))
return
if bluetooth_constants.GATT_CHARACTERISTIC_INTERFACE in interfaces:
properties = interfaces[bluetooth_constants.GATT_CHARACTERISTIC_INTERFACE]
print(" CHR path :", path)
if 'UUID' in properties:
uuid = properties['UUID']
if uuid == bluetooth_constants.TEMPERATURE_CHR_UUID:
found_tc = True
tc_path = path
print(" CHR UUID : ", bluetooth_utils.dbus_to_python(uuid))
print(" CHR name : ", bluetooth_utils.get_name_from_uuid(uuid))
flags = ""
for flag in properties['Flags']:
flags = flags + flag + ","
print(" CHR flags : ", flags)
return
if bluetooth_constants.GATT_DESCRIPTOR_INTERFACE in interfaces:
properties = interfaces[bluetooth_constants.GATT_DESCRIPTOR_INTERFACE]
print(" DSC path :", path)
if 'UUID' in properties:
uuid = properties['UUID']
print(" DSC UUID : ", bluetooth_utils.dbus_to_python(uuid))
print(" DSC name : ", bluetooth_utils.get_name_from_uuid(uuid))
return
def connect():
global bus
global device_interface
try:
device_interface.Connect()
except Exception as e:
print("Failed to connect")
print(e.get_dbus_name())
print(e.get_dbus_message())
if ("UnknownObject" in e.get_dbus_name()):
print("Try scanning first to resolve this problem")
return bluetooth_constants.RESULT_EXCEPTION
else:
print("Connected OK")
return bluetooth_constants.RESULT_OK
if (len(sys.argv) != 2):
print("usage: python3 read_temperature.py [bdaddr]")
sys.exit(1)
bdaddr = sys.argv[1]
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
adapter_path = bluetooth_constants.BLUEZ_NAMESPACE + bluetooth_constants.ADAPTER_NAME
device_path = bluetooth_utils.device_address_to_path(bdaddr, adapter_path)
device_proxy = bus.get_object(bluetooth_constants.BLUEZ_SERVICE_NAME,device_path)
device_interface = dbus.Interface(device_proxy, bluetooth_constants.DEVICE_INTERFACE)
print("Connecting to " + bdaddr)
connect()
print("Discovering services++")
print("Registering to receive InterfacesAdded signals")
bus.add_signal_receiver(interfaces_added,
dbus_interface = bluetooth_constants.DBUS_OM_IFACE,
signal_name = "InterfacesAdded")
print("Registering to receive PropertiesChanged signals")
bus.add_signal_receiver(properties_changed,
dbus_interface = bluetooth_constants.DBUS_PROPERTIES,
signal_name = "PropertiesChanged",
path_keyword = "path")
mainloop = GLib.MainLoop()
mainloop.run()
print("Finished")