ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
dht.cpp
Go to the documentation of this file.
1 /*
2  dht.cpp - dht functions class
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #include "../../include/esp3d_config.h"
22 #ifdef DHT_DEVICE
23 #include "dht.h"
24 #include "../../core/settings_esp3d.h"
25 #include "../../core/esp3doutput.h"
26 #include <DHTesp.h>
27 #if defined (WIFI_FEATURE) || defined(ETH_FEATURE)
28 #include "../websocket/websocket_server.h"
29 #endif // WIFI_FEATURE || ETH_FEATURE
30 
32 DHTesp * dht_device;
33 
34 DHT::DHT()
35 {
36  _temperature =0;
37  _humidity =0;
38  _started = false;
39  _interval = 0;
40  dht_device = nullptr;
41 #if DHT_UNIT == USE_FAHRENHEIT
42  _usecelsius = false;
43 #else
44  _usecelsius = true;
45 #endif
46 }
47 DHT::~DHT()
48 {
49  end();
50 }
51 
52 bool DHT::begin()
53 {
54  bool res = true;
55  end();
56  uint8_t dhttype= Settings_ESP3D::read_byte(ESP_DHT_TYPE);
57  log_esp3d("DHT %d", dhttype);
58  //No DHT defined - exit is not and error
59  if (dhttype == 0) {
60  return true;
61  }
62  //DHT type is unknown - exit as error
63  if (!((dhttype == DHT11_DEVICE) || (dhttype == DHT22_DEVICE))) {
64  return false;
65  }
66  dht_device = new DHTesp;
67 
68  //dht_device = new DHTesp;
69  if (!dht_device) {
70  return false;
71  }
72  //no need to check pin because it is hard coded
73  dht_device->setup(ESP3D_DHT_PIN, (DHTesp::DHT_MODEL_t)dhttype);
75  log_esp3d("DHT status %d", dht_device->getStatus());
76  if (!dht_device->getStatus()) {
77  res = false;
78  }
79  if (!res) {
80  end();
81  }
82  _lastReadTime = millis();
83  _started = res;
84  return _started;
85 }
86 
87 bool DHT::isCelsiusUnit()
88 {
89  return _usecelsius;
90 }
91 void DHT::setCelsiusUnit(bool set)
92 {
93  _usecelsius = set;
94 }
95 
96 void DHT::end()
97 {
98  if(!_started) {
99  return;
100  }
101  if (dht_device) {
102  delete dht_device;
103  dht_device = nullptr;
104  }
105  _started = false;
106  _interval = 0;
107  _temperature =0;
108  _humidity =0;
109 }
110 
111 uint8_t DHT::GetModel()
112 {
113  if (_started) {
114  return dht_device->getModel();
115  } else {
116  return 0;
117  }
118 }
119 
120 float DHT::getHumidity()
121 {
122  if (_started) {
123  return _humidity;
124  }
125  return 0.0;
126 }
127 
128 float DHT::getTemperature()
129 {
130  if (_started) {
131  if (_usecelsius) {
132  return _temperature;
133  } else {
134  return dht_device->toFahrenheit(dht_device->getTemperature());
135  }
136 
137  }
138  return 0.0;
139 }
140 
141 
142 const char * DHT::GetModelString()
143 {
144  if (_started) {
145  if (dht_device->getModel() == 1) {
146  return "DHT11";
147  } else {
148  return "DHT22";
149  }
150  }
151  return "NONE";
152 }
153 
154 bool DHT::started()
155 {
156  return _started;
157 }
158 
159 bool DHT::setInterval(uint interval)
160 {
161  _interval = interval;
162  return true;
163 }
164 uint DHT::interval()
165 {
166  return _interval;
167 }
168 
169 void DHT::handle()
170 {
171  if (_started) {
172  if ((millis() - _lastReadTime) > _interval) {
173  _temperature = dht_device->getTemperature();
174  _humidity = dht_device->getHumidity();
175  _lastReadTime = millis();
176 #if defined (WIFI_FEATURE) || defined(ETH_FEATURE)
177  String s = "DHT:" ;
178  s += String(_humidity,1);
179  if (s !="nan") {
180  s+="% ";
181  s+= String(_temperature,1);
182  s+= _usecelsius?"C ":"F ";
183  } else {
184  s ="DHT:Disconnected ";
185  }
187 #endif // WIFI_FEATURE || ETH_FEATURE
188  }
189  }
190 }
191 
192 #endif //DHT_DEVICE
ESP_DHT_INTERVAL
#define ESP_DHT_INTERVAL
Definition: settings_esp3d.h:56
DHT::setInterval
bool setInterval(uint interval)
DHT::handle
void handle()
ESP_DHT_TYPE
#define ESP_DHT_TYPE
Definition: settings_esp3d.h:71
DHT::interval
uint interval()
DHT22_DEVICE
#define DHT22_DEVICE
Definition: defines.h:92
DHT::started
bool started()
DHT::end
void end()
websocket_terminal_server
WebSocket_Server websocket_terminal_server
Definition: websocket_server.cpp:33
dht.h
esp3d_DHT
DHT esp3d_DHT
DHT::GetModelString
const char * GetModelString()
WebSocket_Server::pushMSG
void pushMSG(const char *data)
Definition: websocket_server.cpp:37
DHT::begin
bool begin()
DHT::getHumidity
float getHumidity()
DHT::DHT
DHT()
DHT11_DEVICE
#define DHT11_DEVICE
Definition: defines.h:91
DHT::getTemperature
float getTemperature()
DHT::setCelsiusUnit
void setCelsiusUnit(bool set)
log_esp3d
#define log_esp3d(format,...)
Definition: debug_esp3d.h:29
Settings_ESP3D::read_uint32
static uint32_t read_uint32(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:919
DHT
Definition: dht.h:26
Settings_ESP3D::read_byte
static uint8_t read_byte(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:715
DHT::~DHT
~DHT()
DHT::GetModel
uint8_t GetModel()
DHT::isCelsiusUnit
bool isCelsiusUnit()