
Adding the 4 channel relay board ks0212 to the MQTT universe
We just hacked a trotec dehumidifier for Herwigs Observatory. The idea was to additionally activate the dehumidifier when the difference between outside and inside humidity is above 10%. Normally there is a fan taking care of it but sometimes the differents gets to high. As there is already a raspberry pi running in the observatory for the weatherstation and the flightradar24 installation we just added the 4 channel relay board ks0212 from keyestudio. Not touching the 220V part we directly used the relay to “press” the TTL switch on the board for 0.5 seconds to turn on and off the dehumidifier. Here are the code snipped we used for this. The control is completely handled via MQTT.
Installing necessary programs and libraries
sudo apt install python python-pip python-dev sudo pip install wiringpi paho-mqtt
For the sake of simplicity we used python and the GPIO library wiringpi. Therefore we first install the python development parts and them the python libraries for wiringpi and MQTT. As this is a dedicated hardware installation we don’t use virtualenv and directly install the library as root system wide.
The python program
import time import wiringpi import paho.mqtt.client as mqtt def setup(): wiringpi.wiringPiSetup() wiringpi.pinMode(3, 1) wiringpi.pinMode(7, 1) wiringpi.pinMode(22, 1) wiringpi.pinMode(25, 1) def short(pin): switch_on(pin) time.sleep(0.5) switch_off(pin) def switch_on(pin): wiringpi.digitalWrite(pin, 1) def switch_off(pin): wiringpi.digitalWrite(pin, 0) def on_connect(self, client, userdata, rc): mqclient.subscribe("sternwarte/relay/#") def on_message(client, userdata, msg): m = msg.topic.split("/") pin = 0 if m[-1] == "j3": pin = 3 if m[-1] == "j2": pin = 7 if m[-1] == "j4": pin = 22 if m[-1] == "j5": pin = 25 if pin != 0: if msg.payload == "on": switch_on(pin) if msg.payload == "off": switch_off(pin) if msg.payload == "press": short(pin) if __name__ == "__main__": setup() mqclient = mqtt.Client(clean_session=True) mqclient.connect("192.168.2.5", 1883, 60) mqclient.on_connect = on_connect mqclient.on_message = on_message mqclient.loop_forever()
Again, a very simple python script, basically attaching to a (you need to change the code, there is no config) mqtt server and subscribes itself to a certain topic. Then it waits for messages and cuts off the last part of the topic to identify the relay. The naming convention is based on the relay name printed on the ks0212 pcb. As payload you can send “on“, “off” and “press“. “press” switches the relay on for half a second in order to simulate a button press as we need it for our dehumidifier.
Adding a systemd service
In order to keep the wantabe daemon up and running and also start it automatically at system start we add this service configuration file in “/lib/systemd/system/relayboard.service“:
#cat /lib/systemd/system/relayboard.service [Unit] Description=ks0212 Relay Board After=multi-user.target [Service] Type=simple ExecStart=/usr/bin/python /home/pi/ks0212.py Restart=on-abort [Install] WantedBy=multi-user.target
Activating the service
The following lines activate the service:
sudo chmod 644 /lib/systemd/system/relayboard.service sudo systemctl daemon-reload sudo systemctl enable relayboard.service sudo systemctl start relayboard.service
Checking the status can be done with:
sudo systemctl status relayboard.service
ks0212 Pinout
If you want to do some hacking with the ks0212 relay board on your own here is the pin mapping table. I used the very cool side https://pinout.xyz/pinout/wiringpi for getting the numbers:
Relay | WiringPi | BCM | GPIO | Link |
J2 | 7 | 4 | 7 | https://pinout.xyz/pinout/pin7_gpio4 |
J3 | 3 | 22 | 15 | https://pinout.xyz/pinout/pin15_gpio22 |
J4 | 22 | 6 | 31 | https://pinout.xyz/pinout/pin31_gpio6 |
J5 | 25 | 26 | 37 | https://pinout.xyz/pinout/pin37_gpio26 |
One Reply to “Adding a ks0212 relay board to the mqtt universe”