1 | #include <OneWire.h>
|
2 | #include <DallasTemperature.h>
|
3 |
|
4 | OneWire ds(2);
|
5 |
|
6 | // Device Adressen der einzelnen Temperatursensoren angeben
|
7 | DeviceAddress sensor1 = { 0x10, 0x38, 0x27, 0x57, 0x2, 0x8, 0x0, 0x79 };
|
8 | DeviceAddress sensor2 = { 0x10, 0x16, 0x2E, 0x57, 0x2, 0x8, 0x0, 0x2F };
|
9 | //DeviceAddress sensor3 = { 0x10, 0x11, 0x40, 0xC0, 0x1, 0x8, 0x0, 0x9A };
|
10 | //DeviceAddress sensor4 = { 0x10, 0x77, 0x38, 0x57, 0x2, 0x8, 0x0, 0x3B };
|
11 |
|
12 | char sensor1Name[] = "Sensor1: ";
|
13 | char sensor2Name[] = "Sensor2: ";
|
14 | //char sensor3Name[] = "Sensor3: ";
|
15 | //char sensor4Name[] = "Sensor4: ";
|
16 |
|
17 | int BlinkRate=0; // Anzahl Blinken für Tests
|
18 | String Input=""; // Input von Serieller Schnittstelle
|
19 | String Befehl="NOP"; // Befehl aus Input
|
20 | int Parameter=0; // Parameter aus Input
|
21 | int Wert=0; // Rückgabewert Analog
|
22 |
|
23 | char buffer[20] = ""; // Speicherbereich für Datenkonvertierung
|
24 |
|
25 |
|
26 | void setup()
|
27 | {
|
28 | Serial.begin(9600);
|
29 | }
|
30 |
|
31 |
|
32 | void loop(void)
|
33 | {
|
34 | if (Serial.available()) Einlesen();
|
35 | //Serial.println("Testausgabe");
|
36 | //delay(2000);
|
37 | }
|
38 |
|
39 | void Einlesen()
|
40 | {
|
41 | while (Serial.available())
|
42 | {
|
43 | char ch = Serial.read();
|
44 | //Serial.print("Echo: ");
|
45 | //Serial.println(ch);
|
46 | Input=Input+ch;
|
47 | //Serial.println(Input);
|
48 | delay(10);
|
49 | }
|
50 | Befehl = Input.substring(0, 4);
|
51 | //Serial.println("Befehl: " + Befehl);
|
52 | Parameter = (Input.substring(4, 6)).toInt();
|
53 | //Serial.print("Parameter: ");
|
54 | //Serial.println(Parameter,DEC);
|
55 | ausfuehren();
|
56 | //Serial.println(Befehl + Parameter + " = " + Wert);
|
57 |
|
58 | Input="";
|
59 | }
|
60 |
|
61 | void ausfuehren()
|
62 | {
|
63 |
|
64 | if (Befehl=="OW01")
|
65 | {
|
66 | float temp1 = getTemperature(sensor1);
|
67 | Serial.println(temp1);
|
68 | }
|
69 | if (Befehl=="OW02")
|
70 | {
|
71 | float temp2 = getTemperature(sensor2);
|
72 | Serial.println(temp2);
|
73 | }
|
74 | else
|
75 | {
|
76 | Serial.println("ERROR");
|
77 | }
|
78 | }
|
79 |
|
80 | void writeTimeToScratchpad(byte* address)
|
81 | {
|
82 | //reset the bus
|
83 | ds.reset();
|
84 | //select our sensor
|
85 | ds.select(address);
|
86 | //CONVERT T function call (44h) which puts the temperature into the scratchpad
|
87 | ds.write(0x44,1);
|
88 | //sleep a second for the write to take place
|
89 | delay(1000);
|
90 | }
|
91 |
|
92 | void readTimeFromScratchpad(byte* address, byte* data)
|
93 | {
|
94 | //reset the bus
|
95 | ds.reset();
|
96 | //select our sensor
|
97 | ds.select(address);
|
98 | //read the scratchpad (BEh)
|
99 | ds.write(0xBE);
|
100 | for (byte i=0;i<9;i++){
|
101 | data[i] = ds.read();
|
102 | }
|
103 | }
|
104 |
|
105 | float getTemperature(byte* address)
|
106 | {
|
107 | int tr;
|
108 | byte data[12];
|
109 |
|
110 | writeTimeToScratchpad(address);
|
111 |
|
112 | readTimeFromScratchpad(address,data);
|
113 |
|
114 | //put in temp all the 8 bits of LSB (least significant byte)
|
115 | tr = data[0];
|
116 |
|
117 | //check for negative temperature
|
118 | if (data[1] > 0x80)
|
119 | {
|
120 | tr = !tr + 1; //two's complement adjustment
|
121 | tr = tr * -1; //flip value negative.
|
122 | }
|
123 |
|
124 | //COUNT PER Celsius degree (10h)
|
125 | int cpc = data[7];
|
126 | //COUNT REMAIN (0Ch)
|
127 | int cr = data[6];
|
128 |
|
129 | //drop bit 0
|
130 | tr = tr >> 1;
|
131 |
|
132 | return tr - (float)0.25 + (cpc - cr)/(float)cpc;
|
133 | }
|