AdSense

Sunday 18 August 2013

"Home Automation" with Arduino and 433 MHz - The shutters, part 2

(Deutsche Version) Now that we now the theory about controlling the shutters, we can use the theory practically.

As I already mentioned, the protocol to control the shutters is similiar to the protocol used to control the power outlets. So I decided to use the already exisiting rc-switch library. Of course I had to make some changes.

Changes in rc-switch

The library comes with three different protocols for three power outlets. They mostly differ in pulse length and the sync bit. I decided to add a fourth protocol.

The first changes had to be made in the function setProtocol():

void RCSwitch::setProtocol(int nProtocol) {
  this->nProtocol = nProtocol;
  if (nProtocol == 1){
    this->setPulseLength(350);
  }
  else if (nProtocol == 2) {
    this->setPulseLength(650);
  }
  else if (nProtocol == 3) {
    this->setPulseLength(100);
  }
  //edit: protocol 4 for shutters with pulse lenth 250µs and 4 repetitions
  else if (nProtocol == 4) {
    this->setPulseLength(250);
    this->setRepeatTransmit(4);
  }
}


I marked all the changes with an //edit comment. I just added a fourth  else-if for protocol 4. If the user chooses protocol 4, the code sets the pulse length to 250µs and calls the function setRepeatTransmit() with the parameter 4. This means that every command has to be sent four times (like the remote control does).

In the next step I had to add a function which allows sending a quad state command. To do so, I extended the sendTriState() function:

//edit: send quad state command
void RCSwitch::sendQuadState(char* sCodeWord) {
  for (int nRepeat=0; nRepeat<nRepeatTransmit; nRepeat++) {
    //sync bit is sent BEFORE transmission!
 this->sendSync(); 
 int i = 0;
    while (sCodeWord[i] != '\0') {
      switch(sCodeWord[i]) {
        case '0':
          this->sendT0();
        break;
        case 'F':
          this->sendTF();
        break;
        case '1':
          this->sendT1();
        break;
        case 'Q':
          this->sendQQ();
        break;
      }
      i++;
    }   
  }
}


A big difference between shutters and power outlets is the sync bit. When "talking" to the power outlets, the bit hast to be sent AFTER sending the commands. The shutter expects the sync bit BEFORE the command. So the function sendSync() is called before sending the command. The next change was adding a fourth case. If the programm has to send a Q, the function sendQQ() is called (this function was also added by me):

/**
 * edit: Sends a Quad-State "Q" Bit
 *            ___   _
 * Waveform: |   |_| |___
 */
void RCSwitch::sendQQ() {
  this->transmit(3,1);
  this->transmit(1,3);
}

The function sends a long HIGH signal by calling transmit(3,1), follows by a short HIGH signal (transmit(1,3)). The first paramter states the duration of the HIGH signal in pulse lengths. The second paramter states the duration of the LOW signal.


The last changes had to be made in the function which calls the sync bit:

void RCSwitch::sendSync() {

    if (this->nProtocol == 1){
        this->transmit(1,31);
    }
    else if (this->nProtocol == 2) {
        this->transmit(1,10);
    }
    else if (this->nProtocol == 3) {
        this->transmit(1,71);
    }
 
    //edit: snyc bit for shutters
    else if (this->nProtocol == 4) {
        this->transmit(18,6);
    }
}


As we know, the sync bit consists of a HIGH phase with a duration of 4500µs (18 pulse lengths) followed by a LOW phase with a duration of 1500µs (6 pulse lengths)


With these little changes, the rc-switch library can be used to control the shutters.

Testing...

Let's test the library!

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  Serial.println("Ready...");
  mySwitch.enableTransmit(10);
  mySwitch.setProtocol(4);
}

void loop() {
  //down
  Serial.println("Down...");
  mySwitch.sendQuadState("0F0F0100QQ0F100F0101");
  mySwitch.sendQuadState("0F0F0100QQ0F100F0110");
  
  delay(5000);
  
  //stop
  Serial.println("Stop...");
  mySwitch.sendQuadState("0F0F0100QQ0F100FFFFF");
  
  delay(1000);
  
  //up
  Serial.println("Up...");
  mySwitch.sendQuadState("0F0F0100QQ0F100F0F0F");
  mySwitch.sendQuadState("0F0F0100QQ0F100F0F1Q");
  
  delay(5000);
  
  //stop
  Serial.println("Stop...");
  mySwitch.sendQuadState("0F0F0100QQ0F100FFFFF");
  
  delay(1000);
}

In the setup() function, the protocol is set to 4. In the loop() function, the new sendQuadState() function is called to send the commands  to move the shutters down, stops them, moves them up and stops them again. How to get these comannds is described in part 1 of this post.

The last step is combining the control of power outlet and shutters to get the desired functionality (power outlet on, move shutters, power outlet off).

1 comment: