Interfacing to EnOcean

Thought I’d share this code that reads data from an EnOcean TCM120 so you can read data from the EnOcean wireless (and batteryless) sensors. Great for remote control and monitoring applications!

// EnOcean reader
// Simple code to read an EnOcean telegram - does not check ID of transmitter at this stage, so will operate from ANY switch

int ledPin=13;
int packet[14]; //used as a rotational buffer
int j=0;

void setup()
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
digitalWrite(ledPin,HIGH);
delay(250);
digitalWrite(ledPin,LOW);
Serial.println(“EnOcean TCM120 reader”);
//send_packet(0xAB,0x48,0,0,0,0,0,0,0,0);
}

void loop()
{
while (Serial.available())
{
// Serial.print(packet[j],HEX); Serial.print(" ");
packet[j++]=Serial.read();
j=j % 14;
if((roly(j)==0xA5) && (roly(j+1)==0x5A))
{
print_packet(j);
if(roly(j+2)==0x0B) //now we have sync bytes for receive packet
{
if((roly(j+3)==5) && (roly(j+4)==80)) digitalWrite(ledPin,HIGH); //switch is pressed ON
if((roly(j+3)==5) && (roly(j+4)==0 )) digitalWrite(ledPin,LOW ); //switch released
}
}
}
}

void print_packet(int j)
{
int i,ch=0;
Serial.print(“ORG:”); Serial.print(roly(j+3),HEX);
Serial.print(" Data:"); for(i=j+4;i<=j+7;i++) { Serial.print(roly(i),HEX); Serial.print(" “); }
Serial.print(“ID:”); for(i=j+8;i<=j+11;i++) { Serial.print(roly(i),HEX); Serial.print(” “); }
Serial.print(“ST:”); Serial.print(roly(j+12),HEX);
Serial.print(” Chk:"); Serial.print(roly(j+13),HEX);
for(i=2;i<13;i++) ch=ch+roly(j+i);
if((ch%0x100)==roly(j+13)) Serial.println(" OK"); else { Serial.print(" “); Serial.print(ch,HEX); Serial.println(” Error"); }
}

void send_packet(int typ,int cmd,int D3,int D2,int D1,int D0,int D_3,int D_2,int D_1,int D_0) //not tested
{
Serial.print(0xA5,BYTE);
Serial.print(0x5A,BYTE);
Serial.print(typ,BYTE);
Serial.print(cmd,BYTE);
Serial.print(D3,BYTE);
Serial.print(D2,BYTE);
Serial.print(D1,BYTE);
Serial.print(D0,BYTE);
Serial.print(D_3,BYTE);
Serial.print(D_2,BYTE);
Serial.print(D_1,BYTE);
Serial.print(D_0,BYTE);
Serial.print(0,BYTE);
Serial.print(0xff & (typ+cmd+D3+D2+D1+D0+D_3+D_2+D_1+D_0));
}

int roly(int n)
{
return packet[n%14];
}