From 59252e81b0a299d04c46ebaf74678ce33280ee1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C5=A1per=20Dobrovoljc?= Date: Sun, 19 Feb 2023 14:16:00 +0100 Subject: [PATCH] Added settings --- .vscode/settings.json | 24 ++++++++++ dashboard/src/lib/sensor.ts | 22 ++++++++++ dashboard/src/routes/+page.svelte | 20 +++------ lib/settings/settings.cpp | 31 +++++++++++++ lib/settings/settings.h | 19 ++++++++ platformio.ini | 6 ++- src/main.cpp | 73 +++++++++++++++++++++++++++---- 7 files changed, 171 insertions(+), 24 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 dashboard/src/lib/sensor.ts create mode 100644 lib/settings/settings.cpp create mode 100644 lib/settings/settings.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..59c0b58 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,24 @@ +{ + "files.associations": { + "*.ejs": "html", + "*.ipp": "cpp", + "__bit_reference": "cpp", + "__hash_table": "cpp", + "__split_buffer": "cpp", + "__tree": "cpp", + "array": "cpp", + "deque": "cpp", + "initializer_list": "cpp", + "list": "cpp", + "map": "cpp", + "regex": "cpp", + "set": "cpp", + "string": "cpp", + "string_view": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "valarray": "cpp", + "vector": "cpp" + }, + "C_Cpp.errorSquiggles": "enabled" +} diff --git a/dashboard/src/lib/sensor.ts b/dashboard/src/lib/sensor.ts new file mode 100644 index 0000000..2ae799e --- /dev/null +++ b/dashboard/src/lib/sensor.ts @@ -0,0 +1,22 @@ +import { browser } from '$app/environment'; +import { readable } from 'svelte/store'; + +export type SensorData = { + temp: number; + hum: number; +}; + +export const sensor = readable(null, (set) => { + if (browser) { + const source = new EventSource('http://10.50.0.113/events'); + + source.addEventListener('open', () => { + console.log('Connected'); + }); + + source.addEventListener('message', (e) => { + const data: SensorData = JSON.parse(e.data); + set(data); + }); + } +}); diff --git a/dashboard/src/routes/+page.svelte b/dashboard/src/routes/+page.svelte index 8d678aa..1eecc28 100644 --- a/dashboard/src/routes/+page.svelte +++ b/dashboard/src/routes/+page.svelte @@ -1,18 +1,8 @@ -

Welcome to SvelteKit

-

Visit kit.svelte.dev to read the documentation

+{#if $sensor} +

Temperature {$sensor.temp} °C

+

Humidity {$sensor.hum}%

+{/if} diff --git a/lib/settings/settings.cpp b/lib/settings/settings.cpp new file mode 100644 index 0000000..a26d73a --- /dev/null +++ b/lib/settings/settings.cpp @@ -0,0 +1,31 @@ +#include "settings.h" +#include + +Settings::Settings() { + refreshInterval = 1000; + sensorPin = 26; +} + +void Settings::load() { + EEPROM.begin(sizeof(Settings)); + EEPROM.get(0, *this); +} + +void Settings::save() { + EEPROM.put(0, *this); + EEPROM.commit(); +} + +void Settings::setRefreshInterval(unsigned long refreshInterval) { + this->refreshInterval = refreshInterval; + save(); +} + +long Settings::getRefreshInterval() { return refreshInterval; } + +void Settings::setSensorPin(unsigned int sensorPin) { + this->sensorPin = sensorPin; + save(); +} + +unsigned int Settings::getSensorPin() { return sensorPin; } \ No newline at end of file diff --git a/lib/settings/settings.h b/lib/settings/settings.h new file mode 100644 index 0000000..8fb6836 --- /dev/null +++ b/lib/settings/settings.h @@ -0,0 +1,19 @@ +class Settings { + +public: + Settings(); + + void load(); + + void setRefreshInterval(unsigned long); + long getRefreshInterval(); + + void setSensorPin(unsigned int); + unsigned int getSensorPin(); + +private: + unsigned long refreshInterval; + unsigned int sensorPin; + + void save(); +}; \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 6fd52bb..68796d6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,5 +12,9 @@ platform = espressif32 board = esp32doit-espduino framework = arduino -lib_deps = ottowinter/ESPAsyncWebServer-esphome@^3.0.0 +lib_deps = + ottowinter/ESPAsyncWebServer-esphome@^3.0.0 + adafruit/DHT sensor library@^1.4.4 + bblanchon/ArduinoJson@^6.20.1 + adafruit/Adafruit Unified Sensor@^1.1.7 monitor_speed = 115200 diff --git a/src/main.cpp b/src/main.cpp index 9893723..4e5b108 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,22 @@ +#include "settings.h" #include +#include +#include +#include +#include #include #include #include +void sendSensorData(); +void setupSensor(); + const char *ssid = "stoenka"; const char *password = "topavem2020"; +DHT *dht; +Settings settings; + AsyncWebServer server(80); AsyncEventSource events("/events"); @@ -20,20 +31,46 @@ void setupWifi() { Serial.println(); Serial.print("Connected: "); Serial.println(WiFi.localIP()); - Serial.println(); } void onConnect(AsyncEventSourceClient *client) { - Serial.println("Client connected to events"); + Serial.println("New client connected"); + sendSensorData(); } void setupServer() { - if (!SPIFFS.begin(true)) { Serial.println("Failed to mount file system"); return; } + // GET /settings - Get the current settings + server.on("/settings", HTTP_GET, [](AsyncWebServerRequest *req) { + StaticJsonDocument<200> json; + json["refreshInterval"] = settings.getRefreshInterval(); + json["sensorPin"] = settings.getSensorPin(); + + String buffer; + serializeJson(json, buffer); + + req->send(200, "application/json", buffer); + }); + + // PATCH /settings - Update settings + AsyncCallbackJsonWebHandler *settingsHandler = + new AsyncCallbackJsonWebHandler( + "/settings", [](AsyncWebServerRequest *req, JsonVariant &json) { + if (!json["refreshInterval"].isNull()) { + settings.setRefreshInterval(json["refreshInterval"].as()); + } + if (!json["sensorPin"].isNull()) { + settings.setSensorPin(json["sensorPin"].as()); + setupSensor(); + } + req->send(200); + }); + server.addHandler(settingsHandler); + events.onConnect(onConnect); server.addHandler(&events); server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); @@ -42,19 +79,39 @@ void setupServer() { Serial.println("Server running"); } +void setupSensor() { + dht = new DHT(settings.getSensorPin(), DHT11); + dht->begin(); +} + +void sendSensorData() { + StaticJsonDocument<200> json; + json["temp"] = (int)(dht->readTemperature() * 100 + 0.5) / 100.0; + json["hum"] = dht->readHumidity(); + + String buffer; + serializeJson(json, buffer); + + events.send(buffer.c_str(), "message", millis()); +} + void setup() { Serial.begin(115200); setupWifi(); setupServer(); + + settings.load(); + + setupSensor(); } -int i = 0; +unsigned long prevMillis = 0; void loop() { - i++; - if (i > 100000) { - i = 0; - events.send("hello", "message", millis()); + unsigned long currMillis = millis(); + if (currMillis - prevMillis > settings.getRefreshInterval()) { + prevMillis = currMillis; + sendSensorData(); } } \ No newline at end of file