modified timings for my sensor
[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   GND      GND
10   +5V      RAW
11
12            2    433Mhz receive
13            10   433Mhz send
14            11   DS18B20
15            13   513Mhz send
16
17 */
18
19 #include <RCSwitch.h>
20
21 #include <OneWire.h>
22 #include <DallasTemperature.h>
23
24 RCSwitch mySwitch = RCSwitch();
25
26 // DS18B20 on pin 11
27 OneWire oneWire(11);
28 DallasTemperature sensors(&oneWire);
29
30
31 // 513Mhz light sockets
32 #define TX_PIN 12
33 #define LED_PIN 13
34
35 int int_0 = 300; // ms
36 int int_1 = 900; // ms
37 int wait  = 2000; // ms
38 int repeat = 5; // times
39
40 void send_513(char *code) {
41   Serial.print("send 513Mhz ");
42   Serial.println(code);
43   
44   // we have to send same signal at least two times
45   for(int r = 0; r < repeat; r++ ) {
46
47     digitalWrite(LED_PIN, HIGH);
48
49     for(int i = 0; i < strlen(code); i++) {
50       int i1 = int_0;
51       int i2 = int_1;
52       if (code[i] == '1' ) {
53         i1 = int_1;
54         i2 = int_0;
55       }
56       digitalWrite(TX_PIN, HIGH);
57       delayMicroseconds(i1);
58       digitalWrite(TX_PIN, LOW);
59       delayMicroseconds(i2);
60     }
61
62     delayMicroseconds(wait); // guess
63   }
64
65   digitalWrite(LED_PIN, LOW);
66 }
67
68
69 void setup() {
70   Serial.begin(9600);
71   mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2  
72   mySwitch.enableTransmit(10); // with sender wired in receiving doesn't work, pin #10
73
74   // DS18B20
75   sensors.begin();
76
77   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");
78 }
79
80 int serial_pos = 0;
81 char serial_data[2]; // socket (0-9), state (0-1)
82 char binary_data[24];
83
84 void loop() {
85   if (mySwitch.available()) {
86     Serial.print(mySwitch.getReceivedBitlength());
87     Serial.print(" bits ");
88     Serial.println(mySwitch.getReceivedValue(), BIN);
89     mySwitch.resetAvailable();
90   }
91   if (Serial.available() > 0) {
92      char input = Serial.read();
93      if ( input == 'T' ) {
94        Serial.print("DS18B20 temperature = ");
95        sensors.requestTemperatures();
96        Serial.println( sensors.getTempCByIndex(0) );       
97      } else
98
99      if ( input == 'B' ) {
100        Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
101        Serial.print("send B");
102        Serial.println( binary_data );
103        mySwitch.send( binary_data );
104      } else
105
106     // light sockets at 513 Mhz
107      if (input == 'a') {
108        send_513("1000100110110000000000010");
109      } else
110      if (input == 'b') {
111        send_513("1011001001011111000000010");
112      } else
113
114      if ( input >= 0x30 && input <= 0x39 ) {
115        input = input - 0x30; // ASCII to number
116        serial_data[serial_pos++] = input;
117      } else {     
118        Serial.print("ignore: ");
119        Serial.println(input, HEX);
120      }
121      
122      if ( serial_pos == 2 ) {
123        Serial.print("socket: ");
124        Serial.print(serial_data[0], DEC);
125        Serial.print(" state: ");
126        Serial.println(serial_data[1] ? "on" : "off");
127        serial_pos = 0;
128        if ( serial_data[1] ) { // on
129          switch ( serial_data[0] ) {
130          case 1:
131            mySwitch.send("110101011101010000001100");
132            break;
133          case 2:
134            mySwitch.send("110101010111010000001100");
135            break;
136          case 3:
137            mySwitch.send("110101010101110000001100");
138            break;
139          default:
140            Serial.print("invalid switch on number ");
141            Serial.println(serial_data[0], DEC);
142          }
143        } else { // off
144          switch ( serial_data[0] ) {
145          case 1:
146            mySwitch.send("110101011101010000000011");
147            break;
148          case 2:
149            mySwitch.send("110101010111010000000011");
150            break;
151          case 3:
152            mySwitch.send("110101010101110000000011");
153            break;
154          default:
155            Serial.print("invalid switch off number ");
156            Serial.println(serial_data[0], DEC);
157          }
158        }
159      }
160   }
161 }