1+ //
2+ // FILE: UserDataDemo.ino
3+ // AUTHOR: Rob Tillaart
4+ // VERSION: 0.1.0
5+ // PURPOSE: use of alarm field as user identification demo
6+ // DATE: 2019-12-23
7+ // URL:
8+ //
9+ // Released to the public domain
10+ //
11+
12+ #include < OneWire.h>
13+ #include < DallasTemperature.h>
14+
15+ #define ONE_WIRE_BUS 2
16+
17+ OneWire oneWire (ONE_WIRE_BUS);
18+ DallasTemperature sensors (&oneWire);
19+
20+ uint8_t deviceCount = 0 ;
21+
22+ // Add 4 prepared sensors to the bus
23+ // use the UserDataWriteBatch demo to prepare 4 different labeled sensors
24+ struct
25+ {
26+ int id;
27+ DeviceAddress addr;
28+ } T[4 ];
29+
30+ float getTempByID (int id)
31+ {
32+ for (uint8_t index = 0 ; index < deviceCount; index++)
33+ {
34+ if (T[index].id == id)
35+ {
36+ return sensors.getTempC (T[index].addr );
37+ }
38+ }
39+ return -999 ;
40+ }
41+
42+ void printAddress (DeviceAddress deviceAddress)
43+ {
44+ for (uint8_t i = 0 ; i < 8 ; i++)
45+ {
46+ // zero pad the address if necessary
47+ if (deviceAddress[i] < 16 ) Serial.print (" 0" );
48+ Serial.print (deviceAddress[i], HEX);
49+ }
50+ }
51+
52+ void setup (void )
53+ {
54+ Serial.begin (115200 );
55+ Serial.println (__FILE__);
56+ Serial.println (" Dallas Temperature Demo" );
57+
58+ sensors.begin ();
59+
60+ // count devices
61+ deviceCount = sensors.getDeviceCount ();
62+ Serial.print (" #devices: " );
63+ Serial.println (deviceCount);
64+
65+ // Read ID's per sensor
66+ // and put them in T array
67+ for (uint8_t index = 0 ; index < deviceCount; index++)
68+ {
69+ // go through sensors
70+ sensors.getAddress (T[index].addr , index);
71+ T[index].id = sensors.getUserData (T[index].addr );
72+ }
73+
74+ // Check all 4 sensors are set
75+ for (uint8_t index = 0 ; index < deviceCount; index++)
76+ {
77+ Serial.println ();
78+ Serial.println (T[index].id );
79+ printAddress (T[index].addr );
80+ Serial.println ();
81+ }
82+ Serial.println ();
83+
84+ }
85+
86+
87+ void loop (void )
88+ {
89+ Serial.println ();
90+ Serial.print (millis ());
91+ Serial.println (" \t req temp" );
92+ sensors.requestTemperatures ();
93+
94+ Serial.print (millis ());
95+ Serial.println (" \t Get temp by address" );
96+ for (int i = 0 ; i < 4 ; i++)
97+ {
98+ Serial.print (millis ());
99+ Serial.print (" \t temp:\t " );
100+ Serial.println (sensors.getTempC (T[i].addr ));
101+ }
102+
103+ Serial.print (millis ());
104+ Serial.println (" \t Get temp by ID" ); // assume ID = 0, 1, 2, 3
105+ for (int id = 0 ; id < 4 ; id++)
106+ {
107+ Serial.print (millis ());
108+ Serial.print (" \t temp:\t " );
109+ Serial.println (getTempByID (id));
110+ }
111+
112+ delay (1000 );
113+ }
114+
115+ // END OF FILE
0 commit comments