Living in a hard water area is a pain, so several years ago we had a water softener installed which has really helped. Unfortunately with it being tucked away in the garage, checking the salt level is very much hit and miss. It tends to be that I notice the signs of the water hardness increasing before I remember to go and check the level and top it up.
I’ve already got a large number of appliances, devices and sensors hooked up to my Home Automation system (https://www.home-assistant.io/) so adding something to alert me when the salt needs topping up seems like something I should have tackled long ago.
Having recently been experimenting with the Wemos D1 Mini (ESP8226 Dev board) with my Broadband Utilisation Display project I turned again to thinking how I could use a Wemos D1 Mini to solve this problem. However, this time rather than needing to write my own code for this, I don’t need to as other people have already solved this problem for me.
I’ve already flashed a number of Sonoff devices with the Tasmota firmware. Tasmota is an alternative firmware for ESP8266 which is what is used in the Sonoff (and many other) devices. It enables devices to be controlled and sensor information shared via MQTT (a lightweight messaging protocol often used in IOT projects) to my Home Assistant instance. This time instead of repurposing a Sonoff I took a Wemos D1 Mini and flashed it with Tasmota, bingo, the device creates a WiFi hotspot which you connect to provide it with your wireless network details and it’s ready to be configured.
Whilst on my workbench I decided that solving the salt level issue is one useful capability it would be easy to add some other sensors too, creating a multi-sensor for my garage and replacing some separate sensors. But we’ll come on to this later, first, the primary problem to solve is detecting when the salt level in the water softener is low.
Salt Level Measurement
Our Water Softener (Tapworks AD11) uses salt tablets that are poured into the body of the water softener, nightly it runs a cycle dissolving some of the salt, slowly lowering the level of salt.
Monitoring could be done by measuring the weight of the unit, but a simpler approach would be to measure the distance from the lid to the salt tablets in the body. For this measurement, there are a number of potential sensors which can be used.
|Pololu Carrier with Sharp GP2Y0A60SZLF|
(more info from Pololu)
|10-150cm||3V or 5V||£26.99|
|2-500cm||5V||£1.60 (£7.99 for 5)|
Sharp Proximity Sensors
All three sensors are designed for proximity/distance measurement. With the sharp sensors using infra-red light reflection and HC-SR04 ultrasonic sound pulses.
A key difference when considering integrating these sensors is that the Sharp IR sensors are analog, with the voltage output changing based on the distance to the object. The voltage change is most rapid at closer ranges, with Sharp making variations of the sensor tailored for different distances.
HC-SR04 Ultrasonic Distance Sensor
The HC-SR04 is a digital sensor which uses Time to represent the distance from the object, rather than voltage. The microcontroller sends a 10µS pulse to trigger the HC-SR04, which then emits a burst of 8 pulses at 40KHz. At the same time sending the Echo pin high for up to 38ms if nothing is detected in range. However, if the pulses are reflected back the pin is taken low sooner, the distance can be calculated based on the duration the pin was high.
Distance = Speed x Time
– Where speed is 340 m/s (the speed of sound), or 0.034cm/µS
– Time is the duration of the echo pulse received
– As the measure is the time to send and receive, the result of the above calculation needs to be divided by 2
For example, a pulse lasting 2000µS would give: (0.034 x 2000) / 2 = 34cm
The LastMinuteEngineers.com has a great post explaining more about this sensor.
There are typically only a couple of analog inputs on microcontrollers, so a digital signal is preferred, and analog signals can be more susceptible to noise, as I found with my PetDoor project. The HC-SR04 does require 2 GPIO pins compared to one, but I have plenty of spare GPIO available. Finally, at a price of < £2ea (assuming you don’t have one already from some other project), the ultrasonic sensor seems great value for money.
Adding More Sensors
As previously mentioned, once I had the distance measuring sensor working, I determined I could also replace a couple of other sensors I had currently running in the garage.
Hot Water Temperature
My garage houses the boiler and hot water storage tank in addition to the water softener. I’d recently hooked up a repurposed Sonoff TH16 running Tasmota to measure the hot water tank temperature via a DS18B20 waterproof temperature sensor pushed into the tank insulation, it was part of a project I’ve started to try and make my Nest Controller hot water system better (or even replace it) by understanding water usage and time required to heat water.
With the Wemos D1 mini running Tasmota for this project, I could reclaim the Sonoff and instead hook the temperature sensor up to the the Wemos D1 Mini and retain the same functionality.
Presence Detection for Light Control
Having given up nagging various members of the household to turn off the garage light, I installed a Zigbee PIR sensor so that after a period of no movement being detected, the light would be turned off automatically. This is a battery powered sensor and now with a mains powered Wemos D1 mini available it seems light a good time to try out combining PIR sensor into this project too.
Again during the PetDoor project, I experimented with some PIR sensors and didn’t find them all that reliable. The HC-SR501 would be candidate for this current project, but the way the sensor operates with gaps in motion detection time and some false positives, had me looking for something better. Around this time Andreas Spiess released a video which included a new Panasonic sensor the EKMC1603111 (datasheet) which is available from CPC Farnell for around £10. This is an expensive alternative to the HC-SR501, where you can pick up 10 of these for around £13, however so far my experience with this sensor has been great, it just works!
I don’t have a 3D printer to create a custom enclosure and having looked at a variety of project boxes, I determined I could make use of something from my local Screwfix.com store. For just £1.53 I could adapt this Schneider junction box, at just 65x65x45mm it was ideally suited to house the project.
Using a cone drill I made a large opening in the lid of the box for the PIR and used hot glue to secure it. I fitted some standoffs to mount the Wemos D1 Mini securely within the enclosure and fed the connections sensors and USB power cables out through one of the cable grommets.
A cable leads to the water softener where after drilling a small hole the HC-SR04 ultrasonic sensor is secured to the underside again using hot melt glue. The glue is mainly around the connector, so should the sensor fail, it’s simple to unplug and plug in another one.
Data pins are connected to the Wemos D1 Mini via header pins. In order to power the various sensors the 5V power feed is taken to a couple of Wago 221 connectors for ease of installation (and expansion).
Once Tasmota is flashed to the Wemos D1 Mini and connected up to the WiFi, it needs to be configured to define the different sensors connected to the GPIO.
First set the Module Type to Generic (18) then Save and restart.
HC-SR04 ultrasonic sensor:
- D7 – Trigger – SR04 Tri/TX (73)
- D8 – Echo – SR04 Ech/Rx (74)
DS18B20 waterproof temperature sensor:
- D4 – DS18x20 (4)
EKMC1603111 – PIR detector:
- D5 – Switch 1n (82)
- I’d connected a pulldown resistor to the sensor. The ‘n’ setting disables the internal pull-up resistor that is normally set.
- In the tasmota web console some additional commands are performed to correctly configured, per: https://tasmota.github.io/docs/PIR-Motion-Sensors/
SwitchMode1 1 SwitchTopic 0 Rule1 on Switch1#state=1 do publish stat/%topic%/PIR1 ON endon on Switch1#state=0 do Publish stat/%topic%/PIR1 OFF endon Rule1 1
Once configured Tasmota will show the distance and temperature sensor measurements, but not the PIR state.
Assuming you’ve configured MQTT these sensor values will be published periodically (per your telemetry period). When the PIR senses movement it sends a message immediately.
Different PIR models behave differently, the Panasonic sensor sends an ON event only when it senses the movement and quickly sends a PIR OFF message when movement isn’t detected. Other sensors may keep the value ON for longer periods. I prefer a fast-acting sensor so I can determine the behaviour I want via software.
The configuration for this sensor in Home Assistant can be seen via the source in my GitHub repository.
The PIR is defined as an MQTT
binary_sensor with the temperature and distance sensors as MQTT
sensor the values of which are extracted via a JSON template.
As I only want to know when there hasn’t been any movement for 15 minutes, I use an
input_select which is set to
motion each time the PIR sends an ON event. With an automation script which changes it back to idle if there hasn’t been activity for 15 mins.
If there hasn’t been activity for 15 mins and the garage light is on, it will turn it off and send me a notification.
The salt level is monitored by Home Assistant and will send a notification when the distance increases to the point it needs refilling. This may need some fine tuning, but currently set to 42cm. I’m also logging this data into an influxDB and plotting via Grafana so that I can get a better feel for the rate of usage so I can predict the usage rate.
|Wemos D1 mini microcontroller (v3.1)||£5.99||eBay|
|HC-SR04 ultrasonic distance sensor||£1.60 (£7.99 – 5 pack)||Amazon|
|Panasonic EKMC1603111 PIR senor||£9.48||CPC Farnell|
|DS18B20 waterproof temperature sensor||£2.12 (£10.59 – 5 pack)||Amazon|
|Schneider Electric junction box||£1.53||Screwfix.com|
|USB power supply||£5 (£9.99 – 2 pack)||Amazon|
|Misc cables and connectors|
Note: Amazon links included are affiliate links.