From f7beced262dd20b731b4efe725a8afd28361409e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Z=C3=A1le=C5=A1=C3=A1k?= Date: Tue, 20 Apr 2021 10:02:26 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + ESPtermostat.ino | 123 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 24 +++++++++ config.h.example | 6 +++ 4 files changed, 154 insertions(+) create mode 100644 .gitignore create mode 100644 ESPtermostat.ino create mode 100644 README.md create mode 100644 config.h.example diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e56cf2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.h diff --git a/ESPtermostat.ino b/ESPtermostat.ino new file mode 100644 index 0000000..c77197c --- /dev/null +++ b/ESPtermostat.ino @@ -0,0 +1,123 @@ +//========== Preprocesor ========== + +//---- Required ---- +#include +#include +//------------------ +#include +#include + +#include "Tasker.h" +#include "OneWire.h" +#include "DallasTemperature.h" + +#include "config.h" + +//========== Create objects ========== + +AsyncWebServer server(80); +Tasker tasker; +OneWire oneWire(DS18B20_PIN); +DallasTemperature sensor(&oneWire); + +//========== Init variables ========== + +float temperature; +String metrics; + +//================================= + + +void setup() { +//========== Wi-Fi setup ========== + + WiFi.mode(WIFI_STA); + WiFi.begin(STASSID, STAPSK); + while (WiFi.waitForConnectResult() != WL_CONNECTED) { + //Connection Failed! Rebooting... + delay(5000); + ESP.restart(); + } + +//========== OTA setup ========== + + // Port defaults to 8266 + // ArduinoOTA.setPort(8266); + + // Hostname defaults to esp8266-[ChipID] + // ArduinoOTA.setHostname("myesp8266"); + + // No authentication by default + // ArduinoOTA.setPassword("admin"); + + // Password can be set with it's md5 value as well + // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 + // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); + ArduinoOTA.begin(); + +//========== Pin setup ========== + + pinMode(RELAY_PIN, OUTPUT); + +//========== Sensor setup ========== + + sensor.begin(); + // do not block during temperature conversion + sensor.setWaitForConversion(false); + +//========== Tasks init ========== + + startConversion(); // First temp read + tasker.setInterval(startConversion, 15000); // read temperature every 15 seconds + //tasker.setTimeout(relayLoop, 5000); + tasker.setInterval(otaHandle, 1000); + +//========== Web server setup ========== + + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/plain", "ESP termostat, tady se bude posílat aplikace z flash paměti"); + }); + if(METRICS_EXPORT){ + server.on("/metrics", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/plain; charset=utf-8", metrics); + }); + } + server.onNotFound(notFound); + server.begin(); + +//================================= +} + +void loop() { + tasker.loop(); +} + +void otaHandle(){ + ArduinoOTA.handle(); +} + +void notFound(AsyncWebServerRequest *request) { + request->send(404, "text/plain", "Not found"); +} + +//void relayLoop(){ +// byte pin = RELAY_PIN; +// bool led = !digitalRead(pin); +// digitalWrite(pin, led); +// tasker.setTimeout(relayLoop, led ? 3000 : 7000); +//} + +void readSensor() { + // read the actual temperature after it's been converted + temperature = sensor.getTempCByIndex(0); + // do what you need with the temperature here + metrics = "temp "; + metrics += temperature; +} + +void startConversion() { + // start temperature conversion (does not block) + sensor.requestTemperatures(); + // schedule reading the actual temperature in 750 milliseconds + tasker.setTimeout(readSensor, 750); +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..760b976 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# ESP8266 - Termostat + +Jedná se o termostat postavený na ESP8266 s možností připojení na Wi-Fi. Díky Wi-Fi konektivitě lze termostat ovládat pomocí jakéhokoli jiného zařízení a také lze z termostatu exportovat metriky. + +Bylo zvoleno **ESP-01**. Tento model ESP má vyvedeny 2 GPIO piny. +Termostat obsahuje teplotní čidlo **DS18B20**, právě kvůli sběrnici OneWire, na kterou lze připojit více čidel. +Ke spínání kotle byl zvolen **relay modul**, který by bylo vhodné do budoucna nahradit nějakým polovodičem, aby termostat necvakal. Případně při návrhu boardu navrhnout osazení jak relé, tak nějakého polovodiče, který by sloužil ke spínání kotle (volba jumprem). +Termostat je napájen 230v. + +Termostat nemusí sloužit pouze k regulaci kotle, ale i jako teplotní čidlo exportující své metriky. +Toto lze nastavit v configu. + +**ToDo list:** +- [ ] Zabezpečení (API + JS app) + - [ ] API + - [ ] JS app + - [ ] OTA update +- [ ] API +- [ ] JS app + - [ ] v flash paměti + - [ ] aktualizace +- [x] OTA update +- [x] Wi-Fi konektivita +- [x] Metric export \ No newline at end of file diff --git a/config.h.example b/config.h.example new file mode 100644 index 0000000..d42696e --- /dev/null +++ b/config.h.example @@ -0,0 +1,6 @@ +#define STASSID "SSID" +#define STAPSK "heslo" + +#define DS18B20_PIN 0 +#define RELAY_PIN 2 +#define METRICS_EXPORT 1