- Gas counter via KY-021 Mini reed magnet
- KY-021 circuit
- Test send and receive data via MQTT message service
- Collecting data via mosquitto
- Collect initial data
Near Stuttgart, Germany, the gas box from the supplier is a GT4 G4 from 2005. It says that one magnetic pulse equals: 1 imp=0,01 m3. The red box is marking a place for putting a magnet for reading the pulses.
I thought it could be a funny project to count the gas, and see gas consumption:
- if usage happens during showering in the morning
- or usage happens during heating in the night / evening
Plan:
- Find a magnetic sensor that picks up the magnetic pulse
- Collect the pulse and send data somewhere for calculation or viewing
The example witill be an Raspberry Pi 4B, with the Explorer Hat Pro from Pimoroni, which will be used for initial circuit exploration on the mini breadboard.
The Explorer Hat Pro is an older, now discontinued, product. I initially tried Raspberry Pi OS 64-bit Bookworm, but I got problems with python installation and sound card for the examples/drums.py. After I downgraded to Legacy Bullseye, 64-bit, Lite, then installation and examples worked as intended.
After verification, the story is continued with a battery powered NodeMCU.
I conveniently found someone selling 3d-printed plastic holder with KY-021 Mini reed magnet for 16 euro on ebay.de. The internal circuit is depicted here
In the default configuration with sensor on the left pin, 5V on the middle pin and ground on the right pin, with internal 10k R1 resistor to lower current. With no magnet, the reed contact is open, we should measure a HIGH/1 voltage signal. With a magnet pulse, the contact closes, voltage is grounded and a LOW/0 voltage is measured.
If we use a Rasperry with a PullUp/PullDown resistor, we would normally initialize the GPIO input pin with something like GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
, to make sure it's initially HIGH and not in a floating state. If the Pin is in a floating stage, it is susceptible to random electromagnetic radiation or static from you, from any devices near or far and from the environment. Using this command, we use built-in pull-up resistors which can be enabled in software.
BUT, when we use the Explorer Hat Pro, Pi's GPIO pin is after the buffer, input signals will not be pulled up or down by enabling the Pi's onboard pull resistors. So, we need to build a PullUp resistor ourselves.
And we test the circuit with the code up-input-event.py
With a PullDown transistor, we should initially measure a LOW/0. With a magnet pulse, the contact closes and a HIGHT/1 is measured.
To achieve this, switch 5V/GND on the KY-021 pins, make a 10k R between Input 1 and GND (instead of 5V) and switch the on/off of led diode in code.
And we test the circuit with the code down-input-event.py
We will install mosquitto as a broker on a device/computer that is always on. In this example, a GL.iNet GL-A1300 (Slate Plus) with OpenWrt 23.05.
# Install
opkg update
opkg install mosquitto-ssl
opkg install mosquitto-client-ssl libmosquitto-ssl
# Optional, if configuration via click in Luci is preferred.
# opkg install luci-app-mosquitto
# Make sure mosquitto service can read/write
chown root:mosquitto /etc/mosquitto
chmod g+w /etc/mosquitto/
# Create mosquitto user: gasuser
mosquitto_passwd -c /etc/mosquitto/passwd gasuser
# Give passwword: helloworld
# Change owner
chown mosquitto:mosquitto /etc/mosquitto/passwd
# Edit configuration file
nano /etc/mosquitto/mosquitto.conf
# Set values
allow_anonymous false
password_file /etc/mosquitto/passwd
listener 1883 0.0.0.0
persistence true
persistence_file mosquitto.db
persistence_location /etc/mosquitto
# Check
ls -la /etc/mosquitto/
grep -v '^#' /etc/mosquitto/mosquitto.conf | grep -v -e '^$'
mosquitto --verbose --config-file /etc/mosquitto/mosquitto.conf
# If no errors then break.
# Enable, Restart
/etc/init.d/mosquitto enable
/etc/init.d/mosquitto restart
Since, the Access Point is named slateplus
in luci system->system->hostname
we can use slateplus.lan
as hostname.
Login 2x with ssh to the OpenWrt router.
# Try from 1 terminal and listen
mosquitto_sub -h slateplus.lan -u "gasuser" -P "helloworld" -t test
# Publish from other terminal
mosquitto_pub -h slateplus.lan -u "gasuser" -P "helloworld" -t test -m "Testing"
Install mosquitto client on Raspberry
# Bash client
sudo apt-get update
sudo apt-get install mosquitto-clients
# Python module
sudo pip install paho-mqtt
Test publish with --retain. When a client is subscribing, it always get retained messages-
# First Publish from other terminal
mosquitto_pub -h slateplus.lan -u "gasuser" -P "helloworld" -r -t test -m "Testing Retain"
# Try from 1 terminal and see if retained message is there
mosquitto_sub -h slateplus.lan -u "gasuser" -P "helloworld" -t test
With python code paho-mqtt.py, try to publish and watch subscription terminal.
On OpenWrt router, create user and restart server
# Create user for reading
mosquitto_passwd -b /etc/mosquitto/passwd gasread Hello
# Pickup new password
/etc/init.d/mosquitto restart
Manuel for mosquitto_sub
and mqtt
. Listen with QoS and enable enable persistent client mode. With -v, topic is added prepended to message lines.
On OpenWrt router, listen
mosquitto_sub -h slateplus.lan -u "gasread" -P "Hello" -v -t "sensors/gas/#" --qos 1 --id "gasread" --disable-clean-session
On raspberry, run script gas-sensor.py.
Try to keep making magnetic pulses, while breaking and connecting again mosquitto_sub
and see all messages is received.
We will make a script that starts on boot and save to file. The boot script gas_counter
which is send to background with &
.
# Make executable
chmod +x /etc/init.d/gas_counter
# Enable and start
/etc/init.d/gas_counter enable
# Check symlink creation
ls -la /etc/rc.d/*gas_counter
# Check it's enabled.
/etc/init.d/gas_counter enabled && echo on
# Please note, that a "restart" only will do a stop() and not a start() again. The script itself is also killed.
/etc/init.d/gas_counter start
# See log
cat /root/gas_data.log
On raspberry, run script gas-sensor.py.
Make some data with a small magnet.
On OpenWrt, follow data with
tail -f /root/gas_data.csv
On OpenWrt:
/etc/init.d/gas_counter stop
rm /root/gas_data.*
/etc/init.d/gas_counter start
On raspberry:
sudo apt-get update
sudo apt-get install screen
screen
# To detach this terminal session, press "CTRL + A", release, and then press "D"
# Attach
screen -list
screen -r 1950.pts-1.raspberrypi
# Start python
python gas-counter-magnetic/raspberry/gas-sensor.py
I needed to re-adjust the KY-021 board a few millimetersv up, so the reed contact was at the very top line of the blue plastic.
When this was working, the initial current consumption was: 3345.71
Get and install miniforge.
Install Visual Studio Code and install extensions python, Jupyter.
Create environment with miniconda and use analyse.ipynb
conda env create --file=environment.yml
# To update from file
conda activate gascounter
conda env update --name gascounter --file environment.yml --prune
conda activate gascounter
There is many pulses recorded at 6 and 7 in the morning. This makes sense, as the Gasherd is programmed to a "morning" program with warm water, starting at 6. And any showers around 8.30 after the morning run is keeping the gasherd busy the following hour.
Making a linear regression between observed consumption versus recorded number of pulses give a perfect regression, with fitted parameters:
- Slope consumption per pulse is: 0.010
- Initial consumption is: 3345.706