Skip to content

Commit 91a1b3c

Browse files
committed
Adding alarm search example sketch
1 parent cc8a517 commit 91a1b3c

File tree

10 files changed

+200
-67
lines changed

10 files changed

+200
-67
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ The OWI library has been developed to support the implementation of
33
1-wire device drivers. The library includes an example device driver
44
and a bus scanner.
55

6-
Version: 1.4
6+
Version: 1.5
77

88
## Classes
99

@@ -16,6 +16,8 @@ Version: 1.4
1616
* [DS18B20](./examples/DS18B20)
1717
* [DS1990A](./examples/DS1990A)
1818
* [Scanner](./examples/Scanner)
19+
* [Search](./examples/Search)
20+
* [Alarm](./examples/Alarm)
1921

2022
## Dependencies
2123

examples/Alarm/Alarm.ino

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "RTC.h"
2+
#include "GPIO.h"
3+
#include "OWI.h"
4+
#include "Software/OWI.h"
5+
#include "Driver/DS18B20.h"
6+
7+
#if defined(ARDUINO_attiny)
8+
#include "Software/Serial.h"
9+
Software::Serial<BOARD::D0> Serial;
10+
Software::OWI<BOARD::D1> owi;
11+
#else
12+
Software::OWI<BOARD::D7> owi;
13+
#endif
14+
15+
DS18B20 sensor(owi);
16+
RTC rtc;
17+
18+
void setup()
19+
{
20+
Serial.begin(57600);
21+
while (!Serial);
22+
23+
// Set sensor alarm triggers (20..25 C) and resolution (10 bits)
24+
// Iterate though all thermometers and configure.
25+
uint8_t* rom = sensor.rom();
26+
int8_t last = owi.FIRST;
27+
do {
28+
last = owi.search_rom(sensor.FAMILY_CODE, rom, last);
29+
if (last == owi.ERROR) break;
30+
sensor.resolution(10);
31+
sensor.set_trigger(20, 25);
32+
sensor.write_scratchpad();
33+
} while (last != owi.LAST);
34+
}
35+
36+
void loop()
37+
{
38+
// Check if any thermometer sersors have exceeded thresholds.
39+
// Broadcast a convert request to all thermometer sensors.
40+
// Print timestamp and sensor identity and temperature.
41+
42+
int8_t last = owi.FIRST;
43+
uint8_t* rom = sensor.rom();
44+
bool triggered = false;
45+
46+
if (!rtc.tick()) return;
47+
if (!sensor.convert_request(true)) return;
48+
do {
49+
last = owi.alarm_search(rom, last);
50+
if (last == owi.ERROR) break;
51+
sensor.read_scratchpad(false);
52+
if (!triggered) {
53+
char daytime[32];
54+
struct tm now;
55+
rtc.get_time(now);
56+
isotime_r(&now, daytime);
57+
Serial.print(daytime + 11);
58+
triggered = true;
59+
}
60+
Serial.print(F(", "));
61+
for (size_t i = 1; i < owi.ROM_MAX - 1; i++) {
62+
if (rom[i] < 0x10) Serial.print(0);
63+
Serial.print(rom[i], HEX);
64+
}
65+
Serial.print(F(", "));
66+
Serial.print(sensor.temperature());
67+
} while (last != owi.LAST);
68+
if (triggered) Serial.println();
69+
}

examples/DS18B20/DS18B20.ino

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,63 @@ void setup()
2121

2222
void loop()
2323
{
24-
uint8_t rom[owi.ROM_MAX] = { 0 };
25-
int8_t last = owi.FIRST;
26-
int i = 0;
27-
2824
// Broadcast a convert request to all thermometer sensors
25+
// Print list of sensors, rom code, and temperature
26+
2927
if (!sensor.convert_request(true)) return;
3028

31-
// Print list of sensors and temperature
29+
int8_t last = owi.FIRST;
30+
uint8_t* rom = sensor.rom();
31+
int id = 0;
3232
do {
33+
// Search for the next digital thermometer
3334
last = owi.search_rom(sensor.FAMILY_CODE, rom, last);
3435
if (last == owi.ERROR) break;
36+
37+
// Read the scratchpad with current temperature, tiggers, etc
3538
sensor.read_scratchpad(false);
36-
Serial.print(i++);
37-
Serial.print(F(":ROM:"));
38-
for (size_t i = 0; i < sizeof(rom); i++) {
39-
Serial.print(' ');
40-
Serial.print(rom[i], HEX);
41-
}
39+
int8_t low, high;
40+
sensor.get_trigger(low, high);
41+
42+
// Print sequence number
43+
Serial.print(id++);
4244
Serial.print(F(": "));
45+
46+
// Print family code
47+
Serial.print(F("family="));
48+
Serial.print(rom[0], HEX);
49+
50+
// Print serial number
51+
Serial.print(F(", sn="));
52+
size_t i = 1;
53+
do {
54+
if (rom[i] < 0x10) Serial.print(0);
55+
Serial.print(rom[i], HEX);
56+
i += 1;
57+
} while (i < owi.ROM_MAX - 1);
58+
59+
// Print cyclic redundancy check sum
60+
Serial.print(F(", crc="));
61+
if (rom[i] < 0x10) Serial.print(0);
62+
Serial.print(rom[i], HEX);
63+
64+
// Print conversion resolution
65+
Serial.print(F(", resolution="));
66+
Serial.print(sensor.resolution());
67+
68+
// Print alarm trigger threshols
69+
Serial.print(F(", trigger=["));
70+
Serial.print(low);
71+
Serial.print(F(".."));
72+
Serial.print(high);
73+
74+
// And temperature
75+
Serial.print(F("], temperature="));
4376
Serial.print(sensor.temperature());
4477
Serial.println(F(" C"));
78+
4579
} while (last != owi.LAST);
46-
Serial.println();
4780

81+
Serial.println();
4882
delay(5000);
4983
}

examples/DS1990A/DS1990A.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22
#include "GPIO.h"
33
#include "Software/OWI.h"
44

5-
// Table with valid keys (64 bit 1-Wire rom code, 8 bytes per entry)
6-
const uint8_t KEY[] PROGMEM = {
7-
0x01, 0x23, 0x81, 0xa3, 0x09, 0x00, 0x00, 0x7b,
8-
0x01, 0x29, 0x01, 0x27, 0x09, 0x00, 0x00, 0xa8,
9-
0x01, 0x26, 0xd9, 0x3e, 0x09, 0x00, 0x00, 0x47
10-
};
11-
125
#if defined(ARDUINO_attiny)
136
GPIO<BOARD::D0> led;
147
Software::OWI<BOARD::D1> owi;
@@ -17,6 +10,13 @@ GPIO<BOARD::D13> led;
1710
Software::OWI<BOARD::D7> owi;
1811
#endif
1912

13+
// Table with valid keys (64 bit 1-Wire rom code, 8 bytes per entry)
14+
const uint8_t KEY[] PROGMEM = {
15+
0x01, 0x23, 0x81, 0xa3, 0x09, 0x00, 0x00, 0x7b,
16+
0x01, 0x29, 0x01, 0x27, 0x09, 0x00, 0x00, 0xa8,
17+
0x01, 0x26, 0xd9, 0x3e, 0x09, 0x00, 0x00, 0x47
18+
};
19+
2020
void setup()
2121
{
2222
led.output();

examples/Scanner/Scanner.ino

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,41 @@ void setup()
1818

1919
void loop()
2020
{
21+
// Scan one-wire bus and print rom code for all detected devices
22+
// Print family, serial number and cyclic redundancy check sum
2123
uint8_t rom[owi.ROM_MAX] = { 0 };
2224
int8_t last = owi.FIRST;
2325
int id = 0;
2426
size_t i;
2527

26-
// Scan one-wire bus and print rom code for all detected devices
2728
do {
2829
last = owi.search_rom(0, rom, last);
2930
if (last == owi.ERROR) break;
31+
32+
// Print sequence number
3033
Serial.print(id++);
31-
Serial.print(':');
34+
Serial.print(F(": "));
35+
36+
// Print family code
3237
Serial.print(F("family="));
3338
Serial.print(rom[0], HEX);
39+
40+
// Print serial number
3441
Serial.print(F(", sn="));
35-
for (i = 1; i < sizeof(rom) - 1; i++) {
42+
size_t i = 1;
43+
do {
44+
if (rom[i] < 0x10) Serial.print(0);
3645
Serial.print(rom[i], HEX);
37-
if (i < sizeof(rom) - 2) Serial.print(' ');
38-
}
46+
i += 1;
47+
} while (i < owi.ROM_MAX - 1);
48+
49+
// Print cyclic redundancy check sum
3950
Serial.print(F(", crc="));
51+
if (rom[i] < 0x10) Serial.print(0);
4052
Serial.println(rom[i], HEX);
53+
4154
} while (last != owi.LAST);
42-
Serial.println();
4355

56+
Serial.println();
4457
delay(5000);
4558
}

examples/Search/Search.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ void setup()
1818

1919
void loop()
2020
{
21+
// Scan one-wire bus and print rom code for all detected devices
22+
// on binary format and indicate discrepancy position.
2123
uint8_t rom[owi.ROM_MAX] = { 0 };
2224
int8_t last = owi.FIRST;
23-
24-
// Scan one-wire bus and print rom code for all detected devices
25+
int i = 0;
2526
do {
2627
last = owi.search_rom(0, rom, last);
2728
if (last == owi.ERROR) break;
29+
int pos = Serial.print(i++);
30+
pos += Serial.print(':');
2831
for (size_t i = 0; i < sizeof(rom); i++)
2932
for (uint8_t mask = 0x80; mask != 0; mask >>= 1)
30-
Serial.print(rom[i] & mask ? '1' : '0' );
33+
Serial.print((rom[i] & mask) != 0);
3134
Serial.println();
32-
for (int i = 0; i < last - 1; i++) Serial.print('-');
35+
for (int i = 0; i < last - 1 + pos; i++) Serial.print('-');
3336
Serial.println('*');
3437
} while (last != owi.LAST);
3538
Serial.println();

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Arduino-OWI
2-
version=1.4
2+
version=1.5
33
author=Mikael Patel
44
maintainer=Mikael Patel <mikael.patel@gmail.com>
55
sentence=One-Wire Interface (OWI) library for Arduino.

mainpage.dox

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The OWI library has been developed to support the implementation of
44
1-wire device drivers. Includes abstract OWI bus manager class, GPIO
55
based Software::OWI bus manager and device driver for DS18B20.
66

7-
Version: 1.4
7+
Version: 1.5
88
*/
99

1010
/** @page License

src/Driver/DS18B20.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
* (Dn)----------+-----2-|DQ | |
3434
* (VCC)---------------3-|VDD |/
3535
* +------------+
36-
*
3736
* @endcode
3837
*
3938
* @section References
@@ -44,14 +43,17 @@ class DS18B20 : public OWI::Device {
4443
/** Device family code. */
4544
static const uint8_t FAMILY_CODE = 0x28;
4645

46+
/** Max conversion time for 12-bit conversion in milli-seconds. */
47+
static const uint16_t MAX_CONVERSION_TIME = 750;
48+
4749
/**
4850
* Construct a DS18B20 device connected to the given 1-Wire bus.
4951
* @param[in] owi bus manager.
5052
* @param[in] rom code (default NULL).
5153
*/
5254
DS18B20(OWI& owi, uint8_t* rom = NULL) :
5355
OWI::Device(owi, rom),
54-
m_start(0L),
56+
m_start(0),
5557
m_converting(false)
5658
{}
5759

@@ -81,9 +83,7 @@ class DS18B20 : public OWI::Device {
8183
/**
8284
* Get the latest temperature reading from the local memory scratchpad.
8385
* Call convert_request() and read_scratchpad() before accessing the
84-
* scratchpad. Returns at highest resolution a fixed point<12,4>
85-
* point number. For 11-bit resolution, bit 0 is undefined, 10-bits
86-
* bit 1 and 0, and so on (LSB).
86+
* scratchpad.
8787
* @return temperature
8888
*/
8989
float temperature() const
@@ -114,8 +114,9 @@ class DS18B20 : public OWI::Device {
114114
}
115115

116116
/**
117-
* Initiate temperature conversion.
118-
* @param[in] broadcast flag.
117+
* Initiate temperature conversion. Call with broadcast parameter
118+
* true(1) to issue skip_rom().
119+
* @param[in] broadcast flag (default false).
119120
* @return true(1) if successful otherwise false(0).
120121
*/
121122
bool convert_request(bool broadcast = false)
@@ -126,7 +127,7 @@ class DS18B20 : public OWI::Device {
126127
else {
127128
if (!m_owi.match_rom(m_rom)) return (false);
128129
}
129-
m_owi.write(CONVERT_T, CHARBITS);
130+
m_owi.write(CONVERT_T);
130131
m_start = millis();
131132
m_converting = true;
132133
return (true);
@@ -135,15 +136,18 @@ class DS18B20 : public OWI::Device {
135136
/**
136137
* Read the contents of the scratchpad to local memory. An internal
137138
* delay will occur if a convert_request() is pending. The delay is
138-
* at most max conversion time (750 ms).
139-
* @param[in] match rom code.
139+
* at most max conversion time (750 ms). Call with match parameter
140+
* false if used with search_rom().
141+
* @param[in] match rom code (default true).
140142
* @return true(1) if successful otherwise false(0).
141143
*/
142144
bool read_scratchpad(bool match = true)
143145
{
146+
// Check if a conversion is in progress
144147
if (m_converting) {
145-
int32_t ms = millis() - m_start;
148+
uint16_t ms = millis() - m_start;
146149
uint16_t conv_time = (MAX_CONVERSION_TIME >> (12 - resolution()));
150+
// May need to wait for the conversion to complete
147151
if (ms < conv_time) {
148152
ms = conv_time - ms;
149153
delay(ms);
@@ -220,12 +224,9 @@ class DS18B20 : public OWI::Device {
220224
static const uint8_t CONFIG_MAX = 3;
221225

222226
/** Watchdog millis on convert_request(). */
223-
uint32_t m_start;
227+
uint16_t m_start;
224228

225229
/** Convert request pending. */
226-
uint8_t m_converting;
227-
228-
/** Max conversion time for 12-bit conversion in milli-seconds. */
229-
static const uint16_t MAX_CONVERSION_TIME = 750;
230+
bool m_converting;
230231
};
231232
#endif

0 commit comments

Comments
 (0)