Skip to content

Swap AIO for HA Autodiscovery - #2

Open
andycb wants to merge 10 commits into
mainfrom
feature/mqtt-autodiscovery-ha
Open

Swap AIO for HA Autodiscovery#2
andycb wants to merge 10 commits into
mainfrom
feature/mqtt-autodiscovery-ha

Conversation

@andycb

@andycb andycb commented Jul 26, 2026

Copy link
Copy Markdown
Owner

No description provided.

Andy added 10 commits July 26, 2026 09:06
- Replace Adafruit IO with direct Home Assistant MQTT connection
- Implement MQTT auto-discovery for 3 main sensors + 3 diagnostic sensors
- Add graceful offline publish queue (50-entry circular buffer)
- Add exponential backoff reconnection with jitter (30s -> 10min)
- Add WiFi recovery monitoring and continuous reconnection
- Add diagnostic sensor publishing (WiFi RSSI, MQTT status, uptime)
- Create MqttHandler class encapsulating all resilience logic
- Add config.h template with device identity and connection settings
- Update .gitignore to exclude config.h credentials from version control

Device info:
- Name: Smart Litter Tray
- Version: 2025.07.26
- Manufacturer: andycb

Resilience improvements:
- Survives prolonged WiFi/MQTT outages without hard reset
- Queues sensor data during disconnection
- Implements exponential backoff to prevent network flooding
- Designed for multi-month uptime without intervention
- Update software version to 2026.07.26
- Add hardware version field 1.0 to device identification
- Add 'Data Age' sensor that tracks queue delay for each reading
  - For immediate publishes: age = 0 seconds
  - For queued publishes: age = time_in_queue_seconds
- Automatically recalculate age when draining offline queue
- Include hardware version in Home Assistant device discovery payload
- New Home Assistant sensor: sensor.smart_litter_tray_data_age

This allows monitoring data freshness and queue performance.
Changed from additive percentage model to multiplicative multiplier:
- Old: delay + (delay * jitterPercent / 100) where jitterPercent=-10 to 10
- New: (delay * jitterMultiplier) / 100 where jitterMultiplier=90 to 110

This produces proper ±10% jitter (0.9x to 1.1x) instead of additive offset.
Diagnostics (WiFi RSSI, MQTT status, uptime) are now ephemeral:
- Only published when MQTT is actively connected
- Not queued to offline buffer
- Changed publishDiagnostics() to use direct mqttClient.publish() instead of publishTopic()
- Added early return if MQTT disconnected

This preserves queue space for actual sensor data (cat weight, poop weight, duration, age).
- Renamed enum values to avoid PubSubClient library macro conflicts:
  - MQTT_DISCONNECTED → MQTT_STATE_DISCONNECTED
  - MQTT_CONNECTING → MQTT_STATE_CONNECTING
  - MQTT_CONNECTED → MQTT_STATE_CONNECTED
  - MQTT_FAILED → MQTT_STATE_FAILED
  - WIFI_DISCONNECTED → WIFI_STATE_DISCONNECTED
  - WIFI_CONNECTING → WIFI_STATE_CONNECTING
  - WIFI_CONNECTED → WIFI_STATE_CONNECTED

- Added forward declaration of OnNewReading() in SmartLitterBoxSketch.ino
  to resolve undefined reference error

This fixes all PubSubClient macro name collision issues.
LitterScale has const member variables, which makes the default move
assignment operator implicitly deleted. Removed the explicit assignment
'this->m_litterScale = LitterScale()' and rely on default initialization
of the member variable instead.

This resolves the compilation error about deleted move assignment operator.
Added extensive debug output to help diagnose connection issues:

MQTT connection debug:
- Print server IP, port, client ID, username, password when attempting
- Add human-readable error messages for all MQTT state codes
- Specifically identify MQTT_CONNECT_FAILED (-2) as bad credentials/rejected

WiFi connection debug:
- Print current WiFi status code when attempting reconnection
- Help identify WiFi status issues

This helps identify whether the issue is:
1. Wrong MQTT credentials
2. MQTT broker not accessible on that port
3. WiFi connection issues
4. Client ID being rejected by broker
The issue was using WiFiClientSecure with setInsecure() for plain MQTT (port 1883).
WiFiClientSecure can interfere with plain MQTT connections.

Now automatically selects the correct client type based on port:
- Port 1883 (plain MQTT): Uses WiFiClient (no TLS overhead)
- Port 8883+ (MQTT with TLS): Uses WiFiClientSecure with setInsecure()

This resolves MQTT_CONNECT_FAILED (-2) errors on plain MQTT brokers.
Print the exact discovery topic and JSON payload being sent to Home Assistant.
This helps verify:
1. Topic structure is correct for MQTT discovery
2. JSON payload is properly formatted
3. Publish operation succeeds with retain flag

This will help diagnose why entities don't appear in Home Assistant.
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.

1 participant