added 4th socket and cleanup code
[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   513Mhz 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 // 513Mhz light sockets
35 #define TX_PIN 12
36 #define LED_PIN 13
37
38 int int_0 = 300; // ms
39 int int_1 = 900; // ms
40 int wait  = 2000; // ms
41 int repeat = 5; // times
42
43 void send_513(char *code) {
44   Serial.print("send 513Mhz ");
45   Serial.println(code);
46   
47   // we have to send same signal at least two times
48   for(int r = 0; r < repeat; r++ ) {
49
50     digitalWrite(LED_PIN, HIGH);
51
52     for(int i = 0; i < strlen(code); i++) {
53       int i1 = int_0;
54       int i2 = int_1;
55       if (code[i] == '1' ) {
56         i1 = int_1;
57         i2 = int_0;
58       }
59       digitalWrite(TX_PIN, HIGH);
60       delayMicroseconds(i1);
61       digitalWrite(TX_PIN, LOW);
62       delayMicroseconds(i2);
63     }
64
65     delayMicroseconds(wait); // guess
66   }
67
68   digitalWrite(LED_PIN, LOW);
69 }
70
71
72 // DHT22
73 #include "DHT.h"
74 DHT dht;
75
76 // setup
77
78 void setup() {
79   Serial.begin(9600);
80   mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2  
81   mySwitch.enableTransmit(10); // with sender wired in receiving doesn't work, pin #10
82
83   // DS18B20
84   sensors.begin();
85
86   // DHT22
87   dht.setup(8);
88
89   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");
90 }
91
92 int serial_pos = 0;
93 char serial_data[2]; // socket (0-9), state (0-1)
94 char binary_data[24];
95
96 void loop() {
97   if (mySwitch.available()) {
98     Serial.print(mySwitch.getReceivedBitlength());
99     Serial.print(" bits ");
100     Serial.println(mySwitch.getReceivedValue(), BIN);
101     mySwitch.resetAvailable();
102   }
103   if (Serial.available() > 0) {
104      char input = Serial.read();
105      if ( input == 'T' ) {
106        Serial.print("DS18B20 temperature = ");
107        sensors.requestTemperatures();
108        Serial.println( sensors.getTempCByIndex(0) );       
109      } else
110
111      if ( input == 'B' ) {
112        Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
113        Serial.print("send B");
114        Serial.println( binary_data );
115        mySwitch.send( binary_data );
116      } else
117
118     // light sockets at 513 Mhz
119      if (input == 'a') {
120        send_513("1000100110110000000000010");
121      } else
122      if (input == 'b') {
123        send_513("1011001001011111000000010");
124      } else
125
126      // DHT22
127      if (input == 'd') {
128        Serial.print("temperature=");
129        Serial.print(dht.getTemperature());
130        Serial.print(" humidity=");
131        Serial.print(dht.getHumidity());
132        Serial.print(" status=");
133        Serial.println(dht.getStatusString());
134      }
135
136      if ( input >= 0x30 && input <= 0x39 ) {
137        input = input - 0x30; // ASCII to number
138        serial_data[serial_pos++] = input;
139      } else {     
140        Serial.print("ignore: ");
141        Serial.println(input, HEX);
142      }
143      
144      if ( serial_pos == 2 ) {
145        Serial.print("socket: ");
146        Serial.print(serial_data[0], DEC);
147        Serial.print(" state: ");
148        Serial.println(serial_data[1] ? "on" : "off");
149
150        byte on = serial_data[1];
151
152         // switches, 433 Mhz set of 3
153         switch ( serial_data[0] ) {
154          case 1:
155            on   ? mySwitch.send("110101011101010000001100")
156                 : mySwitch.send("110101011101010000000011");
157            break;
158          case 2:
159            on   ? mySwitch.send("110101010111010000001100")
160                 : mySwitch.send("110101010111010000000011");
161            break;
162          case 3:
163            on   ? mySwitch.send("110101010101110000001100")
164                 : mySwitch.send("110101010101110000000011");
165            break;
166          case 4:
167            on   ? mySwitch.send("001111110000000011000000")
168                 : mySwitch.send("001111110000000000000000");
169            break;
170          default:
171            Serial.print("invalid switch on number ");
172            Serial.println(serial_data[0], DEC);
173         }
174
175         // reset for later
176         serial_pos = 0;
177      }
178   }
179 }