tryna get bluetooth to work
This commit is contained in:
55
relay/bluetooth_handler.py
Normal file
55
relay/bluetooth_handler.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import bluetooth
|
||||||
|
from micropython import const
|
||||||
|
|
||||||
|
|
||||||
|
# i just made a UUID up and changed the last number to change what protocol you're using
|
||||||
|
SERVICE_UUID = bluetooth.UUID("E1898FF7-5063-4441-a6eb-526073B00001")
|
||||||
|
TX_UUID = bluetooth.UUID("E1898FF7-5063-4441-a6eb-526073B00002")
|
||||||
|
RX_UUID = bluetooth.UUID("E1898FF7-5063-4441-a6eb-526073B00003")
|
||||||
|
|
||||||
|
TX_CHAR = (TX_UUID, bluetooth.FLAG_NOTIFY)
|
||||||
|
RX_CHAR = (RX_UUID, bluetooth.FLAG_WRITE)
|
||||||
|
|
||||||
|
SERVICE = (SERVICE_UUID, (TX_CHAR, RX_CHAR))
|
||||||
|
|
||||||
|
IRQ_CONNECT = const(1)
|
||||||
|
IRQ_DISCONNECT = const(2)
|
||||||
|
IRQ_GATTS_WRITE = const(3)
|
||||||
|
|
||||||
|
|
||||||
|
class BluetoothHandler:
|
||||||
|
def __init__(self):
|
||||||
|
print("initting bluetooth")
|
||||||
|
self.ble = bluetooth.BLE()
|
||||||
|
self.ble.active(True)
|
||||||
|
self.ble.irq(self.irq)
|
||||||
|
|
||||||
|
((self.tx_handle, self.rx_handle),) = self.ble.gatts_register_services((SERVICE,))
|
||||||
|
|
||||||
|
self.connections = set()
|
||||||
|
|
||||||
|
self.advertise()
|
||||||
|
|
||||||
|
def advertise(self):
|
||||||
|
print("advertising")
|
||||||
|
# note: \x02\x01\x06\x0C\x09 is the BLE header that tells other devices we're BLE only and discoverable
|
||||||
|
self.ble.gap_advertise(100_000, b"\x02\x01\x06\x0C\x09MeshConnectorNode")
|
||||||
|
|
||||||
|
def irq(self, event, data):
|
||||||
|
print(f"IRQ | EVENT: {event}, DATA: {data}")
|
||||||
|
if event == IRQ_CONNECT:
|
||||||
|
conn_handle, _, _ = data
|
||||||
|
self.connections.add(conn_handle)
|
||||||
|
elif event == IRQ_DISCONNECT:
|
||||||
|
conn_handle, _, _ = data
|
||||||
|
self.connections.remove(conn_handle)
|
||||||
|
self.advertise()
|
||||||
|
elif event == IRQ_GATTS_WRITE:
|
||||||
|
conn_handle, value_handle = data
|
||||||
|
|
||||||
|
if value_handle == self.tx_handle:
|
||||||
|
msg = self.ble.gatts_read(self.tx_handle)
|
||||||
|
|
||||||
|
if msg == b"ping":
|
||||||
|
for conn in self.connections:
|
||||||
|
self.ble.gatts_notify(conn, self.rx_handle, b"pong")
|
||||||
@@ -2,6 +2,10 @@ from sx1262 import SX1262
|
|||||||
from _sx126x import *
|
from _sx126x import *
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from bluetooth_handler import BluetoothHandler
|
||||||
|
|
||||||
|
LORA_ENABLED = False
|
||||||
|
|
||||||
|
|
||||||
def cb(events):
|
def cb(events):
|
||||||
if events & SX1262.RX_DONE:
|
if events & SX1262.RX_DONE:
|
||||||
@@ -11,7 +15,7 @@ def cb(events):
|
|||||||
elif events & SX1262.TX_DONE:
|
elif events & SX1262.TX_DONE:
|
||||||
print('TX done.')
|
print('TX done.')
|
||||||
|
|
||||||
def main():
|
def init_lora():
|
||||||
print("Creating class...")
|
print("Creating class...")
|
||||||
# initialize our radio, im using the HAT SX1262 hat for the pico
|
# initialize our radio, im using the HAT SX1262 hat for the pico
|
||||||
radio = SX1262(spi_bus=1, clk=10, mosi=11, miso=12, cs=3, irq=20, rst=15, gpio=2)
|
radio = SX1262(spi_bus=1, clk=10, mosi=11, miso=12, cs=3, irq=20, rst=15, gpio=2)
|
||||||
@@ -23,8 +27,20 @@ def main():
|
|||||||
radio.begin(freq=915, bw=125, power=22)
|
radio.begin(freq=915, bw=125, power=22)
|
||||||
radio.setBlockingCallback(False, cb)
|
radio.setBlockingCallback(False, cb)
|
||||||
|
|
||||||
|
def init_bluetooth():
|
||||||
|
bluetooth_handler = BluetoothHandler()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if LORA_ENABLED:
|
||||||
|
init_lora()
|
||||||
|
|
||||||
|
init_bluetooth()
|
||||||
|
|
||||||
|
if LORA_ENABLED:
|
||||||
while True:
|
while True:
|
||||||
print(f"Noise Floor: {radio.getRSSIInst()}")
|
print(f"Noise Floor: {radio.getRSSIInst()} | SNR: {radio.getSNR()}")
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user