Arduino X10 Send/Receive Library

This is an upgraded version of the X10 library that incorporates Tom Igoe’s original X10 send code, with Brohogan’s clever X10 receive code.

Place this library in your Arduino libraries folder. It has been tested with Arduino 0018.

V4 Beta X10.zip

This preliminary version is the most minimal integration possible – no attempt has been made to combine similar structures internally for maximal efficiency.

It is not quite upward compatible with the original v3 X10 send library.

The single letter X10 house codes #defines, which caused me lots of problems, have been replaced with #defines like HOUSE_A, HOUSE_B, rather than just A or B.

There are two sets of functions that return house and unit codes: one that returns the largely unintelligible ‘binary’ house and unit codes, which are used for input in the original v3 library, and a new set of easily readable/printable ascii house codes and integer units codes which are handy for echoing to a display and debugging.

The indicator LED pin is now settable.

There are several constructors, one of which is upward compatible with the v3 X10 library which takes just zCross and data output pin, and a set of additional constructors that can take one or both of the input pin and LED pin.

This library can be used for sending only, but because of its generality it is larger than than the v3 library.

One caveat: this library conveniently hides the fact that you must disable interrupts when you send X10. If you start getting gobbledygook Serial I/O or other wonky behavior, try bracketing the sensitive code with detachInterrupt() and attachInterrupt() calls.

It returns version == 4, the current X10 library returns version == 3.

It’s very easy to use (this is a simple program that both receives and sends on D5 when it gets any unit code of 1)

/* Arduino Interface to the PSC05 X10 Receiver. BroHogan 3/24/09
* SETUP: X10 PSC05/TW523 RJ11 to Arduino (timing for 60Hz)
* – RJ11 pin 1 (BLK) -> Pin 2 (Interrupt 0) = Zero Crossing
* – RJ11 pin 2 (RED) -> GND
* – RJ11 pin 3 (GRN) -> Pin 4 = Arduino receive
* – RJ11 pin 4 (YEL) -> Pin 5 = Arduino transmit (via X10 Lib)
* NOTES:
* – Must detach interrupt when transmitting with X10 Lib
* updated to use X10 library code by creatrope 3/24/10
*/

#include “WProgram.h” // this is needed to compile with Rel. 0013
#include // X10 lib is used for transmitting X10
#include // X10 Lib constants
#define RPT_SEND 2

#define ZCROSS_PIN 2 // BLK pin 1 of PSC05
#define RCVE_PIN 4 // GRN pin 3 of PSC05
#define TRANS_PIN 5 // YEL pin 4 of PSC05
#define LED_PIN 13 // for testing

x10 SX10= x10(ZCROSS_PIN,TRANS_PIN,RCVE_PIN,LED_PIN);// set up a x10 library instance:

void setup() {
Serial.begin(9600);
delay(500);
Serial.print(“x10 receive/send test”);
}

// A simple test program that demonstrates integrated send/receive
// prints X10 input, then sets D5 on/off if unit code on input was 1
void loop(){
if (SX10.received()) { // received a new command
SX10.debug(); // print out the received command
SX10.reset(); // clear received bit
if (SX10.unitCode() == 1){
byte cmndCode = SX10.cmndCode();
SX10.write(HOUSE_D,UNIT_5,RPT_SEND);
if(cmndCode == ON) SX10.write(HOUSE_D,ON,RPT_SEND);
if(cmndCode == OFF) SX10.write(HOUSE_D,OFF,RPT_SEND);
}
}
}

You might also be interested in my other Arduino/X10 articles:

Arduino W800RF32 Connection
Arduino on the ATmega644 Wasp
Minimal Arduino Support

Do you need this library at 50MHZ? Brohogan passes along this tip:

* I’ve received some very good feedback from someone who is using this in the 50Hz world. To convert from 60Hz to 50Hz:

1. define OFFSET_DELAY from 500 to 800
2. define HALF_CYCLE_DELAY from 8334 to 10000

* A corresponding change can be made to the X10.lib in X10constants.h:

1. define BIT_DELAY from 1778 to 2133
2. define BIT_LENGTH from 800 to 900