-
Notifications
You must be signed in to change notification settings - Fork 7
/
sourdough_monitoring_grafana.ino
138 lines (117 loc) · 3.87 KB
/
sourdough_monitoring_grafana.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
#include <Arduino.h>
#include <PromLokiTransport.h>
#include <PrometheusArduino.h>
#include <DHT.h>
#include <HCSR04.h>
#include "certificates.h"
#include "config.h"
// DHT Sensor
DHT dht(DHTPIN, DHTTYPE);
// Ultrasonic Sensor
UltraSonicDistanceSensor distanceSensor(ULTRASONIC_PIN_TRIG, ULTRASONIC_PIN_ECHO);
// Prometheus client and transport
PromLokiTransport transport;
PromClient client(transport);
// Create a write request for 4 series
WriteRequest req(4, 2048);
// Define a TimeSeries which can hold up to 5 samples
TimeSeries ts1(5, "temperature_celsius", "{monitoring_type=\"sourdough\",board_type=\"esp32_devkit1\",sourdough_type=\"rye\"}");
TimeSeries ts2(5, "humidity_percent", "{monitoring_type=\"sourdough\",board_type=\"esp32_devkit1\",sourdough_type=\"rye\"}");
TimeSeries ts3(5, "heat_index_celsius", "{monitoring_type=\"sourdough\",board_type=\"esp32_devkit1\",sourdough_type=\"rye\"}");
TimeSeries ts4(5, "height_centimeter", "{monitoring_type=\"sourdough\",board_type=\"esp32_devkit1\",sourdough_type=\"rye\"}");
int loopCounter = 0;
// Function to set up Prometheus client
void setupClient() {
Serial.println("Setting up client...");
uint8_t serialTimeout;
while (!Serial && serialTimeout < 50) {
delay(100);
serialTimeout++;
}
// Configure and start the transport layer
transport.setUseTls(true);
transport.setCerts(grafanaCert, strlen(grafanaCert));
transport.setWifiSsid(WIFI_SSID);
transport.setWifiPass(WIFI_PASSWORD);
transport.setDebug(Serial); // Remove this line to disable debug logging of the client.
if (!transport.begin()) {
Serial.println(transport.errmsg);
while (true) {};
}
// Configure the client
client.setUrl(GC_PROM_URL);
client.setPath(GC_PROM_PATH);
client.setPort(GC_PORT);
client.setUser(GC_PROM_USER);
client.setPass(GC_PROM_PASS);
client.setDebug(Serial); // Remove this line to disable debug logging of the client.
if (!client.begin()) {
Serial.println(client.errmsg);
while (true) {};
}
// Add our TimeSeries to the WriteRequest
req.addTimeSeries(ts1);
req.addTimeSeries(ts2);
req.addTimeSeries(ts3);
req.addTimeSeries(ts4);
req.setDebug(Serial); // Remove this line to disable debug logging of the write request serialization and compression.
}
// Get height of starter
float getHeight() {
float height_from_bottom = 16.50;
float height = height_from_bottom - distanceSensor.measureDistanceCm();
if (height > 0) {
return height;
};
return 0;
}
// ========== MAIN FUNCTIONS: SETUP & LOOP ==========
// SETUP: Function called at boot to initialize the system
void setup() {
Serial.begin(115200);
setupClient();
dht.begin();
}
// LOOP: Function called in a loop to read from sensors and send them do databases
void loop() {
int64_t time;
time = transport.getTimeMillis();
float hum = dht.readHumidity();
float cels = dht.readTemperature();
float height = getHeight();
// Check if any reads failed and exit early (to try again).
if (isnan(hum) || isnan(cels) || isnan(height) ) {
Serial.println(F("Failed to read from sensor!"));
return;
}
int hic = dht.computeHeatIndex(cels, hum, false);
if (loopCounter >= 5) {
//Send
loopCounter = 0;
PromClient::SendResult res = client.send(req);
if (!res == PromClient::SendResult::SUCCESS) {
Serial.println(client.errmsg);
}
// Reset batches after a succesful send.
ts1.resetSamples();
ts2.resetSamples();
ts3.resetSamples();
ts4.resetSamples();
} else {
if (!ts1.addSample(time, cels)) {
Serial.println(ts1.errmsg);
}
if (!ts2.addSample(time, hum)) {
Serial.println(ts2.errmsg);
}
if (!ts3.addSample(time, hic)) {
Serial.println(ts3.errmsg);
}
if (!ts4.addSample(time, height)) {
Serial.println(ts4.errmsg);
}
loopCounter++;
}
// wait INTERVAL seconds and then do it again
delay(INTERVAL * 1000);
}