Mosquitto Meets Raspberry Pi — Part 1

IoT Project Initialization — Join Our Knowledge About Mosquitto & Raspberry PI — # raspiSerie — Episode #05

J3
Jungletronics
11 min readApr 18, 2020

--

Hi, we’ve already have played with Mosquitto and Raspberry Pi; now it’s time to join these two technologies together in an awesome:

Welcome!

Gif 1. We're gonna flash Alamode using Arduino As ISP. Please, continue to read on!

Here is what you will need to get started in this project:

And here is what we deal with in this post:

00 Step — Motivation

Here is the big picture of all the idea:

Fig 1. The Project mission consists of a series of local & remote sensors satellites gadgets connected by mosquitto brokers with the primary mission of obtaining Earth imagery for land use, agriculture, forestry, geology, cartography, regional planning, water resources, and GIS applications and last but not least my office room sensor & my work sensors are connected to my Pis via Wyolum’s Alamode boards :)

Let me try to explain all the Project Mosquitto + Pi in the cloud!

Let’s begin with my_Broker1 (raspi service on the left).

This is a Mosquitto’s broker. Well, what is Mosquitto?

Mosquitto is a lightweight open-source message broker that Implements MQTT versions 3.1.0, 3.1.1 and version 5.0. It is written in C by Roger Light, and is available as a free download for Windows and Linux and is an Eclipse project.

We dealt with Mosquitto installations on Windows in this post:)

We will start the Mosquitto service broker in our Raspberry Pi.

Fine!

We are going to set my_Broker1 up and running in this post.

This first broker will serve as an entry point of the temperature sensor of my office homeroom. This sensor is hosted by Alamode, which is a stackable Arduino compatible board for the Raspberry Pi.

Don’t be worry! We will make it in a step-by-step fashion!

Alright!

My desktop has the ability to access the internet via Cable & WiFi. From there We will send commands to my local and remote boards.

So far, so good!

My Broker1 can make a bridge with an extern BROKER1 established in the cloud (SPOT -Satellite Pour l’Observation de la Terre, France — as a sample..you might choose others) and this one can bridge BROKER2 and so on, making a hole internet & Satellite Networking. Fine!

At my work, I will have another Raspberry Pi serving another my_BROKER2 which can be connected to my laptop via ethernet, and blah, blah, blah…

Now you have the big picture of the project, right?

Let’s get to work!

First mission: get mosquitto up and running in my Raspberry Pi 1:

Get your Raspberry Pi I connected to WyoLum Alamode (or connect an Arduino Uno in one Pi’s USB port:)

01 Step — Install Mosquitto on your desktop platform;

Yeah, just get it going!

First, get access to your Laptop; Let’s install mosquitto on it; refer to this post, please:)

02 Step — Install Mosquitto on your Raspberry;

Fine!

Now open your PuTTY’s ssh session at your Raspberry Pi.

I will use a personalized user (j3), that resulted from this previous episode, but you can use pi user:)

I think it might work with regular official user pi and raspberry password. But as we go deep inside Raspberry Pi, we might be thinking about security, right? And j3 user uses public-private keys to access Pi.

To install mosquitto in your Pi, please make these procedures in your command line (copy/mouse right-click to paste and execute each of the following commands, in sequence):

Here’s my work, step-by-step for your comparison:)

Fig 2. Running $ sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
Fig 3. Running $ sudo apt-key add mosquitto-repo.gpg.key
Fig 4. Running $ sudo apt-key add mosquitto-repo.gpg.key
$ cd /etc/apt/sources.list.d/

$ sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list and $ cd ~ (3 cmds)
Fig 5. Running $ sudo apt-get update
Fig 6. Running $ sudo apt-get install mosquitto mosquitto-clients
Fig 7. Running $ sudo apt-get install python-pip
Fig 8. Testing mosquitto: Start, Status, Stop, Restart, and Status executions
Fig 9. Configuring /etc/mosquitto/mosquitto.conf file

03 Step — Test Mosquitto services on your Pi;

For the test, we will need to open 2 terminals on Pi (simply call PuTTY two times:)

At the First Terminal, run:

Fig 10. Mosquitto is Active (running)!

Clear the screen in both Terminals and types these commands in each terminal:

1° Terminal (to open a subscription channel called temp = temperature :)

At 2° Terminal, publish at temp channel (test multiples figures):

Fig 11. Ok! Mosquitto is up & running in Raspberry Pi’s console! Just awesome!

04 Step — Test Mosquitto communication between your Computer and Pi;

Now over your Windows platform open Windows Prompter in your desktop (which is in the same Pi’s network, I suppose…) and head over to c:\…\mosquitto directory (see this post to install mosquitto on your win10 platform) and type:

Fig 12. ****WARNING**** The topic (-t) only works if you use DOUBLE QUOTES :/ whereas the number message works with or without quotes :/ just in case something goes wrong…as happened to me… :/ btw, -_pub(publisher), _sub(subscriber), h(host), -p(port), -m(message) and -q(quality of service:)

05Step — Get Alamode board connected to your Pi;

I made strenuous work soldering Alamode (and sometimes, tedious one, too…).

Here is the video. But be aware that this is a long long video; only useful in case you are not proficient at the techniques to soldering boards (neither I am:/. It is a long video and I have done my best; it is totally up to you to see it:/ It makes no difference now…maybe later…

Video 1: presenting Alamode and get Gadget options and Soldering tips and tricks!

06 Step — Get upload an Arduino sketch to the Alamode;

Fine, now the hardest work:/

How to get flashed an Arduino sketch over Alamode?

This turns to be a difficult task…I wasn’t aware of how hard it was…

Let me contextualize: I intended to use a USB ISP programmer or UartSBee v5 — Seeed Studio Boards to flash Alamode, because, as you can see in this video, that flashing Arduino via LXDE (the GUI used by Raspbian) is very time-consuming…but boils down to an almost unsolvable problem… And I intend to use the command line in Pi, preferably.

The USB ISP programmer is a cheap Chinese programmer that did not work…there were driver errors…I had to search for more info…

So I decided to program Alamode in this manner (it worked like a breeze:):

Photo 1: Arduino As ISP Programmer to flash Alamode. Please refer to this post [TODO: LINK TO HOW TO FLAS ARDUINO USING ALAMODE GOES HERE!]
Fritz 1 — A schema for reference — Replicate it at home:)
Photo 2. 5V_Link in ON position.
Photo 3. 5V_Link in OFF position.

07 Step — Get a temperature sensor working at Alamode’s Arduino and spitting values to the Pi;

I chose TPM36:

Fig 13. TMP36 from https://learn.adafruit.com/tmp36-temperature-sensor

Because these sensors have no moving parts, they are precise, never wear out, don’t need calibration, work under many environmental conditions, and are consistent between sensors and readings. Moreover, they are very inexpensive and quite easy to use.

Some TMP36 specs:

Fig 14. TPM’s curve from the Analog’s datasheet
Photo 4. TPM36 connecter to Alamode pin A0; watch out to not short the legs:)
Video 2: Explain how to flash an Alamode board using Arduino As ISP `Programmer:)

08Step — Save the values to the Broker running in your Pi…

Let’s read the serial from Raspberry Pi.

First, log in to your Pi via ssh.

Then install serial:

To search for available serial ports we use the command:

Here is my try:

Fig 15. Searching for available serial ports

The last line indicates that the console is enabled on the serial port ttyAMA0, so we must disable it:

Go to 5 — Interfacing Options > P6 Serial, select it and choose No for this question:

Fig 16. Disabling serial shell
Fig 17. Enabling serial hardware
Fig 18. Confirming the changing for use the serial hardware

Fine!

Now reboot the Pi:

Fig 19. Rebooting the Pi.

Or type:

Now try this command again:

Fig 20. Now we can use ttyAMA0 serial hardware :)

Now we can use serial ttyAMA0:)

Just fine! now we’ll create a file, test_serial.py, that will read the ttyUSB0 port.

Then make a directory at home directory (~/var/serial):

Then create a file serial_read.py:

Please, paste this code:

This will read the serial port and pass it back to the python serial console.

The Arduino code is spitting data to the serial and Raspberry Pi will capture it:

Now type:

Here is the output:

Fig 21. The final resulting: The serial is spitting the temperature in 87°F and 31°C

Here is the temperature of my location: 31°C.

We need to save this value inside the Mosquitto broker.

But this step will be left for the next episode :)

And That’s all!

Hurry, we’ve made to the end of part 1 of another exciting project, this one, IoT project: Mosquitto + Raspberry Pi in the cloud!

If you like this please favorite it and wanna see more of them please subscribe. Below there is a list of the related posts too!

Here is my youtube j3 channel, twitter, Gmail, Facebook, and medium page index (jungletronics & kidsTronics) in case you need to reach me.

If you have some idea you’d like to see me do, well, put’em them in the comments below or make the first contact as you see in the Spielberg movie;)

Alright, j3 signing off… I’ll see you in the next episode!

o/

Download All The Files For This Project

References & Credits

SPOT (Satellite Pour l’Observation de la Terre), France — by crisp.nus.edu.sg

Eclipse Mosquitto™ — An open-source MQTT broker — by mosquitto.org

Steve’s Internet Guide by Steve’s Internet Guide

AlaMode by Wyolum — by wyolum.com

Alamode datasheet — by wyolum.com

Alamode User Guide r1 — by wyolum.com

OpenSSL Win32 — by slproweb.com

An Introduction to Mutual SSL Authentication — by Elvin Cheng (SSL authentication: understand the intricacies of the handshake messages happening under the hood) — Excellent Article!

From: https://www.codeproject.com/Articles/326574/An-Introduction-to-Mutual-SSL-Authentication by Elvin Cheng

IDC Connectors — Insulation-displacement connector — by wikipedia.org

Postbag #28: Arduino Special — Cheap Uno, DIY Duinos, USB Tiny ISP — by Julien Ilett Youtube channel

1-Day Project: Build Your Own Arduino Uno for $5 — by Julien Ilett Youtube channel

ATMega Pin mappings: An awesome graphics (print it!) — by arduino.cc

RS-232 — by wikipedia.org

Arduino as ISP and Arduino Bootloaders — by www.arduino.cc

How to Update the Firmware on a USBasp V2.0 — by Lincomatic’s DIY Blog

SparkFun Pi AVR Programmer HAT — by sparkfun.com

TMP36 Temperature Sensor — by learn.adafruit.com

Read and Write From Serial Port With Raspberry Pi by instructables.com

gif 2. Alamode mounted with USB and GPS:)

Related Posts

01Episode #RaspSeries — Raspberry Pi — How to Set Up a Headless Raspberry Pi — Without Ever Attaching a Monitor!

02Episode #RaspSeries — Raspberry Pi — First Boot — Configuring & Get Acquainted of your Pi

03Episode #RaspSeries — Raspberry Pi — Passwordless SSH Access on Win10 — Using Open SSH Keys to Get to Your Pi Quickly!

04Episode #RaspSeries — Raspberry Pi Meets AlaMode® — An Arduino compatible board for the Raspberry-Pi

05Episode #RaspSeries — Mosquitto Meets Raspberry PiPart 1 — IoT Project Initialization — Join Our Knowledge About Mosquitto & Raspberry PI (this one:)

06Episode #RaspSeries — Mosquitto Meets Raspberry Pi — Part 2 — IoT Project — Installing Apache, Php, Mysql, PHPMyAdmin (LAMP)

--

--

J3
Jungletronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️