From 652398943ba95a1f16c7954d02c468d42edaefa7 Mon Sep 17 00:00:00 2001 From: Alejandro Blanco Date: Fri, 22 Oct 2021 18:27:02 +0200 Subject: [PATCH 1/2] #3 nan checking --- .github/workflows/ci.yml | 30 +++++ .gitignore | 3 - README.md | 21 ++++ grafana/grafana_dashboard.json | 202 --------------------------------- include/config.hpp | 15 +++ include/config.hpp.sample | 16 --- lib/prometheus/prometheus.cpp | 27 +++-- lib/prometheus/prometheus.hpp | 4 +- platformio.ini | 15 +-- src/main.cpp | 4 +- test/test_main.cpp | 8 +- 11 files changed, 100 insertions(+), 245 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 grafana/grafana_dashboard.json create mode 100644 include/config.hpp delete mode 100644 include/config.hpp.sample diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f1c0c2d --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,30 @@ +name: PlatformIO CI + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Cache pip + uses: actions/cache@v2 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + - name: Cache PlatformIO + uses: actions/cache@v2 + with: + path: ~/.platformio + key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} + - name: Set up Python + uses: actions/setup-python@v2 + - name: Install PlatformIO + run: | + python -m pip install --upgrade pip + pip install --upgrade platformio + - name: Run PlatformIO + run: WIFI_SSID='\"ssid\"' WIFI_PASS='\"pass\"' INSTANCE='\"livingroom\"' pio run diff --git a/.gitignore b/.gitignore index e399477..efc6d53 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,3 @@ fp-info-cache # Platform.io .pio tmp_* - -# Project related -include/config.hpp \ No newline at end of file diff --git a/README.md b/README.md index bfe08f4..d304fd4 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,24 @@ Classic ESP8266-DHT22 schematic with pull-up resistor of 4,7k-10k omhs. ## Schematics Schematics uses [kicad-ESP8266](https://github.com/jdunmire/kicad-ESP8266) library. + + +## Deployment + +```bash +WIFI_SSID='\"ssid\"' WIFI_PASS='\"pass\"' INSTANCE='\"livingroom\"' PLATFORMIO_UPLOAD_PORT=192.168.1.2 pio run -t upload -e ota +``` + +The output metrics look like this: + +``` +ambiance_build_info{version="1.0.0",gccversion="10.3.0", instance="bedroom"} 1 +ambiance_temperature{instance="bedroom"} 22.00 +ambiance_humidity{instance="bedroom"} 59.50 +``` + +## Testing + +```bash +WIFI_SSID='\"ssid\"' WIFI_PASS='\"pass\"' INSTANCE='\"livingroom\"' PLATFORMIO_UPLOAD_PORT=192.168.1.2 pio run test +``` diff --git a/grafana/grafana_dashboard.json b/grafana/grafana_dashboard.json deleted file mode 100644 index ba865b7..0000000 --- a/grafana/grafana_dashboard.json +++ /dev/null @@ -1,202 +0,0 @@ -{ - "annotations": { - "list": [ - { - "builtIn": 1, - "datasource": "-- Grafana --", - "enable": true, - "hide": true, - "iconColor": "rgba(0, 211, 255, 1)", - "name": "Annotations & Alerts", - "type": "dashboard" - } - ] - }, - "editable": true, - "gnetId": null, - "graphTooltip": 0, - "id": 9, - "links": [], - "panels": [ - { - "datasource": null, - "fieldConfig": { - "defaults": { - "color": { - "mode": "palette-classic" - }, - "custom": { - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "drawStyle": "line", - "fillOpacity": 0, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 5, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "none" - }, - "thresholdsStyle": { - "mode": "off" - } - }, - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "green", - "value": null - }, - { - "color": "red", - "value": 80 - } - ] - } - }, - "overrides": [] - }, - "gridPos": { - "h": 9, - "w": 23, - "x": 0, - "y": 0 - }, - "id": 2, - "options": { - "legend": { - "calcs": [], - "displayMode": "list", - "placement": "bottom" - }, - "tooltip": { - "mode": "single" - } - }, - "targets": [ - { - "exemplar": true, - "expr": "esp8266_temperature", - "interval": "", - "legendFormat": "", - "refId": "A" - } - ], - "title": "ESP8266 Temperature", - "type": "timeseries" - }, - { - "datasource": null, - "fieldConfig": { - "defaults": { - "color": { - "mode": "palette-classic" - }, - "custom": { - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "drawStyle": "line", - "fillOpacity": 0, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 5, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "none" - }, - "thresholdsStyle": { - "mode": "off" - } - }, - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "green", - "value": null - }, - { - "color": "red", - "value": 80 - } - ] - } - }, - "overrides": [] - }, - "gridPos": { - "h": 10, - "w": 23, - "x": 0, - "y": 9 - }, - "id": 4, - "options": { - "legend": { - "calcs": [], - "displayMode": "list", - "placement": "bottom" - }, - "tooltip": { - "mode": "single" - } - }, - "targets": [ - { - "exemplar": true, - "expr": "esp8266_humidty", - "interval": "", - "legendFormat": "", - "refId": "A" - } - ], - "title": "ESP8266 Humidity", - "type": "timeseries" - } - ], - "schemaVersion": 30, - "style": "dark", - "tags": [ - "data", - "esp8266" - ], - "templating": { - "list": [] - }, - "time": { - "from": "now-6h", - "to": "now" - }, - "timepicker": {}, - "timezone": "", - "title": "ESP8266", - "uid": "PElVrDmgk", - "version": 2 -} \ No newline at end of file diff --git a/include/config.hpp b/include/config.hpp new file mode 100644 index 0000000..ab3ffc8 --- /dev/null +++ b/include/config.hpp @@ -0,0 +1,15 @@ +#pragma once + +// GCC info +#ifdef __VERSION__ +#define GCC_VERSION __VERSION__ +#else +#define GCC_VERSION "unknown" +#endif + +// Http Server config +#define PORT 80 + +// DHT config +#define DHTPIN D2 +#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 diff --git a/include/config.hpp.sample b/include/config.hpp.sample deleted file mode 100644 index 99bebbb..0000000 --- a/include/config.hpp.sample +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -// Rename this file to config.hpp and define there your own configuration - -// Http Server config -#define WIFI_SSID "myssid" -#define WIFI_PASS "mypass" -#define PORT 80 - -// DHT config -#define DHTPIN D2 -#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 - -// Project specific config -#define METRICS_PREFIX "esp8266_ambiance" -#define VERSION "0.4.0" diff --git a/lib/prometheus/prometheus.cpp b/lib/prometheus/prometheus.cpp index 49f4023..6b1d736 100644 --- a/lib/prometheus/prometheus.cpp +++ b/lib/prometheus/prometheus.cpp @@ -1,9 +1,11 @@ #include "prometheus.hpp" +#include #include -PrometheusExporter::PrometheusExporter(const char *version, const char *prefix) - : version(version), prefix(prefix) {} +PrometheusExporter::PrometheusExporter(const char *version, const char *gcc, const char *prefix, + const char *instance) + : version(version), gcc(gcc), prefix(prefix), instance(instance) {} const char *PrometheusExporter::buildRoot() { return "" @@ -16,13 +18,18 @@ const char *PrometheusExporter::buildRoot() { } const char *PrometheusExporter::buildMetrics(char *buffer, float temperature, float humidity) { -#ifdef __VERSION__ - int len = sprintf(buffer, "%s_%s{version=\"%s\",gccversion=\"%s\"} 1\n", prefix, - "build_info", version, __VERSION__); -#else - int len = sprintf(buffer, "%s_%s{version=\"%s\"} 1\n", prefix, "build_info", version); -#endif - len += sprintf(buffer + len, "%s_%s %.2f\n", prefix, "temperature", temperature); - len += sprintf(buffer + len, "%s_%s %.2f\n", prefix, "humidity", humidity); + int len = sprintf(buffer, "%s_%s{version=\"%s\", gccversion=\"%s\", instance=\"%s\"} 1\n", + prefix, "build_info", version, gcc, instance); + + if (!std::isnan(temperature)) { + len += sprintf(buffer + len, "%s_%s{instance=\"%s\"} %.2f\n", prefix, "temperature", + instance, temperature); + } + + if (!std::isnan(humidity)) { + len += sprintf(buffer + len, "%s_%s{instance=\"%s\"} %.2f\n", prefix, "humidity", instance, + humidity); + } + return buffer; } diff --git a/lib/prometheus/prometheus.hpp b/lib/prometheus/prometheus.hpp index 033fccc..b9f1bd0 100644 --- a/lib/prometheus/prometheus.hpp +++ b/lib/prometheus/prometheus.hpp @@ -2,11 +2,13 @@ class PrometheusExporter { public: - PrometheusExporter(const char *version, const char *prefix); + PrometheusExporter(const char *version, const char *gcc, const char *prefix, const char *instance); const char *buildRoot(); const char *buildMetrics(char *buffer, float temperature, float humidity); private: const char *version; + const char *gcc; const char *prefix; + const char *instance; }; diff --git a/platformio.ini b/platformio.ini index 35af601..2b5d26a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,6 +1,6 @@ [platformio] description = Prometheus Exporter for DHT22 sensor -default_envs = debug +default_envs = wire [env] platform = espressif8266 @@ -16,13 +16,14 @@ monitor_filters = default, esp8266_exception_decoder test_speed = 115200 build_flags = -Wall - -D RELEASE + -DVERSION=\"1.0.0\" + -DMETRICS_PREFIX=\"ambiance\" + -DWIFI_SSID=${sysenv.WIFI_SSID} + -DWIFI_PASS=${sysenv.WIFI_PASS} + -DINSTANCE=${sysenv.INSTANCE} -[env:debug] -build_type = debug -build_flags = - -Wall - -D DEBUG +[env:wire] +upload_protocol = esptool [env:ota] upload_protocol = espota diff --git a/src/main.cpp b/src/main.cpp index 9644861..9d5ebc4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,14 +10,14 @@ ESP8266WebServer server(PORT); DHT dht(DHTPIN, DHTTYPE); -PrometheusExporter prometheus(VERSION, METRICS_PREFIX); +PrometheusExporter prometheus(VERSION, GCC_VERSION, METRICS_PREFIX, INSTANCE); void onRootHandler() { server.send(200, "text/html", prometheus.buildRoot()); } void onMetricsPathHandler() { float temperature = dht.readTemperature(); float humidity = dht.readHumidity(); - char body [1024]; + char body [256]; server.send(200, "text/plain", prometheus.buildMetrics(body, temperature, humidity)); } diff --git a/test/test_main.cpp b/test/test_main.cpp index 52b67b7..87d180a 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -21,12 +21,12 @@ void testReadDHTHumidity() { } void testBuildMetrics() { - PrometheusExporter exporter("1.0.0", "test"); + PrometheusExporter exporter("1.0.0", "10.2.1", "ambiance", "livingroom"); char buffer [1024]; const char *actual = exporter.buildMetrics(buffer, 1.0, 1.0); - const char *expectedTemperature = "test_temperature 1.00"; - const char *expectedHumidity = "test_humidity 1.00"; + const char *expectedTemperature = "ambiance_temperature{instance=\"livingroom\"} 1.00"; + const char *expectedHumidity = "ambiance_humidity{instance=\"livingroom\"} 1.00"; TEST_ASSERT_EQUAL_STRING(buffer, actual); TEST_ASSERT_NOT_NULL(strstr(actual, expectedTemperature)); TEST_ASSERT_NOT_NULL(strstr(actual, expectedHumidity)); @@ -52,4 +52,4 @@ void setup() { void loop() { -} \ No newline at end of file +} From a3c1016894dd49f29b2a864bb3a01f1197b2312a Mon Sep 17 00:00:00 2001 From: Alejandro Blanco Date: Fri, 22 Oct 2021 18:52:10 +0200 Subject: [PATCH 2/2] #3 use room instead of instance --- .github/workflows/ci.yml | 2 +- README.md | 10 +++++----- lib/prometheus/prometheus.cpp | 14 +++++++------- lib/prometheus/prometheus.hpp | 4 ++-- platformio.ini | 2 +- src/main.cpp | 2 +- test/test_main.cpp | 4 ++-- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f1c0c2d..aacb587 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,4 +27,4 @@ jobs: python -m pip install --upgrade pip pip install --upgrade platformio - name: Run PlatformIO - run: WIFI_SSID='\"ssid\"' WIFI_PASS='\"pass\"' INSTANCE='\"livingroom\"' pio run + run: WIFI_SSID='\"ssid\"' WIFI_PASS='\"pass\"' ROOM='\"livingroom\"' pio run diff --git a/README.md b/README.md index d304fd4..0aed934 100644 --- a/README.md +++ b/README.md @@ -12,19 +12,19 @@ Schematics uses [kicad-ESP8266](https://github.com/jdunmire/kicad-ESP8266) libra ## Deployment ```bash -WIFI_SSID='\"ssid\"' WIFI_PASS='\"pass\"' INSTANCE='\"livingroom\"' PLATFORMIO_UPLOAD_PORT=192.168.1.2 pio run -t upload -e ota +WIFI_SSID='\"ssid\"' WIFI_PASS='\"pass\"' ROOM='\"livingroom\"' PLATFORMIO_UPLOAD_PORT=192.168.1.2 pio run -t upload -e ota ``` The output metrics look like this: ``` -ambiance_build_info{version="1.0.0",gccversion="10.3.0", instance="bedroom"} 1 -ambiance_temperature{instance="bedroom"} 22.00 -ambiance_humidity{instance="bedroom"} 59.50 +ambiance_build_info{version="1.0.0",gccversion="10.3.0", room="bedroom"} 1 +ambiance_temperature{room="bedroom"} 22.00 +ambiance_humidity{room="bedroom"} 59.50 ``` ## Testing ```bash -WIFI_SSID='\"ssid\"' WIFI_PASS='\"pass\"' INSTANCE='\"livingroom\"' PLATFORMIO_UPLOAD_PORT=192.168.1.2 pio run test +WIFI_SSID='\"ssid\"' WIFI_PASS='\"pass\"' ROOM='\"livingroom\"' pio run test ``` diff --git a/lib/prometheus/prometheus.cpp b/lib/prometheus/prometheus.cpp index 6b1d736..a45c044 100644 --- a/lib/prometheus/prometheus.cpp +++ b/lib/prometheus/prometheus.cpp @@ -4,8 +4,8 @@ #include PrometheusExporter::PrometheusExporter(const char *version, const char *gcc, const char *prefix, - const char *instance) - : version(version), gcc(gcc), prefix(prefix), instance(instance) {} + const char *room) + : version(version), gcc(gcc), prefix(prefix), room(room) {} const char *PrometheusExporter::buildRoot() { return "" @@ -18,16 +18,16 @@ const char *PrometheusExporter::buildRoot() { } const char *PrometheusExporter::buildMetrics(char *buffer, float temperature, float humidity) { - int len = sprintf(buffer, "%s_%s{version=\"%s\", gccversion=\"%s\", instance=\"%s\"} 1\n", - prefix, "build_info", version, gcc, instance); + int len = sprintf(buffer, "%s_%s{version=\"%s\",gccversion=\"%s\",room=\"%s\"} 1\n", + prefix, "build_info", version, gcc, room); if (!std::isnan(temperature)) { - len += sprintf(buffer + len, "%s_%s{instance=\"%s\"} %.2f\n", prefix, "temperature", - instance, temperature); + len += sprintf(buffer + len, "%s_%s{room=\"%s\"} %.2f\n", prefix, "temperature", + room, temperature); } if (!std::isnan(humidity)) { - len += sprintf(buffer + len, "%s_%s{instance=\"%s\"} %.2f\n", prefix, "humidity", instance, + len += sprintf(buffer + len, "%s_%s{room=\"%s\"} %.2f\n", prefix, "humidity", room, humidity); } diff --git a/lib/prometheus/prometheus.hpp b/lib/prometheus/prometheus.hpp index b9f1bd0..e980e01 100644 --- a/lib/prometheus/prometheus.hpp +++ b/lib/prometheus/prometheus.hpp @@ -2,7 +2,7 @@ class PrometheusExporter { public: - PrometheusExporter(const char *version, const char *gcc, const char *prefix, const char *instance); + PrometheusExporter(const char *version, const char *gcc, const char *prefix, const char *room); const char *buildRoot(); const char *buildMetrics(char *buffer, float temperature, float humidity); @@ -10,5 +10,5 @@ class PrometheusExporter { const char *version; const char *gcc; const char *prefix; - const char *instance; + const char *room; }; diff --git a/platformio.ini b/platformio.ini index 2b5d26a..64ecb3c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,7 +20,7 @@ build_flags = -DMETRICS_PREFIX=\"ambiance\" -DWIFI_SSID=${sysenv.WIFI_SSID} -DWIFI_PASS=${sysenv.WIFI_PASS} - -DINSTANCE=${sysenv.INSTANCE} + -DROOM=${sysenv.ROOM} [env:wire] upload_protocol = esptool diff --git a/src/main.cpp b/src/main.cpp index 9d5ebc4..ef0d35b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,7 @@ ESP8266WebServer server(PORT); DHT dht(DHTPIN, DHTTYPE); -PrometheusExporter prometheus(VERSION, GCC_VERSION, METRICS_PREFIX, INSTANCE); +PrometheusExporter prometheus(VERSION, GCC_VERSION, METRICS_PREFIX, ROOM); void onRootHandler() { server.send(200, "text/html", prometheus.buildRoot()); } diff --git a/test/test_main.cpp b/test/test_main.cpp index 87d180a..36a342f 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -25,8 +25,8 @@ void testBuildMetrics() { char buffer [1024]; const char *actual = exporter.buildMetrics(buffer, 1.0, 1.0); - const char *expectedTemperature = "ambiance_temperature{instance=\"livingroom\"} 1.00"; - const char *expectedHumidity = "ambiance_humidity{instance=\"livingroom\"} 1.00"; + const char *expectedTemperature = "ambiance_temperature{room=\"livingroom\"} 1.00"; + const char *expectedHumidity = "ambiance_humidity{room=\"livingroom\"} 1.00"; TEST_ASSERT_EQUAL_STRING(buffer, actual); TEST_ASSERT_NOT_NULL(strstr(actual, expectedTemperature)); TEST_ASSERT_NOT_NULL(strstr(actual, expectedHumidity));