|
| 1 | +# SPDX-FileCopyrightText: 2024 johnpark for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +''' |
| 5 | +BLE BBQ Thermometer to WiFi to Adafruit IO Dashboard |
| 6 | +Feather ESP32-S3 8MB No PSRAM |
| 7 | +''' |
| 8 | +import os |
| 9 | +import time |
| 10 | +import adafruit_connection_manager |
| 11 | +import wifi |
| 12 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 13 | +import adafruit_ble |
| 14 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 15 | +from adafruit_ble_ibbq import IBBQService |
| 16 | + |
| 17 | + |
| 18 | +aio_username = os.getenv("aio_username") |
| 19 | +aio_key = os.getenv("aio_key") |
| 20 | + |
| 21 | +print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") |
| 22 | +wifi.radio.connect( |
| 23 | + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") |
| 24 | +) |
| 25 | +print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}") |
| 26 | + |
| 27 | +### Feeds ### |
| 28 | +feeds = [aio_username + f"/feeds/bbq{i}" for i in range(1, 7)] |
| 29 | +battery_feed = aio_username + "/feeds/bbq_battery" |
| 30 | + |
| 31 | +# Define callback methods which are called when events occur |
| 32 | +# pylint: disable=unused-argument, redefined-outer-name |
| 33 | +def connected(client, userdata, flags, rc): |
| 34 | + print("Connected to Adafruit IO") |
| 35 | + |
| 36 | +def disconnected(client, userdata, rc): |
| 37 | + print("Disconnected from Adafruit IO") |
| 38 | + |
| 39 | +# Create a socket pool |
| 40 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 41 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
| 42 | +connection_manager = adafruit_connection_manager.get_connection_manager(pool) |
| 43 | + |
| 44 | +# Set up a MiniMQTT Client |
| 45 | +mqtt_client = MQTT.MQTT( |
| 46 | + broker="io.adafruit.com", |
| 47 | + port=1883, |
| 48 | + username=aio_username, |
| 49 | + password=aio_key, |
| 50 | + socket_pool=pool, |
| 51 | + ssl_context=ssl_context, |
| 52 | +) |
| 53 | + |
| 54 | +# Setup the callback methods above |
| 55 | +mqtt_client.on_connect = connected |
| 56 | +mqtt_client.on_disconnect = disconnected |
| 57 | + |
| 58 | +# Connect the client to the MQTT broker. |
| 59 | +print("Connecting to Adafruit IO...") |
| 60 | +mqtt_client.connect() |
| 61 | + |
| 62 | +# PyLint can't find BLERadio for some reason so special case it here. |
| 63 | +ble = adafruit_ble.BLERadio() # pylint: disable=no-member |
| 64 | + |
| 65 | +ibbq_connection = None |
| 66 | +battery_percentage = 100 |
| 67 | + |
| 68 | +def c_to_f(temp_c): |
| 69 | + return (temp_c * 9/5) + 32 |
| 70 | + |
| 71 | +def volt_to_percent(voltage, max_voltage): |
| 72 | + return (voltage / max_voltage) * 100 |
| 73 | + |
| 74 | +def probe_check(temp): # if value is wildly high no probe is connected |
| 75 | + return temp if temp <= 11000 else 0 |
| 76 | + |
| 77 | +battery_val = 3.3 |
| 78 | + |
| 79 | + |
| 80 | +while True: |
| 81 | + print("Scanning...") |
| 82 | + for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): |
| 83 | + if IBBQService in adv.services: |
| 84 | + print("found an IBBq advertisement") |
| 85 | + ibbq_connection = ble.connect(adv) |
| 86 | + print("Connected") |
| 87 | + break |
| 88 | + |
| 89 | + # Stop scanning whether or not we are connected. |
| 90 | + ble.stop_scan() |
| 91 | + |
| 92 | + if ibbq_connection and ibbq_connection.connected: |
| 93 | + ibbq_service = ibbq_connection[IBBQService] |
| 94 | + ibbq_service.init() |
| 95 | + while ibbq_connection.connected: |
| 96 | + print( |
| 97 | + "Temperatures:", |
| 98 | + ibbq_service.temperatures, |
| 99 | + "; Battery:", |
| 100 | + ibbq_service.battery_level, |
| 101 | + ) |
| 102 | + |
| 103 | + grill_vals = [probe_check(c_to_f(temp)) for temp in ibbq_service.temperatures] |
| 104 | + battery_val, battery_max = ibbq_service.battery_level |
| 105 | + battery_percentage = (volt_to_percent(battery_val, 3.3)) |
| 106 | + |
| 107 | + mqtt_client.loop(timeout=1) |
| 108 | + |
| 109 | + for feed, val in zip(feeds, grill_vals): |
| 110 | + print(f"Sending grill value: {val} to {feed}...") |
| 111 | + mqtt_client.publish(feed, val) |
| 112 | + |
| 113 | + mqtt_client.publish(battery_feed, battery_percentage) |
| 114 | + print("Sent") |
| 115 | + time.sleep(5) |
0 commit comments