-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESP_SHD_TMP36.cpp
69 lines (55 loc) · 1.44 KB
/
ESP_SHD_TMP36.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
#include "ESP_SHD_TMP36.h"
#define DEBUG 0
ShdTmp36Sensor::ShdTmp36Sensor(){
timerCounter = 0;
snprintf (pubTopic, 50, "%s/Temperature", name);
// debug output:
#if DEBUG > 0
Serial.print("TMP: New temperature sensor registered. It publishes to ");
Serial.println(pubTopic);
Serial.print();
#endif
}
bool ShdTmp36Sensor::handleMqttRequest(char* _topic, unsigned char* _payload, uint16_t _length){
return false;
}
void ShdTmp36Sensor::timer5msHandler(){
timerCounter++;
if (timerCounter > 6000) {
publishTemperature();
timerCounter = 0;
}
}
void ShdTmp36Sensor::publishTemperature() {
int adcValue = adcMeasurement();
float temperature = 0.07326 * adcValue - 39.9767;
for (size_t i = 0; i < 10; i++) {
message[i] = 0;
}
dtostrf(temperature, 3, 1, message);
if (!mqttConnected()) {
timerCounter = 5999; // set close to 6000, so next time timer5ms() is called, it's trying again
return;
}
if (mqttPublish(pubTopic, message)) {
#if DEBUG > 1
Serial.print("Temperature published to ");
Serial.print(pubTopic);
Serial.print(" at ");
Serial.print(millis());
Serial.print(" ms: ");
Serial.println(message);
#endif
}
}
int ShdTmp36Sensor::adcMeasurement() {
double adcValue = 0;
for (uint8_t i = 0; i < OVERSAMPLING; i++) {
adcValue += analogRead(A0);
}
adcValue /= OVERSAMPLING;
return (int)adcValue;
}
void ShdTmp36Sensor::republish() {
publishTemperature();
}