Added settings
This commit is contained in:
parent
af449d1a2b
commit
59252e81b0
24
.vscode/settings.json
vendored
Normal file
24
.vscode/settings.json
vendored
Normal file
|
@ -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"
|
||||||
|
}
|
22
dashboard/src/lib/sensor.ts
Normal file
22
dashboard/src/lib/sensor.ts
Normal file
|
@ -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<SensorData | null>(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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
|
@ -1,18 +1,8 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { browser } from '$app/environment';
|
import { sensor } from '../lib/sensor';
|
||||||
|
|
||||||
if (browser) {
|
|
||||||
const source = new EventSource('http://10.50.0.114/events');
|
|
||||||
|
|
||||||
source.addEventListener('open', (e) => {
|
|
||||||
console.log('Connected');
|
|
||||||
});
|
|
||||||
|
|
||||||
source.addEventListener('message', (e) => {
|
|
||||||
console.log('message', e.data);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>Welcome to SvelteKit</h1>
|
{#if $sensor}
|
||||||
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
|
<p>Temperature {$sensor.temp} °C</p>
|
||||||
|
<p>Humidity {$sensor.hum}%</p>
|
||||||
|
{/if}
|
||||||
|
|
31
lib/settings/settings.cpp
Normal file
31
lib/settings/settings.cpp
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
#include "settings.h"
|
||||||
|
#include <EEPROM.h>
|
||||||
|
|
||||||
|
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; }
|
19
lib/settings/settings.h
Normal file
19
lib/settings/settings.h
Normal file
|
@ -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();
|
||||||
|
};
|
|
@ -12,5 +12,9 @@
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
board = esp32doit-espduino
|
board = esp32doit-espduino
|
||||||
framework = arduino
|
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
|
monitor_speed = 115200
|
||||||
|
|
73
src/main.cpp
73
src/main.cpp
|
@ -1,11 +1,22 @@
|
||||||
|
#include "settings.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <AsyncJson.h>
|
||||||
|
#include <DHT.h>
|
||||||
|
#include <EEPROM.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <SPIFFS.h>
|
#include <SPIFFS.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
|
||||||
|
void sendSensorData();
|
||||||
|
void setupSensor();
|
||||||
|
|
||||||
const char *ssid = "stoenka";
|
const char *ssid = "stoenka";
|
||||||
const char *password = "topavem2020";
|
const char *password = "topavem2020";
|
||||||
|
|
||||||
|
DHT *dht;
|
||||||
|
Settings settings;
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
AsyncEventSource events("/events");
|
AsyncEventSource events("/events");
|
||||||
|
|
||||||
|
@ -20,20 +31,46 @@ void setupWifi() {
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.print("Connected: ");
|
Serial.print("Connected: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
Serial.println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onConnect(AsyncEventSourceClient *client) {
|
void onConnect(AsyncEventSourceClient *client) {
|
||||||
Serial.println("Client connected to events");
|
Serial.println("New client connected");
|
||||||
|
sendSensorData();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupServer() {
|
void setupServer() {
|
||||||
|
|
||||||
if (!SPIFFS.begin(true)) {
|
if (!SPIFFS.begin(true)) {
|
||||||
Serial.println("Failed to mount file system");
|
Serial.println("Failed to mount file system");
|
||||||
return;
|
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<long>());
|
||||||
|
}
|
||||||
|
if (!json["sensorPin"].isNull()) {
|
||||||
|
settings.setSensorPin(json["sensorPin"].as<unsigned int>());
|
||||||
|
setupSensor();
|
||||||
|
}
|
||||||
|
req->send(200);
|
||||||
|
});
|
||||||
|
server.addHandler(settingsHandler);
|
||||||
|
|
||||||
events.onConnect(onConnect);
|
events.onConnect(onConnect);
|
||||||
server.addHandler(&events);
|
server.addHandler(&events);
|
||||||
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
|
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
|
||||||
|
@ -42,19 +79,39 @@ void setupServer() {
|
||||||
Serial.println("Server running");
|
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() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
setupWifi();
|
setupWifi();
|
||||||
setupServer();
|
setupServer();
|
||||||
|
|
||||||
|
settings.load();
|
||||||
|
|
||||||
|
setupSensor();
|
||||||
}
|
}
|
||||||
|
|
||||||
int i = 0;
|
unsigned long prevMillis = 0;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
i++;
|
unsigned long currMillis = millis();
|
||||||
if (i > 100000) {
|
if (currMillis - prevMillis > settings.getRefreshInterval()) {
|
||||||
i = 0;
|
prevMillis = currMillis;
|
||||||
events.send("hello", "message", millis());
|
sendSensorData();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user