Simple Controllable MQTT Sensor Simulator in Python

light-bulbs-sensorsThe 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.



IOT Sensors

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.

Topics used

  • 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.

The MQTT lens client is ideal for this purpose and so is the MQTT dashboard client (Android APP) as they let you see the messages published by the sender as well as sending messages to the sensor.

I use the mosqquitto_pub tool in the examples below.

Example Usage

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

Demo Shots

In the screen shot below I ran the sensor and controlled it using the MQTT dashboard App on my Android tablet.

sensor-control-on

sensor-control-off

If you find it useful you are free to use the code in your own projects, and can download the code here.

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:

Save

Save

Save

Please rate? And use Comments to let me know more
[Total: 2    Average: 3/5]

6 comments

  1. Though your scripts are very good, I do have some suggestions for your tutorials (you asked).
    I think in its current form the examples may leave the beginner with a lot of questions.
    If I look at the “simple controllable mqtt sensor” once one goes to the download to look at the discussd python program, there are in fact two programs. Ofcourse it isnt so hard to find out which the proper one is but tehn where does the other one come in?
    In your tutorial you write that the command line can take several parameters, but you do not really make clear how.
    One can see it is -b -p -t -v -h -c -d -n client ID -i loop interval -s -u username -p Password
    but, it is not fully clear on how to supply those.
    Take for instance the broker, shousl that be the full IP, between quotes or not, or should it be the index in the array that is in the program. should the topic be between quotes or not? Ditto for say password and username.
    How to set chatty mode? or is that the ‘verbose’ output?
    Anyway, I think you get my point.
    I think just giving an actual command line example would greatly improve the tutorial, something along the lines of:
    python simple-sensor.py -b 1 -p 1883 -t “home/sensor” -v yes
    etcetera.
    Please do not get my remark wrong, your programs are great and useful, but maybe a bit too much is expected from the beginner

    1. Tks
      A very helpful comment. It is always difficult to know how well the tutorial described what it was supposed to without good feedback.
      I will take it onboard- many tks

  2. I have an additional question:
    The program simple-sensor is using a module “mqtt_functions”
    I get an error on that. Trying to find anything of the existence of such a module (so I can install it) renders nothing, no info at all.
    Where can I find that module?

    1. The zip file should include two scripts mqtt-functions.py and simple-sensor.py.Place both files in the same directory and it should be ok. Please let me know if you have any problems.
      I have updated it so you may want to download the newer version. I’ve also changed the tutorial to reflect your earlier comments. Tks again

  3. Still some remarks about something unclear:
    you talk about sensors and if used as a door contact with open/closed, it sure is a sensor, but in switching a lamp with on/off, it is an actuator.
    So do I understand correctly this can be used as input as well as output?
    and…… which I/O pin in fact is switched? Or did I completely misunderstand?

    1. It is a simulator. In the real world you would have a light switch here you use the mosquitto_pub tool to send the command. The sensor is the light that turns on or off.

Leave a Reply

Your email address will not be published. Required fields are marked *