-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32-temperature-monitor.ino
138 lines (111 loc) · 4.34 KB
/
esp32-temperature-monitor.ino
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "DashioESP.h" // Dash ESP core library
#include "OneWire.h"
#include "DallasTemperature.h" // Library for Dallas Temperature ICs. Supports DS18B20, DS18S20, DS1822, DS1820
#define DEVICE_TYPE "TempMonitor"
#define DEVICE_NAME "Temperature"
// WiFi
#define WIFI_SSID "yourWiFiSSID"
#define WIFI_PASSWORD "yourWiFiPassword"
// MQTT
#define MQTT_USER "yourMQTTuserName"
#define MQTT_PASSWORD "yourMQTTpassword"
#define GRAPH_UPDATE_SECONDS (60 * 10) // Every 10 mins
const char configC64Str[] PROGMEM =
"jVPbbuIwEP2Vys/JyiGBQt/iONCqELrBpSut9iEQFyxCjBynhVb9p/2G/bId59JC2UqrvMzlzFF8Zs4rGpMxuvr5y0K30ZTU0WxM"
"4zpi4Q8G0SsqS5GiK9Tz/aDfdYlNe8S3vbDv2IRSbLvukOKB5w06IUEWWspcK5ndUBhhBDtQ2iWK57qqBHWlzIUuIP3zO4BMC51x"
"A+fbHVeJLhWH6pqL1VrHiRYSXeFvTtfxLgcN+E4WAuo5DEXTKDQcfK/9TKxMKQgjFsZQfJRqm2gDup9AemjHWlL4NcWXoqiYINss"
"UnbY8Q/WZ5HqdYN2LLQ/J1hmsuC3i3Saz3gOMmlV8jdQj940Mk78uyYIo/s6Gk9HdeDPaat6wBp8MG4COp8/VPoX+lDJQ6YxDeOZ"
"0VirbJLshyD1TLxAz+taaKVEGsis3OagbKdjoWINuteV+r8slJfbd4hzuqtmM4aaSJVyBUCpoBHLQ5JdkKzkbXuzytMvu23jYS30"
"yQRTSV5Up7A81NI1SJIly83RGTRDzKRE7lvCKcyv+EXM00+AM2KjRCyf4Y2uewT9kMvpW0jA46NkWy1b5hzVS/MbQwTXMWtsMIrv"
"ro994PWGQScYEtsNKbW9Hu7bfRpimxLSw/6lGw6Jb47N34tiIkB6B58qfUPZ6B2Q7GFZ3X955NP9uz13gLtfmaUiGycLnp266two"
"ZyYwzoLvmCPW9RqqSlRuSaJAzO7HowwA/4892keaAQdjbFQmjEW1toSNGnMEQ7DEK0r5k1jyGdflDv5g8p0xVB0trepzwZ+bw31c"
"xfwJwre3vw==";
// Create device
DashDevice dashDevice(DEVICE_TYPE, configC64Str, 1);
// Create Connections
DashMQTT mqtt_con(&dashDevice, true, true);
DashWiFi wifi;
// Create Control IDs
const char *TEMPTB_ID = "TB01";
const char *GRAPH_ID = "IDTG";
OneWire oneWire(13); // Temperature sensor connected to pin 13
DallasTemperature tempSensor(&oneWire);
int graphSecondsCounter = 0;
float sampleTempC;
float temperatureC;
float tempSum = 0;
bool oneSecond = false;
void processStatus() {
String message = dashDevice.getTextBoxMessage(TEMPTB_ID, String(temperatureC));
message += dashDevice.getTimeGraphLine(GRAPH_ID, "L1", "Avge Temp", line, "red", yLeft);
mqtt_con.sendMessage(message);
}
void processIncomingMessage(MessageData *messageData) {
switch (messageData->control) {
case status:
processStatus();
break;
default:
break;
}
}
void setTemperatureEverySecond(float temperature) {
String messageToSend = "";
if (temperature > -100) { // i.e. no an error
temperatureC = temperature;
messageToSend = dashDevice.getTextBoxMessage(TEMPTB_ID, String(temperatureC));
// Now calculate the average temperature and send every 10 minutes. Dash server will store this.
tempSum += temperatureC;
graphSecondsCounter++;
if (graphSecondsCounter >= GRAPH_UPDATE_SECONDS) {
float tempFiltered = tempSum / GRAPH_UPDATE_SECONDS;
messageToSend += dashDevice.getTimeGraphPoint(GRAPH_ID, "L1", tempFiltered);
String text = "The temperature is " + String(tempFiltered) + "°C";
graphSecondsCounter = 0;
tempSum = 0;
}
} else {
messageToSend = dashDevice.getTextBoxMessage(TEMPTB_ID, "Err");
}
mqtt_con.sendMessage(messageToSend);
}
void oneSecondTimerTask(void *parameters) {
for(;;) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
oneSecond = true;
}
}
void setup() {
Serial.begin(115200);
dashDevice.setup(wifi.macAddress(), DEVICE_NAME); // unique deviceID
mqtt_con.setup(MQTT_USER, MQTT_PASSWORD);
mqtt_con.setCallback(&processIncomingMessage);
mqtt_con.addDashStore(timeGraph, GRAPH_ID);
wifi.attachConnection(&mqtt_con);
wifi.begin(WIFI_SSID, WIFI_PASSWORD);
// Setup 1 second timer task for measuring the temperature.
// This is way more often than necessary, but isn't a problem.
xTaskCreate(
oneSecondTimerTask,
"One Second",
1024, // stack size
NULL,
1, // priority
NULL // task handle
);
// Start temperature sensor conversion
tempSensor.setWaitForConversion(false);
tempSensor.begin();
tempSensor.requestTemperaturesByIndex(0);
}
void loop() {
wifi.run();
if (oneSecond) {
oneSecond = false;
if (tempSensor.isConversionComplete()) {
sampleTempC = tempSensor.getTempCByIndex(0);
tempSensor.requestTemperaturesByIndex(0);
}
setTemperatureEverySecond(sampleTempC);
}
}