#include "config.h"
#include <PromLokiTransport.h>
#include <PrometheusArduino.h>
#include <Preferences.h>
#include <DHT.h> //引用的库函数
#include <DHT_U.h>
#define DHTPIN 17 //定义引脚 接P17
#define DHTTYPE DHT11 //定义类型
DHT dht(DHTPIN, DHTTYPE); //初始化
PromLokiTransport transport;
PromClient client(transport);
// Create a write request for 4 series.每增加一个指标记得加上1
WriteRequest req(4,1024);
// Check out https://prometheus.io/docs/practices/naming/ for metric naming and label conventions.
// This library does not currently create different metric types like gauges, counters, and histograms
// however internally Prometheus does not differentiate between these types, rather they are both
// a naming convention and a usage convention so it's possible to create any of these yourself.
// See the README at https://github.com/grafana/prometheus-arduino for more info.
// Define a TimeSeries which can hold up to 5 samples, has a name of `uptime_milliseconds`
TimeSeries ts1(5, "uptime_milliseconds_total", "{job=\"esp32-test\",host=\"esp32\"}");
// Define a TimeSeries which can hold up to 5 samples, has a name of `heap_free_bytes`
TimeSeries ts2(5, "heap_free_bytes", "{job=\"esp32-test\",host=\"esp32\",foo=\"bar\"}");
// !!!!!
// IF YOU WANT TO INCREASE THE TOTAL AMOUNT OF TIME SERIES, ALSO INCREASE WriteRequest()
// !!!!!
// Note, metrics with the same name and different labels are actually different series and you would need to define them separately
TimeSeries ts3(5, "temperature_total", "job=\"esp32-test\",host=\"esp32\",foo=\"temperatureTtotal\"");
TimeSeries ts4(5, "humidity_total", "job=\"esp32-test\",host=\"esp32\",foo=\"humidityTotal\"");
int loopCounter = 0;
void setup() {
Serial.begin(115200);
dht.begin(); //DHT开始工作
// Wait 5s for serial connection or continue without it
// some boards like the esp32 will run whether or not the
// serial port is connected, others like the MKR boards will wait
// for ever if you don't break the loop.
uint8_t serialTimeout;
while (!Serial && serialTimeout < 50) {
delay(100);
serialTimeout++;
}
Serial.println("Starting");
// Configure and start the transport layer
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(URL);
client.setPath((char*)PATH);
client.setPort(PORT);
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.
Serial.print("Free Mem After Setup: ");
Serial.println(freeMemory());
};
void loop() {
int64_t time;
time = transport.getTimeMillis();
Serial.println(time);
float humidity = dht.readHumidity(); //读湿度
float temperature = dht.readTemperature(); //读温度
Serial.print("相对湿度: "); // 串口显示
Serial.print(humidity);
Serial.println(" %RH");
Serial.print("温度: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000); //延时1s
if (!ts1.addSample(time, millis())) {
Serial.println(ts1.errmsg);
}
if (!ts2.addSample(time, freeMemory())) {
Serial.println(ts2.errmsg);
}
if (!ts3.addSample(time, temperature)) {
Serial.println(ts3.errmsg);
}
if (!ts4.addSample(time, humidity)) {
Serial.println(ts4.errmsg);
}
loopCounter++;
if (loopCounter >= 4) {
// Send data
loopCounter = 0;
PromClient::SendResult res = client.send(req);
if (!res == PromClient::SendResult::SUCCESS) {
Serial.println(client.errmsg);
// Note: additional retries or error handling could be implemented here.
// the result could also be:
// PromClient::SendResult::FAILED_DONT_RETRY
// PromClient::SendResult::FAILED_RETRYABLE
}
// Batches are not automatically reset so that additional retry logic could be implemented by the library user.
// Reset batches after a succesful send.
ts1.resetSamples();
ts2.resetSamples();
ts3.resetSamples();
ts4.resetSamples();
}
// Prometheus default is 15 second intervals but you can send several times per second if you want to.
// Collection and Sending could be parallelized or timed to ensure we're on a 15 seconds cadence,
// not simply add 15 second to however long collection & sending took.
delay(15000);
};
WIFI配置:
#define WIFI_SSID "XXXX"
#define WIFI_PASSWORD "XXXX"
#define URL "XXXXXXXX" // "192.168.0.1" or "prometheus.yourdomain.local" No http or https, No port or path
#define PATH "/write?db=prometheus"
#define PORT 9201