Skip to content

Add MQTT homeassistant sensors for air quality#13

Open
glenrobertson wants to merge 29 commits intogoat-hill:masterfrom
glenrobertson:mqtt-homeassistant-sensors
Open

Add MQTT homeassistant sensors for air quality#13
glenrobertson wants to merge 29 commits intogoat-hill:masterfrom
glenrobertson:mqtt-homeassistant-sensors

Conversation

@glenrobertson
Copy link
Contributor

@glenrobertson glenrobertson commented Nov 28, 2024

Adds support for homeassistant sensor discovery and state reporting via MQTT

  • Add NVS variable for MQTT url (e.g. mqtt://user:password@mqtt.example.com)
  • Add MQTT lib which exposes a method for sending sensor data to a given MQTT url
  • Add task to call MQTT method when there is an MQTT url present

Notes:

  • I haven't yet tested the nvs variable setting - I tested with a hardcoded URL before adding this.
  • The NVS code changes are based on the weather path NVS code.

Relevant home assistant docs for MQTT based sensor discovery:

Testing:

Blocking things to test:

  • New web UI with old firmware (for people who haven't upgraded)
  • New web UI with new firmware
  • No change in behavior if MQTT server not set
  • No change in behavior if MQTT server removed (Bug: Does not show MQTT url on connect)
  • Stable for a few days with home assistant uploads

Non-blocking things to figure out:

  • Any significant temperature difference if MQTT server set (wifi does heat up device)
  • Packaging new release
  • Web UI update to indicate firmware release

Update eink_display.c

wip2

Update mqtt.c

Update mqtt.c

Update mqtt.c

rename

move to task

Update mqtt.c

Add nvs property for MQTT url
@babldev
Copy link
Member

babldev commented Nov 29, 2024

Thanks! Did a quick first pass just looking at code. I'll have to do a closer look running on device and testing it out. Specifically memory usage, and backwards compatibility with the web app.

@babldev
Copy link
Member

babldev commented Nov 30, 2024

Blocking things to test: (I can help with this too)

  • New web UI with old firmware (for people who haven't upgraded)
  • New web UI with new firmware
  • No change in behavior if MQTT server not set
  • No change in behavior if MQTT server removed (Bug: Does not show MQTT url on connect)
  • Stable for a few days with home assistant uploads

Non-blocking things to figure out:

  • Any significant temperature difference if MQTT server set (wifi does heat up device)
  • Packaging new release
  • Web UI update to indicate firmware release

@babldev
Copy link
Member

babldev commented Nov 30, 2024

Keeping some notes here. If you connect the new web UI to an old bitclock FW version, you will see:

  • Failed to read characteristic: 2ab48c15-934d-11ec-b90a-802748c27b9e: NotFoundError: No Characteristics matching UUID 2ab48c15-934d-11ec-b90a-802748c27b9e found in Service with UUID ec2f3aa0-4ed8-71bd-f147-b2bc37195232.

If you try to set the MQTT url:
Uncaught (in promise) NotFoundError: No Characteristics matching UUID 2ab48c15-934d-11ec-b90a-802748c27b9e found in Service with UUID ec2f3aa0-4ed8-71bd-f147-b2bc37195232.

Other changes seem to work. So I think this is fine but would be nice to branch instead of error if it detects an old firmware version. But I'd need to reveal that somehow.

@babldev
Copy link
Member

babldev commented Nov 30, 2024

If you update to new FW, and connect with new web UI, it initially doesn't work at all
Screenshot 2024-11-30 at 11 57 11 AM

https://github.com/goat-hill/bitclock/tree/master/bitclock-fw#bluetooth-gatt-attributes

Once you "Forget this device" and re-pair there are no errors. But something to think about communicating if people update.

@babldev
Copy link
Member

babldev commented Dec 1, 2024

Annoyingly I think we're running out of memory when parsing the weather API now with cJSON. I bet we were close to the limit and this MQTT stuff pushed us over :(

I (7958) weather_api: HTTP GET Status = 200, content_length = 12906
E (7968) weather_api: Error on JSON parse

I'll have to poke around for ways to reduce memory usage.

@babldev
Copy link
Member

babldev commented Dec 1, 2024

Had to throw a ton of changes in here, mostly because the weather API is extremely expensive to the heap right now. It uses about 30kB via the cJSON library to parse the 17kB giant response from api.weather.gov.

I made a bunch of other tweaks in my testing that leaves about 3kB worst case of extra headroom. Not great because a small weather.api.gov change could push things over the limit.

Long term fix would be to use a streaming parser of JSON instead of a one-shot one. Those are harder to implement but way less memory intensive. https://components.espressif.com/components/espressif/jsmn instead of cJSON

@babldev
Copy link
Member

babldev commented Dec 11, 2025

I merged in master which should hopefully give this PR enough memory headroom since it was improved there.

@glenrobertson glenrobertson force-pushed the mqtt-homeassistant-sensors branch from 238e76a to da0c7b6 Compare December 16, 2025 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants