This class contains the functions that you can use in connection with the temperature sensor of the micro:bit
This sensor measures the temperature of the micro:bit in degrees Celsius.
These are all options offered by the Bluetooth temperature service
See Also: https://lancaster-university.github.io/microbit-docs/ble/temperature-service/
Source code in src/kaspersmicrobit/services/temperature.py
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 | class TemperatureService:
"""
This class contains the functions that you can use in connection with the temperature sensor of the micro:bit
This sensor measures the temperature of the micro:bit in degrees Celsius.
These are all options offered by the Bluetooth temperature service
See Also: https://lancaster-university.github.io/microbit-docs/ble/temperature-service/
"""
def __init__(self, device: BluetoothDevice):
self._device = device
def is_available(self) -> bool:
"""
Checks whether the temperature Bluetooth service is found on the connected micro:bit.
Returns:
true if the temperature service was found, false if not.
"""
return self._device.is_service_available(Service.TEMPERATURE)
def notify(self, callback: Callable[[int], None]):
"""
You can call this method whenever you want to be notified of the temperature. How often you receive data
depends on the period. By default the period is 1 second.
Args:
callback: a function that is called periodically with the temperature as an argument
Raises:
errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
to activate temperature data notifications (normally does not occur)
"""
self._device.notify(Service.TEMPERATURE, Characteristic.TEMPERATURE,
lambda sender, data: callback(int.from_bytes(data[0:1], 'little', signed=True)))
def read(self) -> int:
"""
Reads the temperature of the micro:bit.
Returns:
the temperature in degrees Celsius
Raises:
errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
to read the temperature (normally does not occur)
"""
return int.from_bytes(
self._device.read(Service.TEMPERATURE, Characteristic.TEMPERATURE)[0:1], 'little', signed=True)
def set_period(self, period: int):
"""
Sets the interval at which you receive the temperature if you requested it via the notify method
By default the period is 1 second.
Args:
period (int): the interval at which you receive temperature data in milliseconds
Raises:
errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
to write the temperature period (normally does not occur)
"""
self._device.write(Service.TEMPERATURE, Characteristic.TEMPERATURE_PERIOD, period.to_bytes(2, "little"))
def read_period(self) -> int:
"""
Returns the interval at which you receive the temperature via notify
Returns:
The interval in milliseconds
Raises:
errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
to read the temperature period (normally does not occur)
"""
return int.from_bytes(
self._device.read(Service.TEMPERATURE, Characteristic.TEMPERATURE_PERIOD)[0:2], "little")
|
is_available
Checks whether the temperature Bluetooth service is found on the connected micro:bit.
Returns:
-
bool
–
true if the temperature service was found, false if not.
Source code in src/kaspersmicrobit/services/temperature.py
| def is_available(self) -> bool:
"""
Checks whether the temperature Bluetooth service is found on the connected micro:bit.
Returns:
true if the temperature service was found, false if not.
"""
return self._device.is_service_available(Service.TEMPERATURE)
|
notify
notify(callback: Callable[[int], None])
You can call this method whenever you want to be notified of the temperature. How often you receive data
depends on the period. By default the period is 1 second.
Parameters:
-
callback
(Callable[[int], None]
)
–
a function that is called periodically with the temperature as an argument
Raises:
Source code in src/kaspersmicrobit/services/temperature.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 | def notify(self, callback: Callable[[int], None]):
"""
You can call this method whenever you want to be notified of the temperature. How often you receive data
depends on the period. By default the period is 1 second.
Args:
callback: a function that is called periodically with the temperature as an argument
Raises:
errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
to activate temperature data notifications (normally does not occur)
"""
self._device.notify(Service.TEMPERATURE, Characteristic.TEMPERATURE,
lambda sender, data: callback(int.from_bytes(data[0:1], 'little', signed=True)))
|
read
Reads the temperature of the micro:bit.
Returns:
-
int
–
the temperature in degrees Celsius
Raises:
Source code in src/kaspersmicrobit/services/temperature.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62 | def read(self) -> int:
"""
Reads the temperature of the micro:bit.
Returns:
the temperature in degrees Celsius
Raises:
errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
to read the temperature (normally does not occur)
"""
return int.from_bytes(
self._device.read(Service.TEMPERATURE, Characteristic.TEMPERATURE)[0:1], 'little', signed=True)
|
set_period
Sets the interval at which you receive the temperature if you requested it via the notify method
By default the period is 1 second.
Parameters:
-
period
(int
)
–
the interval at which you receive temperature data in milliseconds
Raises:
Source code in src/kaspersmicrobit/services/temperature.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77 | def set_period(self, period: int):
"""
Sets the interval at which you receive the temperature if you requested it via the notify method
By default the period is 1 second.
Args:
period (int): the interval at which you receive temperature data in milliseconds
Raises:
errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
to write the temperature period (normally does not occur)
"""
self._device.write(Service.TEMPERATURE, Characteristic.TEMPERATURE_PERIOD, period.to_bytes(2, "little"))
|
read_period
Returns the interval at which you receive the temperature via notify
Returns:
-
int
–
The interval in milliseconds
Raises:
Source code in src/kaspersmicrobit/services/temperature.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92 | def read_period(self) -> int:
"""
Returns the interval at which you receive the temperature via notify
Returns:
The interval in milliseconds
Raises:
errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
to read the temperature period (normally does not occur)
"""
return int.from_bytes(
self._device.read(Service.TEMPERATURE, Characteristic.TEMPERATURE_PERIOD)[0:2], "little")
|