IOT SETUP - RASPBERRY PI SETUP [OPENHAB 2 with Mosquitto Broker]

This page logs my setup and ongoing experimentation for development of 'Internet of Things'
(ESP8266 sensors), using the Raspberry Pi, running the latest version of 'Raspian' (Buster),
and the latest version of openHAB 2, with a separate Mosquitto Broker.

It presumes that you already have one or more ESP8266, or similar, IOT modules
running, and currently generating MQTT messages.

I also recently tried several times to load an "openHabian" release - but each time it failed totally.

The following instructions load each element separately - so I can monitor the progress, and make corrections.

Setting up a new Raspberry Pi:

This section describes the steps in setting up a new Raspberry PI.

  • Go to the RASPIAN download page at RASPIAN (currently Ver 4.19)
  • Download "Raspbian Buster with desktop and recommended software" ZIP file.
  • .... Note where the File ( eg: "2020-02-13-raspbian-buster-full.zip") is downloaded to
  • .... - usually the 'Downloads' directory - leave it 'zipped' for now.
  • Insert and Format your microSD card to FAT32
  • Download the image writer program 'Etcher' from Etcher
  • .... - (Ensure it is correct Version for your 'Operating System' )
  • Choose 'Save File'.
  • .... Locate where this File is downloaded to (usually 'Downloads' directory)
  • Create a working directory (eg. "G:\Raspberry-Pi") and drag both files there.
  • Double click the 'Etcher-Setup-xxx' application - it will install and run immediately.
  • In the 'Etcher' box click 'Select image' - choose eg. "2020-02-13-raspbian-buster-full.img"
  • In the 'Etcher' box (confirm the SD drive info) and click 'Flash'.
  • ... wait for it to complete 'de-compression', 'flashing', and complete the 100% 'verify'.
  • .
  • Connect a Screen, Keyboard and Mouse and 'Insert' Raspian microSD card...
  • Power up, and wait for boot-up completion...
  • ... When the Raspberry Pi Graphical Scrren is displayed ...
  • Click the mouse on the "Command Box" on the top bar..
  • ... then update your distribution ...
  • Refer to Raspberry Pi Update Learning Resources
  • sudo apt-get update
  • ---
  • .. For security reasons remote 'Putty' does not work in recent releases...
    .. here is the 'noob' instruction to get this problem fixed..

    Recording the original IP Address...

  • Lookup the IP Address so you can use a PuTTY window.
  • sudo ifconfig
  • Note "eth0" specifically to get the IP Address - and write it down for later use.
  • sudo raspi-config
  • 1) Choose first 'update' (to this application)
  • 2) In Advanced Options -> expand_root file system
  • 3) Interfacing Options -> ssh [enable]
  • 4) change Localization Options->Locale ->[en_AU.UTF-8 UTF-8]
  • ..... This is important for MySQL sort order later (Accept the en_AU.UTF-8 default).....
  • 5) and while you still here, it's good to change the timezone as well
  • --- Localization Options->Timezone -> Australia -> Melbourne
  • ... As you Exit you can ask for a 'Reboot' ...
  • Open a PuTTY window for that IP address that you recorded earlier
  • Setting A Fixed IP Address...

  • In the PuTTY command window get the current configuration
  • sudo ifconfig
  • Note "eth0" specifically for the IP address.
  • Now we must set up interface eth0: in config file.
  • "inet addr:192.168.1.20, Bcast:192.168.1.255, Mask:255.255.255.0"
    ... see this article for Jessie (older Raspian version)... Refer to Setting Static IP
  • sudo nano /etc/dhcpcd.conf
  • ... Go to the end of the file and add these lines...
    ... This presumes and Address of 192.168.1.20 (change to preferred values)...
    ... To copy a line in 'Nano' first 'Cut' (Ctrl+K) and 'Uncut' (Ctrl+U) twice...
  • To exit the editor, press ctrl+x
    To save your changes press the letter “Y” then hit enter
  • Shut down Raspberry Pi ('sudo shutdown -h now') and power off and on to Restart
  • Don't forget you need to change your PuTTY address to new IP that you just set.
  • Changing the 'Hostname' from 'raspberrypi' to a unique name...

  • In the PuTTY command window run the 'Configuration Tool'
  • sudo raspi-config
  • ... then select "2 Network Options" then "1 Hostname"
  • ... Change the Hostname to "Pi20" and 'tab' to "[OK]" and Enter.
  • ... When asked if 'Ok to Reboot?' say '[OK]'.

Installing Mosquitto on the the Raspberry Pi:

First you should update your distribution:
  • sudo apt-get update
Install pre-requisite software (THESE STEPS ARE PROBABLY NOT REQUIRED):
Where prompted about memory usage answer 'Y' 'Enter'.
  • sudo apt-get install libssl-dev
  • sudo apt-get install cmake
  • sudo apt-get install libc-ares-dev
  • sudo apt-get install uuid-dev
  • sudo apt-get install daemon
  • sudo apt-get update
  • sudo apt-get install mosquitto
In 'putty' run the following commands to ensure that 'Mosquitto' is running and listening on Port 1883,
  • service mosquitto status
  • netstat -tln
A important tool for testing Mosquitto and MQTT is MQTT SPY which can be installed on a Windows Desktop Computer.
It is quite powerful but requires that a recent version of Java be installed and active.
.

Setting the Connection Details in "mqqt-spy" for Raspberry Pi:
... Chrome MQTT SPY

Subscribing to MQTT messages

.

Once connected you should Subscribe to "#" (means 'all' messages).
.
You will then see any 'Subscribed Messages' appear in the lower box.
.
You can confirm it by typing anything into the "Publish Message" "Topic" and "Data" box and press "Publish".

If 'Mosquitto' is working on the Raspberry Pi the text should appear in the "Messages" window.

Chrome MQTT SPY

  • If you have not made any IOT sensors yet, then this is the exact point where you should make an ESP8266 sensor.
  • I would recommend not trying to setup openHAB 2, until you are able to see MQTT messages appearing in this window.
  • MQTT-Sender Making an ESP8266 MQTT Sender

To be able to make effective use of the data being returned from "Internet Things" some form of configurable Web based application is required. There are a number of applications but 'OpenHAB' (now version 2) seems to fulfill most of the requirements, and is available for Raspberry Pi.

.

Installing OpenHAB 2 on the the Raspberry Pi:

Installing OpenHAB 2 in the Raspberry Pi.



The following instructions quoted directly (with extra comments and steps added) from...

Verify Java Version

openHAB makes extensive use of Java and version 1.6 or higher is required. The first step is to ensure our Raspberry Pi includes a recent version of Java.


     java -version  

The Raspberry Pi will present the version of Java installed as shown below.


    openjdk version "11.0.6" 2020-01-14 
    OpenJDK Runtime Environment (build 11.0.6+10-post-Raspbian-1deb10u1) 
    OpenJDK Server VM (build 11.0.6+10-post-Raspbian-1deb10u1, mixed mode) 

Documentation for openHAB says "For best compatibility, namely with the
openHAB Cloud service myopenhab.org and the Eclipse IoT Marketplace, the minimum
recommended Java 8 revision is "161".
In my case, I have an install version of openjdk version 11.0.6

The is the the OpenJDK version and "OpenJDK may also be used, but it
has some known limitations with openHAB and is not recommended." - so keep an
eye open for problems.

.

Apt Based Systems

First, add the openHAB 2 Bintray repository key to your package manager and allow Apt to use the HTTPS Protocol:
    wget -qO - 'https://bintray.com/user/downloadSubjectPublicKey?username=openhab' | sudo apt-key add -      
    sudo apt-get install apt-transport-https  
Normally you can choose between, Official (Stable), Beta or Snapshot builds:
I recommend only using the 'Stable Release' unless you are looking for a new feature.
Add the openHAB 2 Stable Repository to your systems apt sources list:
    echo 'deb https://dl.bintray.com/openhab/apt-repo2 stable main' | sudo tee /etc/apt/sources.list.d/openhab2.list 
 
Next, resynchronize the package index:
    sudo apt-get update 
Now install openHAB with the following command:
      sudo apt-get install openhab2   
When you choose to install an add-on, openHAB will download it from the internet on request. If you plan on disconnecting your machine from the internet, then you will want to also install the add-ons package.
      sudo apt-get install openhab2-addons    
Optionally, you may in addition install the legacy add-ons package openhab2-addons-legacy. This package contains 1.x bindings, for which there is already a 2.x version available. This might be useful if you're coming from openHAB 1.x for example.

.

If everything went well, you can start openHAB and register it to be automatically executed at system startup.

# Systems based on systemd (e.g. Debian 8, Ubuntu 15.x, Raspbian Jessie and newer):

   
    sudo systemctl start openhab2.service
    sudo systemctl stop openhab2.service
    sudo systemctl status openhab2.service   
    sudo systemctl daemon-reload 
    sudo systemctl enable openhab2.service 
    
[openHAB] Please use the following commands to launch openHAB on a system restart.
            sudo /bin/systemctl daemon-reload
            sudo /bin/systemctl enable openhab2.service

[openHAB] You can start openhab2 manually by executing the command:
            sudo /bin/systemctl start openhab2.service

The first start may take up to 15 minutes, this is a good time to reward yourself with hot coffee or a freshly brewed tea! You should be able to reach the openHAB 2 Dashboard at http://openhab-device:8080 at this point. If you're new to openHAB, then you should checkout the beginner's tutorial!

.

Once running connect a browser on "http://192.168.1.20:8080/start/index" (insert your own IP address) and see the following screen.

.

.

Initial Setup of openHAB2:


.

... openHAB2 Initial Setup

.

Chosing the openHAB 2 package

.

Of the four standard packages, the preferred one is the "Expert Package", because it is most suitable for experienced Users.
.
However, I prefer to add the 'Demo' Package first as it sets up the various Icons, and example Items files needed.
I expect this will be best used for setting up existing 'Items' and "Sitemaps".
Select it now and and wait for its' Intallation. You will be able to use the PaperUI later anyhow.

When it is completed we need to set up the 'Samba' shares so that we can edit the configuration files directly from the Desktop PC.

.

    Installing Samba on the Raspberry Pi

    Samba is a file sharing application that allows you to share files directly between your desktop PC and the Raspberry Pi.
    • Samba Installation Process

    • sudo apt update
    • sudo apt install samba samba-common-bin smbclient cifs-utils
    • Answer 'Y' when questioned and when it asks the following question answer 'No' as we are using a fixed IP address...
      Chrome MQTT SPY
    • Setup Windows for using a shared Windows folder

    • On the Desktio PC open "Control Panel\Network and Internet\Network and Sharing Center\Advanced sharing settings"
    • Select 'Turn on network discovery'
    • Select 'Turn on file and printer sharing'
    • 'Save' changes or 'Cancel' if already set
    • ......................
    • ...Edit the Samba Config file..
    • sudo nano /etc/samba/smb.conf
    • ..Paste the following lines into the file after the "workgroup = WORKGROUP" line...
    • ..just leave the ';' in front of the "wins server = w.x.y.z" line for now..
    • ..Next, add all these desired share configuration lines to the end of the file:...
    • ......................
    • press control x then y and then enter
    • ......................
    • ... The shares are configured to be not open for guests nor to the public. ..
    • ... Let's activate the "openhab" user as a samba user and set his password (e.g. "habopen"): ..
    • sudo smbpasswd -a openhab
    • .You must actually type in habopen when propted (New SMB password:) - twice
    • ...Now setup the permissions for the new User..
    • ... The original command was"sudo chown -hR openhab:openhab /etc/openhab2 /opt/openhab2/conf" but failed...
    • sudo chown -hR openhab:openhab /etc/openhab2
    • ...check the config file for correctness ...
    • testparm
    • ...Then restart the Samba Service...
    • systemctl restart smbd.service
    • ..you will have to enter Authentication to proceed - use pi, raspberry ....
    • Opening the shared directories in Windows

    • ...Don't forget - The default username/password is 'openhab:habopen', so enter 'habopen' at the password prompt.
    • ..Open a File Explorer Window...
    • ..Type \\192.168.1.20 in the top box....
    • ...After logging in you will see the three 'Shared directories"...
    • Samba Shared Directories
    • ......................
    • Map openHAB config
    • ..
    • Assign openHAB config
    • Install and Setup Visual Studio Code (OpenHAB) extensions in Windows

    • Go to https://code.visualstudio.com/updates/v1_44
    • Download "VSCodeUserSetup-x64-1.44.1.exe"
    • In a known location create Directory (eg. "G:\Visual-Studio-Code-Openhab")
    • copy the "VSCodeUserSetup-x64-1.44.1.exe" file to that location
    • Start the installation by double clicking the exe file
    • Visual-Studio-Code
    • Continue with the Installation ov Visual Studio and run the application
    • On the left hand side click on the 5th Icon (Extensions)
    • Type "openhab" in the search box and when "openHAB 0.70" is displayed click on it
    • You will then be able to click on "Install" button.
    • ..
    • Once Visual Studio Code is running you can select "Open Folder" and open "Drive Z:"
    • .[view at 9 min:27 sec in 'BK Hobby' youtube video below]...
    • ..
    The following Video (from BK Hobby) shows how to set up Visual Studio editing of files on the Raspberry Pi.
The following Video (from BK Hobby) shows "openHAB 2 Basics - Items & Links | Creation and Configuration".

.

    SETTING UP OPENHAB2...


  • openHAB 1 migration to openHAB2 step by step instructions
  • .

    Choose the UI in openHAB2 from the following options:
    To quote from the above article: "Navigate your browser to .... Select "Expert" from the list of default packages.
    Now select "PaperUI" and navigate to Configuration -> System on the left hand side.
    Enable "Include Legacy 1.x Bindings" and "Access Remote Repositories".
    This will allow you to install your current bindings even if there is an openHAB 2 version available. Press "Save".

    ... openHAB2 Initial Setup

    .

    .

    File Locations

      Repository Installation
    • openHAB application.... /usr/share/openhab2
    • Additional add-on files.... /usr/share/openhab2/addons
    • Site configuration....... /etc/openhab2
    • Log files................ /var/log/openhab2
    • Userdata like rrd4j databases../var/lib/openhab2
    • Backups folder..../var/lib/openhab2/backups
    • Service configuration.... /etc/default/openhab2
    • Detailed Config files..... /etc/openhab2/services

    INITIAL OPENHAB2 CONFIGURATION USING THE PAPERUI

    .

    .

    First we have to install the 'MQTT Broker' Add-on supporting the Mosquitto Broker.

    Installing the MQTT Binding

    .

    Start the PaperUI
    .
    Then you must select the "Add-ons" option.
    .
    Click on the "BINDINGS" tab at the top of the page
    .
    Type "MQTT" into the search bar and look for 'MQTT Binding'
    .
    On the far right of the screen click on the "INSTALL"
    and wait for it to complete.
    .
    The following configuration files will set up everything.

    Add MQTT Binding

    .

    .

  • agrinode: OpenHab2 - MQTT binding demo
  • The above 'blog' shows how to add the MQTT binding by edit to mqtt.config file, and provided the basis for the following files.
  • Using Visual Studio, in a SAMBA share, go the "things" directory.
  • Right Click on "things" to create a 'New File' which is named to "mqtt.things"
  • then copy the following text and paste to that file.
  • "Save All"
  • ....
  • Right Click on "items" to create a 'New File' which is named to "mqtt.items"
  • ..Then enter this text into that new file. ...
  • Right Click on "items" to create a 'New File' which is now named to "myhome1.items"
  • ..You will also need at least these same lines in that file ...
  • ..The reason for having the items in both files is not completely clear - but is absolutely needed to work,...
  • ... and could be related to the 'Channel' needing a 'item' definition at both ends!
  • ....
  • ....
  • Right Click on "sitemaps" to create a 'New File' which is named to "myhome1.sitemap"
  • ..Then copy and paste this text into that new file. ...
  • ....


  • Save All files
  • In a browser window type http://192.168.1.20:8080/basicui/app?sitemap=myhome1 to view the myhome1 site.
  • ..
  • if the sitemap does not appear as shown here then stop and restart openhab2 ...

  • Stop openhab2 service..
    sudo systemctl stop openhab2.service

  • Wait about two minutes

  • Start openhab2 service..
    sudo systemctl start openhab2.service

  • Wait about five minutes

  • If this does not work then refresh the main pages in PaperUI - 'Add ons' and 'Control' pages.
  • The same numbers and titles should appear in the PaperUI 'Control' page.



  • The further setup of persistence and graphical displays will be handled in a separate page - to be added soon..
  • Raspberry Pi Setup [latest 'Raspian OS', 'openHAB2', and 'Mosquitto Broker'] PART2

General Raspberry Pi Linux Commands and Hints:

This section Lists the useful Linux Commands that I often forget.

    ... Report on Free Space on the SD Card ...
  • df -Bm
  • ... Clear the Putty screen ...
  • clear
  • ... update your distribution ...
  • sudo apt-get update

  • ... To totally shutdown Raspberry Pi...
  • sudo shutdown -h now

  • ... check up on Mosquitto (MQTT Broker) ...
  • service mosquitto status
  • netstat -tln

  • To stop and restart the OpenHAB2 as required.
  • sudo systemctl start openhab2.service
  • sudo systemctl stop openhab2.service
  • sudo systemctl status openhab2.service
  • sudo systemctl daemon-reload
  • sudo systemctl enable openhab2.service

  • To modify an OpenHAB2 config Files (use sudo nano (filename) ...:
  • openHAB application.... /usr/share/openhab2
  • Additional add-on files.... /usr/share/openhab2/addons
  • Site configuration....... /etc/openhab2
  • Log files................ /var/log/openhab2
  • Userdata like rrd4j databases../var/lib/openhab2
  • Backups folder..../var/lib/openhab2/backups
  • Service configuration.... /etc/default/openhab2
  • Detailed Config files..... /etc/openhab2/services

  • To List the Files in the MySQL directory on the Hard Disk ...:
  • sudo ls -l /mnt/usbdrive/mysql
  • To test the command line mysql with user='openhab' password='openhab'...
  • mysql -p -u openhab
  • show databases;
  • use openhab;
  • SHOW COLUMNS FROM Item1;
  • show tables in openhab;
  • SHOW TABLE STATUS FROM db_name;
  • quit;
Last updated: 05-AUG-2023 at 9:40 AM.
©2004-2014 Paradigm Electronics
Home | About Paradigm Electronics | Products & Services | Contact us | Site map | Useful Links | Client | ANMF test | Android | Ham Data | Logout |