From 2f210d3db642045ddaca5170e60b0d9947f2312a Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 14 Jan 2021 18:29:24 +0100 Subject: [PATCH] Initial version that does nothing but replies to the /metrics endpoint --- .gitignore | 2 + README.md | 11 ++++++ WeatherStation.ino | 97 ++++++++++++++++++++++++++++++++++++++++++++++ config.h.sample | 34 ++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 WeatherStation.ino create mode 100644 config.h.sample diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5c73ce6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/build/ +/config.h diff --git a/README.md b/README.md new file mode 100644 index 0000000..3365533 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# Modular Weather Station + +## Compiling + +Copy the file `config.h.sample` to `config.h` and edit the parameters within. + +You can either compile the whole sketch from within the Arduino IDE or, if you are familiar with the CLI, you can use the official [Arduino CLI tools](https://arduino.github.io/arduino-cli/): + +``` +arduino-cli compile -b --fqbn esp8266:esp8266:nodemcuv2 /path/to/the/sketch /path/to/the/sketch/build +``` diff --git a/WeatherStation.ino b/WeatherStation.ino new file mode 100644 index 0000000..692515b --- /dev/null +++ b/WeatherStation.ino @@ -0,0 +1,97 @@ +/* + * ModularWeatherStation + * + * Copyright (C) 2021 Gergely Polonkai + * Author: Gergely POLONKAI + * + * ModularWeatherStation is free software: you can redistribute it + * and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * ModularWeatherStation is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * As additional permission under GNU GPL version 3 section 7, you may + * distribute non-source form of the Program without the copy of the + * GNU GPL normally required by section 4, provided you inform the + * receipents of GNU GPL by a written offer. + * + */ + +#include +#include +#include +#include + +#include "config.h" + +ESP8266WebServer server(80); + +static const char TEXT_PLAIN[] PROGMEM = "text/plain"; + +void +reply_not_found() +{ + server.send(404, FPSTR(TEXT_PLAIN), "Not Found"); +} + +void +send_metrics() +{ + char message[1]; + + snprintf(message, 1, ""); + + server.send(200, "text/plain; version=0.0.4; charset=utf-8", message); +} + +void +setup() +{ + IPAddress address; + + Serial.begin(9600); + + WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.println("Waiting for WiFi to connect..."); + } + + address = WiFi.localIP(); + Serial.print("WiFi Address: "); + address.printTo(Serial); + Serial.println(""); + + if (!MDNS.begin(MDNS_NAME)) { + Serial.println("Error setting up mDNS responder!"); + + while (1) { + delay(1000); + } + } + + server.on("/metrics", HTTP_GET, send_metrics); + server.onNotFound(reply_not_found); + + server.begin(); + + MDNS.addService("http", "tcp", 80); + + Serial.println("All set, starting the loop."); +} + +void +loop() +{ + MDNS.update(); + server.handleClient(); +} diff --git a/config.h.sample b/config.h.sample new file mode 100644 index 0000000..74c3f33 --- /dev/null +++ b/config.h.sample @@ -0,0 +1,34 @@ +/* + * Configuration values for ModularWeatherStation + * + * Copyright (C) 2021 Gergely Polonkai + * Author: Gergely POLONKAI + * + * ModularWeatherStation is free software: you can redistribute it + * and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * ModularWeatherStation is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * As additional permission under GNU GPL version 3 section 7, you may + * distribute non-source form of the Program without the copy of the + * GNU GPL normally required by section 4, provided you inform the + * receipents of GNU GPL by a written offer. + * + */ + +// The SSID (name) and password of the WiFi network you want to connect to +#define WIFI_SSID "YourWiFiName" +#define WIFI_PASS "YourWiFiPass" + +// The mDNS hostname you want this station to be visible as. This should be +// different for each station you install (e.g. if you want to measure the +// temperature and humidity of each room separately) +#define MDNS_NAME "internal-hostname"