Markdown source

# CM3-home - OpenHAB getting started 

<abstract>
During the development and test phases of the [CM3-Home]((https://www.acmesystems.it/CM3-HOME)), the **[Tanzolab Group](http://www.tanzolab.it/)** wrote many practical articles about the different interfaces used on the board, reporting links, experiences and suggestions acknowledged meanwhile. In this article they are collected in order to achieve a "real world" tutorials archive about using CM3-Home with [OpenHAB](https://docs.openhab.org/installation/openhabian.html).
</abstract>

* [CM3-Home features](https://www.acmesystems.it/CM3-HOME)
* [CM3-Home schematic (c) Acme Systems srl](http://www.tanzolab.it/CM3-HOME_schematic)
* [Report on Raspberry CM3 e CM3L modules](http://www.tanzolab.it/cm3)
* [Other Home Automation Software and board similar to CM3-Home](http://tanzolab.it/CM3-HOME_links)

##OpenHABian Installation
For a comprehensive tutorial about OpneHABian installation please follow the instructions on the [official web site](https://docs.openhab.org/installation/openhabian.html)

[Basic installation download on RPI and compatible boards](http://docs.openhab.org/installation/openhabian.html#raspberry-pi)

*ATTENTION* all the installation procedures are really long lasting, please be patient.

Once installed OpenHABian following the instructions, you need to proceed with customizations and add-ons installation.

Many operations are eased using the OpenHABian environment equivalent to raspi-config.
<pre class="terminal">
sudo openhabian-config
</pre>

The CM3-Home serial ports must be prepared to be used in OpenHAB, freeing them from standard use:

* 30 | System Setting
	* 35 | Serial Port 
		* [*] 1  (RPi) Disable serial console 
        * [*] 2  (RPi3) Disable Bluetooth module
		* [*] 3  Add common serial ports to openHAB JVM 
	 
	
use menu items according to your needs

* 30 | System Setting
	* to change password, host name, WiFi, etc.
* 01 | Update
* 02 | Upgrade System
* 20 | Install optional add-ons
	* Mosquitto
	* Grafana
	* knxd

In order to verify the configuration you can use the karaf console. Let's take the knx binding as an example.

<pre class="terminal">
[12:44:08] openhabian@RasPIguiott:~$ ssh -p 8101 openhab@localhost
Password authentication
Password: 

                          __  _____    ____      
  ____  ____  ___  ____  / / / /   |  / __ )     
 / __ \/ __ \/ _ \/ __ \/ /_/ / /| | / __  | 
/ /_/ / /_/ /  __/ / / / __  / ___ |/ /_/ /      
\____/ .___/\___/_/ /_/_/ /_/_/  |_/_____/     
    /_/                        2.1.0
                               - release build -   

Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '<ctrl-d>' or type 'system:shutdown' or 'logout' to shutdown openHAB.

openhab> config:list "(service.pid=org.openhab.knx)"
----------------------------------------------------------------
Pid:            org.openhab.knx
BundleLocation: mvn:org.openhab.binding/org.openhab.binding.knx/1.10.0
Properties:
   localIp = localhost
   service.pid = org.openhab.knx
   type = ROUTER
openhab> 
</pre>

In case of problems it can be useful to increase debug level to check for possible configuration errors:

<pre class="terminal">
openhab> log:set debug tuwien.auto.calimero
</pre>

or enable the trace

<pre class="terminal">
log:set TRACE org.openhab.binding.knx
log:set TRACE tuwien.auto.calimero
</pre>

It is advisable to disable them when the debug is over:

<pre class="terminal">
log:set DEFAULT org.openhab.binding.knx
log:set DEFAULT tuwien.auto.calimero
</pre>

All the useful information can be read in:

<pre class="terminal">
/var/log/openhab2/event
/var/log/openhab2/openhab.log
</pre>

Or with the console command 
log:tail

or in the browser:
[http://cm3home.local:9001/](http://cm3home.local:9001/)


The configuration is dynamically loaded as soon as the cfg file is saved (/etc/openhab2/services/knx.cfg in the example). 
It could happen that the previous configurations are cached even at service restart. In that case clean up the cache before restarting the service:

<pre class="terminal">
openhab> config:delete org.openhab.knx

openhabian@RasPIguiott:~$ sudo systemctl restart openhab2
</pre>

There are some little differences between CM3-Home and Raspberry PI 3 because of the greater hardware equipment. A comprehensive guide to all the peripherals and how to use them, can be found at [CM3-Home main page](https://www.acmesystems.it/CM3-HOME)

To use all of the available hardware you have to:

* Assign the serial debug port (UART1) to GPIO32-GPIO33 instead of GPIO14-GPIO15
* [Assign the RGB LED GPIOs ](https://www.acmesystems.it/CM3-HOME_rgbled)
* Change the audio PWM GPIOs
* Assign the 1-wire line
* Assign the IR reciver port to GPIO 20
* If needed, assign the J3 socket to SPI display
* Set up the I2C port
* Avoid to wait for HDMI device attached at boot (save more than 10sec at startup)

To do that, the following instructions must be added to the /boot/config.txt file:

<pre class="terminal">
#Serial port to GPIO 32 and 33
force_turbo=1
dtoverlay=uart1,txd1_pin=32,rxd1_pin=33

#CM3-Home led definition
dtoverlay=cm3-home-leds

#Enable PWM on GPIO 40 and 41 for the audio out
dtoverlay=pwm-2chan,pin=40,func=4,pin2=41,func2=4

# Enable the 1-wire bus
dtoverlay=w1-gpio,gpiopin=16

#Enable the IR Linux driver
dtoverlay=lirc-rpi,gpio_in_pin=20

dtparam=spi=on
dtoverlay=rpi-display,speed=32000000,rotate=270
#dtoverlay=ads7846,cs=0,penirq=34,speed=10000, swapxy=0, pmax=255, xohms=60, xmin=200, xmax=3900, ymin=200, ymax=3900

#I2C on Grove socket
#(https://github.com/raspberrypi/linux/blob/rpi-4.9.y/arch/arm/boot/dts/overlays/README#L783)
dtparam=i2c_arm=on
dtparam=i2c_arm_baudrate=400000
dtoverlay=i2c1-bcm2708,sda1_pin=44,scl1_pin=45,pin_func=6

#Avoid waiting for HDMI device attached at boot (save more than 10sec at startup)
hdmi_ignore_hotplug=1
</pre>

Take a look also at [Create a Raspbian Stretch microSD](https://www.acmesystems.it/CM3-HOME_microsd) article.

To assign system functions to RGB LED you must create an overlay [Using this tutorial](https://www.acmesystems.it/CM3-HOME_rgbled)

the already prepared overlays are available:

* [dts](./cm3-home-leds.dts)
* [dtbo](cm3-home-leds.dtbo)

To redirect the system console to the debug port you must edit also the file /boot/cmdline.txt adding the related instructions:

<pre class="terminal">
dwc_otg.lpm_enable=0 enable_uart=1 dtoverlay=pi3-disable-bt console=serial1,115200 root=PARTUUID=75a5cf8a-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
</pre>

##Integration with directly attachable hardware interfaces examples

* [KNX integration via TPUART (TP-Bus)](http://www.tanzolab.it/openhab_knx)
The open source [knxd](https://github.com/knxd/knxd) service is used for this purpose. The demon has a permanent handshake with TPUART, it starts with discovery and proced with a periodic heartbeat. The KNX circuitry and TPUART itself are powered up by the external bus. In order to guarantee the correct operation, the KNX bus must be connected and powered before starting the CM3-Home.
* Two possible utilizations for the available RS485 interfaces
	* [Modbus](http://www.tanzolab.it/openhab_modbus)
	* [DMX-512](http://www.tanzolab.it/dmx)
* In the case below, the opto-isolated inputs and the relays are driven through the OpenHAB GPIO binding. This binding has be written for the OpenHAB 1, the OpenHAB 2 compatibility could create some problems, please follow the [official documentations](http://docs.openhab.org/addons/bindings/gpio1/readme.html)
    * [GPIO](http://www.tanzolab.it/openhab_gpio)
    * [GPIO example](http://tanzolab.it/openhab_gpio_example)
* [Execute command line operations](http://www.tanzolab.it/openhab_executeCommandLine)
* [DALI (Light-bus)](http://www.tanzolab.it/openhab_dali)
* As an example for the serial port, a simple connection with an Arduino board has been used as an example.
	* [Serial binding configuration](http://www.tanzolab.it/openhab_serial)
	* [GSM/GPRS communication](http://tanzolab.it/openhab_gsm)
	* Please verify that the openhab user is included in dialout group.

<pre class="terminal">
sudo usermod -a -G dialout openhab openhabian
</pre>
	
    
* [1-wire connection example using a temperature sensor](http://www.tanzolab.it/openhab_onewire)
* [Using the CM3-Home internal IR receiver with an IR remote control](http://www.tanzolab.it/openhab_ir)
* [Notes on LIRC, another way to use a standard remote control with IR receiver](http://tanzolab.it/lirc)
* [Using the audio out](http://www.tanzolab.it/openhab_audio)
* [SPI display on J3](http://www.tanzolab.it/CM3-HOME_spi_display)
* [Using the internal YARM D21 module](https://www.acmesystems.it/yarmcm3test)
* [Using the CM3-Home FULL analog input](http://www.tanzolab.it/openhab_analogin)

##Integration with services available through WiFi or LAN

* [Using MQTT with OpenHAB](http://www.tanzolab.it/openhab_mqtt)
* [SONOFF TH16 integration tests](http://www.tanzolab.it/openhab_sonoff)
* [SONOFF code examples](http://www.tanzolab.it/CM3-HOME_sonoff)
* [Telegram integration](http://www.tanzolab.it/openhab_telegram)
* [IKEA Tradfri lamps integration](http://www.tanzolab.it/tradfri)
* [Weather binding configuration example](http://www.tanzolab.it/openhab_weather)
* [Widgets management in OpenHab](http://www.tanzolab.it/openhab_widget)
* [How to realize dinamyc charts with OpenHAB items](http://tanzolab.it/openhab_grafici)
* [Controlling appliances through IR communication using Broadlink RM mini 3](http://tanzolab.it/openhab_broadlink)
* [Measuring environment parameters with Netatmo sensors](http://www.tanzolab.it/openhab_netatmo)
* [Manage the internet router](http://tanzolab.it/openhab_speedtest)
* [Amazon Dash Buttons integration](http://tanzolab.it/openhab_dash-button)

##Automatic configurations backup
[How to setup backup procedures](http://www.tanzolab.it/openhab_backup)

##Setting-up WiFi module as an Access Point

For the details follow the [Andrea Montefusco presentation](https://docs.google.com/presentation/d/1jEVEeuFrS9mTMH9Ry9aRXdGOLEdC6woTip3wJyqIKMs/edit#slide=id.p)

After hostapd service has been installed, the automatic startup of the service must be disabled and managed through /etc/rc.local so that each service will be launched only when the device is ready.  The dhcpcd service as well must be disabled. So, add the following instructions to rc.local:

<pre class="terminal">
# WiFi module AP mode, bridge configuration with eth0 and uap0(wlan)
#

echo 37 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio37/direction                                   
echo 1 > /sys/class/gpio/gpio37/value

sleep 1

ifup eth0
ifconfig eth0 up

ifconfig

rfkill list

ifup wlan0
ifconfig wlan0 up

ifconfig

brctl addbr br0
brctl addif br0 eth0

ifconfig br0 up

sleep 1

ifup br0

sleep 1

route add -net 0.0.0.0/0 gw 192.168.1.1


iw dev wlan0 interface add uap0 type __ap
ifup uap0

hostapd /etc/hostapd/hostapd.conf &

exit 0
</pre>

Add the following instructions to /etc/dhcpcd.conf configuration file if a static IP is needed:

<pre class="terminal">
interface eth0
static ip_address=192.168.1.2
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
</pre>

<a name="cloud"></a>
## Mobile App

To use all the CM3-Home OpenHAB features on mobile devices even when not in the same domestic network, it can be used the open cloud service offered by [OpenHAB](https://myopenhab.org/) and the corresponding app available for both  [iOS](https://itunes.apple.com/it/app/openhab/id492054521?mt=8) and [Android](https://play.google.com/store/apps/details?id=org.openhab.habdroid)

After installing the openHAB Cloud Connector  

<img src="./OHcloud.png" class="img-responsive center-block" width="40%"/>

the following files will be available:

<pre class="terminal">
/var/lib/openhab2/uuid
/var/lib/openhab2/openhabcloud/secret
</pre>

The files content is needed to setup the cloud service. A complete tutorial about installation and configuration is available on [Youtube](https://youtu.be/joz5f4ejJVc).

The same functions available on local UI can be used also on mobile device once connected with the cloud service.

<img src="./AppOH.png" class="img-responsive center-block" width="25%"/>

## Display
How to use an external, dedicated [panel display](https://www.acmesystems.it/CM3-PANEL) to interact with CM3-Home through HABpanel UI.

Configuration:

how to launch the HABpanel at startup.

In the display user home directory configuration file:
<pre class="terminal">
~/.config/lxsession/LXDE-pi/autostart
</pre>

add 

<pre class="terminal">
chromium-browser --kiosk  http://192.168.1.2:8080/habpanel/index.html#/
</pre>

with the CM3-Home IP address.

To disable screen saver and cursor in the browser, in file

<pre class="terminal">
/etc/lightdm/lightdm.conf
</pre>

un-comment the Xserver line es add the needed parameters:

<pre class="terminal">
[SeatDefaults]
xserver-command=X -s 0 -dpms -nocursor
</pre>

[To disable the low battery icon](https://www.raspberrypi.org/forums/viewtopic.php?t=82373)

add the instruction avoid_warnings=1 at /boot/config.txt



@include='bio_guido_ottaviani'