new params
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import { ValidatorForm } from 'react-material-ui-form-validator';
|
import { ValidatorForm, TextValidator } from 'react-material-ui-form-validator';
|
||||||
|
|
||||||
import { Typography, Box, Checkbox } from '@material-ui/core';
|
import { Typography, Box, Checkbox } from '@material-ui/core';
|
||||||
import SaveIcon from '@material-ui/icons/Save';
|
import SaveIcon from '@material-ui/icons/Save';
|
||||||
@@ -25,7 +25,7 @@ class FanStatusRestController extends Component<FanStatusRestControllerProps> {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<SectionContent title='REST Controller' titleGutter>
|
<SectionContent title='Fan Control Settings' titleGutter>
|
||||||
<RestFormLoader
|
<RestFormLoader
|
||||||
{...this.props}
|
{...this.props}
|
||||||
render={props => (
|
render={props => (
|
||||||
@@ -48,9 +48,10 @@ function FanStatusRestControllerForm(props: FanStatusRestControllerFormProps) {
|
|||||||
<ValidatorForm onSubmit={saveData}>
|
<ValidatorForm onSubmit={saveData}>
|
||||||
<Box bgcolor="primary.main" color="primary.contrastText" p={2} mt={2} mb={2}>
|
<Box bgcolor="primary.main" color="primary.contrastText" p={2} mt={2} mb={2}>
|
||||||
<Typography variant="body1">
|
<Typography variant="body1">
|
||||||
The form below controls the LED via the RESTful service exposed by the ESP device.
|
The form below allow you to define the fan behaviour depending of the temperature.
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
<BlockFormControlLabel
|
<BlockFormControlLabel
|
||||||
control={
|
control={
|
||||||
<Checkbox
|
<Checkbox
|
||||||
@@ -59,8 +60,60 @@ function FanStatusRestControllerForm(props: FanStatusRestControllerFormProps) {
|
|||||||
color="primary"
|
color="primary"
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
label="Fan State ?"
|
label="Enable Fan"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<TextValidator
|
||||||
|
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:100']}
|
||||||
|
errorMessages={['Low Temperature required', "Must be a number", "Must be 0 or higher", "Max value is 100"]}
|
||||||
|
name="temperature_thres_low"
|
||||||
|
label="Low Temperature Threshold"
|
||||||
|
fullWidth
|
||||||
|
variant="outlined"
|
||||||
|
value={data.temperature_thres_low}
|
||||||
|
type="number"
|
||||||
|
onChange={handleValueChange('temperature_thres_low')}
|
||||||
|
margin="normal"
|
||||||
|
/>
|
||||||
|
<TextValidator
|
||||||
|
validators={['required', 'isNumber', 'minNumber:30', 'maxNumber:200']}
|
||||||
|
errorMessages={['High Temperature required', "Must be a number", "Must be 0 or higher", "Max value is 100"]}
|
||||||
|
name="temperature_thres_high"
|
||||||
|
label="High Temperature Threshold"
|
||||||
|
fullWidth
|
||||||
|
variant="outlined"
|
||||||
|
value={data.temperature_thres_high}
|
||||||
|
type="number"
|
||||||
|
onChange={handleValueChange('temperature_thres_high')}
|
||||||
|
margin="normal"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<TextValidator
|
||||||
|
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:40']}
|
||||||
|
errorMessages={['Fan GPIO pin is required', "Must be a number", "Must be 0 or higher", "Max value is 40"]}
|
||||||
|
name="fan_gpio"
|
||||||
|
label="Fan GPIO pin"
|
||||||
|
fullWidth
|
||||||
|
variant="outlined"
|
||||||
|
value={data.fan_gpio}
|
||||||
|
type="number"
|
||||||
|
onChange={handleValueChange('fan_gpio')}
|
||||||
|
margin="normal"
|
||||||
|
/>
|
||||||
|
<TextValidator
|
||||||
|
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:100']}
|
||||||
|
errorMessages={['Fan MAX is required', "Must be a number", "Must be 0 or higher", "Max value is 1024"]}
|
||||||
|
name="fan_max_speed"
|
||||||
|
label="Fan Max Speed"
|
||||||
|
fullWidth
|
||||||
|
variant="outlined"
|
||||||
|
value={data.fan_max_speed}
|
||||||
|
type="number"
|
||||||
|
onChange={handleValueChange('fan_max_speed')}
|
||||||
|
margin="normal"
|
||||||
|
/>
|
||||||
|
|
||||||
|
|
||||||
<FormActions>
|
<FormActions>
|
||||||
<FormButton startIcon={<SaveIcon />} variant="contained" color="primary" type="submit">
|
<FormButton startIcon={<SaveIcon />} variant="contained" color="primary" type="submit">
|
||||||
Save
|
Save
|
||||||
|
|||||||
@@ -13,4 +13,6 @@ export interface FanStatus {
|
|||||||
temperature_thres_low : number;
|
temperature_thres_low : number;
|
||||||
temperature_thres_high : number;
|
temperature_thres_high : number;
|
||||||
fan_status : boolean;
|
fan_status : boolean;
|
||||||
|
fan_gpio : number;
|
||||||
|
fan_max_speed : number;
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,18 @@
|
|||||||
#include <FanStateService.h>
|
#include <FanStateService.h>
|
||||||
|
|
||||||
FanStateService::FanStateService(AsyncWebServer* server,
|
FanStateService::FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
|
||||||
SecurityManager* securityManager) :
|
_httpEndpoint(FanSettings::read,
|
||||||
_httpEndpoint(FanState::read,
|
FanSettings::update,
|
||||||
FanState::update,
|
|
||||||
this,
|
this,
|
||||||
server,
|
server,
|
||||||
FAN_SETTINGS_ENDPOINT_PATH,
|
FAN_SETTINGS_ENDPOINT_PATH,
|
||||||
securityManager,
|
securityManager,
|
||||||
AuthenticationPredicates::IS_AUTHENTICATED) {
|
AuthenticationPredicates::IS_AUTHENTICATED),
|
||||||
|
_fsPersistence(FanSettings::read,
|
||||||
|
FanSettings::update,
|
||||||
|
this,
|
||||||
|
fs,
|
||||||
|
FAN_SETTINGS_FILE) {
|
||||||
pinMode(FAN_PIN, OUTPUT);
|
pinMode(FAN_PIN, OUTPUT);
|
||||||
// configure settings service update handler to update LED state
|
// configure settings service update handler to update LED state
|
||||||
addUpdateHandler([&](const String& originId) { onConfigUpdated(); }, false);
|
addUpdateHandler([&](const String& originId) { onConfigUpdated(); }, false);
|
||||||
@@ -18,11 +22,17 @@ FanStateService::FanStateService(AsyncWebServer* server,
|
|||||||
void FanStateService::begin() {
|
void FanStateService::begin() {
|
||||||
_state.fanStatus = DEFAULT_FAN_STATE;
|
_state.fanStatus = DEFAULT_FAN_STATE;
|
||||||
Serial.print("Starting The Fan Service");
|
Serial.print("Starting The Fan Service");
|
||||||
|
_fsPersistence.readFromFS();
|
||||||
onConfigUpdated();
|
onConfigUpdated();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FanStateService::onConfigUpdated() {
|
void FanStateService::onConfigUpdated() {
|
||||||
Serial.print(" ** Fan UPDATE ");
|
Serial.print(" ** Fan UPDATE ");
|
||||||
digitalWrite(FAN_PIN, _state.fanStatus ? FAN_ON : FAN_OFF);
|
//digitalWrite(FAN_PIN, _state.fanStatus ? FAN_ON : FAN_OFF);
|
||||||
|
analogWrite(FAN_PIN, _state.fanMaxSpeed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FanStateService::save() {
|
||||||
|
Serial.print(" ** Fan Write FS ");
|
||||||
|
_fsPersistence.writeToFS();
|
||||||
|
}
|
||||||
@@ -1,16 +1,20 @@
|
|||||||
#ifndef FanStateService_h
|
#ifndef FanStateService_h
|
||||||
#define FanStateService_h
|
#define FanStateService_h
|
||||||
|
|
||||||
|
#include <FSPersistence.h>
|
||||||
#include <HttpEndpoint.h>
|
#include <HttpEndpoint.h>
|
||||||
|
|
||||||
|
|
||||||
#define FAN_PIN 13
|
#define FAN_PIN 13
|
||||||
#define PRINT_DELAY 5000
|
#define PRINT_DELAY 5000
|
||||||
|
|
||||||
#define DEFAULT_FAN_STATE false
|
#define DEFAULT_FAN_STATE false
|
||||||
#define OFF_STATE "OFF"
|
#define DEFAULT_FAN_GPIO 13
|
||||||
#define ON_STATE "ON"
|
#define DEFAULT_THRES_LOW 25
|
||||||
|
#define DEFAULT_THRES_HIGH 70
|
||||||
|
#define DEFAULT_FAN_MAX_SPEED 1024
|
||||||
|
#define OFF_STATE "OFF"
|
||||||
|
#define ON_STATE "ON"
|
||||||
|
|
||||||
// Note that the built-in LED is on when the pin is low on most NodeMCU boards.
|
// Note that the built-in LED is on when the pin is low on most NodeMCU boards.
|
||||||
// This is because the anode is tied to VCC and the cathode to the GPIO 4 (Arduino pin 2).
|
// This is because the anode is tied to VCC and the cathode to the GPIO 4 (Arduino pin 2).
|
||||||
@@ -22,37 +26,54 @@
|
|||||||
#define FAN_OFF 0x1
|
#define FAN_OFF 0x1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define FAN_SETTINGS_FILE "/config/fanSettings.json"
|
||||||
#define FAN_SETTINGS_ENDPOINT_PATH "/rest/fanState"
|
#define FAN_SETTINGS_ENDPOINT_PATH "/rest/fanState"
|
||||||
|
|
||||||
|
|
||||||
class FanState {
|
class FanSettings {
|
||||||
public:
|
public:
|
||||||
bool fanStatus;
|
bool fanStatus;
|
||||||
int fanSpeed;
|
int fanSpeed;
|
||||||
|
int fanGpio;
|
||||||
|
int fanMaxSpeed;
|
||||||
|
int thresLow;
|
||||||
|
int thresHigh;
|
||||||
|
|
||||||
static void read(FanState& settings, JsonObject& root) {
|
static void read(FanSettings& settings, JsonObject& root) {
|
||||||
root["fan_status"] = settings.fanStatus;
|
root["fan_status"] = settings.fanStatus;
|
||||||
|
root["fan_gpio"] = settings.fanGpio;
|
||||||
|
root["fan_max_speed"] = settings.fanMaxSpeed;
|
||||||
|
root["temperature_thres_low"] = settings.thresLow;
|
||||||
|
root["temperature_thres_high"] = settings.thresHigh;
|
||||||
String s = root["fan_status"];
|
String s = root["fan_status"];
|
||||||
Serial.print(" ** Fan read [" + s + "]\n");
|
Serial.print(" ** Fan read [" + s + "]\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static StateUpdateResult update(JsonObject& root, FanState& fanState) {
|
static StateUpdateResult update(JsonObject& root, FanSettings& fanState) {
|
||||||
boolean newState = root["fan_status"] | DEFAULT_FAN_STATE;
|
boolean newFanState = root["fan_status"] | DEFAULT_FAN_STATE;
|
||||||
Serial.print(" ** Fan update [" + (String)newState + "]\n");
|
int newFanGpio = root["fan_gpio"] | DEFAULT_FAN_GPIO;
|
||||||
if (fanState.fanStatus != newState) {
|
int newMaxSpeed = root["fan_max_speed"] | DEFAULT_FAN_MAX_SPEED;
|
||||||
fanState.fanStatus = newState;
|
int newThresLow = root["temperature_thres_low"] | DEFAULT_THRES_LOW;
|
||||||
|
int newThresHigh = root["temperature_thres_high"] | DEFAULT_THRES_HIGH;
|
||||||
|
Serial.print(" ** Fan update [" + (String)newFanState + "]\n");
|
||||||
|
if (fanState.fanStatus != newFanState || fanState.fanGpio != newFanGpio ) {
|
||||||
|
fanState.fanStatus = newFanState;
|
||||||
|
fanState.fanGpio = newFanGpio;
|
||||||
|
fanState.fanMaxSpeed = newMaxSpeed;
|
||||||
|
fanState.thresLow = newThresLow;
|
||||||
|
fanState.thresHigh = newThresHigh;
|
||||||
return StateUpdateResult::CHANGED;
|
return StateUpdateResult::CHANGED;
|
||||||
}
|
}
|
||||||
return StateUpdateResult::UNCHANGED;
|
return StateUpdateResult::UNCHANGED;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void haRead(FanState& settings, JsonObject& root) {
|
static void haRead(FanSettings& settings, JsonObject& root) {
|
||||||
root["state"] = settings.fanStatus ? ON_STATE : OFF_STATE;
|
root["state"] = settings.fanStatus ? ON_STATE : OFF_STATE;
|
||||||
String s = root["state"];
|
String s = root["state"];
|
||||||
Serial.print(" ** Fan haRead [" + s + "] \n");
|
Serial.print(" ** Fan haRead [" + s + "] \n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static StateUpdateResult haUpdate(JsonObject& root, FanState& fanState) {
|
static StateUpdateResult haUpdate(JsonObject& root, FanSettings& fanState) {
|
||||||
String state = root["state"];
|
String state = root["state"];
|
||||||
String s = root["state"];
|
String s = root["state"];
|
||||||
Serial.print(" ** Fan haUpdate [" + s + "] \n");
|
Serial.print(" ** Fan haUpdate [" + s + "] \n");
|
||||||
@@ -72,13 +93,15 @@ class FanState {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class FanStateService : public StatefulService<FanState> {
|
class FanStateService : public StatefulService<FanSettings> {
|
||||||
public:
|
public:
|
||||||
FanStateService(AsyncWebServer* server, SecurityManager* securityManager);
|
FanStateService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||||
void begin();
|
void begin();
|
||||||
|
void save();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HttpEndpoint<FanState> _httpEndpoint;
|
HttpEndpoint<FanSettings> _httpEndpoint;
|
||||||
|
FSPersistence<FanSettings> _fsPersistence;
|
||||||
|
|
||||||
void registerConfig();
|
void registerConfig();
|
||||||
void onConfigUpdated();
|
void onConfigUpdated();
|
||||||
|
|||||||
17
src/main.cpp
17
src/main.cpp
@@ -8,7 +8,7 @@
|
|||||||
#define SERIAL_BAUD_RATE 115200
|
#define SERIAL_BAUD_RATE 115200
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
ESP8266React esp8266React(&server);
|
// ESP8266React esp8266React(&server);
|
||||||
/*
|
/*
|
||||||
LightMqttSettingsService lightMqttSettingsService =
|
LightMqttSettingsService lightMqttSettingsService =
|
||||||
LightMqttSettingsService(&server, esp8266React.getFS(), esp8266React.getSecurityManager());
|
LightMqttSettingsService(&server, esp8266React.getFS(), esp8266React.getSecurityManager());
|
||||||
@@ -20,7 +20,20 @@ LightStateService lightStateService = LightStateService(&server,
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
FanStateService fanStateService = FanStateService(&server,esp8266React.getSecurityManager());
|
/*
|
||||||
|
#ifdef ESP32
|
||||||
|
SPIFFS.begin(true);
|
||||||
|
ESP8266React EMSESP::esp8266React(&server, &LittleFS);
|
||||||
|
FanStateService fanStateService = FanStateService(&server,&SPIFFS,esp8266React.getSecurityManager());
|
||||||
|
#elif defined(ESP8266)
|
||||||
|
LittleFS.begin();
|
||||||
|
ESP8266React EMSESP::esp8266React(&server, &LittleFS);
|
||||||
|
FanStateService fanStateService = FanStateService(&server,&LittleFS,esp8266React.getSecurityManager());
|
||||||
|
#endif
|
||||||
|
*/
|
||||||
|
ESP8266React esp8266React(&server);
|
||||||
|
FanStateService fanStateService = FanStateService(&server,esp8266React.getFS(),esp8266React.getSecurityManager());
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// start serial and filesystem
|
// start serial and filesystem
|
||||||
|
|||||||
Reference in New Issue
Block a user