Add async mqtt connection
This commit is contained in:
parent
a7189d3714
commit
cffdf32735
|
@ -1,43 +1,62 @@
|
||||||
|
import os, logging
|
||||||
|
DEBUG = os.environ.get('DEBUG')
|
||||||
|
logging.basicConfig(
|
||||||
|
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
|
||||||
|
level=logging.DEBUG if DEBUG else logging.INFO)
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
import asyncio
|
||||||
|
|
||||||
import bleak
|
import bleak
|
||||||
from adafruit_ble import BLERadio
|
from adafruit_ble import BLERadio
|
||||||
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
||||||
from adafruit_ble.services.nordic import UARTService
|
from adafruit_ble.services.nordic import UARTService
|
||||||
|
|
||||||
|
from aiomqtt import Client
|
||||||
|
|
||||||
ble = BLERadio()
|
ble = BLERadio()
|
||||||
|
|
||||||
def scan():
|
def scan():
|
||||||
print("disconnected, scanning")
|
logging.info('Disconnected, scanning')
|
||||||
for advertisement in ble.start_scan(ProvideServicesAdvertisement, timeout=1):
|
for advertisement in ble.start_scan(ProvideServicesAdvertisement, timeout=1):
|
||||||
if not advertisement.complete_name.startswith('Feather'):
|
if not advertisement.complete_name.startswith('Feather'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
ble.connect(advertisement)
|
ble.connect(advertisement)
|
||||||
print("connected to", advertisement.complete_name)
|
logging.info('Connected to %s', advertisement.complete_name)
|
||||||
break
|
break
|
||||||
ble.stop_scan()
|
ble.stop_scan()
|
||||||
|
|
||||||
def read(connection):
|
async def read(connection, mqtt_client):
|
||||||
uart = connection[UARTService]
|
uart = connection[UARTService]
|
||||||
if uart.in_waiting:
|
if uart.in_waiting:
|
||||||
res = uart.read(uart.in_waiting)
|
res = uart.read(uart.in_waiting)
|
||||||
print(res)
|
data = res.decode().strip().split()
|
||||||
|
logging.info('Received data: %s', data)
|
||||||
|
|
||||||
def loop():
|
await mqtt_client.publish('test', payload=str(data))
|
||||||
|
|
||||||
|
async def main():
|
||||||
while True:
|
while True:
|
||||||
while ble.connected:
|
async with Client('dev.tannercollin.com') as mqtt_client:
|
||||||
for connection in ble.connections:
|
while ble.connected:
|
||||||
try:
|
for connection in ble.connections:
|
||||||
read(connection)
|
try:
|
||||||
except bleak.exc.BleakError:
|
await read(connection, mqtt_client)
|
||||||
print('Disconnected, waiting 3 seconds and trying to reconnect...')
|
except bleak.exc.BleakError:
|
||||||
connection.disconnect()
|
logging.info('Disconnected, waiting 3 seconds and trying to reconnect...')
|
||||||
time.sleep(3)
|
connection.disconnect()
|
||||||
|
await asyncio.sleep(3)
|
||||||
|
|
||||||
time.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
|
scan()
|
||||||
|
|
||||||
scan()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
loop()
|
logging.info('')
|
||||||
|
logging.info('==========================')
|
||||||
|
logging.info('Booting up...')
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
loop.run_until_complete(main())
|
||||||
|
|
|
@ -3,8 +3,7 @@
|
||||||
#include <InternalFileSystem.h>
|
#include <InternalFileSystem.h>
|
||||||
#include "HX711.h"
|
#include "HX711.h"
|
||||||
|
|
||||||
//#define calibration_factor -6980.0 // black wired load cell
|
#define calibration_factor -6980.0 // black wired load cell
|
||||||
#define calibration_factor -5760.0 // orange wired load cell
|
|
||||||
|
|
||||||
#define DOUT 30
|
#define DOUT 30
|
||||||
#define CLK 27
|
#define CLK 27
|
||||||
|
@ -91,7 +90,7 @@ void loop()
|
||||||
int reading = (int) scale.get_units();
|
int reading = (int) scale.get_units();
|
||||||
String message = String(reading) + "\n";
|
String message = String(reading) + "\n";
|
||||||
message.toCharArray(buf, message.length()+1);
|
message.toCharArray(buf, message.length()+1);
|
||||||
bleuart.write(buf, message.length()+1);
|
bleuart.write(buf, message.length());
|
||||||
|
|
||||||
delay(50);
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user