Raspberry Pi: Running Mosquitto MQTT server

  • Raspberry Pi 3 Model B

Install and run as service

Taken from YouTube.

$ sudo apt-get update
$ sudo apt-get dist-upgrade
### Install mosquitto
$ sudo apt-get install mosquitto
### Optional - install mosquitto clients
$ sudo apt-get install mosquitto-clients

Install and run in Docker container

Based on this.

install docker
### Install Docker
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh

### Pull Mosquitto image
$ docker pull eclipse-mosquitto
RUN with default configuration
$ docker run -it -p 1883:1883 -p 9001:9001 --restart unless-stopped eclipse-mosquitto
run with custom mosquitto configuration

Say, we want to persist Mosquitto log in local file system

  • create local mosquitto.conf (e.g. at /home/pi/mosquitto/config/mosquitto.conf location)
  • add this line to it:
log_dest /mosquitto/log/mosquitto.log

Then run the container:

$ docker run -it -p 1883:1883 -p 9001:9001 --restart unless-stopped -v /home/pi/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf -v /mosquitto/log eclipse-mosquitto

Inspect the container:

### Figure out container id
$ docker ps
4aaf067565c9 eclipse-mosquitto "/docker-entrypoint.…" 14 minutes...

### Inspect rge container
$ docker inspect 4aaf067565c9
"Mounts": [
"Type": "bind",
"Source": "/home/pi/mosquitto/config/mosquitto.conf",
"Destination": "/mosquitto/config/mosquitto.conf",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
"Type": "volume",
"Name": "7cbeec58f1dee862c14b65b1ab59b180df1ec5fc4736a0a88136a28c88e5e68c",
"Source": "/var/lib/docker/volumes/7cbeec58f1dee862c14b65b1ab59b180df1ec5fc4736a0a88136a28c88e5e68c/_data",
"Destination": "/mosquitto/log",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
"Type": "volume",
"Name": "e5f9ff21ba6a94a84347bcf6aa7203e510db1ea578da63cbe8ce256d96b44914",
"Source": "/var/lib/docker/volumes/e5f9ff21ba6a94a84347bcf6aa7203e510db1ea578da63cbe8ce256d96b44914/_data",
"Destination": "/mosquitto/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""

### Check log file
$ sudo cat /var/lib/docker/volumes/7cbeec58f1dee862c14b65b1ab59b180df1ec5fc4736a0a88136a28c88e5e68c/_data/mosquitto.log
1548170247: mosquitto version 1.5.5 starting
1548170247: Config loaded from /mosquitto/config/mosquitto.conf.
1548170247: Opening ipv4 listen socket on port 1883.
1548170247: Opening ipv6 listen socket on port 1883.

See more on how to run mosquitto in docker container here.


### Console 1
$ mosquitto_sub -t "hellomosquitto"

### Console 2
$ mosquitto_pub -t "hellomosquitto" -m "Hello, Mosquitto!"

### Console 1 prints
Hello, Mosquitto!


Raspberry Pi: cross-compile Boost libraries

  • Ubuntu 16.04 LTS
  • arm-linux-gnueabihf (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
  • Raspberry Pi 3 Model B
Install toolchain

See C++ cross-compiling.

Download boost source code

Boost source on Sourceforge. In my case boost_1_63_0.tar.bz2.


Say, we need boost-iostreams, installation folder /usr/local/boost:

# Unpack the archive
$ tar --bzip2 -xf boost_1_63_0.tar.bz2
$ cd boost_1_63_0
$ ./bootstrap.sh
# In the project-config.jam file replace
# using gcc ;
# by
# using gcc : arm : arm-linux-gnueabihf-g++ ;
$ sudo ./bjam install toolset=gcc-arm --prefix=/usr/local/boost --with-iostreams


Eclipse: slow start – speed up

  • Ubuntu 16.04 LTS
  • Eclipse Neon.3 Release 4.6.3 (Eclipse IDE for C/C++ Developers)

A couple of advices from Stackoverflow work quite well – startup time reduced from tens of seconds to less than 10 seconds and HDD became much more quiet during startup.

Eclipse preferences

In Window > Preferences > General > Startup and Shutdown disable Plug-ins (if you do not use them) activated on startup:

  • Eclipse Automated Error Reporting
  • Marketplace Client
  • Mylyn Tasks UI
  • Mylyn Team UI
  • RSE UI
  • UI Responsiveness Monitoring

Add at the bottom the bold text:


The verify option enables/disables validation of all the .class files that Eclipse loads on startup.

Android Studio and IntelliJ IDEA

The verify option seems to reduce startup time of Android Studio and IntelliJ IDEA too.

Android Studio
$ cp ${ANDROID_STUDIO_DIR}/bin/*.vmoptions ~/.AndroidStudio1.5/config

Then add the verify option to the copied files as described above.

IntelliJ IDEA
$ cp ${INTELLIJ_IDEA_DIR}/bin/*.vmoptions ~/.IdeaIC2017.1/config

Then add the verify option to the copied files as described above.


Raspberry Pi: static IP address

  • Raspberry Pi 3 model B

To set Raspberry Pi static IP address, e.g

  • Modify /etc/network/interfaces
#iface eth0 inet dhcp
iface eth0 inet static
  • Add to /etc/dhcpcd.conf
interface eth0
static ip_address=
static routers=
static domain_name_servers=


Raspberry Pi: slow mouse cursor

  • Raspberry Pi 3 model B

If changing mouse cursor speed in Raspberry Pi Preferences does not work for you try this:

$ sudo nano /boot/cmdline.txt
# Add the bold text at the end of the line:
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles usbhid.mousepoll=0

Taken from Mouse pointer speed.


Raspberry Pi: Dart Hello BC2835

  • Ubuntu 16.04 LTS
  • arm-linux-gnueabihf (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
  • Dart SDK, master 511963d71509de50ac577ac22f3bac8d33c17e34
  • Raspberry Pi 3 model B
Install ARM toolchain

See Raspberry Pi: C++ cross-compiling

Build and install Dart SDK for ARM

See Dart: build Dart SDK for x86 and ARM

Optionally, copy dart-api.h to /usr/local/include:

$ cd dart-sdk/sdk/out
$ find . -name dart_sdk.h
$ cp ./ReleaseXARM/dart-sdk/include/dart_api.h /usr/local/include
Build and install libbcm2835.so

See Raspberry Pi: cross-compile bcm2835 library

Build  missing Dart SDK ARM libraries
# libunsafe_extension.git
$ git clone https://github.com/mezoni/unsafe_extension.git
$ cd unsafe_extension/lib/src/
$ arm-linux-gnueabihf-gcc -shared -fPIC -I/home/username/tools/sdk-dart/sdk/out/ReleaseXARM/dart-sdk/include unsafe_extension.cc -o libunsafe_extension.cc
$ scp libunsafe_extension.so pi@192.168.0.xxx:~
# libffi6_extension.so
$ git clone https://github.com/mezoni/libffi6_extension.git
$ cd libffi6_extension/lib/src/
$ arm-linux-gnueabihf-gcc -shared -fPIC -I/home/username/tools/sdk-dart/sdk/out/ReleaseXARM/dart-sdk/include ffi6_extension.cc -o libffi6_extension.so
$ scp libffi6_extension.so pi@192.168.0.xxx:~
Install the libraries on Raspberry Pi
# On Raspberry Pi
$ sudo mv libbcm2835.so /usr/lib
$ sudo mv libunsafe_extension.so /usr/lib
$ sudo mv libffi6_extension.so /usr/lib
$ sudo ldconfig
Dart Hello BCM2835 project
# On Raspberry Pi
$ git clone https://github.com/yolkhovyy/dart-hello-bcm2835.git
$ cd dart-hello-bcm2835
$ pub get
Resolving dependencies... (1.6s)
Got dependencies!
$ dart bin/main.dart
Hello bcm2835!
Unhandled exception:
Bad state: File not found: bcm2835.lib.h
#0      Declarations.Declarations (package:binary_declarations/src/binary_declarations/declarations.dart:25:7)
#1      _Declarations.declare (package:binary_types/src/declarations.dart:37:28)
#2      BinaryTypeHelper.declare (package:binary_types/src/type_helper.dart:143:24)
#3      Bcm2835Lib.Bcm2835Lib (package:bcm2835_bindings/src/bcm2835_bindings/bcm2835.lib.dart:17:14)
#4      loadBcm2835Library (package:bcm2835_bindings/src/bcm2835_bindings/bcm2835_lib_loader.dart:23:14)
#5      main (file:///home/pi/projects/spikes/bcm2835/dart/bin/main.dart:13:27)
#6      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:263)
#7      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:151)

TODO: figure out why bcm2835.lib.h is missing.

Update: bcm2835_bindings is not maintained anymore – dead end?

Raspberry Pi: cross-compile bcm2835 library

  • Ubuntu 16.04 LTS
  • arm-linux-gnueabihf (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
  • Raspberry Pi 3 Model B

The bcm2835 library “provides access to GPIO and other IO functions on the Broadcom BCM 2835 chip, as used in the RaspberryPi, allowing access to the GPIO pins on the 26 pin IDE plug on the RPi board so you can control and interface with various external devices”.

Install ARM toolchhain

See Raspberry Pi: C++ cross-compiling.

Download and cross-compile bcm2835
Static library

Note, that:

  • In the example below version 1.52 of the library is used – replace with the latest version.
  • The toolchain prefix arm-linux-gnueabihf- should be replaced by your toolchain prefix. See more on toolchains and cross-compiling in Raspberry Pi: C++ cross-compiling blog.
  • The library and its header will be installed in /usr/local/lib and /usr/local/include.
$ wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.52.tar.gz
$ tar zxvf bcm2835-1.52.tar.gz
$ cd bcm2835-1.52
$ ./configure -host=arm -prefix=/usr/local CC=arm-linux-gnueabihf-gcc ar=arm-linux-gnueabihf-ar
$ make
# Skip the check (test) - it will fail in Ubuntu
# sudo make check
$ sudo make install
 /bin/mkdir -p '/usr/local/lib'
 /usr/bin/install -c -m 644  libbcm2835.a '/usr/local/lib'
 ( cd '/usr/local/lib' && ranlib libbcm2835.a )
 /bin/mkdir -p '/usr/local/include'
 /usr/bin/install -c -m 644 bcm2835.h '/usr/local/include'

To change installation directories to, e.g. /usr/local/include/rpi and /usr/local/lib/rpi, use the -includedir and -libdir option instead of -prefix:

$ ./configure -host=arm CC=arm-linux-gnueabihf-gcc ar=arm-linux-gnueabihf-ar -includedir=/usr/local/include/rpi -libdir=/usr/local/lib/rpi
$ sudo make install
 /bin/mkdir -p '/usr/local/lib'
 /usr/bin/install -c -m 644  libbcm2835.a '/usr/local/lib/rpi'
 ( cd '/usr/local/lib' && ranlib libbcm2835.a )
 /bin/mkdir -p '/usr/local/include'
 /usr/bin/install -c -m 644 bcm2835.h '/usr/local/include/rpi'
Shared library

Quick solution without involving autotools:

$ cd bcm2835-1.52/src
$ arm-linux-gnueabihf-gcc -shared -fPIC bcm2835.c -o libbcm2835.so

Copy the shared library to the /lib on your Raspberry Pi.

Raspberry Pi: serial terminal setup

  • Ubuntu 16.04 LTS
  • Raspberry Pi 3 Model B
Serial terminal setup
Serial cable

We need a FTDI USB-cable, e.g. such one.

Connect the cable to Raspberry Pi GPIO connector.

Disclaimer: I’ll take no responsibility for the correctness of the wiring information below and potential damage of your hardware. Please re-check the wiring.

  • RxD (green wire) – pin 10
  • TxD (white wire) – pin 8
  • Ground (black wire) – pin 6
  • Power (red wire) – pin 4, connect only if you want to power your Raspberry Pi via the serial cable, otherwise – leave unconnected


One of the options is the Screen Shell Session Manager  as described on Intel’s Set up a serial terminal page:

$ sudo apt-get install screen
$ sudo screen /dev/ttyUSB0 115200

Other option is Putty, install and configure as shown below:

$ sudo apt-get install putty