Internet of Things – Control Sensors

Today I will talk about how you can publish sensor data depending on a threshold and control a sensor using an MQTT broker. I will publish information to the MQTT broker about the light depending on a threshold, and also show how the threshold can be changed using a subscription on the MQTT broker.

In my previous video, I was publishing light and temperature data to an MQTT broker at a fixed time interval (every 5 seconds). However, in most Internet of Things solutions it’s more interesting to know when a threshold value has been passed or when an interval has been exceeded. A real-world example is a wine cellar, which need to keep the wine in a certain temperature interval (usually 7-18°C or 45-64°F), and the important thing is not to know the exact temperature, but if the temperature goes outside of that interval.

In this lab I will show you how to implement the logic of a threshold value, and also how that value could be changed using an MQTT broker. This means that the behavior of the sensor can be changed remotely, and this is a very powerful feature of any Internet of Things solution. I will use a light sensor to determine whether it’s dark or light, and then act on that. When it’s dark a LED will be turned on, and when it’s light the LED will be turned off. As you can see in my video “Internet of Things – Like Lamp”, the LED could be replaced with a normal lamp. A real-world example would be a welcoming light in front of the house or office that is turned on when it’s dark (at night or due to bad weather).

Here you can see the needed hardware, and in addition to the Arduino, you need a light sensor, or photocell, a LED, a 220 ohm resistor, and to connect everything, you need a breadboard and five jumper cables that have a (male) pin on each end.

To setup the hardware, you start by disconnecting everything from its power source, as usual. Then you connect the sensor to the breadboard, and the Arduino 5V to the power (plus) on the sensor. Connect the Arduino ground (GND) to the ground (minus) on the breadboard, and then connect the breadboard ground to the sensor ground. Connect the sensor signal (S) to the first analog pin (A0), and connect the Arduino pin 13 to the positive (long) end of the LED. Connect the negative (short) end of the LED to the resistor that in turn connects to the breadboard ground.

Here is the Arduino code (sketch)…

…and after the initial includes, constants for the pins are defined (on line 4). The MQTT client object is defined and created (on lines 5-6), followed by the declaration of a number of variables (on line 7-10). In the setup, we set the LED pin to output (on line 14), and initialize the bridge between the microcontroller and the microprocessor (on line 15). Then we connect to the MQTT broker and set up a subscription (on line 16). The eternal loop starts by checking if a one second interval has passed, and if so, read the value from the light sensor (on line 24), check if the value has passed the threshold (on line 25), and if so, publish the new status to the MQTT broker (on lines 27-31). In the callback function, which is called each time a new value is published on the topic that was subscribed to, the message (payload) is read (on line 39-41) and parsed (on line 42), and the new threshold is updated (on line 43).

I’m assuming that you have an MQTT broker running (see my previous video called “Internet of Things – Why You Need MQTT” to see how the server can be set up and how to start the MQTT broker), and to see the published data, connect to the server (replace AwsKeyPar.pem with your key file, and 0.0.0.0 with the IP address of your server)…

ssh -i AwsKeyPair.pem ubuntu@0.0.0.0

…and then run the command:

mosquitto_sub -d -t lightsensor/value

This starts a subscription to the topic “lightsensor/value” where you can see the published sensor data.

To change the threshold, open a new terminal window, connect to the server (using the same ssh command above), and run the command…

mosquitto_pub -d -t lightsensor/control -m "{\"value\":300}"

…that will publish a message on the MQTT broker that will be picked up by the Arduino, which will use this value as the new threshold.

That is how you can publish sensor data depending on a threshold and also control the sensor by updating the threshold using an MQTT broker.