Browse Source

change read sensors, trigger via web possible, modify js to fetch with HTTP_POST

tags/v1.0.0
Simon Schmidt 3 years ago
parent
commit
770c2436fd
4 changed files with 68 additions and 39 deletions
  1. 8
    2
      data/input.js
  2. 18
    5
      lib/httpserver/httpserver.cpp
  3. 33
    29
      lib/treppe/treppe.cpp
  4. 9
    3
      lib/treppe/treppe.h

+ 8
- 2
data/input.js View File

@@ -35,7 +35,7 @@ function rangeToOutput(range, output) {
document.getElementById(range).oninput = function () {
document.getElementById(output).innerHTML = this.value;
// add to pending range changes
rangeValues[range] = val;
rangeValues[this.id] = this.value;
}
}

@@ -75,7 +75,13 @@ document.addEventListener('DOMContentLoaded', () => {
// use data- attributes for action
document.querySelectorAll('.control').forEach((button) => {
button.onclick = () => {
fetch(`/action=${button.dataset.action}`)
fetch(`/action`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `control=${button.dataset.action}`
})
.then(response => console.log(response))
.catch(error => console.log('Error:', error));
}

+ 18
- 5
lib/httpserver/httpserver.cpp View File

@@ -31,20 +31,33 @@ auto HTTPServer::start() -> bool {
// add static root file handler for http
this->serveStatic("/", LittleFS, "/");
this->begin();
Serial.printf("Server active on Port 80 !\n\r");
logf("Server active on Port 80 !\n\r");
return true;
}

void HTTPServer::start_apps() {
this->on("/action=s_oben", [this]() {
// treppe->setsensor("oben")
this->on("/action", HTTP_POST, [this]() {
if (args()) {
logf("%s=%s\n", argName(0).c_str(), arg(0).c_str());

if (argName(0).equals("control")) {
if (arg(0).equals("s_oben")) {
treppe->overwrite_sensors(true, false);
logt("control => s_oben !\n");
}
else if (arg(0).equals("s_unten")) {
treppe->overwrite_sensors(false, true);
logt("control => s_unten !\n");
}
}
}
send(200, "text/plain", "accepted");
});

this->on("/update", HTTP_POST, [this]() {
if (args()) {
for (int i = 0; i < args() - 1; i++) {
Serial.printf("%s=%s\n", argName(i).c_str(), arg(i).c_str());
logf("%s=%s\n", argName(i).c_str(), arg(i).c_str());

if (argName(i).equals("range_act_pwm")) {
treppe->set_active_pwm(arg(i).toInt(), VORGABE_PROZENT);
@@ -73,7 +86,7 @@ void HTTPServer::start_apps() {
});

this->on("/terminal", HTTP_POST, [this]() {
// Serial.printf("got /terminal\n");
// logf("got /terminal\n");
if (tbuf_head) {
send(200, "text/plain", tbuf);
tbuf_head = 0;

+ 33
- 29
lib/treppe/treppe.cpp View File

@@ -27,8 +27,7 @@ bool Treppe::dimmer_tick(dimmer_t *dimmer, bool dim_type) {
Serial.printf("DIM_LDR: start: %d, ziel: %d\n", dimmer->start_pwm,
dimmer->ziel_pwm);
return true;
} else // DIM_STUFEN
{
} else { // DIM_STUFEN
Serial.printf("DIM_STUFEN: stufe: %d, start: %d, ziel: %d\n",
dimmer->stufe, dimmer->start_pwm, dimmer->ziel_pwm);

@@ -36,8 +35,7 @@ bool Treppe::dimmer_tick(dimmer_t *dimmer, bool dim_type) {
if (dimmer->stufe >= stufen - 1)
return true;
dimmer->stufe++;
} else // LR_RUNTER
{
} else { // LR_RUNTER
if (dimmer->stufe <= 0)
return true;
dimmer->stufe--;
@@ -108,33 +106,38 @@ void Treppe::print_state_on_change() {
}
}

bool Treppe::read_sensor(int sensor) {
void Treppe::overwrite_sensors(bool s_oben, bool s_unten) {
fsm_pend.web_ctrl_s_oben = s_oben;
fsm_pend.web_ctrl_s_unten = s_unten;
}

/*
reads sensors with edge detection
void Treppe::read_sensors() {
const bool s_oben = digitalRead(SENSOR_OBEN);
const bool s_unten = digitalRead(SENSOR_UNTEN);

returns true if motion was detected
returns false if no motion was detected
returns false if motion was detected, but state did not change back to not
detected
*/
uint8_t pegel = digitalRead(sensor);
static uint8_t pegel_alt[2] = {0, 0};
fsm_pend.sensor_oben = false;
fsm_pend.sensor_unten = false;

uint8_t index = 0;
if (sensor == SENSOR_OBEN)
index = 0;
else
index = 1;
// rising trigger => 1 cycle true !
if (s_oben && !fsm_pend.last_s_oben) {
fsm_pend.sensor_oben = true;
}
if (s_unten && !fsm_pend.last_s_unten) {
fsm_pend.sensor_unten = true;
}

if (pegel == 1 && pegel_alt[index] == 0) {
pegel_alt[index] = pegel;
return true;
} else {
pegel_alt[index] = pegel;
return false;
fsm_pend.last_s_oben = s_oben;
fsm_pend.last_s_unten = s_unten;

// check for manipulation over webserver
if(fsm_pend.web_ctrl_s_oben) {
fsm_pend.sensor_oben = true;
fsm_pend.web_ctrl_s_oben = false;
}
if(fsm_pend.web_ctrl_s_unten) {
fsm_pend.sensor_unten = true;
fsm_pend.web_ctrl_s_unten = false;
}
// return static_cast<bool>(pegel);
}

float Treppe::read_ldr() {
@@ -201,7 +204,6 @@ void Treppe::task() {

// TODO wenn LDR geändert => idle_pwm_soll anpassen
// fsm_pend.ldr_changed = true;

fsm_inputs.ldr_schwelle = check_ldr();

#ifdef DEBUG_TIMING
@@ -210,8 +212,10 @@ void Treppe::task() {
m = micros();
#endif

fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN);
fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN);
read_sensors();
fsm_inputs.sensor_oben = fsm_pend.sensor_oben;
fsm_inputs.sensor_unten = fsm_pend.sensor_unten;

fsm_inputs.anim_beendet = fsm_pend.anim_beendet;

#ifdef DEBUG_TIMING

+ 9
- 3
lib/treppe/treppe.h View File

@@ -38,7 +38,11 @@ private:
struct fsm_pending_inputs_t {
bool anim_beendet = true;
bool sensor_unten = false;
bool last_s_unten = false;
bool web_ctrl_s_unten = false;
bool sensor_oben = false;
bool last_s_oben = false;
bool web_ctrl_s_oben = false;
bool ldr_changed = false;
};
fsm_pending_inputs_t fsm_pend;
@@ -77,13 +81,13 @@ private:
enum fsm_laufrichtung_t { LR_RUNTER = 0, LR_HOCH = 1 };
enum fsm_dimmrichtung_t { DR_ABDIMMEN = 0, DR_AUFDIMMEN = 1 };

void read_sensors();
void print_state_on_change();

/* DIMM */
// bool dimmer(dimmer_t* dimmer, bool dim_type);
bool dimmer_tick(dimmer_t *dimmer, bool dim_type);
void start_animation(dimmer_t *dimmer, bool dim_type, uint16_t on_pwm,
uint16_t off_pwm);
// void berechne_dimmer(dimmer_t* dimmer, bool dim_type);
void print_state_on_change();

/* LDR */
bool read_sensor(int sensor);
@@ -105,6 +109,8 @@ public:
void set_time_ldr(const uint16_t value);
void set_time_per_stair(const uint16_t value);
void set_ldr_schwelle(const uint16_t value, const vorgabe_typ_t vorgabe_typ);

void overwrite_sensors(bool s_oben, bool s_unten);
};

#endif // __TREPPE_H

Loading…
Cancel
Save