upm  0.8.0
Sensor/Actuator repository for libmraa (v1.1.1)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Public Types | Public Member Functions | Static Public Attributes | Protected Attributes
DS18B20 Class Reference

API for the DS18B20 1-Wire Temperature Sensor. More...

Detailed Description

This driver supports, and was tested with, a DS18B20 with external power.

Multiple DS18B20 devices can be connected to this bus. This module will identify all such devices connected, and allow you to access them using an index starting at 0.

Parasitic power is not currently supported due to the very tight 10us limit on switching a GPIO properly to supply power during certain operations. For this reason, you should use external power for your sensors.

Setting the alarm values (Tl, Th) is also not supported, since this is only useful when doing a 1-wire device search looking for devices in an alarm state, a capability not yet supported in MRAA. In reality, this is trivial to handle yourself in your application.

This device requires the use of a UART to provide access to a Dallas 1-wire bus, via a new facility supported by MRAA (once the relevant PR is accepted), using the UartOW access class. It is important to realize that the UART is only being used to access and control a Dallas 1-wire compliant bus, it is not actually a UART device.

cout << "Initializing..." << endl;
// Instantiate an DS18B20 instance using the default values (uart 0)
upm::DS18B20 sensor;
// locate and setup our devices
sensor.init();
cout << "Found " << sensor.devicesFound() << " device(s)" << endl;
cout << endl;
// bail if we didn't find anything
if (!sensor.devicesFound())
return 1;
// update and print available values every second
while (shouldRun)
{
// update our values for the first sensor
sensor.update(0);
// we show both C and F for temperature for the first sensor
cout << "Temperature: " << sensor.getTemperature(0)
<< " C / " << sensor.getTemperature(0, true) << " F"
<< endl;
sleep(1);
}
cout << "Exiting..." << endl;

Data Structures

struct  sensor_info_t
 

Public Types

enum  CMD_T {
  CMD_CONVERT = 0x44, CMD_WRITE_SCRATCHPAD = 0x4e, CMD_READ_SCRATCHPAD = 0xbe, CMD_COPY_SCRATCHPAD = 0x48,
  CMD_RECALL_EEPROM = 0xb8, CMD_READ_POWER_SUPPLY = 0xb4
}
 
enum  CFG_BITS_T { CFG_RESOLUTION_R0 = 0x20, CFG_RESOLUTION_R1 = 0x40, _CFG_RESOLUTION_MASK = 3, _CFG_RESOLUTION_SHIFT = 5 }
 
enum  RESOLUTIONS_T { RESOLUTION_9BITS = 0, RESOLUTION_10BITS = 1, RESOLUTION_11BITS = 2, RESOLUTION_12BITS = 3 }
 

Public Member Functions

 DS18B20 (int uart=DS18B20_DEFAULT_UART)
 
 ~DS18B20 ()
 
void init ()
 
void update (int index=-1)
 
float getTemperature (int index, bool fahrenheit=false)
 
void setResolution (int index, RESOLUTIONS_T res)
 
void copyScratchPad (int index)
 
void recallEEPROM (int index)
 
int devicesFound ()
 
std::string getId (int index)
 

Static Public Attributes

static const uint8_t DS18B20_FAMILY_CODE = 0x28
 

Protected Attributes

mraa::UartOW m_uart
 
int m_devicesFound
 
std::map< int, sensor_info_tm_deviceMap
 

Constructor & Destructor Documentation

DS18B20 ( int  uart = DS18B20_DEFAULT_UART)

DS18B20 object constructor

Parameters
uartDefault UART to use (0 or 1). Default is 0.
~DS18B20 ( )

DS18B20 object destructor

Member Function Documentation

void init ( )

This method will search the 1-wire bus and store information on each device detected on the bus. If no devices are found, an exception is thrown. Once this function completes successfully, you can use devicesFound() to determine how many devices were detected. This method must be executed first before any others below.

void update ( int  index = -1)

Update our stored temperature for a device. This method must be called prior to getTemperature().

Parameters
indexThe device index to access (starts at 0). Specify -1 to query all detected devices. Default: -1
float getTemperature ( int  index,
bool  fahrenheit = false 
)

Get the current temperature. update() must have been called prior to calling this method.

Parameters
indexThe device index to access (starts at 0).
fahrenheittrue to return the temperature in degrees fahrenheit, false to return the temperature in degrees celsius. The default is false (degrees Celsius).
Returns
The last temperature reading in Celsius or Fahrenheit
void setResolution ( int  index,
RESOLUTIONS_T  res 
)

Set the device resolution for a device. These devices support 9, 10, 11, and 12 bits of resolution, with the default from the factory at 12 bits.

Parameters
indexThe device index to access (starts at 0).
resOne of the RESOLUTIONS_T values
void copyScratchPad ( int  index)

Copy the device's scratchpad memory to the EEPROM. This includes the configuration byte (resolution).

Parameters
indexThe device index to access (starts at 0).
void recallEEPROM ( int  index)

Copy the device's EEPROM memory to the scratchpad. This method will return when the copy completes. This operation is performed by the device automatically on power up, so it is rarely needed.

Parameters
indexThe device index to access (starts at 0).
int devicesFound ( )
inline

This method will return the number of DS18B20 devices that were found on the bus by init().

Returns
number of DS18B20's that were found on the bus
std::string getId ( int  index)
inline

Return an 8 byte string representing the unique device ID (1-wire romcode) for a given device index.

Parameters
indexThe device index to access (starts at 0).
Returns
8 byte string representing the 1-wire device's unique romcode.

The documentation for this class was generated from the following files: