I hope you guys liked my last article. The intent was to show how easy was it to get started. I am not going to talk about real sensors or actuators until we have built our base understanding strong.
“The secret of getting ahead is getting started. The secret of getting started is breaking your complex, overwhelming tasks into smaller manageable tasks, and then starting on the first one.” — Mark Twain
I always like to break big problem statements into a chunk of smaller ones, which help me focus and then knock them down one at a time. It's just a matter of time, we could stitch these different solutions together and achieve our ultimate goal.
So, in this article, I shall demonstrate how we could build an IoT application without Internet. Basically, set up the control center locally in this case would be a MQTT broker. In the course of my further articles, I will cover how can we secure the local MQTT broker which would demonstrate the IoT security aspect which is a major design consideration when we build an IoT application be it on the edge or the cloud.
We shall install the MQTT broker on our Raspberry Pi IoT gateway which we got ready here. Our client would be a laptop that will have a MQTT client. I recommend MQTT.Fx, since it's a free and offers cross-platform support. Overall, what we are trying to achieve is depicted in the below image.
Let us begin…!
Login to the Raspberry Pi IoT gateway and install the broker and the clients if not installed using the below commands. This will install the Mosquitto MQTT broker on the gateway. After the installation mosquitto starts as background service automatically. This is also true after the reboot of the gateway.
sudo apt-get install -y mosquitto mosquitto-clients
Install MQTT.Fx on your laptop and start the application. This would be our MQTT client which would connect to our IoT gateway, and then we could send and receive messages locally without being connected to the Internet.
Create a connection profile from the menu options. It should look something like this.
Use your Raspberry Pi IoT gateway’s IP address in the broker address field. Leave the default values for the rest of the parameters. Click apply and save the configuration.
Select the profile you created and click connect.
If everything was okay, you should see the connection status as Green in the top right corner which was earlier Red. In case of error, it will still be Red.
Congratulations…! You are in the game now :)
Go to the "subscribe tab" and subscribe to a Topic e.g /IoTSensor/ID/01
After you subscribe to the topic, go to the publisher tab, set the topic to the same Topic /IoTSensor/ID/01, enter a message in the below text area and press publish.
If everything went well, you should see your published message in the subscriber tab where you subscribed to the Topic.
Simple isn’t it… 😊
Good news, we could also do this programmatically. I have put a simple sample code here. The reason I showcased MQTT.fx application is that its very handy when we want to do quick debugging.
Just run the subscriber and the publisher like we did in our previous article. It does exactly the same thing we did above with MQTT.fx.
Now that we have successfully setup a local MQTT broker, the possibilities are endless. You could try all the MQTT features like message retention, last-will and testament, QOS, etc and see how they could be used in the IoT app.I found it very interesting when I did that. What I would cover in my next tutorial is, how can we secure our MQTT broker using various method like credentials and with self-signed TLS certificates. It would be an advanced tutorial. I personally love it as it gives one a great insight not only when working with IoT cloud platforms like AWS or Microsoft Azure but also when you develop Edge or Fog computing centric IoT application.
Stay tuned… 😊