Mosquitto Meets Raspberry Pi — Part 1
IoT Project Initialization — Join Our Knowledge About Mosquitto & Raspberry PI — # raspiSerie — Episode #05
Hi, we’ve already have played with Mosquitto and Raspberry Pi; now it’s time to join these two technologies together in an awesome:
IoT project: Mosquitto + Raspberry Pi in the cloud!
Welcome!
Here is what you will need to get started in this project:
- Alamode board;
- Arduino UNO;
- Raspberry Pi 1;
- TPM36;
- 1 x LED;
- 1 x 230 Ohm Resistor;
- * Jumpers;
- UartSBee V4;
- Micro SD Card with the OS loaded; See this post to get yours:)
- WiFi Dongle (and RJ-45 Cable) connected to internet;
- USB Type‑A to Micro‑USB lead (power supply)
- Motivation:)
And here is what we deal with in this post:
Step 00 — Explainning all the Project Mosquitto + Pi in the cloud! using an awesome graphics;Step 01 — Install Mosquitto on your desktop platform;Step 02 — Install Mosquitto on your Pi;Step 03 — Test Mosquitto services on your Pi;Step 04 — Test Mosquitto communications between your Computer and Pi;Step 05 — Get Alamode board connected to your Pi;Step 06 — Get upload an Arduno sketch to the Alamode;Step 07 - Get a temperature sensor working at Alamode’s Arduino and spitting values to the Pi;Step 08 — Save the values to the Broker running in your Pi;
00 Step — Motivation
Here is the big picture of all the idea:
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:)
RaspSeries # Episode 03
Raspberry Pi — Passwordless SSH Access on Win10Using A Public
Private Keys Pair to Get To You PI Quickly!
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):
# Installing MQTT Mosquitto Broker in Raspberry Pi.
$ sudo apt-get update
# Update the Mosquitto repository
# *****************************************************
$ sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
$ sudo apt-key add mosquitto-repo.gpg.key
$ cd /etc/apt/sources.list.d/
$ sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
# Installing Mosquitto
# *****************************************************
$ cd ~
$ sudo apt-get update
$ sudo apt-get install mosquitto mosquitto-clients
$ sudo apt-get install python-pip
# Commands used by Mosquitto
# *****************************************************
# Initialize, stop, restart & status Mosquitto (1ª try)
$ sudo service mosquitto start
$ sudo service mosquitto stop
$ sudo service mosquitto restart
$ sudo service mosquitto status
*****************************************************
$ sudo /etc/init.d/mosquitto start (2º try)
$ sudo /etc/init.d/mosquitto stop
$ sudo /etc/init.d/mosquitto restart
$ sudo /etc/init.d/mosquitto status*****************************************************
# Configuring Mosquitto.conf
$ sudo nano /etc/mosquitto/mosquitto.conf
******************COPY/ PASTE IT************************
pid_file /var/run/mosquitto.pidpersistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
allow_anonymous true
# password_file /etc/mosquitto/passwd
# acl_file /etc/mosquitto/acl
port 1883
Here’s my work, step-by-step for your comparison:)
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:
$ sudo /etc/init.d/mosquitto start
Clear the screen in both Terminals and types these commands in each terminal:
1° Terminal (to open a subscription channel called temp = temperature :)
mosquitto_sub -h localhost -p 1883 -t 'temp' -q 0
At 2° Terminal, publish at temp channel (test multiples figures):
mosquitto_pub -h localhost -p 1883 -t 'temp' -q 0 -m 45
mosquitto_pub -h localhost -p 1883 -t 'temp' -q 0 -m 46
mosquitto_pub -h localhost -p 1883 -t 'temp' -q 0 -m 47
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:
mosquitto_pub 192.168.0.20 -p 1883 -t "temp" -m "48"
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…
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:):
***Important***
Set Alamode to 5V_Link in OFF position!Powering the AlaModeThe AlaMode can be powered in three ways:(1) Directly from Raspberry Pi, you can put jumper on the pins labeled “5V_Link” to the ON position. With this jumper in this position, the AlaMode gets its power through the GPIO interface, so you will need a power supply for the Raspberry Pi (RPi) that is capable of supplying a current of at least 1A.
(2) From external source by connecting it to 5V0 using micro USB port available on the other side of the board. In this case the “5V_Link” jumper has to be put in OFF position.(3) Using a 5V FTDI cable. In this case the “5V_Link” jumper has to be put in OFF position. WARNING: If you leave this jumper in the ON position you will ruin your Alamode board. (To use the FTDI from Windows, select Arduino UNO in the Board selection of the IDE.)
07 Step — Get a temperature sensor working at Alamode’s Arduino and spitting values to the Pi;
I chose TPM36:
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:
- Size: TO-92 package (about 0.2" x 0.2" x 0.2") with three leads;
- Price: $1.50 at the Adafruit shop;
- Temperature range: -40°C to 150°C / -40°F to 302°F;
- Output range: 0.1V (-40°C) to 2.0V (150°C) but accuracy decreases after 125°C;
- Power supply: 2.7V to 5.5V only, 0.05 mA current draw;
- Datasheet
08Step — Save the values to the Broker running in your Pi…
Set Alamode to 5V_Link in ON position! (the power must be off)
Let’s read the serial from Raspberry Pi.
First, log in to your Pi via ssh.
Then install serial:
sudo apt-get install python-serial
To search for available serial ports we use the command:
dmesg | grep tty
Here is my try:
The last line indicates that the console is enabled on the serial port ttyAMA0, so we must disable it:
sudo raspi-config
Go to 5 — Interfacing Options > P6 Serial, select it and choose No for this question:
Fine!
Now reboot the Pi:
Or type:
sudo reboot
Now try this command again:
dmesg | grep tty
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):
cd /var
sudo mkdir serial
Then create a file serial_read.py:
sudo nano ~/serial/serial_read.py
Please, paste this code:
# GNU nano 3.2 serial_read.py#!/usr/bin/env pythonimport time
import serialser = serial.Serial(port='/dev/ttyAMA0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)counter=0
while 1:
x=ser.readline()
print x
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:
sudo python /var/serial/serial_read.py
Here is the output:
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!
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
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 Pi — Part 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)