-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
95 lines (77 loc) · 3.37 KB
/
code.py
File metadata and controls
95 lines (77 loc) · 3.37 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
# Based on example from https://github.com/adafruit/Adafruit_CircuitPython_BLE - "ble_json_peripheral"
# Sensor values from https://github.com/furbrain/CircuitPython_seeed_xiao_nRF52840 - "CircuitPython_seeed_xiao_nRF52840"
import time
from ble_json_service import SensorService
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.uuid import VendorUUID, StandardUUID
from adafruit_ble.services import Service
from adafruit_ble.characteristics import Characteristic
from adafruit_ble.characteristics.json import JSONCharacteristic
from adafruit_ble.characteristics.stream import StreamOut, StreamIn
from adafruit_ble.characteristics.string import StringCharacteristic
from adafruit_ble.characteristics.int import IntCharacteristic, Uint8Characteristic
import array
import time
import board
import digitalio
from seeed_xiao_nrf52840 import IMU, Battery
# Create BLE radio, custom service, and advertisement.
ble = BLERadio()
# name
ble.name = "Sleeep Watch"
# service = SensorService()
class streamedService(Service):
uuid = VendorUUID("84cdd328-bc04-4340-87f0-44b9fa8d65e6")
acceldata = StringCharacteristic(
uuid=VendorUUID("f2af6a35-23c3-4a98-ba9d-f695c4db44ef"),
properties=(Characteristic.READ | Characteristic.NOTIFY),
)
idealHz = Uint8Characteristic(
uuid=VendorUUID("f2af6a35-23c3-4a98-ba9d-f695c4db44e1"),
properties=(Characteristic.READ | Characteristic.WRITE),
initial_value=15,
)
contents = StreamOut(uuid=VendorUUID("f2af6a35-23c3-4a98-ba9d-f695c4db44e0"))
strServInstance = streamedService()
# advertisement = ProvideServicesAdvertisement(service)
advertisement = ProvideServicesAdvertisement(strServInstance)
# advertisement.short_name ="Sleeep Watch"
# define onboard LED
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
with Battery() as bat:
print(f"Charge_status: {bat.charge_status}")
print(f"Voltage: {bat.voltage}")
print(f"Charge_current high?: {bat.charge_current}")
print("Setting charge current to high")
bat.charge_current = bat.CHARGE_100MA
print(f"Charge_current high?: {bat.charge_current}")
with IMU() as imu:
packetcounter = 0
# Advertise until another device connects, when a device connects, provide sensor data.
while True:
print("Advertise services")
ble.stop_advertising() # you need to do this to stop any persistent old advertisement
ble.start_advertising(advertisement)
print("Waiting for connection...")
while not ble.connected:
led.value = False # on
print("Connected")
while ble.connected:
led.value = True # off
strServInstance.acceldata = '{"accel": [%f, %f, %f], "gyro": [%f, %f, %f], "temp": %f, "bat": %f, "id": %d}' % (
imu.acceleration[0],
imu.acceleration[1],
imu.acceleration[2],
imu.gyro[0],
imu.gyro[1],
imu.gyro[2],
imu.temperature,
bat.voltage,
packetcounter,
)
print(packetcounter)
packetcounter += 1
time.sleep(1/strServInstance.idealHz)
print("Disconnected")