-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from RobTillaart/master
Created 2 examples of how to use the Alarm field as an ID
- Loading branch information
Showing
2 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// | ||
// FILE: UserDataDemo.ino | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.0 | ||
// PURPOSE: use of alarm field as user identification demo | ||
// DATE: 2019-12-23 | ||
// URL: | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
#include <OneWire.h> | ||
#include <DallasTemperature.h> | ||
|
||
#define ONE_WIRE_BUS 2 | ||
|
||
OneWire oneWire(ONE_WIRE_BUS); | ||
DallasTemperature sensors(&oneWire); | ||
|
||
uint8_t deviceCount = 0; | ||
|
||
// Add 4 prepared sensors to the bus | ||
// use the UserDataWriteBatch demo to prepare 4 different labeled sensors | ||
struct | ||
{ | ||
int id; | ||
DeviceAddress addr; | ||
} T[4]; | ||
|
||
float getTempByID(int id) | ||
{ | ||
for (uint8_t index = 0; index < deviceCount; index++) | ||
{ | ||
if (T[index].id == id) | ||
{ | ||
return sensors.getTempC(T[index].addr); | ||
} | ||
} | ||
return -999; | ||
} | ||
|
||
void printAddress(DeviceAddress deviceAddress) | ||
{ | ||
for (uint8_t i = 0; i < 8; i++) | ||
{ | ||
// zero pad the address if necessary | ||
if (deviceAddress[i] < 16) Serial.print("0"); | ||
Serial.print(deviceAddress[i], HEX); | ||
} | ||
} | ||
|
||
void setup(void) | ||
{ | ||
Serial.begin(115200); | ||
Serial.println(__FILE__); | ||
Serial.println("Dallas Temperature Demo"); | ||
|
||
sensors.begin(); | ||
|
||
// count devices | ||
deviceCount = sensors.getDeviceCount(); | ||
Serial.print("#devices: "); | ||
Serial.println(deviceCount); | ||
|
||
// Read ID's per sensor | ||
// and put them in T array | ||
for (uint8_t index = 0; index < deviceCount; index++) | ||
{ | ||
// go through sensors | ||
sensors.getAddress(T[index].addr, index); | ||
T[index].id = sensors.getUserData(T[index].addr); | ||
} | ||
|
||
// Check all 4 sensors are set | ||
for (uint8_t index = 0; index < deviceCount; index++) | ||
{ | ||
Serial.println(); | ||
Serial.println(T[index].id); | ||
printAddress(T[index].addr); | ||
Serial.println(); | ||
} | ||
Serial.println(); | ||
|
||
} | ||
|
||
|
||
void loop(void) | ||
{ | ||
Serial.println(); | ||
Serial.print(millis()); | ||
Serial.println("\treq temp"); | ||
sensors.requestTemperatures(); | ||
|
||
Serial.print(millis()); | ||
Serial.println("\tGet temp by address"); | ||
for (int i = 0; i < 4; i++) | ||
{ | ||
Serial.print(millis()); | ||
Serial.print("\t temp:\t"); | ||
Serial.println(sensors.getTempC(T[i].addr)); | ||
} | ||
|
||
Serial.print(millis()); | ||
Serial.println("\tGet temp by ID"); // assume ID = 0, 1, 2, 3 | ||
for (int id = 0; id < 4; id++) | ||
{ | ||
Serial.print(millis()); | ||
Serial.print("\t temp:\t"); | ||
Serial.println(getTempByID(id)); | ||
} | ||
|
||
delay(1000); | ||
} | ||
|
||
// END OF FILE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// | ||
// FILE: UserDataWriteBatch.ino | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.0 | ||
// PURPOSE: use of alarm field as user identification demo | ||
// DATE: 2019-12-23 | ||
// URL: | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
#include <OneWire.h> | ||
#include <DallasTemperature.h> | ||
|
||
#define ONE_WIRE_BUS 2 | ||
|
||
OneWire oneWire(ONE_WIRE_BUS); | ||
DallasTemperature sensors(&oneWire); | ||
|
||
uint8_t deviceCount = 0; | ||
|
||
void printAddress(DeviceAddress deviceAddress) | ||
{ | ||
for (uint8_t i = 0; i < 8; i++) | ||
{ | ||
// zero pad the address if necessary | ||
if (deviceAddress[i] < 16) Serial.print("0"); | ||
Serial.print(deviceAddress[i], HEX); | ||
} | ||
} | ||
|
||
|
||
|
||
void setup(void) | ||
{ | ||
Serial.begin(115200); | ||
Serial.println(__FILE__); | ||
Serial.println("Write user ID to DS18B20\n"); | ||
|
||
sensors.begin(); | ||
|
||
// count devices | ||
deviceCount = sensors.getDeviceCount(); | ||
Serial.print("#devices: "); | ||
Serial.println(deviceCount); | ||
|
||
Serial.println(); | ||
Serial.println("current ID's"); | ||
for (uint8_t index = 0; index < deviceCount; index++) | ||
{ | ||
DeviceAddress t; | ||
sensors.getAddress(t, index); | ||
printAddress(t); | ||
Serial.print("\t\tID: "); | ||
int id = sensors.getUserData(t); | ||
Serial.println(id); | ||
} | ||
|
||
Serial.println(); | ||
Serial.print("Enter ID for batch: "); | ||
int c = 0; | ||
int id = 0; | ||
while (c != '\n' && c != '\r') | ||
{ | ||
c = Serial.read(); | ||
switch(c) | ||
{ | ||
case '0'...'9': | ||
id *= 10; | ||
id += (c - '0'); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
Serial.println(); | ||
Serial.println(id); | ||
Serial.println(); | ||
|
||
Serial.println("Start labeling ..."); | ||
for (uint8_t index = 0; index < deviceCount; index++) | ||
{ | ||
Serial.print("."); | ||
DeviceAddress t; | ||
sensors.getAddress(t, index); | ||
sensors.setUserData(t, id); | ||
} | ||
Serial.println(); | ||
|
||
Serial.println(); | ||
Serial.println("Show results ..."); | ||
for (uint8_t index = 0; index < deviceCount; index++) | ||
{ | ||
DeviceAddress t; | ||
sensors.getAddress(t, index); | ||
printAddress(t); | ||
Serial.print("\t\tID: "); | ||
int id = sensors.getUserData(t); | ||
Serial.println(id); | ||
} | ||
Serial.println("Done ..."); | ||
|
||
} | ||
|
||
void loop(void) {} | ||
|
||
// END OF FILE |