Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DS18x20.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <unistd.h>
3 
4 #include "../hubolib.h"
5 
6 using namespace std;
7 
8 using namespace HuboLib;
9 
10 /*
11 Compile and link:
12  g++ DS18x20.cpp -L../ -lhubo -lpthread -lrt -o DS18x20.out
13 Run:
14  ./DS18x20.out
15 Purpose:
16  This demo shows how to list all 1wire devices, how to check whether a devixe is of the
17  type of a DS18S20 or DS18B20 sensor and how to read the data from the sensor.
18  It does not require further initialization.
19 */
20 
21 void PrintTemperatureInfo (vector <string>& devList);
22 
23 int main(void)
24 {
25  vector <string> devList;
26 
27  printf ("DS18x20 demo. Press Stg-C to exit.\n");
28 
29  while (1)
30  {
31  Get_1w_Devices(devList);
32  PrintTemperatureInfo(devList);
33  usleep (1000L * 10L);
34  }
35  return 0;
36 }
37 
38 
39 void PrintTemperatureInfo (vector <string>& devList)
40 {
41  double temperature;
42  bool bCRC;
43  long t_duration_ms;
44 
45  // For each device in the list...
46  for (int i=0; i<devList.size(); i++)
47  {
48  // ... print the device ID...
49  printf ("%02d. %s: ", i+1, devList[i].c_str());
50 
51  // ... and if it's a DS18x20 temperature sensor...
52  if (Is_DS18x20_Devices(devList[i].c_str()))
53  {
54  // ... retrieve the data.
55  bool bResult = Get_DS18x20_Temperature (devList[i].c_str(), temperature, bCRC, t_duration_ms);
56  printf ("Result=%s ", bResult?"true":"false");
57  printf ("bCRC=%s ", bCRC?"true":"false");
58  printf ("temp=%lf duration=%ld\n", temperature, t_duration_ms);
59  }
60  else
61  printf ("\n");
62  }
63 }
int main(void)
Definition: DS18x20.cpp:23
bool Is_DS18x20_Devices(const char *pSensorID)
Check whether a 1wire device name refers to a DS18S20 or DS18B20 temperature sensor.
bool Get_DS18x20_Temperature(const char *pSensorID, double &temperature, bool &bCRC, long &t_duration_ms)
Retrieve temperature, CRC value and the time that was required to read the temperature.
bool Get_1w_Devices(std::vector< std::string > &deviceList)
Returns a list of 1wire devices found on the bus.
void PrintTemperatureInfo(vector< string > &devList)
Definition: DS18x20.cpp:39