advertising via bluetooth is done
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
import bluetooth
|
import bluetooth
|
||||||
import struct
|
import struct
|
||||||
|
import time
|
||||||
from micropython import const
|
from micropython import const
|
||||||
|
from queue import Queue
|
||||||
|
import uasyncio
|
||||||
|
|
||||||
|
|
||||||
# i just made a UUID up and changed the last number to change what protocol you're using
|
# i just made a UUID up and changed the last number to change what protocol you're using
|
||||||
@@ -39,6 +42,12 @@ class BluetoothHandler:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
print("Initializing Bluetooth...")
|
print("Initializing Bluetooth...")
|
||||||
self.ble = bluetooth.BLE()
|
self.ble = bluetooth.BLE()
|
||||||
|
|
||||||
|
print("Resetting bluetooth...")
|
||||||
|
# Deactivate BLE to clear the state
|
||||||
|
self.ble.active(False)
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
print("Activating...")
|
print("Activating...")
|
||||||
self.ble.active(True)
|
self.ble.active(True)
|
||||||
print("Setting IRQ callback...")
|
print("Setting IRQ callback...")
|
||||||
@@ -53,11 +62,12 @@ class BluetoothHandler:
|
|||||||
|
|
||||||
self.connections = set()
|
self.connections = set()
|
||||||
|
|
||||||
|
self.packets = Queue()
|
||||||
|
|
||||||
self.advertise()
|
self.advertise()
|
||||||
|
|
||||||
def deserialize_msg(self, s: bytes):
|
def deserialize_msg(self, s: bytes):
|
||||||
# returns packet type (int) and deserialized data
|
# returns packet type (int) and deserialized data
|
||||||
print(s[1:].decode())
|
|
||||||
return s[0], eval(s[1:].decode())
|
return s[0], eval(s[1:].decode())
|
||||||
|
|
||||||
def _get_mac_address(self):
|
def _get_mac_address(self):
|
||||||
@@ -118,4 +128,5 @@ class BluetoothHandler:
|
|||||||
packet_type, msg = self.deserialize_msg(msg)
|
packet_type, msg = self.deserialize_msg(msg)
|
||||||
|
|
||||||
print(f"Received: \"{msg}\"")
|
print(f"Received: \"{msg}\"")
|
||||||
|
uasyncio.create_task(self.packets.put((packet_type, msg)))
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import uasyncio as asyncio
|
|||||||
|
|
||||||
|
|
||||||
class Connection:
|
class Connection:
|
||||||
def __init__(self):
|
def __init__(self, bluetooth_handler=None):
|
||||||
|
self.bluetooth_handler = bluetooth_handler
|
||||||
self.load_public_constants()
|
self.load_public_constants()
|
||||||
|
|
||||||
# generate diffie-hellman keys
|
# generate diffie-hellman keys
|
||||||
@@ -19,16 +20,23 @@ class Connection:
|
|||||||
# start lora handler
|
# start lora handler
|
||||||
self.lora = LoRaHandler()
|
self.lora = LoRaHandler()
|
||||||
|
|
||||||
|
async def bluetooth_listener(self):
|
||||||
|
while True:
|
||||||
|
msg_type, payload = await self.bluetooth_handler.packets.get()
|
||||||
|
|
||||||
|
if msg_type == 3: # advertise self
|
||||||
|
self.lora.advertise()
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
print("Starting connection...")
|
print("Starting connection...")
|
||||||
self.lora.advertise()
|
|
||||||
|
if self.bluetooth_handler:
|
||||||
|
asyncio.create_task(self.bluetooth_listener())
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
msg: LoRaMessage = await self.lora.message_queue.get()
|
msg: LoRaMessage = await self.lora.message_queue.get()
|
||||||
|
|
||||||
print(f"Received message from {msg.sender_id}: {msg.payload}")
|
print(f"Received message from {msg.sender_id}: {msg.payload}")
|
||||||
print("Sending response...")
|
|
||||||
self.lora.send(b"Pong!", msg.sender_id)
|
|
||||||
|
|
||||||
def load_public_constants(self):
|
def load_public_constants(self):
|
||||||
with open("public_constants.txt", "r") as f:
|
with open("public_constants.txt", "r") as f:
|
||||||
|
|||||||
@@ -3,29 +3,11 @@ import uuid
|
|||||||
from sx1262 import SX1262
|
from sx1262 import SX1262
|
||||||
|
|
||||||
import uasyncio as asyncio
|
import uasyncio as asyncio
|
||||||
|
from queue import Queue
|
||||||
|
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
led = Pin(25, Pin.OUT)
|
led = Pin(25, Pin.OUT)
|
||||||
|
|
||||||
class Queue:
|
|
||||||
def __init__(self):
|
|
||||||
self.items = []
|
|
||||||
self.waiters = []
|
|
||||||
|
|
||||||
async def put(self, item):
|
|
||||||
self.items.append(item)
|
|
||||||
if self.waiters:
|
|
||||||
waiter = self.waiters.pop(0)
|
|
||||||
waiter.set()
|
|
||||||
|
|
||||||
async def get(self):
|
|
||||||
while not self.items:
|
|
||||||
event = asyncio.Event()
|
|
||||||
self.waiters.append(event)
|
|
||||||
await event.wait()
|
|
||||||
|
|
||||||
return self.items.pop(0)
|
|
||||||
|
|
||||||
class FixedDeque:
|
class FixedDeque:
|
||||||
def __init__(self, maxlen):
|
def __init__(self, maxlen):
|
||||||
self.maxlen = maxlen
|
self.maxlen = maxlen
|
||||||
|
|||||||
@@ -7,16 +7,16 @@ import time
|
|||||||
import crypt_random
|
import crypt_random
|
||||||
import uasyncio as asyncio
|
import uasyncio as asyncio
|
||||||
|
|
||||||
BLUETOOTH_ENABLED = False
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
bluetooth_handler = None
|
bluetooth_handler = None
|
||||||
if BLUETOOTH_ENABLED:
|
try:
|
||||||
from bluetooth_handler import BluetoothHandler
|
from bluetooth_handler import BluetoothHandler
|
||||||
bluetooth_handler = BluetoothHandler()
|
bluetooth_handler = BluetoothHandler()
|
||||||
|
except ImportError:
|
||||||
|
print("Bluetooth is disabled. Continuing without it...")
|
||||||
|
|
||||||
conn = Connection()
|
conn = Connection(bluetooth_handler)
|
||||||
asyncio.run(conn.start())
|
asyncio.run(conn.start())
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
21
relay/queue.py
Normal file
21
relay/queue.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import uasyncio as asyncio
|
||||||
|
|
||||||
|
|
||||||
|
class Queue:
|
||||||
|
def __init__(self):
|
||||||
|
self.items = []
|
||||||
|
self.waiters = []
|
||||||
|
|
||||||
|
async def put(self, item):
|
||||||
|
self.items.append(item)
|
||||||
|
if self.waiters:
|
||||||
|
waiter = self.waiters.pop(0)
|
||||||
|
waiter.set()
|
||||||
|
|
||||||
|
async def get(self):
|
||||||
|
while not self.items:
|
||||||
|
event = asyncio.Event()
|
||||||
|
self.waiters.append(event)
|
||||||
|
await event.wait()
|
||||||
|
|
||||||
|
return self.items.pop(0)
|
||||||
Reference in New Issue
Block a user