talking to wireless switches

Hello! This is my first post here.

Anyway, im currently doing a simple home automation project where im controlling some cheap remote mains switches over rf link (433mhz) by reading the datasheet of the chip in the remote and analyzing the output with a soundcard scope (i really have to get a real one) i managed to recreate the codewords (bytes?) with some forloops and then hack the remote to send them, and it works!

The problem is the code, i would really like to implement real serial communication through the serial library if possible to make it simpler, im not shure about the baudrate, and the bits look strange, there are more than just 1s and 0s :slight_smile: i have to learn more about serial communication.

is baudrate = bits/second?
then i suspect it would be 7418 baud if one bit is 1348us

are there floating bits and sync bits in normal serial communication, and how can the sync bit be four times as long as a regular bit?

here is the code if you want to look, it might not be optimal as im quite new to this and its not very well commented but i think its pretty self explanatory:

//the controll messages, 2 = sync bit
int oneOn[]    = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2};
int oneOff[]   = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 2};
int twoOn[]    = {0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 2};
int twoOff[]   = {0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 2};
int threeOn[]  = {0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 2};
int threeOff[] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 2};
int fourOn[]   = {0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 2};
int fourOff[]  = {0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 2};

int period = 326; //actually 337us but digitalWrite takes some time
int rfPin = 13;

//just for controlling it via uart
boolean one = false;
boolean two = false;
boolean three = false;
boolean four = false;

void setup(){
  pinMode(rfPin, OUTPUT);
  Serial.begin(9600);
}
  
void loop(){
  if(Serial.available() > 0){
    char adr = Serial.read();
    Send(adr);
    //Serial.println(adr);
    Serial.print("channel 1   "); if(one == true){Serial.println("on");} else{Serial.println("off");}
    Serial.print("channel 2   "); if(two == true){Serial.println("on");} else{Serial.println("off");}
    Serial.print("channel 3   "); if(three == true){Serial.println("on");} else{Serial.println("off");}
    Serial.print("channel 4   "); if(four == true){Serial.println("on");} else{Serial.println("off");}
  }
}
      
    
void Send(char adr){
  
  if(adr == '1' && one == false){  
    for(int i = 0; i < 5; i++){for(int i = 0; i < 13; i++){rfSend(oneOn[i]);}}
    one = true; 
  }
  else if(adr == '1' && one == true){
    for(int i = 0; i < 5; i++){for(int i = 0; i < 13; i++){rfSend(oneOff[i]);}}
    one = false;   
  }
  if(adr == '2' && two == false){
    for(int i = 0; i < 5; i++){for(int i = 0; i < 13; i++){rfSend(twoOn[i]);}}
    two = true; 
  }
  else if(adr == '2' && two == true){
    for(int i = 0; i < 5; i++){for(int i = 0; i < 13; i++){rfSend(twoOff[i]);}}
    two = false;
  }
  if(adr == '3' && three == false){
    for(int i = 0; i < 5; i++){for(int i = 0; i < 13; i++){rfSend(threeOn[i]);}} 
    three = true;
  }
  else if(adr == '3' && three == true){
    for(int i = 0; i < 5; i++){for(int i = 0; i < 13; i++){rfSend(threeOff[i]);}}
    three = false;
  }
  if(adr == '4' && four == false){
    for(int i = 0; i < 5; i++){for(int i = 0; i < 13; i++){rfSend(fourOn[i]);}} 
    four = true;
  }
  else if(adr == '4' && four == true){
    for(int i = 0; i < 5; i++){for(int i = 0; i < 13; i++){rfSend(fourOff[i]);}}
    four = false;
  }
}


void rfSend(int bit){
  if(bit == 0){
    digitalWrite(rfPin, HIGH);
    delayMicroseconds(period);
    digitalWrite(rfPin, LOW);
    delayMicroseconds(period*3);
    
    digitalWrite(rfPin, HIGH);
    delayMicroseconds(period);
    digitalWrite(rfPin, LOW);
    delayMicroseconds(period*3);
  }
  
  if(bit == 1){
    digitalWrite(rfPin, HIGH);
    delayMicroseconds(period);
    digitalWrite(rfPin, LOW);
    delayMicroseconds(period*3);
    
    digitalWrite(rfPin, HIGH);
    delayMicroseconds(period*3);
    digitalWrite(rfPin, LOW);
    delayMicroseconds(period);
  }
  
  if(bit == 2){
    digitalWrite(rfPin, HIGH);
    delayMicroseconds(period);
    digitalWrite(rfPin, LOW);
    delayMicroseconds(period*28);
  }
}

later i would like to ad ethernet suport through an ethernet shield so i can controll it through my cellphone or whatever, im waiting for it to arrive from seeed studios atm!

i will be greatfull for any answer!
/Fredrik

here is the datasheet to the transmitter, i was not aloud to put it in my first post:
http://www.escol.com.my/Datasheets_specs/pt2262_1.pdf

Have you looked at the VirtualWire library for the 433MHz RF modules?

--Phil.

yes, i did look in to that (althoug i never actually tried using it) seems like the messages or words are fundamentally different, on the pt2262 each message is 13 bits (or 16 if you count the syncbit as 3 regular bits) and for the viritual wire the words seem to be 32 bit and adressing etc seems to be a lot different, but mabe i can modify it somehow.
it would be nifty to just be able to feed some hex code for each command as you would a ir remote, and to be able to read in new messages easy with just a reciever module.