Configuration of MQTT
This guide helps you connecting a Raspberry Pi with Tinkerforge using MQTT and Mosquitto as Broker. Mosquitto is an open-source message broker that implements the MQTT protocol. It enables devices to publish and subscribe to topics, facilitating real-time data exchange in distributed networks.
Prerequisites
Before starting with the setup, ensure that the following components are installed and configured:
- A Raspberry Pi (with a suitable operating system, e.g., Raspbian)
- A Tinkerforge Master Brick or Bricklet connected to the Raspberry Pi
- An available MQTT Broker like Mosquitto available. For our project we can use
iot.hs-pforzheim.deas host with8883as port - Python installed (for using the Python MQTT binding)
- You are logged in via SSH to your Raspberry Pi
Configuration Guide
Download and configure Tinkerforge MQTT bindings:
Tinkerforge has an MQTT API Bindings package. You need to run the Tinkerforge MQTT service to allow the Bricklets to communicate over MQTT. You can download the Tinkerforge MQTT bindings from the official Tinkerforge website, or use a package manager (we use our previously added APT repository for this task):
sudo apt-get install tinkerforge-mqttDownload your Group Config Script
We have provided you a script for easy setting your connection up. For further explaination in this documentation we will use a demo connection setting.
To automatically download the demo connection onto your Raspberry Pi, run the following command:
wget -O "$HOME/group0.zip" "https://bae6600.ainz.one/downloads/group0.zip"
unzip -d "$HOME/group0" "$HOME/group0.zip"
cd "$HOME/group0"To automatically download it onto your Raspberry Pi, run the following command:
wget -O "$HOME/group1.zip" "https://bae6600.ainz.one/downloads/group1.zip"
unzip -d "$HOME/group1" "$HOME/group1.zip"
cd "$HOME/group1"To automatically download it onto your Raspberry Pi, run the following command:
wget -O "$HOME/group2.zip" "https://bae6600.ainz.one/downloads/group2.zip"
unzip -d "$HOME/group2" "$HOME/group2.zip"
cd "$HOME/group2"To automatically download it onto your Raspberry Pi, run the following command:
wget -O "$HOME/group3.zip" "https://bae6600.ainz.one/downloads/group3.zip"
unzip -d "$HOME/group3" "$HOME/group3.zip"
cd "$HOME/group3"To automatically download it onto your Raspberry Pi, run the following command:
wget -O "$HOME/group4.zip" "https://bae6600.ainz.one/downloads/group4.zip"
unzip -d "$HOME/group4" "$HOME/group4.zip"
cd "$HOME/group4"To automatically download it onto your Raspberry Pi, run the following command:
wget -O "$HOME/group5.zip" "https://bae6600.ainz.one/downloads/group5.zip"
unzip -d "$HOME/group5" "$HOME/group5.zip"
cd "$HOME/group5"Manual approach for copying the files
If you want to copy the .zip file from your personal computer to your Raspberry Pi using SSH, you can use the scp (secure copy) command. For this, the general process to copy the file over SSH and then navigate to it on the Raspberry Pi:
scp /path/to/local/file.zip pi@<raspberry-pi-ip>:~/groupX.zipReplace /path/to/local/file.zip with the path to the file on your personal computer, pi with your Raspberry Pi’s username (mostly iot), the label groupX with your correct group number and <raspberry-pi-ip> with the IP address of your Raspberry Pi.
Afterwards, you can unzip the file and change the directory:
unzip -d ~/groupX ~/groupX.zip
cd ~/groupX
echo "Current directory: $(pwd)"Run the setup
The script_groupX.sh within your downloaded filder contains the necessary steps to automate configuration. Run the script with execute permissions (as superuser):
sudo chmod +x script_group0.sh
./script_group0.shsudo chmod +x script_group1.sh
./script_group1.shsudo chmod +x script_group2.sh
./script_group2.shsudo chmod +x script_group3.sh
./script_group3.shsudo chmod +x script_group4.sh
./script_groupX.shsudo chmod +x script_group5.sh
./script_group5.shTesting the setup
You can verify the connection with Tinkerforge MQTT by checking for successful messages being sent from the Bricklets (like weather or particulate matter data) through the external Mosquitto broker.
- You can monitor the logs when running the command to see whether the Raspberry Pi successfully connects to the broker.
- Additionally, if you want to test with a simple publish-subscribe model, you can use
mosquitto_pubandmosquitto_subfrom another machine that connects to the same broker.
This method keeps your Raspberry Pi lightweight by avoiding unnecessary installations while still allowing communication with the broker.