Tranceiver code

Here is the current Arduino code for my transceiver. It uses the virtual wire library to receive data from the room sensors and the energy monitor. If controls LightwaveRF sockets directly using the send function (not using virtual wire). The byte arrays, id1on etc., switch my three LightWaveRF sockets on and off. I am emulating a remote control which is already paired with the sockets. I obtained the data packets using the RF sniffer that I made, feeding the input into Audacity on the Line In of a sound card on on a PC. The bit patterns are quite easy to detect.


#include <VirtualWire.h>

int txPin=3;
int rxPin=2;

byte id1on[] = {0xf6,0xf6,0xf6,0xee,0x6f,0xeb,0xbe,0xed,0xb7,0x7b};
byte id1off[] = {0xf6,0xf6,0xf6,0xf6,0x6f,0xeb,0xbe,0xed,0xb7,0x7b};
byte id2on[] = {0xf6,0xf6,0xee,0xee,0x6f,0xeb,0xbe,0xed,0xb7,0x7b};
byte id2off[] = {0xf6,0xf6,0xee,0xf6,0x6f,0xeb,0xbe,0xed,0xb7,0x7b};
byte id3on[] = {0xf6,0xf6,0xed,0xee,0x6f,0xeb,0xbe,0xed,0xb7,0x7b};
byte id3off[] = {0xf6,0xf6,0xed,0xf6,0x6f,0xeb,0xbe,0xed,0xb7,0x7b};
int bitDelay = 636; 

void setup()
{
  Serial.begin(9600);

  vw_set_rx_pin(rxPin);
  vw_set_tx_pin(txPin);
  vw_setup(2000);                 //Bits per sec
  vw_rx_start();                  // Start the receiver
}

void loop()
{
  char tvon[] = {100,0}; // Turn on TV
  char code[] = {0,0};
  
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  
  if (Serial.available()) {
    unsigned char c = Serial.read();
    
    /*if (c == '1') {
      //Serial.println("Sending TV on message");
      vw_send((uint8_t *)tvon, strlen(tvon));
      vw_wait_tx(); // Wait for message to finish
    } else if (c == 'f') {
      Serial.println("Turning socket on");
      send(id1on);
    } else if (c == 'o') {
      Serial.println("Turning socket off");
      send(id1off);
    } else if (c == 'p') {
      Serial.println("Turning photo frame on");
      send(id2on);
    } else if (c == 'q') {
      Serial.println("Turning photo frame off");
      send(id2off);
    } else if (c == 'c') {
      Serial.println("Turning camera on");
      send(id3on);
    } else if (c == 'd') {
      Serial.println("Turning camera off");
      send(id3off);
    } else */
    if (c < 240) {
      //Serial.print("Sending TiVo code:");
      code[0] = c;
      vw_send((uint8_t *)code, strlen(code));
      vw_wait_tx(); // Wait for message to finish   
    } else {
      if (c == 240) send(id1on);
      else if (c == 241) send(id1off);
      else if (c == 242) send(id2on);
      else if (c == 243) send(id2off);
      else if (c == 244) send(id3on);
      else if (c == 245) send(id3off);
    }
  }
  
  if (vw_get_message(buf, &buflen)) // check to see if anything has been received
  {
    // Message with a good checksum received.  
    if (buf[0] < 100) { // Sensor data
      for (byte i = 0; i < buflen; i++)
      {
        Serial.write(buf[i]);
      }
    } 
  }
}

void sendBit(byte b) {
  digitalWrite(txPin,b);
  delayMicroseconds(bitDelay/2);
  digitalWrite(txPin,LOW);
  delayMicroseconds(bitDelay/2);
}

void RF_tx(uint8_t b)                 // this function writes the serial MSB first
{
  byte mask;
  //Serial.print("Sending byte: ");
  //Serial.println(b);

  sendBit(HIGH);

  for (mask = B10000000; mask; mask >>= 1) {
    sendBit(b & mask);
  }
}


void send(byte *id) {
  cli();
  for(byte j=0;j<12;j++) {
    // send a start bit
    sendBit(HIGH);
    
    for(byte i=0;i<10;i++) RF_tx(id[i]);
    
    // send end bit
    sendBit(HIGH);
    delayMicroseconds(10000);
  } 
  sei();  
}

Advertisements
This entry was posted in Electronics, Home automation. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s