🛠 Connecting GPIO Sensors to KNOT HL9
Objectifs
This tutorial aims to interact with the GPIO (General Purpose Input/Output) pins of the MikroTik KNOT Gateway HL9. Although this gateway is optimized for wireless connectivity, it offers local resources for simple data processing. We will configure a pin as an input to detect a binary signal (such as a dry contact or digital sensor) and transmit this state via MQTT. This allows integrating wired peripherals into a wireless IoT network.
Matériel nécessaire
- A powered MikroTik KNOT Gateway HL9 (PoE or DC).
- A computer with SSH or WinBox access for configuration.
- A simple digital sensor (e.g., push button, PIR motion detector with digital output) or pull-up/down resistors depending on the electrical schematic.
- Connection wires (Dupont).
- MQTT account (public or local broker) for verification.
Étapes
Connect to the gateway via SSH (port 22) or WinBox. Ensure RouterOS v7 is up to date for better GPIO support.
- Console Access
In the terminal, type the following command to list available GPIO interfaces: ``bash /interface gpio print ` Note the interface name (e.g., gpio1`). On KNOT models, pins are often exposed via specific connectors or accessible pads. Check the technical documentation for the exact pinout corresponding to your enclosure.
- Identify GPIO Pins
Set the chosen pin as a digital input with an internal pull-up resistor if necessary: ``bash /interface gpio set gpio1 type=input pull-up=yes `` This configuration allows reading a logical state (0 or 1) without electrical floating.
- Configure Pin as Input
To test manual reading, use the print command. The high or low state indicates if the circuit is open or closed: ``bash /interface gpio print detail `` Press your button or trigger the sensor to observe the state change.
- Read Pin State
Create a script that reads the state and publishes to MQTT. Modify the connection parameters to your broker: ``bash /system script add name=send_gpio_state source={ :local state [/interface gpio get gpio1 value] /tool mqtt-client publish topic="knot/hl9/gpio1" message=\"$state\" } ` Add this script to a scheduler for periodic execution (e.g., every 10 seconds): `bash /system scheduler add name=pub_gpio interval=10s start-time=startup on-event=send_gpio_state ``
- Automate Transmission via Script
Vérification
Connect to your MQTT client (e.g., MQTT.fx or mosquitto_sub) and subscribe to the topic knot/hl9/gpio1. Change the physical state of the sensor (press the button). You should see the message change between "true" (or "high") and "false" (or "low") in the data stream. If nothing appears, check the MQTT client configuration (/tool mqtt-client print) and network connectivity (Wi-Fi HaLow or LTE).
Pour aller plus loin
For analog sensors (temperature, humidity), the KNOT HL9 often requires external modules connected via USB or serial interfaces, as native GPIOs are digital. Explore using RouterOS scripts to trigger local actions (LED, relay) or integrate data into more complex HTTP streams for real-time supervision.
Other tutorials for this board
Code examples for this board
gnss-position-tracking.pyPython-Bashble-advertising-sensor.inoRouterOS-Scriptlte-failover-monitor.pyPython-Bashnand-storage-cleanup.pyPython-Bashpoe-power-status.inoRouterOS-Scriptrouteros-mqtt-telemetry.inoRouterOS Script