0e2cd18313e8aba13920d4a23dc05ec54e4e20d4
[Arduino] / RF433_Sockets / RF433_Sockets.ino
1 /*
2   Example for receiving wirelss signals and sending them out based on serial input
3
4   Based on examples from RC-Swtich which this sketch uses
5   
6   http://code.google.com/p/rc-switch/
7 */
8
9 #include <RCSwitch.h>
10
11 RCSwitch mySwitch = RCSwitch();
12
13 void setup() {
14   Serial.begin(9600);
15   mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
16   
17   mySwitch.enableTransmit(10); // with sender wired in receiving doesn't work, pin #10
18
19   Serial.print("press buttons on remote or send AB where A = socket (0..9), B = state (0 = off, 1 = on)\n");
20 }
21
22 int serial_pos = 0;
23 char serial_data[2]; // socket (0-9), state (0-1)
24
25 void loop() {
26   if (mySwitch.available()) {
27     Serial.print(mySwitch.getReceivedBitlength());
28     Serial.print(" bits ");
29     Serial.println(mySwitch.getReceivedValue(), BIN);
30     mySwitch.resetAvailable();
31   }
32   if (Serial.available() > 0) {
33      char input = Serial.read();
34      if ( input >= 0x30 && input <= 0x39 ) {
35        input = input - 0x30; // ASCII to number
36        serial_data[serial_pos++] = input;
37      } else {     
38        Serial.print("ignore: ");
39        Serial.println(input, HEX);
40      }
41      
42      if ( serial_pos == 2 ) {
43        Serial.print("socket: ");
44        Serial.print(serial_data[0], DEC);
45        Serial.print(" state: ");
46        Serial.println(serial_data[1] ? "on" : "off");
47        serial_pos = 0;
48        if ( serial_data[1] ) { // on
49          switch ( serial_data[0] ) {
50          case 1:
51            mySwitch.send("110101010111010000001100");
52            break;
53          case 2:
54            mySwitch.send("110101010111010000001100");
55            break;
56          case 3:
57            mySwitch.send("110101010101110000001100");
58            break;
59          default:
60            Serial.print("invalid switch on number ");
61            Serial.println(serial_data[0], DEC);
62          }
63        } else { // off
64          switch ( serial_data[0] ) {
65          case 1:
66            mySwitch.send("110101011101010000000011");
67            break;
68          case 2:
69            mySwitch.send("110101010111010000000011");
70            break;
71          case 3:
72            mySwitch.send("110101010101110000000011");
73            break;
74          default:
75            Serial.print("invalid switch off number ");
76            Serial.println(serial_data[0], DEC);
77          }
78        }
79      }
80   }
81 }