read DS18B20 temperature
[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
16 */
17
18 #include <RCSwitch.h>
19
20 #include <OneWire.h>
21 #include <DallasTemperature.h>
22
23 RCSwitch mySwitch = RCSwitch();
24
25 // DS18B20 on pin 11
26 OneWire oneWire(11);
27 DallasTemperature sensors(&oneWire);
28
29 void setup() {
30   Serial.begin(9600);
31   mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2  
32   mySwitch.enableTransmit(10); // with sender wired in receiving doesn't work, pin #10
33
34   // DS18B20
35   sensors.begin();
36
37   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");
38 }
39
40 int serial_pos = 0;
41 char serial_data[2]; // socket (0-9), state (0-1)
42 char binary_data[24];
43
44 void loop() {
45   if (mySwitch.available()) {
46     Serial.print(mySwitch.getReceivedBitlength());
47     Serial.print(" bits ");
48     Serial.println(mySwitch.getReceivedValue(), BIN);
49     mySwitch.resetAvailable();
50   }
51   if (Serial.available() > 0) {
52      char input = Serial.read();
53      if ( input == 'T' ) {
54        Serial.print("DS18B20 temperature = ");
55        sensors.requestTemperatures();
56        Serial.println( sensors.getTempCByIndex(0) );       
57      } else
58      if ( input == 'B' ) {
59        Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
60        Serial.print("send B");
61        Serial.println( binary_data );
62        mySwitch.send( binary_data );
63      } else
64      if ( input >= 0x30 && input <= 0x39 ) {
65        input = input - 0x30; // ASCII to number
66        serial_data[serial_pos++] = input;
67      } else {     
68        Serial.print("ignore: ");
69        Serial.println(input, HEX);
70      }
71      
72      if ( serial_pos == 2 ) {
73        Serial.print("socket: ");
74        Serial.print(serial_data[0], DEC);
75        Serial.print(" state: ");
76        Serial.println(serial_data[1] ? "on" : "off");
77        serial_pos = 0;
78        if ( serial_data[1] ) { // on
79          switch ( serial_data[0] ) {
80          case 1:
81            mySwitch.send("110101011101010000001100");
82            break;
83          case 2:
84            mySwitch.send("110101010111010000001100");
85            break;
86          case 3:
87            mySwitch.send("110101010101110000001100");
88            break;
89          default:
90            Serial.print("invalid switch on number ");
91            Serial.println(serial_data[0], DEC);
92          }
93        } else { // off
94          switch ( serial_data[0] ) {
95          case 1:
96            mySwitch.send("110101011101010000000011");
97            break;
98          case 2:
99            mySwitch.send("110101010111010000000011");
100            break;
101          case 3:
102            mySwitch.send("110101010101110000000011");
103            break;
104          default:
105            Serial.print("invalid switch off number ");
106            Serial.println(serial_data[0], DEC);
107          }
108        }
109      }
110   }
111 }