library 434 MHz wireless control power socket (non-dimmable only)

Hi guy,

recently I came to play around with these power sockets, you get to buy everywhere for little money.
They communicate over 434Mhz wireless.
I followed a tutorial by Dominik Krupke http://www.dserv01.de/howtos/funksteckdose-fernsteuern-mit-arduino/
.
It just worked out fine. (The non-dimmable sockets only!!! The dimmable ones operate with another protocol!)
For wiring up the sender, follow his instructions! Its is very straight forward.
Alternatively, use this datasheet: http://www.sparkfun.com/datasheets/Wireless/General/MO-SAWR.pdf

On the basis of his tutorial and encrypted communication protocol I wrote a library for easy application in Arduino.

I hope, it helps you as much as it helped me!

Here is the header:

// library class header to control a wireless power socket
// set the binary encoding for identifier
// build control-stream for A, B, C, D: on and off

//use sender module http://www.sparkfun.com/datasheets/Wireless/General/MO-SAWR.pdf

// object instance call generates a power-socket object, taking the arguments VCC-pin, DATA-pin and a 5-bit indentifier prefix (32 different variations possible)
// library written by Paul Naethe, modified from original code by Dominik Krupke http://www.dserv01.de/howtos/funksteckdose-fernsteuern-mit-arduino/
// V 00.140.B  June 18th, 2013

#ifndef power_socket_h
#define power_socket_h

#include "Arduino.h"


class power_socket{
  
  private:
    void wait(int time);
    void sendByte(char input); //takes bit stream as a String, created from 5-bit indentifier prefix, channel and operation's mode postfix
    boolean sendCode(String code);
    void setChannel(char Channel);
    String _suffix;
    String _prefix;
    int rc_pin;
    int _vcc;        //
    int _overload;  //switches states for overloaded operation mode(s)
  
  public:
    power_socket(int VCC, int DATA, String prefix); //VCC and DATA pins, prefix a five-bit string e.g. 01001, as set at the socket.
    power_socket(int DATA, String prefix);          // overload constructor for permanent power supply on the VCC-pin, or operates the pin 12
    void init();                                    // needs to be called in the setup routine of the Arduino to set pins and serial connection
    void switchOn(char Channel);                    // takes a channel: A, B, C, D and operates the power socket
    void switchOff(char Channel);                   // takes a channel: A, B, C, D and operates the power socket
} ;
  
#endif

the source:

// library class header to control a wireless power socket
// set the binary encoding for identifier
// build control-stream for A, B, C, D: on and off

//use sender module http://www.sparkfun.com/datasheets/Wireless/General/MO-SAWR.pdf

// object instance call generates a power-socket object, taking the arguments VCC-pin, DATA-pin and a 5-bit indentifier prefix (32 different variations possible)
// library written by Paul Naethe, modified from original code by Dominik Krupke http://www.dserv01.de/howtos/funksteckdose-fernsteuern-mit-arduino/
// V 00.140.B  June 18th, 2013


#include "Arduino.h"
#include "power_socket.h"


power_socket::power_socket(int VCC, int DATA, String prefix){ //Prefix like "10001"

  rc_pin = DATA;
  _vcc = VCC;
  _prefix = prefix;
  _overload = 0;  
}

power_socket::power_socket(int DATA, String prefix){ //Prefix like "10001"
   
  rc_pin = DATA;
  _prefix = prefix; 
 _overload = 1; 
}

void power_socket::init(){
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  Serial.print("sensor connecting... data pin: ");
  pinMode(rc_pin, OUTPUT);
  Serial.println(rc_pin); 
  
  if(_overload == 0){
  Serial.print("power pin: ");
  pinMode(_vcc, OUTPUT);
  Serial.println(_vcc); 
  }
}

void power_socket::switchOn(char Channel){
  setChannel(Channel);
  String output = _prefix + _suffix + "10x" ;
  Serial.println(output);   //debugging
  sendCode(output); 
}

void power_socket::switchOff(char Channel){
  setChannel(Channel);
  String output = _prefix + _suffix + "01x" ;
   Serial.println(output);   //debugging
  sendCode(output);
}

boolean power_socket::sendCode(String code){ //empfange den Code in Form eines Char[]
  
  digitalWrite(13, HIGH);  
  digitalWrite(_vcc, HIGH);
  for(byte z = 0; z<7; z++){ //wiederhole den Code 7x
    for(byte i = 0; i<13; i++){ //ein Code besteht aus 12bits plus "x"
      sendByte(code[i]);
    }
  //sendByte('x'); //da der code immer mit x/sync abschliesst, brauchen wir den nicht im code und haengen es automatisch immer hinten ran.
  }
  digitalWrite(_vcc, LOW);
  digitalWrite(13, LOW);
  return true;
}

  
void power_socket::sendByte(char i) { //Diese Funktion soll 0,1 oder x senden koennen. Wir speichern die gewuenschte Ausgabe in der Variabel i

  switch(i){ //nun gucken wir was i ist
  case '0':{ //Der Code fuer '0'
    digitalWrite(rc_pin,HIGH);
    wait(1); //da die Pausen x*350us lang sind, machen wir daraus eine Funktion
    digitalWrite(rc_pin,LOW);
    wait(3);
    digitalWrite(rc_pin,HIGH);
    wait(3);
    digitalWrite(rc_pin,LOW);
    wait(1);
    return;
  }
  case '1':{ //Der Code fuer '1'
    digitalWrite(rc_pin,HIGH);
    wait(1);
    digitalWrite(rc_pin,LOW);
    wait(3);
    digitalWrite(rc_pin,HIGH);
    wait(1);
    digitalWrite(rc_pin,LOW);
    wait(3);
    return;
  }
  case 'x':{ //Der Code fuer x(sync)
    digitalWrite(rc_pin,HIGH);
    wait(1);
    digitalWrite(rc_pin,LOW);
    wait(31);
  }
 
}

}
 
void power_socket::wait(int x) {
  delayMicroseconds(x*350); //warte x*350us
}

void power_socket::setChannel(char Channel){
  switch (Channel){
    case 'A':{
      _suffix = "10000";
      break;
    }
    case 'B':{
      _suffix = "01000";
      break;
    }
    case 'C':{
      _suffix = "00100";
      break;
    }
    case 'D':{
      _suffix = "00010";
      break;
    }
    default: {
      _suffix = "00000";
      Serial.println("ERROR: no valid Channel: should be 'A', 'B', 'C' or 'D'.");
   }
  }
}

And an example:

/*Arduio skech demonstrtating the power_socket library,
switching on the socket everytime the Arduiono gets powered.

*************************************
V1.2, June 18th, 2013, by Paul Naethe
*************************************/



#include "power_socket.h"

char channel = 'C';
String identifier = "00000";

power_socket Socket(11, 12, identifier); //assigning VCC-pin(11) and DATA-pin(12)

enum State {OFF, ON} state;  //creating state-variable

void setup(){
  Socket.init();
 
  Socket.switchOn(channel);
  state = ON;
}

void loop(){
}

Cheers!