Created
May 7, 2026 11:02
-
-
Save benevpi/b8b72b7934efd1c7288d2ea9128f55e7 to your computer and use it in GitHub Desktop.
Pico W BME home assistant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import json | |
| import ssl | |
| import wifi | |
| import socketpool | |
| import board | |
| import busio | |
| import digitalio | |
| from adafruit_bme280 import basic as adafruit_bme280 | |
| import adafruit_minimqtt.adafruit_minimqtt as MQTT | |
| # ========================= | |
| # WIFI + MQTT SETTINGS | |
| # ========================= | |
| WIFI_SSID = "YOUR_WIFI_NAME" | |
| WIFI_PASSWORD = "YOUR_WIFI_PASSWORD" | |
| MQTT_HOST = "homeassistant.local" | |
| MQTT_PORT = 1883 | |
| MQTT_USERNAME = "mqtt_pico" | |
| MQTT_PASSWORD = "yourpassword" | |
| DEVICE_ID = "pico_w_bme280" | |
| DEVICE_NAME = "Pico W BME280" | |
| # ========================= | |
| # BME280 SPI SETUP | |
| # ========================= | |
| spi = busio.SPI( | |
| clock=board.GP2, | |
| MOSI=board.GP3, | |
| MISO=board.GP4, | |
| ) | |
| cs = digitalio.DigitalInOut(board.GP5) | |
| bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, cs) | |
| # ========================= | |
| # WIFI | |
| # ========================= | |
| print("Connecting to WiFi...") | |
| wifi.radio.connect(WIFI_SSID, WIFI_PASSWORD) | |
| print("Connected:", wifi.radio.ipv4_address) | |
| pool = socketpool.SocketPool(wifi.radio) | |
| # ========================= | |
| # MQTT | |
| # ========================= | |
| mqtt = MQTT.MQTT( | |
| broker=MQTT_HOST, | |
| port=MQTT_PORT, | |
| username=MQTT_USERNAME, | |
| password=MQTT_PASSWORD, | |
| socket_pool=pool, | |
| ssl_context=ssl.create_default_context(), | |
| ) | |
| availability_topic = f"{DEVICE_ID}/status" | |
| state_topic = f"{DEVICE_ID}/state" | |
| def connect_mqtt(): | |
| print("Connecting to MQTT...") | |
| mqtt.connect() | |
| mqtt.publish(availability_topic, "online", retain=True) | |
| print("MQTT connected") | |
| def publish_discovery(): | |
| device = { | |
| "identifiers": [DEVICE_ID], | |
| "name": DEVICE_NAME, | |
| "manufacturer": "Raspberry Pi", | |
| "model": "Pico W + BME280 SPI", | |
| } | |
| sensors = [ | |
| { | |
| "key": "temperature", | |
| "name": "Temperature", | |
| "device_class": "temperature", | |
| "unit": "°C", | |
| }, | |
| { | |
| "key": "humidity", | |
| "name": "Humidity", | |
| "device_class": "humidity", | |
| "unit": "%", | |
| }, | |
| { | |
| "key": "pressure", | |
| "name": "Pressure", | |
| "device_class": "pressure", | |
| "unit": "hPa", | |
| }, | |
| ] | |
| for sensor in sensors: | |
| config_topic = ( | |
| f"homeassistant/sensor/" | |
| f"{DEVICE_ID}_{sensor['key']}/config" | |
| ) | |
| payload = { | |
| "name": f"{DEVICE_NAME} {sensor['name']}", | |
| "unique_id": f"{DEVICE_ID}_{sensor['key']}", | |
| "state_topic": state_topic, | |
| "value_template": | |
| "{{ value_json." + sensor["key"] + " }}", | |
| "availability_topic": availability_topic, | |
| "payload_available": "online", | |
| "payload_not_available": "offline", | |
| "device_class": sensor["device_class"], | |
| "unit_of_measurement": sensor["unit"], | |
| "device": device, | |
| } | |
| mqtt.publish(config_topic, json.dumps(payload), retain=True) | |
| time.sleep(0.2) | |
| print("Discovery published") | |
| connect_mqtt() | |
| publish_discovery() | |
| # ========================= | |
| # MAIN LOOP | |
| # ========================= | |
| while True: | |
| try: | |
| mqtt.loop() | |
| payload = { | |
| "temperature": round(bme280.temperature, 2), | |
| "humidity": round(bme280.humidity, 2), | |
| "pressure": round(bme280.pressure, 2), | |
| } | |
| print(payload) | |
| mqtt.publish( | |
| state_topic, | |
| json.dumps(payload), | |
| retain=True, | |
| ) | |
| except Exception as e: | |
| print("Error:", e) | |
| time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment