The aim of this project is to create a very simple two state sensor, that can be controlled externally using MQTT.
The sensor could be used to simulate real world objects like lights, doors etc that have two states on or off, open or closed etc in IOT projects.
Currently real world objects like light bulbs are controlled by a switch.
There is no feedback from the light bulb, that it is on or off, other than the obvious. However someone in another room wouldn’t know the state of the light bulb .
Because IOT light bulbs will be controllable from anywhere then the state of the bulb should also be available from anywhere.
MQTT is ideal in this scenario as it allows the sensor to be controlled from multiple locations/clients and each one will be aware of the current state.
MQTT Sensor Overview
The sensor subscribes to a control topic and sits in a loop publishing it’s current state, and waiting for a command.
The commands accepted are:
on and off which turn the light on and off. or
Open and Closed if functioning as door sensor
All other commands are ignored and commands are not case sensitive.
The sensor changes its state according to the command, and publishes its current state.
The sensor can operate in:
- Chatty mode were it publishes data at regular intervals.
- Non Chatty were it publishes data only when its status has changed.
It will also publish data on a keep alive interval which is set to be every 5 minutes.
- topic prefix/sensor-name – used to publish sensor state
- topic prefix/sensor-name/control/ -used to change state
- topic prefix/connected/sensor-name/ – Used to indicated connection status 0=disconnected and 1=connected
The sensor can be called from the command line . Common arguments to pass are:
- sensor name
- topic prefix (optional)
- broker address
- broker port
- chatty or not chatty
- sensor type light or door
- publish interval default =10 seconds in chatty mode
Note: If started from the IDE it will create random sensor names.
The sensor publishes with the retain message flag set to True.
Controlling the Sensor
You can use any MQTT client that can publish messages.
I use the mosqquitto_pub tool in the examples below.
Note: you may not need to use the python prefix or may need to use python3 simple-sensor.py (Linux)
Use defaults with ip address
python simple-sensor -b 192.168.1.157
Use defaults with name
python simple-sensor -b ws4
Use Chatty mode -v option don’t need yes or no:
python simple-sensor.py -b 192.168.1.157 -v
Publish every 20 seconds -i option
python simple-sensor.py -b 192.168.1.157 -v -i 20
Set client name to testclient
python simple-sensor.py -b 192.168.1.157 -n testclient
To use as a door sensor open/closed use -s option
python simple-sensor.py -b 192.168.1.157 -n testclient -s
Controlling the sensor using mosquitto_pub tool.
If using as a light sensor.
mosquitto_pub -h 192.168.1.157 sensors/testclient/control -m on
If using as a door sensor
mosquitto_pub -h 192.168.1.157 sensors/testclient/control -m open
In the screen shot below I ran the sensor and controlled it using the MQTT dashboard App on my Android tablet.
If you find it useful you are free to use the code in your own projects.
The zip package contains two files mqtt_functions.py and simple-sensor.py.
The mqtt_functions.py contains helper functions that I use in some of my scripts.
Place both files in the same folder
Comments and FeedBack
Was there enough detail in this tutorial for you to follow?
Please help me improve these tutorials by leaving your comments,rating them,asking questions.
Related Tutorials and Resources:
- Introduction the Paho Python client
- Creating an MQTT Dashboard Using Thingsboard
- IOT and MQTT Dashboards and Platforms-Working Notes
- MQTT sensor traffic observations
- Simple Python MQTT data Logger