IoT Hello World :)

Every IoT project, big or small, consists of 3 main components namely the sensors, the actuators and the communication medium by which the things talk to each other also known as the control center.

Image for post
Image for post

In this article, I would like to demonstrate how we could simulate a sensor action, a control center and an actuation based on the sensor condition. I am therefore calling it a “Hello World” as this is what we try to do when we learn something new :)

Before we move any further, refer to my IoT device setup article to have the device ready for this tutorial.

Firstly, we would set up the control center, in this case a managed MQTT broker. Message Queuing Telemetry Transport (MQTT) is a most widely used IoT protocol due to its inherent properties which make it most ideal for IoT applications. Go to CloudMQTT website and create an account. We would use the free plan in this tutorial. Since MQTT is open source, we even could have a local MQTT broker setup, but let’s use something out there. In my future series, I will show you how we can set up a local MQTT broker.

Go to the details of the instance and take a note of the server, port and the credentials which we would need when we create the application to send and receive simulated sensor data. If you see the left side panel, there are a bunch of other options like creating more users, attaching self-signed certificates, which is out of scope of this tutorial but are interesting things for exploration. Might be a good topic for one of my future articles.

Image for post
Image for post
MQTT Broker Connection Parameters

Now that the control center is setup, let's dive into in creating the simulated sensor which can send data to our MQTT Broker.

Simulated Sensor and Actuator Setup

Boot the Raspberry Pi, and it should automatically connect itself with the Wi-Fi router. If you have it hooked up to a monitor, keyboard and mouse, open 2 terminals. If not, try to connect to it via SSH using Putty on Windows or on a Mac using the native terminal. Use the username and password for the Pi configured to login

On Windows

Image for post
Image for post
Connect using Putty to the Raspberry Pi

Mac terminal

ssh username@192.168.10.2

After logging in via ssh

Install git and mosquitto client packages

sudo apt-get -y install git mosquitto-clients mosquitto

Install python packages

pip install --upgrade pippip install paho-mqtt

I have put my code on GitHub to create a simulated sensor data and send it to the MQTT Broker which is a control center. There is an actuator simulator which receives the data from the MQTT Broker and does simulated actuation.

Get the code

https://github.com/bhatikar/IoTHelloWorld.git

The code is based on Python, since is quick. However, in IoT projects code could be written in multiple languages based on the simplicity and what’s feasible as in IoT there are some devices which have a very low memory footprint on which you might write the code in C language as well. I find writing code in C language most preferable as it works on most of the devices be it a small device like Arduino or Expressif microcontrollers units (MCUs) or as large as Intel based NUC.

For now, Python is best suited. I will show how we could write a module in different language linking to this application seamlessly may be in one of my future articles. It would be real fun and interesting…!

Let’s begin our business…

In our example code, the sensor will send ‘on’ or ‘off to the control center and the actuator will get this information, and it will display the status on the console. This example could be considered an actual template for a real IoT scenario. e.g. the sensor could be a temperature sensor sending temperature values and the actuator simulator could take an action based on the temperature values, may be would trigger the cooling or heating in the room, a smart home heating system. How is cool it that :)

Image for post
Image for post

Let's run the code to see how it works …

Connect to the Raspberry Pi using SSH and navigate to the directory where the code is downloaded. Make sure the Pi is connected to the Internet.

Start the simulated actuator.

python actuator.py

Connect to the Raspberry Pi using SSH in another terminal and navigate to the directory where the code is downloaded.

Start the simulated sensor

python sensor.py
Image for post
Image for post

What happens here is the actuator subscribes or listens to a Topic (/IoTSensor/Sensor01)or a channel of the broker or the control center. On this Topic, the sensor sends data periodically. The actuator receives the data, processes it and takes an action. In our case it's simply printing it on the screen.

Simple isn’t it …!

Linux Enthusiast, Embedded systems, Quick Learner, IoT Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store