-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathesp8266-thermometer-ds18b20.ino
161 lines (123 loc) · 4.57 KB
/
esp8266-thermometer-ds18b20.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
//#include <ArduinoOTA.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <FS.h>
#include <Hash.h>
#include <SPIFFSEditor.h>
char buf[16];
// Import board-specifics
#include "LoLin-NodeMCU-board.h"
// Things we don't want the outside world to see...
#include "devicemeta.h"
#include "secrets.h"
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
// Output lead:red (VCC), yellow(DATA) , black(GND) -55'C~125'C
OneWire oneWire(D5);
DallasTemperature sensors(&oneWire);
int deviceCount;
// SKETCH BEGIN
AsyncWebServer server(80);
char hostString[16] = {0};
DeviceAddress address;
DeviceAddress *deviceAddressList;
void setup() {
Serial.begin(115200);
delay(10);
sprintf(hostString, "ESP_%06X", ESP.getChipId());
Serial.print("Hostname: ");
Serial.println(hostString);
WiFi.hostname(hostString);
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, LOW);
// Connect to WiFi network
Serial.println();
Serial.printf("Connecting to %s ", SECRET_WIFI_SSID);
WiFi.mode(WIFI_STA); // STA = STand-Alone? Doesn't start an AP on the side, which is nice.
WiFi.begin(SECRET_WIFI_SSID, SECRET_WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" connected");
// Start mDNS
if (!MDNS.begin(hostString)) {
Serial.println("Error setting up MDNS responder!");
}
MDNS.addService("prometheus-http", "tcp", 80); // Announce esp tcp service on port 80
MDNS.addService("http", "tcp", 80);
// Start thermometers
sensors.begin();
deviceCount = sensors.getDeviceCount();
Serial.printf("DS18B20's initialized, %d found\n", deviceCount);
deviceAddressList = (DeviceAddress *)malloc(sizeof(DeviceAddress) * deviceCount);
//char* metadata = (char*) malloc(1024);
for (int i = 0; i < deviceCount; i += 1) {
sensors.getAddress(deviceAddressList[i], i);
sensors.setResolution(deviceAddressList[i], 12);
// Get meta-data for sensors
//get_device_meta(metadata, 1024, deviceAddressList[i]);
//Serial.println(metadata);
}
//free(metadata);
SPIFFS.begin();
// Reachable on /edit
server.addHandler(new SPIFFSEditor("t", "t"));
server.onNotFound([](AsyncWebServerRequest *request) {
request->send(404);
});
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/html", "SERVER<hr><a href='/metrics'>/metrics</a>, <a href='/edit'>/edit</a>");
});
server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
server.on("/metrics", HTTP_GET, [](AsyncWebServerRequest *request) {
digitalWrite(BUILTIN_LED, LOW);
AsyncResponseStream *response = request->beginResponseStream("text/prometheus; version=0.4");
sensors.requestTemperatures(); // Send the command to get temperatures
delay(1);
response->print("# HELP temperature_c Calculated temperature in centigrade\n");
response->print("# TYPE temperature_c gauge\n");
char *metadata = (char *)malloc(1024);
for (int i = 0; i < deviceCount; i += 1) {
sprintAddress(buf, deviceAddressList[i]);
// Get meta-data for sensors
if (get_device_meta(metadata + 1, 1023, deviceAddressList[i])) {
metadata[0] = 44; // ","
} else {
metadata[0] = 0;
}
// Read temperature
float temperature = sensors.getTempCByIndex(i);
if (temperature != 85) { // 85 C is some kind of NO-OP data for DS18B20's
response->printf("temperature_c{address=\"%s\"%s} ", buf, metadata);
response->print(sensors.getTempCByIndex(i));
response->print("\n");
}
}
free(metadata);
response->print("\n");
response->print("# HELP wifi_rssi_dbm Received Signal Strength Indication, dBm\n");
response->print("# TYPE wifi_rssi_dbm counter\n");
response->printf("wifi_rssi_dbm{} %d\n\n", WiFi.RSSI());
response->print("# HELP heap_free_b Uptime in milliseconds\n");
response->print("# TYPE heap_free_b gauge\n");
response->printf("heap_free_b{} %d\n\n", ESP.getFreeHeap());
response->print("# HELP uptime_ms Uptime in milliseconds\n");
response->print("# TYPE uptime_ms gauge\n");
response->printf("uptime_ms{} %lu\n\n", millis());
request->send(response);
digitalWrite(BUILTIN_LED, HIGH);
});
// Print the IP address
Serial.print("Server started on: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
// Start the server
server.begin();
}
void loop() {}