Merge branch 'master' of mjesec.ffzg.hr:/git/Arduino
[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   BCM25    RST
9   GND      GND
10   +5V      RAW
11
12            2    433Mhz receive
13 //           3    433Mhz outdoor temperature sensor receiver # DISABLED
14            8    DHT22 (VCC from arduino VCC)
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 = 5; // 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 #include "RunningAverage.h"
80
81 RunningAverage temp_avg(10);
82 RunningAverage hum_avg(10);
83
84 // setup
85
86 void help() {
87   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");
88 }
89
90 void setup() {
91   Serial.begin(9600);
92   mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2  
93   mySwitch.enableTransmit(10); // with sender wired in receiving doesn't work, pin #10
94   mySwitch.setRepeatTransmit(repeat); // or change to be different for 433 and 315 MHz
95
96   // DS18B20
97   sensors.begin();
98
99   // DHT22
100   dht.setup(8);
101
102     temp_avg.addValue( dht.getTemperature() );
103     hum_avg.addValue( dht.getHumidity() );
104
105 }
106
107 int serial_pos = 0;
108 char serial_data[2]; // socket (0-9), state (0-1)
109 char binary_data[32];
110 unsigned int dht22_errors = 0;
111
112 unsigned long time = millis();
113
114 void loop() {
115   if ( millis() - time > 2000 ) {
116     float t = dht.getTemperature();
117     float delta_t = abs(t - temp_avg.getAverage());
118     if ( dht.getStatus() == 0 && delta_t < 10 )
119       temp_avg.addValue( t );
120     else dht22_errors++;
121     float h = dht.getHumidity();
122     float delta_h = abs(h - hum_avg.getAverage());
123     if ( dht.getStatus() == 0 && delta_h < 10 )
124       hum_avg.addValue( h );
125     else dht22_errors++;
126     time = millis();
127   }
128
129   if (mySwitch.available()) {
130     Serial.print(mySwitch.getReceivedBitlength());
131     Serial.print(" bits ");
132     Serial.println(mySwitch.getReceivedValue(), BIN);
133     mySwitch.resetAvailable();
134   }
135   if (Serial.available() > 0) {
136      char input = Serial.read();
137
138      if (input == '?' || input == 'h') {
139        help();
140      } else
141
142      if ( input == 'T' ) {
143        Serial.print("DS18B20 temperature = ");
144        sensors.requestTemperatures();
145        Serial.println( sensors.getTempCByIndex(0) );       
146      } else
147
148      if ( input == 'B' ) {
149        Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
150        Serial.print("# send B");
151        Serial.println( binary_data );
152        LED_ON
153        mySwitch.send( binary_data );
154        LED_OFF
155      } else
156
157      if ( input == 'R' ) {
158        Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
159        Serial.print("# send 315 binary ");
160        Serial.println( binary_data );
161        send_315( binary_data );
162      } else
163
164     // light sockets at 315 Mhz
165      if (input == 'a') {
166        send_315("1000100110110000000000010");
167      } else
168      if (input == 'b') {
169        send_315("1011001001011111000000010");
170      } else
171
172      // DHT22
173      if (input == 'd') {
174        Serial.print("temperature=");
175        Serial.print(temp_avg.getAverage());
176        Serial.print(" humidity=");
177        Serial.print(hum_avg.getAverage());
178        Serial.print(" errors=");
179        Serial.println(dht22_errors);
180      }
181
182      if ( input >= 0x30 && input <= 0x39 && serial_pos < 2 ) {
183        input = input - 0x30; // ASCII to number
184        serial_data[serial_pos++] = input;
185      } else {     
186        Serial.print("# ignore: ");
187        Serial.println(input, HEX);
188      }
189      
190      if ( serial_pos == 2 ) {
191        Serial.print("switch=");
192        Serial.print(serial_data[0], DEC);
193        Serial.print(" state=");
194        Serial.println(serial_data[1] ? "on" : "off");
195
196        byte on = serial_data[1];
197
198        LED_ON
199
200         // switches, 433 Mhz set of 3
201         switch ( serial_data[0] ) {
202          case 1:
203            on   ? mySwitch.send("110101011101010000001100")
204                 : mySwitch.send("110101011101010000000011");
205            break;
206          case 2:
207            on   ? mySwitch.send("110101010111010000001100")
208                 : mySwitch.send("110101010111010000000011");
209            break;
210          case 3:
211            on   ? mySwitch.send("110101010101110000001100")
212                 : mySwitch.send("110101010101110000000011");
213            break;
214          case 4:
215            on   ? mySwitch.send("001111110000000011000000")
216                 : mySwitch.send("001111110000000000000000");
217            break;
218          case 5:
219 /*
220                 cca. 320Mhz 4 channel 4.8A 220V relay
221
222                 A       1101001010011010011100010
223                 B       1101001010011010011101000
224                 C       1101001010011010011100100
225                 D       1101001010011010011110000
226                 off     1101001010011010011111000
227                 on      1101001010011010011100110
228 */
229                 switch ( on ) {
230                         case '0': send_315( "1101001010011010011111000" );
231                         case '1': send_315( "1101001010011010011100110" );
232                         case 'A': send_315( "1101001010011010011100010" );
233                         case 'B': send_315( "1101001010011010011101000" );
234                         case 'C': send_315( "1101001010011010011100100" );
235                         case 'D': send_315( "1101001010011010011110000" );
236                         default:  Serial.println("# ERROR: use 0-off 1-on A B C D");
237                 }
238          default:
239            Serial.print("# invalid switch number ");
240            Serial.println(serial_data[0], DEC);
241         }
242
243         LED_OFF
244
245         // reset for later
246         serial_pos = 0;
247      }
248   }
249 }