Attach Stiebel Eltron LWZ 504 using USBtin and CAN bus

This is about how to setup the OpenHab Raspberry to get information from a Stiebel Eltron LWZ 504 without buying much to expensive Gateways. To reach that target we will use the CAN bus, that is available at the LWZ 504. We will not use the serial- or USB-Port that is in front of the heating.

This tutorial may also work for other Stiebel Eltron Heatings that have a CAN bus.

Required Hardware

You will need a USB device, that is able to connect to the CAN bus. I use USBtin to connect.
You can get it on Amazon* or directly at the manufacturer. If you have prime, at time of writing Amazon is cheaper (incl. shipping costs), but if you don’t have it, the direct order is cheaper.

You will have to connect or solder a cable to the three PINs of the USBtin and connect the other side to the LWZ 504. I am using a standard Ethernet cable to connect.

USBtin with soldered connection cables

Connection to the CAN Bus interface at the heating

Prepare

beside of updating and upgrading Raspbian and it’s application it is also required to update the kernel and the firmware of the Raspberry. Todo so, execute:

sudo apt-get update
sudo apt-get upgrade
sudo rpi-update
sudo reboot

After the reboot, be sure, that the USBtin is not connected to the Raspberry right now.
Execute the following commands to load required kernel modules.

sudo modprobe can
sudo modprobe can-raw
sudo modprobe slcan

Now plug in your USBtin and execute

tail /var/log/kern.log

you should see logs, that a USB device, the USBtin, was connected and in the last line you see the logical interface name.

That will be the interface, you can use to communicate with the CAN Bus using USBtin.

Now we configure the interface. Clone the CAN utils Github repository and compile it:

cd ~
sudo apt-get install -y git
git clone https://github.com/linux-can/can-utils.git
cd can-utils
make

Now we attach and configure the interface. In case your interface was not ttyACM0, you have to replace the interface name with your value.
Note: the -s1 in the first statement means 20.000 bit as bitrate which seems to be the bitrate Stiebel Eltron uses. -b 11 determinates to use 11 bit based encoding.

sudo ./slcan_attach -f -s1 -b 11 -o /dev/ttyACM0
sudo ./slcand ttyACM0 slcan0
sudo ifconfig slcan0 up

Let’s check the configuration:

Execute

./candump slcan0

In case your CAN bus has any active devices right now, you should see CAN bus message in the output. In case of Stiebel Eltron heatings, there should be at least one package each minute that is transmitting the current time of the heating.

Finally we want that the slcan0 port is created on each reboot. Create a scriptfile:

nano /home/pi/createSlcan.sh

Paste the following content:

#! /bin/sh
sudo /home/pi/can-utils/slcan_attach -f -s1 -b 11 -o /dev/ttyACM0
sudo /home/pi/can-utils/slcand ttyACM0 slcan0
sudo ifconfig slcan0 up
exit 0

Save with Ctrl+O and quit with Ctrl+X.

Now we want to execute that file on each reboot. To do that we add the path the the file int the file /etc/rc.local:

sudo mv /home/pi/createSlcan.sh /etc/init.d

Finally mark the file as executable and set the startup settings:

cd /etc/init.d
sudo chmod 755 createSlcan.sh
sudo update-rc.d createSlcan.sh defaults

On each reboot the file will be executed and connects the canbus to the Raspberry.

Install can2mqtt

To read the USBtin and send information to OpenHab we will use can2mqtt. OpenHab will be able to handle the MQTT messages.

We will download the can2mqtt library to a new folder. Create the folder:

sudo mkdir /etc/can2mqtt

change to that folder:

cd /etc/can2mqtt

Go to https://github.com/Hunv/can2mqtt/releases and get the download link of the latest release. i.e. https://github.com/Hunv/can2mqtt/releases/download/v2/can2mqtt_core_v2.zip

Execute the following commands to download and extract that file:

sudo wget https://github.com/Hunv/can2mqtt/releases/download/v2/can2mqtt_core_v2.zip
sudo unzip ./can2mqtt_core_v2.zip

Now we need to download and install .Net Core 2.2 or later to execute the can2mqtt application. The following part may change because Microsoft changes the .Net Core website very often. First we need to get the downloadlink. Goto https://dotnet.microsoft.com/download/thank-you/dotnet-runtime-2.2.6-linux-arm32-binaries and copy the link address shown (see arrow below). (i.e. https://download.visualstudio.microsoft.com/download/pr/428aaa32-f66c-4847-b845-aa21f90504e4/1cf033db866414997140c2672bd75069/dotnet-runtime-2.2.6-linux-arm.tar.gz):

sudo apt-get -y update
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo add-apt-repository universe
sudo apt-get install apt-transport-https
sudo dpkg --purge packages-microsoft-prod && sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install dotnet-runtime-2.2

Remark: the last command will change for newer versions. Replace the 2.2 with the version you have.

Check .Net Core installation:

dotnet --info

You should see something similar to this:

Configure OpenHab MQTT Broker to receive MQTT messages

This is really easy. Open the PaperUI in OpenHAB and navigate to “Addons” and select the “Misc” tab. Install the “Embedded MQTT Broker” addon. Done.

Register canlogserver as service

Execute this to create a new service definition file using the nano editor:

sudo nano /etc/systemd/system/canlogserver.service

Paste the following to your file:

[Unit]
Description=canlogserver
After=network.target

[Service]
ExecStart=/home/pi/can-utils/canlogserver slcan0
WorkingDirectory=/home/pi/can-utils
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Execute this to reload the systemctl daemon-configs:

sudo systemctl daemon-reload

execute this to start the service and check the status:

sudo systemctl start canlogserver.service
sudo systemctl status canlogserver.service
sudo systemctl enable canlogserver.service

the result should look like this:

Register and configure can2mqtt as service

Execute this to create a new service definition file using the nano editor:

sudo nano /etc/systemd/system/can2mqtt.service

Paste the following to your file and replace the IPs in that ExecStart line. If everything is running on your device, it should be the local IP. Be aware, that the loopback-address (127.0.0.1) does not work!

[Unit]
Description=can2mqtt
After=network.target

[Service]
ExecStart=/usr/local/bin/dotnet /etc/can2mqtt/can2mqtt_core.dll --Daemon:CanServer="192.168.0.10" --Daemon:MqttServer="192.168.0.10" --Daemon:MqttClientId="Can2Mqtt" --Daemon:MqttTopic="Heating" --Daemon:MqttTranslator="StiebelEltron" --Daemon:CanlogserverPath="/home/pi/can-utils/canlogserver" --Daemon:CanlogserverSocket="slcan0" --Daemon:NoUnits="true"
WorkingDirectory=/etc/can2mqtt/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Execute this to reload the systemctl daemon-configs:

sudo systemctl daemon-reload

execute this to start the service and check the status:

sudo systemctl start can2mqtt.service
sudo systemctl status can2mqtt.service
sudo systemctl enable can2mqtt.service

the result should look like this:

Setup Heating thing in OpenHab

comming soon…

Configure CAN requests

To actively request the latest values of the heating and not just waiting for it to send, we can execute the application cansend via Exec Binding. To do so, we need to install the Exec Binding via Paper UI at “Addons” and the “Bindings” tab. Search for “Exec” and install the binding.

After that in the Paper UI navigate to “Configuration” => “Things” and click the blue + to add a new thing manually.

In the list of bindings, select the Exec Binding, followed by selecting “Command” from the next list.

Now we can configure what we need. Enter a good name, in this case for trigger the sending of the outside temperature. Enter the command “/home/pi/can-utils/cansend slcan0 6A2#3100FA000C0000” to make the binding executing the cansend application we installed before with the parameters required to read the outside temperature. Also set the interval to a setting that makes sense. i.e. 10 Minutes (=600 seconds) is very frequent for that value.

Click the blue bubble with the check to save our new thing, that just executes the command every 10 minutes. You will see, that the temperature property we configured for our heating thing before will have a value now!

Remark: If it don’t work, it may be possible, that you have to replace the “6A2” with “680” in each command. This seems to be related to the heating model you have. For Stiebel Eltron LWZ 504 the 6A2 is correct.

To configure the same for other settings, just replace the last part of the command for the following properties:

Outside temperature /home/pi/can-utils/cansend slcan0 6A2#3100FA000C0000
Operational Status /home/pi/can-utils/cansend slcan0 6A2#3100FA01760000
Summer mode? /home/pi/can-utils/cansend slcan0 6A2#3100FA033B0000
Program setting /home/pi/can-utils/cansend slcan0 6A2#3100FA01120000
Return flow temperature /home/pi/can-utils/cansend slcan0 6A2#3100FA00160000
Boiler temperature (to be) /home/pi/can-utils/cansend slcan0 6A2#3100FA00030000
Boiler temperature (current) /home/pi/can-utils/cansend slcan0 6A2#3100FA000E0000
Power usage heating kWh (sum) /home/pi/can-utils/cansend slcan0 6A2#3100FA09200000
Power usage heating MWh (sum) /home/pi/can-utils/cansend slcan0 6A2#3100FA09210000
Power usage heating /home/pi/can-utils/cansend slcan0 6A2#3100FA02CC0000
Power usage heating kWh (today) /home/pi/can-utils/cansend slcan0 6A2#3100FA091F0000
Power usage heating Wh (today) /home/pi/can-utils/cansend slcan0 6A2#3100FA091E0000
Power usage Boiler /home/pi/can-utils/cansend slcan0 6A2#3100FA02CE0000
Power usage warm water kWh (sum) /home/pi/can-utils/cansend slcan0 6A2#3100FA091C0000
Power usage warm water MWh (sum) /home/pi/can-utils/cansend slcan0 6A2#3100FA091D0000
Power usage warm water kWh (today) /home/pi/can-utils/cansend slcan0 6A2#3100FA091B0000
Power usage warm water Wh (today) /home/pi/can-utils/cansend slcan0 6A2#3100FA091A0000
Excavator temperature /home/pi/can-utils/cansend slcan0 6A2#3100FA00140000
Volume flow /home/pi/can-utils/cansend slcan0 6A2#3100FA01DA0000
Flow temperature /home/pi/can-utils/cansend slcan0 6A2#3100FA000F0000
Heat recovery heating kWh (sum) /home/pi/can-utils/cansend slcan0 6A2#3100FA03B10000
Heat recovery heating kWh (today) /home/pi/can-utils/cansend slcan0 6A2#3100FA03B00000
Heat recovery heating MWh (sum) /home/pi/can-utils/cansend slcan0 6A2#3100FA03B60000
Heat recovery heating Wh (today) /home/pi/can-utils/cansend slcan0 6A2#3100FA03AF0000
Room temperature /home/pi/can-utils/cansend slcan0 6A2#3100FA00110000

Bonus: Set the room temperature of the heating

Comming soon…

The heating has a value to read or write the room temperature. By default, if you don’t have an additional console somewhere in the living room, there is the value of 19°C that is used by the heating to calculate the heating level. Using any source that is attached to OpenHab, we can set this value.

In the following example, I use the temperature value my KNX switch in the living room that reports to OpenHab. I am using this value to set the room temperature in the heating using a rule, that is triggered everytime the KNX switch temperature changes. You can use any temperature source you like (i.e. ZigBee devices, weather stations, …).

Right now I am stuck. I have setup everything and writing the value to the heating but it is overwritten after some minutes with 19°C. I haven’t figured out why this is happening and how to change this behaviour. I will report as soon as I have a solution.

Good to know…

I had the problem, that the CAN BUS Id 6A1 is my contol unit (the display) at the heating. This was configured to be terminal ID 4. As long this was the case I had the problem, that this control unit wasn’t working. Doesn’t matter if I use 6A2 or 6A1 for communicating with the Bus. After I changed the terminal ID to 3, the CAN BUS Id of the control Unit also changed to 6A0. After that, everything was working.

References

USBtin setup: https://www.fischl.de/usbtin/linux_can_socketcan/
Elster definitions: http://juerg5524.ch/data/ElsterTable.inc
More IDs: https://elkement.blog/2016/08/24/hacking-my-heat-pump-part-2-logging-energy-values/

 

 

Setup the Raspberry Pi 3B+

Setup the Raspberry Pi 3B+

At the first step you need a RaspberryPi. I recommend to get the latest version that is a Raspberry Pi 3B+ at the moment. If you already have a Raspberry Pi that is an older version that should not be a problem. If you don’t have a RaspberryPi, get one. For example here:

Raspberry Pi 3B+ naked:

Amazon* Raspberry 1373331 Pi 3 Modell B+ Mainboard, 1GB
BuyZero Raspberry pi 3 model b+ (“pi 3b plus”)
Reichelt RASPBERRY PI 3B+ :: Raspberry Pi 3 B+, 4x 1,4 GHz, 1 GB RAM, WLAN, BT

or as a bundle

Amazon* Raspberry Pi 3 Model B+ Bundle “S” (weiß)
BuyZero Raspberry pi 3 model b+ (“pi 3b plus”)
Funk24.net RPi3 B+ – Bundle 16GB, weiß

You will also need a card reader for the SD-Card in case your PC or Laptop doesn’t have one. I am using this cheap one that is connected via USB. Attention, the shipping takes a while!
LogiLink Cardreader USB 2.0 Stick via Amazon*

If you have your Raspberry ready to use, we need the latest image of Raspbian. That is the operating system the Raspberry Pi Foundation made and optimized for the Raspberry Pi.
To get the operatingsystem, open a browser and browse to https://www.raspberrypi.org/. At the navigation bar at the top, you have “Download”. Click on that link.

 

On that page at the top you have the option to download “NOOBS” or “Raspbian”. Click on “Raspbian”.

 

On the next page you have the option to download “Raspbian Stretch with Desktop” or “Raspbian Stretch Lite”. Select “Download ZIP” next to the “Raspbian Stretch with Desktop” to download the image of Raspbian.

 

Save the file on your local PC somewhere where you will find the file. When the download is done, you have to extract the image the downloaded zip-file. To do so, right click the file and select “Extract all..” and click “Extract” at the bottom of the window.

 

After this is done, a window will open with the “2018-06-27-raspbian-stretch.img”-File. Maybe the file is named slightly different in your download. If you have this, the img-file is ready to be copied to the SD Card.

The next step is to format copy the downloaded image to the SD Card. Plug in your card reader with your SD Card in the SD Slot. You also need a Software to copy the image. I recommend “Win32 Disk Imager”. That one is free and easy to use. You can download it here: https://sourceforge.net/projects/win32diskimager/
After the download, please install and start the software. You will see this window:

 

In the first field, select the img file you just extracted and select the SD Card-Reader drive at the right (1). If you have no USB Stick or external HDD connected to your system, the SD-Card Reader should be the only drive that is available (2). After you entered this options, click the “Write”-Button (3).

Finally you will get a success message and the write process is done. You can now remove the SD-Card and/or SD-Card reader from your system and attach the Micro SD Card to the Raspberry Pi.

In the next steps we will start the initial configuration of the Raspberry Pi that is running with Raspbian Stretch. So attach all wires except power (minimum: screen, keyboard and mouse) and be sure the SD Card is well mounted to the Raspberry.

When you plug in the power, you will see a Raspberry Pi Startup screen. After about 1-2 minutes, you will see the desktop and a configuration wizard. In the wizard click “Next” to start the configuration.

 

In the first dialog you have to set the Country, Language and Timezone.

 

In the second dialog you have to specify your password, that will be used for the user “pi” you are using to login to the Raspberry.

 

In the third dialog you have to select the WiFi Network you like to connect to. You can skip this if you are using a wired network connection or you have to select you Wifi and enter the passphrase for the connection.
In the fourth dialog the Raspberry will search for updates. This requires a properly configured network connection with internet access. Click the “Next” button to start searching for updates. The search will take a while depending on the number of available updates. Finally you should see a message, that the “System is up to date”.

On the last dialog page the Raspberry likes to reboot. Click the reboot-button to reboot the Raspberry.
After the reboot you will get a message that Raspbian was updated.

Now we continue with the configuration because the hostname and some services are still missing.
Click the Raspberry Icon at the top left corner and select “Preferences” => “Raspberry Pi Configuration”.

 

On the first “System” tab enter your custom hostname for the Raspberry. You can also leave this default but if you have more than one, you should change this to another name.

 

On the second tab “Interfaces” you should enable “SSH” and “VNC”. SSH is used to connect to the Raspberry via console. VNC is used to connect to the desktop you see right now. Also check if you need to configure to localisation using the “Localization” tab. The default settings (like Timezone) are always for Great Britain and you may want to change it. After you confirm the dialog, you need to restart the Raspberry to apply the new hostname.

 

To connect remotely to the Raspberry we need the IP. On the Raspberry click the black so called “Terminal” icon (like the Command Line on Windows).

 

In the Terminal window type the command “ifconfig” (without the quotes) and press enter. You will see the following output:

We need to search for the IP the Raspberry has. If you are using the wifi connection like in the screenshot, you will find the IP address in the “wlan0”-section just after the “inet” in the line that starts with “inet”. If you are using a wired connection, you will see this information in the section “eth0” at the top.
Note this IP address for future use.

To connect to the Raspberry via console using SSH, we need a SSH client. The most popular client is Putty. You can download it here: https://www.putty.org/
Click on “here” in the “Download PuTTY”-Area.

After this, download the MSI installer in the “Package files” section that fits to your system. After the download run the setup and install Putty.

To start the connection to the Raspberry using Putty, start Putty and enter the IP address of the Raspberry into the field “Host Name” and click on the “Open”-button.

 

On your first connection, you will be asked if you trust the footprint of the system you are connecting to. Simply click on “yes”.

 

After that the console will show “login as:”. Enter “pi” and press enter, because “pi” is the default username for the Raspberry. The next line will ask for the password. Enter your password. Don’t wonder if nothing happens when you hit a key. In Linux (so also in Raspbian) passwords are almost never shown in any kind. Just type your password blind and press enter.

Welcome to the SSH session. You can do the almost same things you can do in the local “Terminal” program using this connection. We will need this later over and over again.

From now on you don’t need the HDMI-, Mouse- and Keyboard-Cable anymore connected to your Raspberry Pi. Everything we will do from now on will be via a SSH connection using Putty.

Optional: Disable Wifi and Bluetooth

In case you just want to use the Ethernet connection and you do not need or want to use Wifi and/or Bluetooth, configure the following settings:

Execute:

sudo nano /boot/config.txt

This in the lower part of the file, you may see a line that starts with dtoverlay= or #dtoverlay=. Below that line add this line to disable Wifi:

dtoverlay=pi3-disable-wifi

to disable Bluetooth, add this line:

dtoverlay=pi3-disable-bt

Press Ctrl+O to write the changes and Ctrl+X to exit the file.
To apply this setting, you need to restart the Raspberry:

sudo reboot

Optional: Configure static IP

To configure a static IP instead of a DHCP address, follow the following steps. Open the config-file for network configuration:

sudo nano /etc/dhcpcd.conf

Go to the section, that starts with “# Example static IP configuration:”. Remove the hash characters at the beginning (except the ip6-line).
In the line starting with “static ip_address” configure the new IP-Address and the subnetmask. For example 192.168.0.10. If you subnet is 255.255.255.0 add /24 to the end. This is usually the case in home configurations.
In the line starting with “static routers” add your Gateway (i.e. the IP of your router).
In the line starting with “static domain_name_servers” add your DNS Servers. This is usually also your router but as a fallback it may be helpful to add another public DNS Server like 1.1.1.1.

Save the file with Ctrl+O and exit the file with Ctrl+X.

To apply this setting, you need to restart the Raspberry:

sudo reboot

 

 

 

Remarks:
Product links marked with * are affiliate links. That means I get some percent of the money you will spend when you are using this links. I’m not trying to influence you. In most cases there are also other sellers for the products where I don’t get money for. I am not trying to influence you to spend more money than you need. If you are using this links, you don’t have to pay anything extra. It is the same price as if you enter the shop on your own.