Using mpr121 toch sensor in openhab

I am trying to make touch switch with mpr121. It communicates over I2c. I am confused if openhab can take inputs via i2c. Is there any way to make it work.

Hello Arghya_Ghosh

Maybe a bit late, but anyway there is an answer.

Yes I have it working over I2c and it works very well too. I used the python library for reading the information. You can find this lib at

I made copy of the python library to my openhab python lib and made a small modification in this file by adding a function start.
The only change with the begin function is, that it doesn’t reset the device every time you call the function on a interrupt. The polling in the example didn’t work reliable at all. You miss a lot of signals with polling, it is slow and it takes a lot of CPU time. With the interrupt installed, it is very fast, reacts allways with hardly no CPU time.

The change in python lib MPR121 is shown below
The new function start is a copy of the begin function, but without the reset.

  def start(self, address=MPR121_I2CADDR_DEFAULT, i2c=None, **kwargs):
        # Assume we're using platform's default I2C bus if none is specified.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
            # Require repeated start conditions for I2C register reads.  Unfortunately
            # the MPR121 is very sensitive and requires repeated starts to read all
            # the registers.
            I2C.require_repeated_start()
        # Save a reference to the I2C device instance for later communication.
        self._device = i2c.get_i2c_device(address, **kwargs)
        return True  

Then I wrote 2 python programms for initing and reading the touch info.

mpr121init.py

#!/usr/bin/env python
import sys
import time
import MPR121 as MPR121

# Create MPR121 instance.
cap = MPR121.MPR121()

# Initialize communication with MPR121 using default I2C bus of device, and
# default I2C address (0x5A).     
if not cap.begin():      // This is the old function in the library
    print('Error initializing MPR121.  Check your wiring!')
    sys.exit(1)

mpr121read.py

#!/usr/bin/python
import sys
import time
import MPR121 as MPR121

# Create MPR121 instance.
cap = MPR121.MPR121()

# init function in mpr121init.py

if not cap.start():      // this is the new function in the library
   print('Error ininitalizing MPR121. Check your wiring!')
   sys.exit(1)

current_touched = cap.touched()
print (current_touched) 

I connected the mpr121 device to I2C bus and also connected the IRQ output of the mpr121 to an input on the pi

This input will be triggered as you touch one of the inputs 11 in my setup. The name Interrupt is just an item name, not a keyword or so.

Contact Interrupt "Interrupt mpr121 " {gpio="pin:11 debounce:1" }

In the rules a have added the following function

rule "mpr interrupt handler"
when
    Item Interrupt changed from OPEN to CLOSED     // interrupt mpr_121 geset  (= GPIO pin 11) 
then

       var tmp =  new Integer(executeCommandLine("sudo python /usr/share/openhab/python/mpr121read.py", 5000))   //read the string and map it on a tmp var.

     
       var tmp2 =  new DecimalType(tmp)  

       var state = tmp2.toBigDecimal.toBigInteger

        
       if(state.testBit(0))  // for the first input
       {
                // do something you like
       }
     
       if(state.testBit(1)) // for the next etc....
       {
                // do something you like
       }
end

And a rule when the system is started

rule "init the system"
	when 
		System started
	then

    	executeCommandLine("sudo python /usr/share/openhab/python/mpr121init.py", 5000)  // init touchsensor mpr121 

end

don’t forget to change owner of your python files with
sudo chown 777 *.py

Good luck, and if you have any questions, be my guest.

Thanks for your informations.
mpr121_simpletest.py works, but mpr121read.py returns allway 0, also if I start from command line.
I modified MPR121.py and used start.
It looks like using begin function.
What can I do?

1 Like

Did you make a copy of the MPR121 python lib in same directory where the mpr121read.py file is?

Thanks for your quick answer.
Yes I do as described once more. I use the actual openHABian-image on Raspi3.

[10:16:58] openhabian@openHABianPi:/etc/openhab2/scripts$ python /etc/openhab2/scripts/mpr121_simpletest.py
Adafruit MPR121 Capacitive Touch Sensor Test
Press Ctrl-C to quit.
8 touched!
4 touched!
0 touched!
4 released!
8 released!
0 released!
^CTraceback (most recent call last):
  File "/etc/openhab2/scripts/mpr121_simpletest.py", line 73, in <module>
    time.sleep(0.1)
KeyboardInterrupt
[10:43:48] openhabian@openHABianPi:/etc/openhab2/scripts$ sudo python /etc/openhab2/scripts/mpr121init.py
MPR121.py begin
MPR121.py _reset
[10:47:39] openhabian@openHABianPi:/etc/openhab2/scripts$ sudo python /etc/openhab2/scripts/mpr121read.py
MPR121.py start
MPR121.py touched
0
[10:47:54] openhabian@openHABianPi:/etc/openhab2/scripts$ sudo python /etc/openhab2/scripts/mpr121read.py
MPR121.py start
MPR121.py touched
0
[10:48:02] openhabian@openHABianPi:/etc/openhab2/scripts$

Before start read I entered some keys and the interruped changed to LOW after this back to HIGH.

Did you connect the IRQ interrupt line of the mpr121 tot an input of the pi?

All together you need 5 connections between the mpr121 and your pi.

The touch sensor test is based on polling and does not use an interrupt. This will work, but only as long as the program is running. That is the reason why I made it based on an interrupt.

Yes, the interruped works, openHAB-rule starts the python-programm und gets back a 0 also.
Thats, why I disconnect the IRQ-line to openHAB, extend the program with the log-infos and tested at command-line.
It looks like, reset is running, but it doesn’t.
Could it be, that a Raspi 3 or my python work different?

I run it on a raspi 3 with standard python on it.
This you also changed owner of the python files. It needs to read

“-rwxrwxrwx 1 root root 8907 feb 11 2017 MPR121.py”

I don’t changed the owner, but I started as sudo on command-line, so I think, it isn’t a authority-problem.