cf22f81a4abe6992b80b8ee95f97acace675d21f
[Arduino] / rpi_promini / rpi_promini.ino
1 /*
2
3   Connect Arduino ProMini 3.3V 8Mhz Atmega328 to Raspberry Pi
4   
5   RPI pin  Arduino
6   RXD      TXD
7   TXD      RXD
8   GPIO6    RST
9
10 */
11
12 #include <RCSwitch.h>
13
14
15 RCSwitch mySwitch = RCSwitch();
16
17 void setup() {
18   Serial.begin(9600);
19   mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2  
20   mySwitch.enableTransmit(10); // with sender wired in receiving doesn't work, pin #10
21
22
23
24   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");
25 }
26
27 int serial_pos = 0;
28 char serial_data[2]; // socket (0-9), state (0-1)
29 char binary_data[24];
30
31 void loop() {
32   if (mySwitch.available()) {
33     Serial.print(mySwitch.getReceivedBitlength());
34     Serial.print(" bits ");
35     Serial.println(mySwitch.getReceivedValue(), BIN);
36     mySwitch.resetAvailable();
37   }
38   if (Serial.available() > 0) {
39      char input = Serial.read();
40      if ( input == 'B' ) {
41        Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
42        Serial.print("send B");
43        Serial.println( binary_data );
44        mySwitch.send( binary_data );
45      } else
46      if ( input >= 0x30 && input <= 0x39 ) {
47        input = input - 0x30; // ASCII to number
48        serial_data[serial_pos++] = input;
49      } else {     
50        Serial.print("ignore: ");
51        Serial.println(input, HEX);
52      }
53      
54      if ( serial_pos == 2 ) {
55        Serial.print("socket: ");
56        Serial.print(serial_data[0], DEC);
57        Serial.print(" state: ");
58        Serial.println(serial_data[1] ? "on" : "off");
59        serial_pos = 0;
60        if ( serial_data[1] ) { // on
61          switch ( serial_data[0] ) {
62          case 1:
63            mySwitch.send("110101011101010000001100");
64            break;
65          case 2:
66            mySwitch.send("110101010111010000001100");
67            break;
68          case 3:
69            mySwitch.send("110101010101110000001100");
70            break;
71          default:
72            Serial.print("invalid switch on number ");
73            Serial.println(serial_data[0], DEC);
74          }
75        } else { // off
76          switch ( serial_data[0] ) {
77          case 1:
78            mySwitch.send("110101011101010000000011");
79            break;
80          case 2:
81            mySwitch.send("110101010111010000000011");
82            break;
83          case 3:
84            mySwitch.send("110101010101110000000011");
85            break;
86          default:
87            Serial.print("invalid switch off number ");
88            Serial.println(serial_data[0], DEC);
89          }
90        }
91      }
92   }
93 }