cleanup serial 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   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 help() {
79   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");
80 }
81
82 void setup() {
83   Serial.begin(9600);
84   mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2  
85   mySwitch.enableTransmit(10); // with sender wired in receiving doesn't work, pin #10
86
87   // DS18B20
88   sensors.begin();
89
90   // DHT22
91   dht.setup(8);
92
93 }
94
95 int serial_pos = 0;
96 char serial_data[2]; // socket (0-9), state (0-1)
97 char binary_data[24];
98
99 void loop() {
100   if (mySwitch.available()) {
101     Serial.print(mySwitch.getReceivedBitlength());
102     Serial.print(" bits ");
103     Serial.println(mySwitch.getReceivedValue(), BIN);
104     mySwitch.resetAvailable();
105   }
106   if (Serial.available() > 0) {
107      char input = Serial.read();
108
109      if (input == '?' || input == 'h') {
110        help();
111      } else
112
113      if ( input == 'T' ) {
114        Serial.print("DS18B20 temperature = ");
115        sensors.requestTemperatures();
116        Serial.println( sensors.getTempCByIndex(0) );       
117      } else
118
119      if ( input == 'B' ) {
120        Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
121        Serial.print("# send B");
122        Serial.println( binary_data );
123        mySwitch.send( binary_data );
124      } else
125
126     // light sockets at 513 Mhz
127      if (input == 'a') {
128        send_513("1000100110110000000000010");
129      } else
130      if (input == 'b') {
131        send_513("1011001001011111000000010");
132      } else
133
134      // DHT22
135      if (input == 'd') {
136        Serial.print("temperature=");
137        Serial.print(dht.getTemperature());
138        Serial.print(" humidity=");
139        Serial.print(dht.getHumidity());
140        Serial.print(" status=");
141        Serial.println(dht.getStatusString());
142      }
143
144      if ( input >= 0x30 && input <= 0x39 ) {
145        input = input - 0x30; // ASCII to number
146        serial_data[serial_pos++] = input;
147      } else {     
148        Serial.print("# ignore: ");
149        Serial.println(input, HEX);
150      }
151      
152      if ( serial_pos == 2 ) {
153        Serial.print("socket=");
154        Serial.print(serial_data[0], DEC);
155        Serial.print(" state=");
156        Serial.println(serial_data[1] ? "on" : "off");
157
158        byte on = serial_data[1];
159
160         // switches, 433 Mhz set of 3
161         switch ( serial_data[0] ) {
162          case 1:
163            on   ? mySwitch.send("110101011101010000001100")
164                 : mySwitch.send("110101011101010000000011");
165            break;
166          case 2:
167            on   ? mySwitch.send("110101010111010000001100")
168                 : mySwitch.send("110101010111010000000011");
169            break;
170          case 3:
171            on   ? mySwitch.send("110101010101110000001100")
172                 : mySwitch.send("110101010101110000000011");
173            break;
174          case 4:
175            on   ? mySwitch.send("001111110000000011000000")
176                 : mySwitch.send("001111110000000000000000");
177            break;
178          default:
179            Serial.print("# invalid switch on number ");
180            Serial.println(serial_data[0], DEC);
181         }
182
183         // reset for later
184         serial_pos = 0;
185      }
186   }
187 }