Introduction: Connect Tinkerforge Sensors to Cayenne IOT Dashboard Via MQTT on PI

How to connect Tinkerforge sensors to Cayenne IOT dashboard via MQTT on PI

I had a Raspberry PI Model B since some time and wanted to monitor the environment of a small rack.

As I am not an electronic guy nor a programmer, I wanted an easy way to connect sensors without soldering.

Found the Tinkerforge concept interesting and build on it.

Then I saw the Cayenne IOT dashboard and tried to combine the two via MQTT protocol.

Thanks to the help of both Tinkerforge and Cayenne forums, here is how I did it.

I detailed this doc as far as I could, so that someone else with as little knowledge as me could replicate it.

P.S. Had some problems with the instructables editor, so I add my original pdf file.

Step 1: List of Parts:

1x raspberry PI model B

1x switch with POE ports with Ethernet cables

Tinkerforge:

· 1x Master brick

· 1x Ethernet Master Extension

· 1x Temperature Bricklet

· 1x Humidity Bricklet

· 1x Server Room Monitoring Case

· 3x Bricklet Cable Black 15cm

· 1x Bricklet Cable Black 50cm

· 6x Mounting Kit 12mm

Step 2: DIAGRAM

Step 3: Installing and Configuring Jessie on PI:

Install raspbian Jessie lite from https://www.raspberrypi.org/downloads/raspbian/

See https://www.raspberrypi.org/documentation/installation/installing-images/windows.md how to do it.

Also install putty (http://www.putty.org/) on your pc

After loaded the raspbian image on the SD card, start the pi

IP is normally the first free dhcp lease from your router or via terminal command “ ifconfig “ if you have a screen connected to your PI

Easiest way is to have an usb keyboard and an hdmi screen connected to your PI.

Default login is pi and password is raspberry

Configure the config file:

sudo raspi-config 

or use the PI menu, preference, Rasperberry Configuration on the screen

Change the start config:

-1expand file system : to use all the sd card memory

-2change password to pi (easier to type) or something else

-4 international option: # I live in Belgium so set the stings for your country

local : keep EN GB as default otherwise you will find some Russian texts

timezone: europe, brussels #put your timezone

keyboard: logitech cordless desktop, other, Belgian, first belgian, the default, no compose key,yes. #put your keyboard settings

-8 advanced settings

A2 hostname : pi (or something else)

A3 memory split :32 or lower (no screen once configured, so all memory to system)

A4 SSH enable

finish and reboot

Configure static IP instead of DHCP:

Use putty or screen to login with Pi and password you changed just above.

At prompt

sudo nano /etc/dhcpcd.conf

At end of file add the fixed ip #replace 192.168.1.20 with your IP and use your router and gateway IP

# A hook script is provided to lookup the hostname if not set by the DHCP
# server, but it should not be run by default.
nohook lookup-hostname
interface eth0
static ip_address=192.168.1.20/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

Ctrl x to exit, y(es) to save modified buffer and click “enter” to finish

After reboot open putty with ip 192.168.1.20 with pi and raspberry or your password if you changed it

Update/upgrade packages:

sudo apt-get update
sudo apt-get upgrade

Step 4: Connect the Tinkerforge Brick and Bricklets

In my case I have 3 bricklets (temperature, humidity and barometer) connected to a master brick with master Ethernet with POE extension (see info https://www.tinkerforge.com/en/doc/) . Connect the bricklets to the master brick with the appropriate cables. Stack the POE Ethernet master extension on top of the master brick and connect your network cable to a POE port on your switch.

Configure the IP address see https://www.tinkerforge.com/en/doc/Hardware/Master_Extensions/Ethernet_Extension.html#ethernet-configuration

For the first connect, you will have to find IP address of the brick the DHCP lease in your router.

Change connection from DHCP to Static. Put your IP (my case 192.168.1.41), subnet mask, gateway and save Ethernet configuration.

Install the brick deamon https://www.tinkerforge.com/en/doc/Software/Brickd.html and the brick viewer https://www.tinkerforge.com/en/doc/Software/Brickv.html on your pc.

Open the brick viewer and connect to the host, in my case 192.168.1.41 and port 4223 (default)

Step 5: Brick Viewer Screenshot

Here you can see the UID of the 3 bricklets (case sensitive !!!)

Temperature = t6Q

Humidity = uk9

Barometer = vGZ

Take note of those values, you will need them later.

Step 6: Install the Tinkerforge MQTT Proxy

Thanks to “rwblinn” from the Tinkerforge forum

Open putty with ip 192.168.1.20 with pi and raspberry or your password if you changed it

sudo apt-get install python3-pip
sudo apt-get install python-pip
sudo pip install tinkerforge paho-mqtt
sudo apt-get install mosquitto mosquitto-clients python-mosquitto
sudo apt-get install libusb-1.0-0 libudev0 pm-utils
mkdir /home/pi/tinkerforge             
cd /home/pi/tinkerforge 
wget "http://download.tinkerforge.com/tools/brickd/linux/brickd_linux_latest_armhf.deb 
sudo dpkg -i brickd_linux_latest_armhf.deb
rm brickd_linux_latest_armhf.deb     #delete the file brickd_linux_latest_armhf.deb:  
wget https://raw.githubusercontent.com/Tinkerforge/brick-mqtt-proxy/master/brick-mqtt-proxy.py

This last command is to manually start tinkerforge MQTT proxy.

python brick-mqtt-proxy.py --brickd-host 192.168.1.41 --brickd-port 4223 --broker-host localhost --broker-port 1883 --update-interval 5& 

info about this command:

* my brick IP is 192.168.1.41, replace it with your brick address

* the interval 5 is the number of seconds between 2 mqtt messages, replace 5 with your interval, but do not forget the & at the end. Otherwise you could have problems.

*You can replace localhost with ip rapsberry broker/server

*Type sudo python brick-mqtt-proxy.py –help for more info

This last command has to be rerun in the /home/pi/tinkerforge directory of your PI after every reboot. To run automatically after every reboot, add this line with your values at the end of the existing crontab file with:

sudo crontab -e
@reboot python /home/pi/tinkerforge/brick-mqtt-proxy.py --brickd-host 192.168.1.41 --brickd-port 4223 --broker-host localhost --broker-port 1883 --update-interval 5&

Save the file with Ctrl x , y for yes and enter to confirm.

Check if broker is running:

You can check if your broker is running by for instance installing mqttfx (http://mqttfx.jfx4ee.org/index.php/download) on your pc.

Connect to your raspberry. Give a name to the profile, broker IP of the pi and port 1883 (default value used in the command line above).

Leave the rest as is

Step 7: Raspberry Connect Screenshot

Then subscribe to your sensor(s). In my case to the temperature, barometer and humidity.

You can find how to subscribe to a sensor on this page https://www.tinkerforge.com/en/doc/Software/Brick_MQTT_Proxy.html , in the Topic and payload structure paragraphe:

The proxy publishes retained messages about value and configuration changes of supported devices on topics with the following pattern:

tinkerforge/ <prefix>/<UID>/<suffix>

For example, for a Temperature Bricklet with UID XYZ the temperature value is published on:

tinkerforge/bricklet/temperature/XYZ/temperature

You can find the prefix and the suffix of your bricklet on the same page in the supported device topic paragraph and his corresponding UID at the end of the step 2 paragraph of this document.

Subscribe to your bricklet(s) with the tinkerforge/bricklet/your blicklet prefix/his UID/one of the suffix

You can see the result in the right down corner

Step 8: Subscribe Screenshot

Step 9: Connecting to the CAYENNE IOT Dashboard

With a lot of thanks to “Adam S” of the Cayenne forum for his help.

First of all GET STARTED or SIGN IN on the Cayenne IOT page https://mydevices.com/

Once on the blank dashboard, add your raspberry pi with Add new, device/widget and chose Raspberry

Step 10: Add Device Screenshot

You will receive two option to connect your Raspberry. I choose the second as I already have a SSH connection to my PI.

Step 11: Add Raspberry

Copy and paste the two commands on your putty terminal.

REMARQUE: If you format your PI for any raison, you have to delete the formatted PI on your dashboard and redo the procedure.

The two commands line change with every new Pi.

You can edit some of your Raspberry Pi for ease of use by clicking on the setting “wheel” in the right corner

Step 12: Screenshot PI Details in Cayenne

This is also the place you can remove an old Raspberry with the Remove Device button.

Now you have to connect the MQTT sensor to the dashboard. For this you will need to add a new device/widget a “Bring Your Own Thing”

Step 13: Screenshot Bring Your Own Thing

You receive a new page:

Step 14: Screenshot Connect Your Device

With a very long unique MQTT username shorted to XXX, MQTT password shorted to YYY and Client ID shorted to ZZZ for the rest of this document.

You will need the reel values in the cayenne-mqtt.py script later.

You can also edit the Name of your Device to a suitable name; I use Tinkerforge

REMARK: if you do not see this page after clicking on the Bring Your Own Device but a continuous turning wheel, use another browser.

I had some problems with IE11 and no problems with Chrome.

Step 15: Python Script to Publish MQTT

Now add a python script on your Pi to publish the MQTT values to the cayenne dashboard.

Here is the code (download the file) that Adam S (many thanks) from the Cayenne forum made for my arrangement

I use notepad++ to edit the file on my pc

Make the necessary changes by replacing XXX, YYY, ZZZ with the reel values you have on your “connect your device” cayenne page.

Adapt the code to other or extra sensors if needed..

Step 16: Save Scrypt

Save the file as cayenne-mqtt.py.

I used WinSCP (https://winscp.net/eng/index.php) to copy the file from my pc to the tinkerforge directory on my Raspberry PI.

Via putty terminal to the raspberry, go to the tinkerforge directory and run the cayenne-mqtt.py

cd /home/pi/tinkerforge 
python cayenne-mqtt.py

Go to you cayenne dashboard and you should see, in my case, 3 new widgets temperature, humidity and airpressure.

Step 17: Cayenne Dashboard With Sensors

You can customize the page with the “setting wheels”

If you have problems with the new widgets, hit ctrl+f5, try logging out and back in, or delete and re-add the widget.

REMARK: you have to retype the

cd /home/pi/tinkerforge 
python cayenne-mqtt.py

commands every time you reboot your PI.

To have it start automatically at every reboot you have to modify the crontab file.

Hope this document can help you.

If you have problems please contact the Tinkerforge or Cayenne IOT forums who helped me realize this project.

Without them I could not have done it alone.

Success.