In order to make this work in Hubitat and use my RPi Mosquito MQTT Broker I am using the Alpha MQTT code posted just recently and also some inspiration from @kevin. I wanted to use a NodeMCU for the Arduino but unfortunately the HC-SR04 requires 5v to operate and the NodeMCU I have only has pinouts for 3.3v. So now using a Wemo D1.
I created an Arduino sketch that establishes a connection to WiFi, connects to your MQTT Broker, sends the current distance to salt level and also accepts incoming delay time variances from Hubitat MQTT.
Last piece was using @bangali WATO application. Hubitat doesn't support a distance driver perse' so using state attributes that are dynamically created by the MQTT Generic Driver allows me to take action with a virtual switch to then send a notification.
Setup is fairly simple and documentation is in the Arduino sketch for what pins you need to use.
Setup - Generic MQTT Driver
- MQTT Broker Address: IP or DNS name of MQTT Broker (I use Mosquito on a RPi)
- MQTT Username: your broker username account
- MQTT Password: your broker password for above username account
- Topic to Subscribe: this is your root/device topic. Example: wardhome/salt-tank/#
- Topic to Publish: this is the value you want to publish for a MQTT device to subscribe to. Example: wardhome/salt-tank/delay
- Wemo D1 pinouts: VCC-5V, Trig-D6, Echo-D7, Grnd-Grnd
- Modify Basic Setup section. Note: distance_topic and subscribe_topic are key to matching. Example: distance_topic - wardhome/salt-tank/distance subscribe_topic wardhome/salt-tank/delay
- Upload sketch
- Runs initially every hour unless sent a message through HE Generic MQTT Driver
Hope this is fun for others!