Input / Output

Using the GPIO lines

This article illustrates how to manage the Rasberry Pi input/output (GPIO) lines from openHAB. Four
of these lines are available to the user in form of dry contact inputs and relays.

Dry contact input terminals

CM3-Home board has two dry-contact inputs designed to get the state of mechanical contacts like switches or push-buttons.
The contacts are available on screw terminals with a return common line as shown below:

The input lines are opto-isolated from the CPU side and are in the same “electrical domain”
of the RS485 ports.

The GPIOs used are:

  • GPIO 28: Left side contact #1. When closed the GPIO level is low
  • GPIO 29: Right side contact #2. When closed the GPIO level is low
Relay output terminals

Two relays are available on the screw terminals as shown below:

  •  Normally-Open and Normally-Closed contacts available on screw terminals
  •  Max rated: 24 VAC/DC @ 1A
  •  Snubbers already on-board to increase reliability with inductive loads

The GPIO used are:

  • RL1 (left relay) GPIO21
  • RL2 (right relay) GPIO22
Manage the input/output lines via openHAB

Some usage example of the GPIOs directly driving them with the user interface.

The devices controlled by the GPIO binding are automatically driven by some rules in certain conditions.

Through the user interfaces the automatic commands can be bypassed.

Habpanel

Classic UI or mobile App
Basic UI

GPIO

How to use the CM3-Home GPIOs

The opto-isolated inputs, the relays and the RGB led are driven through the OpenHAB GPIO binding. The WiFi module too can be switched on and off with OpenHAB items.

This binding has be written for the OpenHAB 1, the OpenHAB 2 compatibility could create some problems, please follow the GPIO binding documentations

After installing the GPIO binding:


Define the items to use them in user interfaces and in rules:
items/gpio.items

Switch Rele1 "Relay"  {gpio="pin:21 activelow:no initialValue:low"} 
Switch Rele2 "Relay" {gpio="pin:22 activelow:no initialValue:low"}
Contact Pushbutton_left "Switch 1 [%s]" {gpio="pin:28 debounce:1 activelow:no"}
Contact Pushbutton_right "Switch 2 [%s]" {gpio="pin:29 debounce:1 activelow:no"}
Switch WiFi "WiFi" {gpio="pin:37 activelow:no initialValue:high"}
Switch LedR "Red LED" {gpio="pin:36 activelow:no initialValue:high"}
Switch LedG "Green LED" {gpio="pin:35 activelow:no initialValue:high"}
Switch LedB "Blue LED" {gpio="pin:34 activelow:no initialValue:high"}

To show the items as defined in Classic UI, in Basic UI and in mobile APP define them in the sitemap:
sitemaps/cm3home.sitemap

Frame label="GPIO" 
{
   Text label="Input/Output" icon=poweroutlet
   {
      Switch item=Flash 
      Switch item=Rele2
      Text item=Pushbutton_left
      Text item=Pushbutton_right
      Switch item=WiFi
      Switch item=LedR
      Switch item=LedG 
      Switch item=LedB 
   }
}

In the distribution supplied the GPIO items are configured in some rules to be automatically activated under precise conditions.

Example 1: restart the router in case of internet connection fault
rules/speedtest.rules

.....
rule "Router Restart"
when 
   Item RouterRestart changed
then
   sendCommand(Rele2, ON)
   set_timer = createTimer(now.plusSeconds(5))
   [
      sendCommand(Rele2, OFF)
      RouterRestart.postUpdate(ON)
      set_timer = null
   ] 
end 
.....

Example 2: blink RGB LED in different color to advise on power consumption and switch on a beeper as an alarm
rules/modbus.rules

.....
else if (Power > 3000)
{
   sendCommand(LedR, OFF)
   sendCommand(LedB, ON)
   sendCommand(LedG, ON)
   sendCommand(Rele1, ON)
   set_timer = createTimer(now.plusSeconds(0.1))
   [
      sendCommand(LedR, ON)
      set_timer = null
   ] 
   set_timer = createTimer(now.plusSeconds(1))
   [
      sendCommand(Rele1, OFF)
      set_timer = null
   ] 
}
.....
Links

GPIO binding documentations

Share