b493673d839beb8693cca3135842bc203290ddf9
[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)\nB00...00 (24 digits) to send binary\n");
20 }
21
22 int serial_pos = 0;
23 char serial_data[2]; // socket (0-9), state (0-1)
24 char binary_data[24];
25
26 void loop() {
27   if (mySwitch.available()) {
28     Serial.print(mySwitch.getReceivedBitlength());
29     Serial.print(" bits ");
30     Serial.println(mySwitch.getReceivedValue(), BIN);
31     mySwitch.resetAvailable();
32   }
33   if (Serial.available() > 0) {
34      char input = Serial.read();
35      if ( input == 'B' ) {
36        Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
37        Serial.print("send B");
38        Serial.println( binary_data );
39        mySwitch.send( binary_data );
40      } else
41      if ( input >= 0x30 && input <= 0x39 ) {
42        input = input - 0x30; // ASCII to number
43        serial_data[serial_pos++] = input;
44      } else {     
45        Serial.print("ignore: ");
46        Serial.println(input, HEX);
47      }
48      
49      if ( serial_pos == 2 ) {
50        Serial.print("socket: ");
51        Serial.print(serial_data[0], DEC);
52        Serial.print(" state: ");
53        Serial.println(serial_data[1] ? "on" : "off");
54        serial_pos = 0;
55        if ( serial_data[1] ) { // on
56          switch ( serial_data[0] ) {
57          case 1:
58            mySwitch.send("110101011101010000001100");
59            break;
60          case 2:
61            mySwitch.send("110101010111010000001100");
62            break;
63          case 3:
64            mySwitch.send("110101010101110000001100");
65            break;
66          default:
67            Serial.print("invalid switch on number ");
68            Serial.println(serial_data[0], DEC);
69          }
70        } else { // off
71          switch ( serial_data[0] ) {
72          case 1:
73            mySwitch.send("110101011101010000000011");
74            break;
75          case 2:
76            mySwitch.send("110101010111010000000011");
77            break;
78          case 3:
79            mySwitch.send("110101010101110000000011");
80            break;
81          default:
82            Serial.print("invalid switch off number ");
83            Serial.println(serial_data[0], DEC);
84          }
85        }
86      }
87   }
88 }