-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathGravityTemperature.cpp
107 lines (96 loc) · 3.3 KB
/
GravityTemperature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*********************************************************************
* GravityTemperature.cpp
*
* Copyright (C) 2017 [DFRobot](http://www.dfrobot.com),
* GitHub Link :https://github.com/DFRobot/watermonitor
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Description:
*
* author : Jason(jason.ling@dfrobot.com)
* version : V1.0
* date : 2017-04-12
**********************************************************************/
#include "GravityTemperature.h"
#include <OneWire.h>
#include "Debug.h"
GravityTemperature::GravityTemperature(int pin)
{
this->oneWire = new OneWire(pin);
}
GravityTemperature::~GravityTemperature()
{
}
//********************************************************************************************
// function name: setup ()
// Function Description: Initializes the sensor
//********************************************************************************************
void GravityTemperature::setup()
{
}
//********************************************************************************************
// function name: update ()
// Function Description: Update the sensor value
//********************************************************************************************
void GravityTemperature::update()
{
if ( millis () - tempSampleTime >= tempSampleInterval)
{
tempSampleTime = millis ();
temperature = TempProcess(ReadTemperature); // read the current temperature from the DS18B20
TempProcess(StartConvert); //after the reading,start the convert for next reading
}
}
//********************************************************************************************
// function name: getValue ()
// Function Description: Returns the sensor data
//********************************************************************************************
double GravityTemperature::getValue()
{
return temperature;
}
//********************************************************************************************
// function name: TempProcess ()
// Function Description: Analyze the temperature data
//********************************************************************************************
double GravityTemperature::TempProcess(bool ch)
{
static byte data[12];
static byte addr[8];
static float TemperatureSum;
if (!ch) {
if (!oneWire->search(addr)) {
Debug::println("no temperature sensors on chain, reset search!");
oneWire->reset_search();
return 0;
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Debug::println("CRC is not valid!");
return 0;
}
if (addr[0] != 0x10 && addr[0] != 0x28) {
Debug::println("Device is not recognized!");
return 0;
}
oneWire->reset();
oneWire->select(addr);
oneWire->write(0x44, 1); // start conversion, with parasite power on at the end
}
else {
byte present = oneWire->reset();
oneWire->select(addr);
oneWire->write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = oneWire->read();
}
oneWire->reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
TemperatureSum = tempRead / 16;
}
return TemperatureSum;
}