single line output
[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 //           3    433Mhz outdoor temperature sensor receiver # DISABLED
14            8    DHT22
15            10   433Mhz send
16            11   DS18B20
17            12   315Mhz send
18            13   status LED
19            
20 */
21
22 #include <RCSwitch.h>
23
24 #include <OneWire.h>
25 #include <DallasTemperature.h>
26
27 RCSwitch mySwitch = RCSwitch();
28
29 // DS18B20 on pin 11
30 OneWire oneWire(11);
31 DallasTemperature sensors(&oneWire);
32
33
34 // 315Mhz light sockets
35 #define TX_PIN 12
36 #define LED_PIN 13
37 #define LED_ON  digitalWrite(LED_PIN, HIGH);
38 #define LED_OFF digitalWrite(LED_PIN, LOW);
39
40 int int_0 = 300; // ms
41 int int_1 = 900; // ms
42 int wait  = 2000; // ms
43 int repeat = 20; // times (5 times seem a little low for sensors which are more than 10m away)
44
45 void send_315(char *code) {
46   Serial.print("send 315Mhz ");
47   Serial.println(code);
48   
49   // we have to send same signal at least two times
50   for(int r = 0; r < repeat; r++ ) {
51
52     digitalWrite(LED_PIN, HIGH);
53
54     for(int i = 0; i < strlen(code); i++) {
55       int i1 = int_0;
56       int i2 = int_1;
57       if (code[i] == '1' ) {
58         i1 = int_1;
59         i2 = int_0;
60       }
61       digitalWrite(TX_PIN, HIGH);
62       delayMicroseconds(i1);
63       digitalWrite(TX_PIN, LOW);
64       delayMicroseconds(i2);
65     }
66
67     digitalWrite(LED_PIN, LOW);
68
69     delayMicroseconds(wait); // guess
70   }
71
72 }
73
74
75 // DHT22
76 #include "DHT.h"
77 DHT dht;
78
79 // setup
80
81 void help() {
82   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");
83 }
84
85 void setup() {
86   Serial.begin(9600);
87   mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2  
88   mySwitch.enableTransmit(10); // with sender wired in receiving doesn't work, pin #10
89
90   // DS18B20
91   sensors.begin();
92
93   // DHT22
94   dht.setup(8);
95
96 }
97
98 int serial_pos = 0;
99 char serial_data[2]; // socket (0-9), state (0-1)
100 char binary_data[32];
101
102 void loop() {
103   if (mySwitch.available()) {
104     Serial.print(mySwitch.getReceivedBitlength());
105     Serial.print(" bits ");
106     Serial.println(mySwitch.getReceivedValue(), BIN);
107     mySwitch.resetAvailable();
108   }
109   if (Serial.available() > 0) {
110      char input = Serial.read();
111
112      if (input == '?' || input == 'h') {
113        help();
114      } else
115
116      if ( input == 'T' ) {
117        Serial.print("DS18B20 temperature = ");
118        sensors.requestTemperatures();
119        Serial.println( sensors.getTempCByIndex(0) );       
120      } else
121
122      if ( input == 'B' ) {
123        Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
124        Serial.print("# send B");
125        Serial.println( binary_data );
126        LED_ON
127        mySwitch.send( binary_data );
128        LED_OFF
129      } else
130
131      if ( input == 'R' ) {
132        Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
133        Serial.print("# send 315 binary ");
134        Serial.println( binary_data );
135        send_315( binary_data );
136      } else
137
138     // light sockets at 315 Mhz
139      if (input == 'a') {
140        send_315("1000100110110000000000010");
141      } else
142      if (input == 'b') {
143        send_315("1011001001011111000000010");
144      } else
145
146      // DHT22
147      if (input == 'd') {
148        Serial.print("temperature=");
149        Serial.print(dht.getTemperature());
150        Serial.print(" humidity=");
151        Serial.print(dht.getHumidity());
152        Serial.print(" status=");
153        Serial.println(dht.getStatusString());
154      }
155
156      if ( input >= 0x30 && input <= 0x39 ) {
157        input = input - 0x30; // ASCII to number
158        serial_data[serial_pos++] = input;
159      } else {     
160        Serial.print("# ignore: ");
161        Serial.println(input, HEX);
162      }
163      
164      if ( serial_pos == 2 ) {
165        Serial.print("switch=");
166        Serial.print(serial_data[0], DEC);
167        Serial.print(" state=");
168        Serial.println(serial_data[1] ? "on" : "off");
169
170        byte on = serial_data[1];
171
172        LED_ON
173
174         // switches, 433 Mhz set of 3
175         switch ( serial_data[0] ) {
176          case 1:
177            on   ? mySwitch.send("110101011101010000001100")
178                 : mySwitch.send("110101011101010000000011");
179            break;
180          case 2:
181            on   ? mySwitch.send("110101010111010000001100")
182                 : mySwitch.send("110101010111010000000011");
183            break;
184          case 3:
185            on   ? mySwitch.send("110101010101110000001100")
186                 : mySwitch.send("110101010101110000000011");
187            break;
188          case 4:
189            on   ? mySwitch.send("001111110000000011000000")
190                 : mySwitch.send("001111110000000000000000");
191            break;
192          case 5:
193 /*
194                 cca. 320Mhz 4 channel 4.8A 220V relay
195
196                 A       1101001010011010011100010
197                 B       1101001010011010011101000
198                 C       1101001010011010011100100
199                 D       1101001010011010011110000
200                 off     1101001010011010011111000
201                 on      1101001010011010011100110
202 */
203                 switch ( on ) {
204                         case '0': send_315( "1101001010011010011111000" );
205                         case '1': send_315( "1101001010011010011100110" );
206                         case 'A': send_315( "1101001010011010011100010" );
207                         case 'B': send_315( "1101001010011010011101000" );
208                         case 'C': send_315( "1101001010011010011100100" );
209                         case 'D': send_315( "1101001010011010011110000" );
210                         default:  Serial.println("# ERROR: use 0-off 1-on A B C D");
211                 }
212          default:
213            Serial.print("# invalid switch number ");
214            Serial.println(serial_data[0], DEC);
215         }
216
217         LED_OFF
218
219         // reset for later
220         serial_pos = 0;
221      }
222   }
223 }