-
Notifications
You must be signed in to change notification settings - Fork 7
Feature/several improvements #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2077ff3
15aa9b9
e31ee58
8c70f4a
4034cd7
3c5d9a0
e1439e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great. I was forgot to move this value into
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be best to store it as int in milliseconds, because the usage is always in miliseconds. Currently it is converted every time the BLE stack sends new data. I was also thinking if it would be good to have to separate values for Websockets and BLE. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| #pragma once | ||
|
|
||
| // Thermocouple pin assignments | ||
| #if defined(ESP8266) | ||
| #if defined(NODEMCU_V2) | ||
| #define SCK_PIN D5 | ||
| #define SO_PIN D7 | ||
| #define CS_PIN_BT D8 | ||
| #define CS_PIN_ET D6 | ||
| #elif defined(ESP32) | ||
| #elif defined(ESP32_C3) | ||
| #define SCK_PIN 4 | ||
| #define SO_PIN 5 | ||
| #define CS_PIN_BT 7 | ||
| #define CS_PIN_ET 6 | ||
| #elif defined(ESP32_DEV) | ||
| #define SCK_PIN SCK | ||
| #define SO_PIN MISO | ||
| #define CS_PIN_BT SS | ||
| #define CS_PIN_ET MOSI | ||
| #endif |
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'll move
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, we have |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
|
|
||
| import asyncio | ||
| import json | ||
| import logging | ||
| import time | ||
|
|
||
| from websockets.asyncio.server import serve | ||
| from websockets.exceptions import ConnectionClosedOK | ||
| from bleak import BleakScanner, BleakClient | ||
|
|
||
| DATA_UUID = "d56d0059-ad65-43f3-b971-431d48f89a69" | ||
|
|
||
| logger = logging.getLogger("BLEtoWebsocketsBridge") | ||
| logging.basicConfig(level=logging.INFO, format="%(asctime)s\t%(levelname)s\t%(message)s") | ||
| ble_queue = asyncio.Queue() | ||
| first_time = True | ||
|
|
||
| # see https://github.com/hbldh/bleak/issues/801 | ||
| async def ble_callback(sender, data: bytearray): | ||
| global ble_queue | ||
| while not ble_queue.empty(): | ||
| await ble_queue.get() | ||
| ble_data = json.loads(data) | ||
| rec_time = time.time() | ||
| await ble_queue.put((rec_time, ble_data)) | ||
|
|
||
|
|
||
| # Find the Croaster by name, return None if not found | ||
| async def find_croaster(): | ||
| devices = await BleakScanner.discover() | ||
| for d in devices: | ||
| logging.debug(str(d)) | ||
| if d.name is not None and "[C4C4]" in d.name: | ||
| logging.info("Found device " + d.name ) | ||
| return d | ||
| return None | ||
|
|
||
| async def request_handler(websocket): | ||
| global ble_queue, first_time | ||
| try: | ||
| while True: | ||
| message = await websocket.recv() | ||
| decoded_message = json.loads(message) | ||
| if decoded_message["command"] == "getArtisanData": | ||
| # something like: {"command":"getArtisanData","id":6120,"roasterID":0} | ||
| response_id = decoded_message["id"] | ||
| # data = { "BT": 137, "ET": 117} | ||
| # response = {"id": response_id, "data": data } | ||
| # if first_time: | ||
| # first_time = False | ||
| # # discard first sample for better syncroniization. | ||
| # rec_time, ble_data = await ble_queue.get() | ||
|
|
||
| start_time = time.time() | ||
| rec_time, ble_data = await ble_queue.get() | ||
|
|
||
| stop_time = time.time() | ||
| artisan_response = ble_data | ||
| artisan_response["id"] = response_id | ||
| response_json = json.dumps(artisan_response) | ||
| et = artisan_response["data"]["ET"] | ||
| bt = artisan_response["data"]["BT"] | ||
| elapsed_time_ms = (stop_time - start_time) * 1000 | ||
| data_age_ms = (stop_time - rec_time) * 1000 | ||
| logging.info(f"Response for {response_id}: ET: {et:.2f}C, BT {bt:.2f}C (delay: {elapsed_time_ms:.0f}ms, age: {data_age_ms:.0f}ms)") | ||
| await websocket.send(response_json) | ||
| else: | ||
| logging.warning(f"Unknown command {decoded_message['command']}") | ||
| except ConnectionClosedOK: | ||
| logging.info("Artisan disconnected") | ||
|
|
||
|
|
||
|
|
||
| async def main(): | ||
| logging.info("Bridge started") | ||
| global croaster_client | ||
| croaster_adress = await find_croaster() | ||
| if croaster_adress is None: | ||
| logging.error("Croaster not found") | ||
| exit() | ||
| croaster_client = BleakClient(croaster_adress) | ||
| await croaster_client.connect() | ||
| if not croaster_client.is_connected: | ||
| exit() | ||
| await croaster_client.start_notify(DATA_UUID, ble_callback) | ||
|
|
||
| async with serve(request_handler, "localhost", 8765) as server: | ||
| await server.serve_forever() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| asyncio.run(main()) | ||
| except KeyboardInterrupt: | ||
| logger.info("Shutting down") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| bleak | ||
| websockets |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is comment is not necessary. Since
debugln(x)is macroSerial.println(x), it will not shown while not monitor it.