Skip to content

Commit

Permalink
Adding alarm search example sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelpatel committed Oct 5, 2017
1 parent cc8a517 commit 91a1b3c
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 67 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ The OWI library has been developed to support the implementation of
1-wire device drivers. The library includes an example device driver
and a bus scanner.

Version: 1.4
Version: 1.5

## Classes

Expand All @@ -16,6 +16,8 @@ Version: 1.4
* [DS18B20](./examples/DS18B20)
* [DS1990A](./examples/DS1990A)
* [Scanner](./examples/Scanner)
* [Search](./examples/Search)
* [Alarm](./examples/Alarm)

## Dependencies

Expand Down
69 changes: 69 additions & 0 deletions examples/Alarm/Alarm.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "RTC.h"
#include "GPIO.h"
#include "OWI.h"
#include "Software/OWI.h"
#include "Driver/DS18B20.h"

#if defined(ARDUINO_attiny)
#include "Software/Serial.h"
Software::Serial<BOARD::D0> Serial;
Software::OWI<BOARD::D1> owi;
#else
Software::OWI<BOARD::D7> owi;
#endif

DS18B20 sensor(owi);
RTC rtc;

void setup()
{
Serial.begin(57600);
while (!Serial);

// Set sensor alarm triggers (20..25 C) and resolution (10 bits)
// Iterate though all thermometers and configure.
uint8_t* rom = sensor.rom();
int8_t last = owi.FIRST;
do {
last = owi.search_rom(sensor.FAMILY_CODE, rom, last);
if (last == owi.ERROR) break;
sensor.resolution(10);
sensor.set_trigger(20, 25);
sensor.write_scratchpad();
} while (last != owi.LAST);
}

void loop()
{
// Check if any thermometer sersors have exceeded thresholds.
// Broadcast a convert request to all thermometer sensors.
// Print timestamp and sensor identity and temperature.

int8_t last = owi.FIRST;
uint8_t* rom = sensor.rom();
bool triggered = false;

if (!rtc.tick()) return;
if (!sensor.convert_request(true)) return;
do {
last = owi.alarm_search(rom, last);
if (last == owi.ERROR) break;
sensor.read_scratchpad(false);
if (!triggered) {
char daytime[32];
struct tm now;
rtc.get_time(now);
isotime_r(&now, daytime);
Serial.print(daytime + 11);
triggered = true;
}
Serial.print(F(", "));
for (size_t i = 1; i < owi.ROM_MAX - 1; i++) {
if (rom[i] < 0x10) Serial.print(0);
Serial.print(rom[i], HEX);
}
Serial.print(F(", "));
Serial.print(sensor.temperature());
} while (last != owi.LAST);
if (triggered) Serial.println();
}
58 changes: 46 additions & 12 deletions examples/DS18B20/DS18B20.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,63 @@ void setup()

void loop()
{
uint8_t rom[owi.ROM_MAX] = { 0 };
int8_t last = owi.FIRST;
int i = 0;

// Broadcast a convert request to all thermometer sensors
// Print list of sensors, rom code, and temperature

if (!sensor.convert_request(true)) return;

// Print list of sensors and temperature
int8_t last = owi.FIRST;
uint8_t* rom = sensor.rom();
int id = 0;
do {
// Search for the next digital thermometer
last = owi.search_rom(sensor.FAMILY_CODE, rom, last);
if (last == owi.ERROR) break;

// Read the scratchpad with current temperature, tiggers, etc
sensor.read_scratchpad(false);
Serial.print(i++);
Serial.print(F(":ROM:"));
for (size_t i = 0; i < sizeof(rom); i++) {
Serial.print(' ');
Serial.print(rom[i], HEX);
}
int8_t low, high;
sensor.get_trigger(low, high);

// Print sequence number
Serial.print(id++);
Serial.print(F(": "));

// Print family code
Serial.print(F("family="));
Serial.print(rom[0], HEX);

// Print serial number
Serial.print(F(", sn="));
size_t i = 1;
do {
if (rom[i] < 0x10) Serial.print(0);
Serial.print(rom[i], HEX);
i += 1;
} while (i < owi.ROM_MAX - 1);

// Print cyclic redundancy check sum
Serial.print(F(", crc="));
if (rom[i] < 0x10) Serial.print(0);
Serial.print(rom[i], HEX);

// Print conversion resolution
Serial.print(F(", resolution="));
Serial.print(sensor.resolution());

// Print alarm trigger threshols
Serial.print(F(", trigger=["));
Serial.print(low);
Serial.print(F(".."));
Serial.print(high);

// And temperature
Serial.print(F("], temperature="));
Serial.print(sensor.temperature());
Serial.println(F(" C"));

} while (last != owi.LAST);
Serial.println();

Serial.println();
delay(5000);
}
14 changes: 7 additions & 7 deletions examples/DS1990A/DS1990A.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
#include "GPIO.h"
#include "Software/OWI.h"

// Table with valid keys (64 bit 1-Wire rom code, 8 bytes per entry)
const uint8_t KEY[] PROGMEM = {
0x01, 0x23, 0x81, 0xa3, 0x09, 0x00, 0x00, 0x7b,
0x01, 0x29, 0x01, 0x27, 0x09, 0x00, 0x00, 0xa8,
0x01, 0x26, 0xd9, 0x3e, 0x09, 0x00, 0x00, 0x47
};

#if defined(ARDUINO_attiny)
GPIO<BOARD::D0> led;
Software::OWI<BOARD::D1> owi;
Expand All @@ -17,6 +10,13 @@ GPIO<BOARD::D13> led;
Software::OWI<BOARD::D7> owi;
#endif

// Table with valid keys (64 bit 1-Wire rom code, 8 bytes per entry)
const uint8_t KEY[] PROGMEM = {
0x01, 0x23, 0x81, 0xa3, 0x09, 0x00, 0x00, 0x7b,
0x01, 0x29, 0x01, 0x27, 0x09, 0x00, 0x00, 0xa8,
0x01, 0x26, 0xd9, 0x3e, 0x09, 0x00, 0x00, 0x47
};

void setup()
{
led.output();
Expand Down
25 changes: 19 additions & 6 deletions examples/Scanner/Scanner.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,41 @@ void setup()

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

// Scan one-wire bus and print rom code for all detected devices
do {
last = owi.search_rom(0, rom, last);
if (last == owi.ERROR) break;

// Print sequence number
Serial.print(id++);
Serial.print(':');
Serial.print(F(": "));

// Print family code
Serial.print(F("family="));
Serial.print(rom[0], HEX);

// Print serial number
Serial.print(F(", sn="));
for (i = 1; i < sizeof(rom) - 1; i++) {
size_t i = 1;
do {
if (rom[i] < 0x10) Serial.print(0);
Serial.print(rom[i], HEX);
if (i < sizeof(rom) - 2) Serial.print(' ');
}
i += 1;
} while (i < owi.ROM_MAX - 1);

// Print cyclic redundancy check sum
Serial.print(F(", crc="));
if (rom[i] < 0x10) Serial.print(0);
Serial.println(rom[i], HEX);

} while (last != owi.LAST);
Serial.println();

Serial.println();
delay(5000);
}
11 changes: 7 additions & 4 deletions examples/Search/Search.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ void setup()

void loop()
{
// Scan one-wire bus and print rom code for all detected devices
// on binary format and indicate discrepancy position.
uint8_t rom[owi.ROM_MAX] = { 0 };
int8_t last = owi.FIRST;

// Scan one-wire bus and print rom code for all detected devices
int i = 0;
do {
last = owi.search_rom(0, rom, last);
if (last == owi.ERROR) break;
int pos = Serial.print(i++);
pos += Serial.print(':');
for (size_t i = 0; i < sizeof(rom); i++)
for (uint8_t mask = 0x80; mask != 0; mask >>= 1)
Serial.print(rom[i] & mask ? '1' : '0' );
Serial.print((rom[i] & mask) != 0);
Serial.println();
for (int i = 0; i < last - 1; i++) Serial.print('-');
for (int i = 0; i < last - 1 + pos; i++) Serial.print('-');
Serial.println('*');
} while (last != owi.LAST);
Serial.println();
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Arduino-OWI
version=1.4
version=1.5
author=Mikael Patel
maintainer=Mikael Patel <[email protected]>
sentence=One-Wire Interface (OWI) library for Arduino.
Expand Down
2 changes: 1 addition & 1 deletion mainpage.dox
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The OWI library has been developed to support the implementation of
1-wire device drivers. Includes abstract OWI bus manager class, GPIO
based Software::OWI bus manager and device driver for DS18B20.

Version: 1.4
Version: 1.5
*/

/** @page License
Expand Down
33 changes: 17 additions & 16 deletions src/Driver/DS18B20.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* (Dn)----------+-----2-|DQ | |
* (VCC)---------------3-|VDD |/
* +------------+
*
* @endcode
*
* @section References
Expand All @@ -44,14 +43,17 @@ class DS18B20 : public OWI::Device {
/** Device family code. */
static const uint8_t FAMILY_CODE = 0x28;

/** Max conversion time for 12-bit conversion in milli-seconds. */
static const uint16_t MAX_CONVERSION_TIME = 750;

/**
* Construct a DS18B20 device connected to the given 1-Wire bus.
* @param[in] owi bus manager.
* @param[in] rom code (default NULL).
*/
DS18B20(OWI& owi, uint8_t* rom = NULL) :
OWI::Device(owi, rom),
m_start(0L),
m_start(0),
m_converting(false)
{}

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

/**
* Initiate temperature conversion.
* @param[in] broadcast flag.
* Initiate temperature conversion. Call with broadcast parameter
* true(1) to issue skip_rom().
* @param[in] broadcast flag (default false).
* @return true(1) if successful otherwise false(0).
*/
bool convert_request(bool broadcast = false)
Expand All @@ -126,7 +127,7 @@ class DS18B20 : public OWI::Device {
else {
if (!m_owi.match_rom(m_rom)) return (false);
}
m_owi.write(CONVERT_T, CHARBITS);
m_owi.write(CONVERT_T);
m_start = millis();
m_converting = true;
return (true);
Expand All @@ -135,15 +136,18 @@ class DS18B20 : public OWI::Device {
/**
* Read the contents of the scratchpad to local memory. An internal
* delay will occur if a convert_request() is pending. The delay is
* at most max conversion time (750 ms).
* @param[in] match rom code.
* at most max conversion time (750 ms). Call with match parameter
* false if used with search_rom().
* @param[in] match rom code (default true).
* @return true(1) if successful otherwise false(0).
*/
bool read_scratchpad(bool match = true)
{
// Check if a conversion is in progress
if (m_converting) {
int32_t ms = millis() - m_start;
uint16_t ms = millis() - m_start;
uint16_t conv_time = (MAX_CONVERSION_TIME >> (12 - resolution()));
// May need to wait for the conversion to complete
if (ms < conv_time) {
ms = conv_time - ms;
delay(ms);
Expand Down Expand Up @@ -220,12 +224,9 @@ class DS18B20 : public OWI::Device {
static const uint8_t CONFIG_MAX = 3;

/** Watchdog millis on convert_request(). */
uint32_t m_start;
uint16_t m_start;

/** Convert request pending. */
uint8_t m_converting;

/** Max conversion time for 12-bit conversion in milli-seconds. */
static const uint16_t MAX_CONVERSION_TIME = 750;
bool m_converting;
};
#endif
Loading

0 comments on commit 91a1b3c

Please sign in to comment.