-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathavocado_monitoring_grafana.ino
257 lines (216 loc) · 7.52 KB
/
avocado_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <Arduino.h>
#include <PromLokiTransport.h>
#include <PrometheusArduino.h>
#include <DHT.h>
#include <HCSR04.h>
#include <LedControl.h>
#include "certificates.h"
#include "config.h"
// Sensors
DHT dht(DHT_PIN, DHT_TYPE);
UltraSonicDistanceSensor distanceSensor(ULTRASONIC_PIN_TRIG, ULTRASONIC_PIN_ECHO);
// LED
LedControl lc=LedControl(LED_DIN,LED_CLK,LED_CS,0);
// LED visualisations
byte smile[8] = {0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C};
byte neutral[8] = {0x3C, 0x42, 0xA5, 0x81, 0xBD, 0x81, 0x42, 0x3C};
byte sad[8] = {0x3C, 0x42, 0xA5, 0x81, 0x99, 0xA5, 0x42, 0x3C};
byte off[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte err[8] = {0x00, 0x00, 0x78, 0x40, 0x70, 0x40, 0x78, 0x00};
// Prometheus client and transport
PromLokiTransport transport;
PromClient client(transport);
// Create a write request for 6 series
WriteRequest req(6, 3074);
// Define a TimeSeries which can hold up to 5 samples
TimeSeries ts1(5, "temperature_celsius", "{monitoring_type=\"avocado\",board_type=\"esp32_devkit1\",room=\"living_room\"}");
TimeSeries ts2(5, "humidity_percent", "{monitoring_type=\"avocado\",board_type=\"esp32_devkit1\",room=\"living_room\"}");
TimeSeries ts3(5, "heat_index_celsius", "{monitoring_type=\"avocado\",board_type=\"esp32_devkit1\",room=\"living_room\"}");
TimeSeries ts4(5, "height_centimeter", "{monitoring_type=\"avocado\",board_type=\"esp32_devkit1\",room=\"living_room\"}");
TimeSeries ts5(5, "light_lux", "{monitoring_type=\"avocado\",board_type=\"esp32_devkit1\",room=\"living_room\"}");
TimeSeries ts6(5, "soil_moisture", "{monitoring_type=\"avocado\",board_type=\"esp32_devkit1\",room=\"living_room\"}");
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.addTimeSeries(ts5);
req.addTimeSeries(ts6);
req.setDebug(Serial); // Remove this line to disable debug logging of the write request serialization and compression.
}
// Function to display state of the plant on LED Matrix
void displayState(byte character []) {
for (int i = 0; i < 8; i++) {
lc.setRow(0, i, character[i]);
}
}
// Function to read soil moisture measurement
int getSoilMoisture() {
// Turn the sensor ON
digitalWrite(MOISTURE_POWER, HIGH);
// Allow power to settle
delay(10);
// Read the digital value form sensor
int val = digitalRead(MOISTURE_PIN);
// Turn the sensor OFF
digitalWrite(MOISTURE_POWER, LOW);
// Return moisture value
// Serial.println(val);
return val;
}
// Function to read height of the plant
double getHeight() {
double manualCurrentHeight = 25;
double height = DISTANCE_FROM_POT - distanceSensor.measureDistanceCm();
// Serial.println(distanceSensor.measureDistanceCm());
if (height > manualCurrentHeight - 5 && height < manualCurrentHeight + 5) {
return height;
}
return manualCurrentHeight;
}
// Function to read light condition
float getLightLux() {
float sensor_value = analogRead(TEMP6000_PIN); // Get raw sensor reading
float volts = sensor_value * TEMP6000_VCC / 1024.0; // Convert reading to voltage
float amps = volts / 10000.0; // Convert to amps across 10K resistor
float microamps = amps * 1000000.0; // Convert amps to microamps
float lux = microamps * 2.0;
return lux;
}
// Function to check if any reading failed
bool checkIfReadingFailed(float hum, float cels, int moist, double height, float light) {
if (isnan(hum) || isnan(cels) || isnan(moist) || isnan(height) || isnan(light)) {
// Print letter E as error
displayState(err);
Serial.println(F("Failed to read from one of the sensors!"));
return true;
}
return false;
}
// Function to create message and display current state of the plant
String createAndDisplayState (int moist, float cels) {
String currentState = "";
if (moist) {
currentState = "DRY critical";
displayState(sad);
} else if( cels < 16 ) {
currentState = "COLD warning";
displayState(neutral);
} else if( cels > 26 ) {
currentState = "HOT warning";
displayState(neutral);
} else {
currentState = "OK info";
displayState(smile);
}
return currentState;
}
// ========== MAIN FUNCTIONS: SETUP & LOOP ==========
// SETUP: Function called at boot to initialize the system
void setup() {
// Set up soil moisture pins - initially keep the moisture sensor OFF (prevents sensor rusting)
pinMode(MOISTURE_POWER, OUTPUT);
digitalWrite(MOISTURE_POWER, LOW);
// Start the serial output at 115,200 baud
Serial.begin(115200);
// Connect to WiFi
setupClient();
// Start the DHT sensor
dht.begin();
// Start LED Matrix
lc.shutdown(0,false);
lc.setIntensity(0,0.0000001); //Adjust the brightness maximum is 15
lc.clearDisplay(0);
}
// LOOP: Function called in a loop to read from sensors and send them do databases
void loop() {
// Get current timestamp
int64_t time;
time = transport.getTimeMillis();
// Read humidity
float hum = dht.readHumidity();
// Read temperature as Celsius (the default)
float cels = dht.readTemperature();
// Read soil moisture (DRY: 1, WET: 0)
int moist = getSoilMoisture();
// Read height of plant
double height = getHeight();
//Read the light in lux
float light = getLightLux();
// Check if any reads failed and return early
if (checkIfReadingFailed(hum, cels, moist, height, light)) {
return;
}
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(cels, hum, false);
// Convert data to state of the plant
String message = createAndDisplayState(moist, cels);
// Submit data
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();
ts5.resetSamples();
ts6.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);
}
if (!ts5.addSample(time, light)) {
Serial.println(ts5.errmsg);
}
if (!ts6.addSample(time, moist)) {
Serial.println(ts6.errmsg);
}
loopCounter++;
}
// wait INTERVAL seconds, then do it again
delay(2 * 1000);
}