Skip to content

Commit 9cc81f0

Browse files
authored
Merge pull request #105 from RobTillaart/master
Created 2 examples of how to use the Alarm field as an ID
2 parents b342b5f + b93e549 commit 9cc81f0

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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("\treq temp");
92+
sensors.requestTemperatures();
93+
94+
Serial.print(millis());
95+
Serial.println("\tGet 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("\tGet 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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// FILE: UserDataWriteBatch.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+
void printAddress(DeviceAddress deviceAddress)
23+
{
24+
for (uint8_t i = 0; i < 8; i++)
25+
{
26+
// zero pad the address if necessary
27+
if (deviceAddress[i] < 16) Serial.print("0");
28+
Serial.print(deviceAddress[i], HEX);
29+
}
30+
}
31+
32+
33+
34+
void setup(void)
35+
{
36+
Serial.begin(115200);
37+
Serial.println(__FILE__);
38+
Serial.println("Write user ID to DS18B20\n");
39+
40+
sensors.begin();
41+
42+
// count devices
43+
deviceCount = sensors.getDeviceCount();
44+
Serial.print("#devices: ");
45+
Serial.println(deviceCount);
46+
47+
Serial.println();
48+
Serial.println("current ID's");
49+
for (uint8_t index = 0; index < deviceCount; index++)
50+
{
51+
DeviceAddress t;
52+
sensors.getAddress(t, index);
53+
printAddress(t);
54+
Serial.print("\t\tID: ");
55+
int id = sensors.getUserData(t);
56+
Serial.println(id);
57+
}
58+
59+
Serial.println();
60+
Serial.print("Enter ID for batch: ");
61+
int c = 0;
62+
int id = 0;
63+
while (c != '\n' && c != '\r')
64+
{
65+
c = Serial.read();
66+
switch(c)
67+
{
68+
case '0'...'9':
69+
id *= 10;
70+
id += (c - '0');
71+
break;
72+
default:
73+
break;
74+
}
75+
}
76+
Serial.println();
77+
Serial.println(id);
78+
Serial.println();
79+
80+
Serial.println("Start labeling ...");
81+
for (uint8_t index = 0; index < deviceCount; index++)
82+
{
83+
Serial.print(".");
84+
DeviceAddress t;
85+
sensors.getAddress(t, index);
86+
sensors.setUserData(t, id);
87+
}
88+
Serial.println();
89+
90+
Serial.println();
91+
Serial.println("Show results ...");
92+
for (uint8_t index = 0; index < deviceCount; index++)
93+
{
94+
DeviceAddress t;
95+
sensors.getAddress(t, index);
96+
printAddress(t);
97+
Serial.print("\t\tID: ");
98+
int id = sensors.getUserData(t);
99+
Serial.println(id);
100+
}
101+
Serial.println("Done ...");
102+
103+
}
104+
105+
void loop(void) {}
106+
107+
// END OF FILE

0 commit comments

Comments
 (0)