Building a door switch using Domoticz, X10RF and Arduino

Today, I built a door switch for Domoticz which checks if a certain door in my house is opened or closed.

I already have a combination of a Raspberry Pi running Domoticz and an RFLink Gateway which is listening using a 433Mhz transmitter. So I was suggested to look at the X10 protocol. This protocol is already supported by Domoticz and just sending the state of a switch is simple with this protocol.

I also already had a handful of 433MHz transmitters and receivers, bought in the past. So first I had to check which ones were the transmitters 🙂

fu4ujyahm8dg3q3-medium

And I have these nice micro switches with a roller at the tip of the lever, perfect for the job!

Then I found this Arduino library which makes it possible to send X10 messages using Arduino and 433MHz transmitters. Just create a Zip file with the source code from GIT and import it into your Arduino IDE.

I attached the ATAD (this is actually reading DATA) pin (on the left side) to pin number 12, GND to Ground and VCC to 5 Volt.

And I attached two wires to the microswitch at these contacts (do you see the one at the bottom?):

switch

 

One wire is attached to Ground and the other is attached to Pin number 6.

The Arduino was loaded with this sketch:

#include <x10rf.h>

#define tx 12 // Pin number for the 433mhz OOK transmitter
#define reps 5 // Number of times that a RF command must be repeated.
#define ledpin 13 //BLUE_LED // Pin for the led that blinks when a command is send. (0 = no blink)

#define BUTTON_PIN 6 // Button

x10rf myx10 = x10rf(tx,ledpin,reps);

void setup()
{
  // initialize the pushbutton pin as an input:
  pinMode(BUTTON_PIN, INPUT);
  digitalWrite(BUTTON_PIN, HIGH); // pull-up

  myx10.begin();
}

boolean handle_button()
{
  int button_pressed = !digitalRead(BUTTON_PIN); // pin low -> pressed
  return button_pressed;
}

void loop()
{
  // read the state of the pushbutton value:
  boolean button_pressed = handle_button();

  if (button_pressed) {
    myx10.x10Switch('D',15,0); //ID is D15, Action = Closed (0) 
  } else {
    myx10.x10Switch('D',15,1); //ID is D15, Action = Open (1)
  } 
  delay(5000); 
} 

As you can see, this simple switch can be used without any resistor by making use of the ‘pull-up’ mechanism. When the switch changes, it is programmed to send a special X10switch command.

Now the switch was put in place (with the door closed, the switch is pressed in) and the rig is running.

I opened my Domoticz website and navigated to the Switches page and then put my site in learning mode:

x10-04

A few seconds later, my switch was detected. I identified it as a contact. Domoticz will check the X10 protocol whether the door is ‘open’ or ‘closed’:

x10-02

And I repeated this for the door being open. This actually is seen as a ‘second’ switch:

x10-01

So now I have ‘two’ switches and as you can see, the door was open for about five minutes.

A more detailed view is provided by the Setup|Devices page:

x10-03

Here you can see that the X10 protocol tells that the command is ‘Closed’ or ‘Open’.

So this is the bare minimum you have to do to get a door switch integrated with Domoticz.

Now it’s up to you to think about what to do it the switch is toggled in Domoticz. What notifications could be sent?

Pro tip

It’s good to know the X10 protocol (and the x10rf library) supports much more commands. These are some of them:

  • myx10.RFXmeter(12,0,123457); // Send ‘123457’ as meter reading. = ID = 12
  • myx10.RFXsensor(1,’t’,’T’,21); //Send temperature 21c (two decimals.) ID = 1
  • myx10.RFXsensor(5,’v’,’t’,21); //Send voltage = ID = 5
  • myx10.x10Security(3,0xCC); // Send state ‘Normal + Tamper’ ID =3
  • myx10.x10Security(9,0xF0); // Send state ‘Darkness detected’ ID =9
  • myx10.x10Switch(‘B’,4, 1); // Switch B4 on