From 79f110c0ce4b23606d2a373ecb0e07e3fc8ceb5d Mon Sep 17 00:00:00 2001 From: henne Date: Sun, 24 Jul 2022 03:09:22 +0200 Subject: [PATCH] feature: api with highscore, last 10 speed measures and manual flashing as well as setting the speedlimit via api, hardcoded for mch2022 --- .gitignore | 2 +- blitzercontroller/platformio.ini | 5 +- blitzercontroller/src/index.html | 71 +++++++++++++++ blitzercontroller/src/main.cpp | 120 ++----------------------- blitzercontroller/src/main.h | 6 ++ blitzercontroller/src/site.h | 75 ++++++++++++++++ blitzercontroller/src/speed.cpp | 131 ++++++++++++++++++++++++++++ blitzercontroller/src/speed.h | 16 ++++ blitzercontroller/src/webserver.cpp | 51 +++++++++++ blitzercontroller/src/webserver.h | 12 +++ 10 files changed, 370 insertions(+), 119 deletions(-) create mode 100644 blitzercontroller/src/index.html create mode 100644 blitzercontroller/src/main.h create mode 100644 blitzercontroller/src/site.h create mode 100644 blitzercontroller/src/speed.cpp create mode 100644 blitzercontroller/src/speed.h create mode 100644 blitzercontroller/src/webserver.cpp create mode 100644 blitzercontroller/src/webserver.h diff --git a/.gitignore b/.gitignore index 90552b0..b262bf5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ .clang_complete .gcc-flags.json .pio - +.DS_Store diff --git a/blitzercontroller/platformio.ini b/blitzercontroller/platformio.ini index d7ea76b..f620857 100644 --- a/blitzercontroller/platformio.ini +++ b/blitzercontroller/platformio.ini @@ -12,6 +12,5 @@ platform = espressif8266 board = d1_mini framework = arduino - - -monitor_speed = 115200 \ No newline at end of file +monitor_speed = 115200 +lib_deps = me-no-dev/ESP Async WebServer@^1.2.3 diff --git a/blitzercontroller/src/index.html b/blitzercontroller/src/index.html new file mode 100644 index 0000000..5ce2ed0 --- /dev/null +++ b/blitzercontroller/src/index.html @@ -0,0 +1,71 @@ + + + + + + + Blitzer + + + +

Blitzercontrol

+

Highscore

+ 23km/h +

Letzte Messungen

+ +

Manuelle Steuerung

+ +

Speed setting

+ km/h
+ + + + \ No newline at end of file diff --git a/blitzercontroller/src/main.cpp b/blitzercontroller/src/main.cpp index ce409ce..216a916 100644 --- a/blitzercontroller/src/main.cpp +++ b/blitzercontroller/src/main.cpp @@ -1,121 +1,11 @@ -#include - -float flashspeed=20; //in kmh -unsigned long flashdeadtime=1000; //in ms - -#define PIN_SW1 D6 -#define PIN_SW2 D5 -#define PIN_TRIGGER D7 -volatile boolean sw1_flag = false; -volatile boolean sw2_flag = false; - -unsigned long sw1_lastTime=0; -unsigned long sw2_lastTime=0; - -float calib_distance=0.062; //distance of sensors in meters - -#define SWDEBOUNCE 100000 - -ICACHE_RAM_ATTR void interrupt_sw1(); -ICACHE_RAM_ATTR void interrupt_sw2(); -float getLastSpeed1(); -float getLastSpeed2(); -void doTrigger1(); -void doTrigger2(); +#include void setup() { - pinMode(PIN_SW1, INPUT_PULLUP); - pinMode(PIN_SW2, INPUT_PULLUP); - pinMode(PIN_TRIGGER, OUTPUT); - attachInterrupt(digitalPinToInterrupt(PIN_SW1), interrupt_sw1, FALLING); - attachInterrupt(digitalPinToInterrupt(PIN_SW2), interrupt_sw2, FALLING); - digitalWrite(PIN_TRIGGER, HIGH); //active low - + handleSetup(); Serial.begin(115200); + beginWiFi(); } void loop() { - - if (sw1_flag){ - sw1_flag=false; - sw1_lastTime=micros(); - Serial.println("SW1"); - doTrigger1(); - } - if (sw2_flag){ - sw2_flag=false; - sw2_lastTime=micros(); - Serial.println("SW2"); - doTrigger2(); - } - -} - - -void doTrigger1() { - static unsigned long last_flash=0; - - - float speed=getLastSpeed1(); - - if (speed*3.6<0.1) { - return; - } - - if (millis()-last_flash>flashdeadtime) { //deadtime - last_flash=millis(); - - if (speed*3.6 >= flashspeed) { - Serial.print("> Speed="); Serial.print(speed*3.6); Serial.println(" km/h"); - Serial.println("Flash"); - pinMode(PIN_TRIGGER, INPUT); //high impedance - delay(100); - pinMode(PIN_TRIGGER, OUTPUT); digitalWrite(PIN_TRIGGER, LOW); - } - } -} - - -void doTrigger2() { - static unsigned long last_flash=0; - - float speed=getLastSpeed2(); - - - if (speed*3.6<0.1) { - return; - } - - if (millis()-last_flash>flashdeadtime) { //deadtime - last_flash=millis(); - - if (speed*3.6 >= flashspeed) { - Serial.print("> Speed="); Serial.print(speed*3.6); Serial.println(" km/h"); - Serial.println("Flash"); - pinMode(PIN_TRIGGER, INPUT); //high impedance - delay(100); - pinMode(PIN_TRIGGER, OUTPUT); digitalWrite(PIN_TRIGGER, LOW); - } - } -} - - - -ICACHE_RAM_ATTR void interrupt_sw1() { - if (sw1_lastTime+SWDEBOUNCE +#include +#include +#endif \ No newline at end of file diff --git a/blitzercontroller/src/site.h b/blitzercontroller/src/site.h new file mode 100644 index 0000000..5d070ac --- /dev/null +++ b/blitzercontroller/src/site.h @@ -0,0 +1,75 @@ +#include + +const char index_html[] PROGMEM = R"rawliteral( + + + + + + + Blitzer + + + +

Blitzercontrol

+

Highscore

+ 23km/h +

Letzte Messungen

+
    +
  • +
  • +
  • +
  • +
  • +
  • +
  • +
  • +
  • +
  • +
+

Manuelle Steuerung

+ +

Speed setting

+ km/h
+ + + + +)rawliteral"; \ No newline at end of file diff --git a/blitzercontroller/src/speed.cpp b/blitzercontroller/src/speed.cpp new file mode 100644 index 0000000..12a5b7f --- /dev/null +++ b/blitzercontroller/src/speed.cpp @@ -0,0 +1,131 @@ +#include + +#define PIN_SW1 D6 +#define PIN_SW2 D5 +#define PIN_TRIGGER D7 +volatile boolean sw1_flag = false; +volatile boolean sw2_flag = false; + +unsigned long sw1_lastTime = 0; +unsigned long sw2_lastTime = 0; + +float flashspeed = 20; // in kmh +unsigned long flashdeadtime = 1000; // in ms +float calib_distance = 0.062; // distance of sensors in meters +float lastMeasuredSpeeds[10]; +float highscore = 0; + +unsigned long last_flash = 0; + +#define SWDEBOUNCE 100000 + +ICACHE_RAM_ATTR void interrupt_sw1(); +ICACHE_RAM_ATTR void interrupt_sw2(); +float getLastSpeed1(); +float getLastSpeed2(); + +void handleSetup() +{ + pinMode(PIN_SW1, INPUT_PULLUP); + pinMode(PIN_SW2, INPUT_PULLUP); + pinMode(PIN_TRIGGER, OUTPUT); + attachInterrupt(digitalPinToInterrupt(PIN_SW1), interrupt_sw1, FALLING); + attachInterrupt(digitalPinToInterrupt(PIN_SW2), interrupt_sw2, FALLING); + digitalWrite(PIN_TRIGGER, HIGH); // active low +} + +void handleLoop() +{ + // reset micros within the first half second to care for overflowing micros + if (micros() < 500000) { + sw1_flag = false; + sw1_lastTime = 0; + sw2_flag = false; + sw2_lastTime = 0; + } + if (millis() < 500) { + last_flash = 0; + } + if (sw1_flag) + { + sw1_flag = false; + sw1_lastTime = micros(); + Serial.println("SW1"); + doTrigger(getLastSpeed1()); + } + if (sw2_flag) + { + sw2_flag = false; + sw2_lastTime = micros(); + Serial.println("SW2"); + doTrigger(getLastSpeed2()); + } +} + +void doTrigger(float speed) +{ + + if (speed < 0.1) + { + return; + } + + if (millis() - last_flash > flashdeadtime) + { // deadtime + last_flash = millis(); + + if (speed >= flashspeed) + { + addLastSpeed(speed); + Serial.print("> Speed="); + Serial.print(speed); + Serial.println(" km/h"); + flash(); + } + } +} + +void flash() { + Serial.println("Flash"); + pinMode(PIN_TRIGGER, INPUT); // high impedance + delay(100); + pinMode(PIN_TRIGGER, OUTPUT); + digitalWrite(PIN_TRIGGER, LOW); +} + +void addLastSpeed(float speed) +{ + for (int i = 0; i < 9; i++) + { + lastMeasuredSpeeds[i] = lastMeasuredSpeeds[i + 1]; + } + lastMeasuredSpeeds[9] = speed; + if (highscore < speed) { + highscore = speed; + } +} + +ICACHE_RAM_ATTR void interrupt_sw1() +{ + if (sw1_lastTime + SWDEBOUNCE < micros()) + { + sw1_flag = true; + } +} + +ICACHE_RAM_ATTR void interrupt_sw2() +{ + if (sw2_lastTime + SWDEBOUNCE < micros()) + { + sw2_flag = true; + } +} + +float getLastSpeed1() +{ + return calib_distance / ((sw1_lastTime - sw2_lastTime) / 1000000.0) * 3.6; +} +float getLastSpeed2() +{ + return calib_distance / ((sw2_lastTime - sw1_lastTime) / 1000000.0) * 3.6; +} diff --git a/blitzercontroller/src/speed.h b/blitzercontroller/src/speed.h new file mode 100644 index 0000000..dd6e8dc --- /dev/null +++ b/blitzercontroller/src/speed.h @@ -0,0 +1,16 @@ +#ifndef speed_h +#define speed_h +#include + +extern float flashspeed; +extern float highscore; +extern unsigned long flashdeadtime; +extern float calib_distance; + +extern float lastMeasuredSpeeds[10]; +void flash(); +void handleLoop(); +void handleSetup(); +void doTrigger(float speed); +void addLastSpeed(float speed); +#endif \ No newline at end of file diff --git a/blitzercontroller/src/webserver.cpp b/blitzercontroller/src/webserver.cpp new file mode 100644 index 0000000..c916553 --- /dev/null +++ b/blitzercontroller/src/webserver.cpp @@ -0,0 +1,51 @@ +#include + +AsyncWebServer server(80); + +void beginWiFi() { + WiFi.setHostname("chaoswestbliz"); + WiFi.begin("MCH2022-open", ""); + while(WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println(WiFi.localIP()); + server.on("/", HTTP_GET, [] (AsyncWebServerRequest *request) { + request->send(200, "text/html", index_html); + }); + server.on("/set", HTTP_GET, [] (AsyncWebServerRequest *request) { + String s; + String message; + if (request->hasParam("speed")) { + s = request->getParam("speed")->value(); + flashspeed = s.toFloat(); + message = "Speed set successfully"; + } else { + message = "No message sent"; + } + Serial.print("Speed set to "); + Serial.print(s); + Serial.println("km/h"); + request->send(200, "text/plain", "OK"); + }); + server.on("/flash", HTTP_GET, [] (AsyncWebServerRequest *request) { + flash(); + request->send(200, "text/plain", "OK"); + }); + server.on("/data.json", HTTP_GET, [] (AsyncWebServerRequest *request) { + String response = "{\"highscore\":"; + response.concat(highscore); + response.concat(",\"lastSpeeds\": ["); + for(int i=0;i<10;i++) { + response.concat(lastMeasuredSpeeds[i]); + if(i<9) { + response.concat(", "); + } + } + response.concat("]}"); + request->send(200, "application/json", response); + }); + + server.begin(); +} \ No newline at end of file diff --git a/blitzercontroller/src/webserver.h b/blitzercontroller/src/webserver.h new file mode 100644 index 0000000..d3f2c74 --- /dev/null +++ b/blitzercontroller/src/webserver.h @@ -0,0 +1,12 @@ +#ifndef webserver_h +#define webserver_h + +#include +#include +#include +#include +#include + +void beginWiFi(); + +#endif \ No newline at end of file