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..f6a04aa 100644 --- a/blitzercontroller/platformio.ini +++ b/blitzercontroller/platformio.ini @@ -12,6 +12,7 @@ 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 +upload_port=/dev/tty.usbserial-1320 +monitor_port=/dev/tty.usbserial-1320 diff --git a/blitzercontroller/src/fluroclock.cpp b/blitzercontroller/src/fluroclock.cpp new file mode 100644 index 0000000..09afb47 --- /dev/null +++ b/blitzercontroller/src/fluroclock.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +String url = "http://151.217.19.22/api/panel/numeric"; +WiFiClient client; +HTTPClient http; + +void setPanel(int, char); + +void displaySpeed(float speed) { + char buffer[6]; + dtostrf(speed, 6, 2, buffer); + setPanel(4, buffer[1]); + setPanel(3, buffer[2]); + setPanel(2, buffer[4]); + setPanel(1, buffer[5]); +} + +void setPanel(int panel_id, char value) { + http.begin(client, url.c_str()); + http.addHeader("Content-Type", "application/json"); + String request = "{\"panel_id\":\""; + request.concat(panel_id); + request.concat("\", \"value\": \""); + request.concat(value); + request.concat("\"}"); + Serial.println(request); + http.POST(request); + http.end(); +} \ No newline at end of file diff --git a/blitzercontroller/src/fluroclock.h b/blitzercontroller/src/fluroclock.h new file mode 100644 index 0000000..1cd9605 --- /dev/null +++ b/blitzercontroller/src/fluroclock.h @@ -0,0 +1,5 @@ +#ifndef fluroclock_h +#define fluroclock_h + +void displaySpeed(float speed); +#endif \ No newline at end of file diff --git a/blitzercontroller/src/index.html b/blitzercontroller/src/index.html new file mode 100644 index 0000000..b2513c7 --- /dev/null +++ b/blitzercontroller/src/index.html @@ -0,0 +1,78 @@ + + + + + + + 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..526540b 100644 --- a/blitzercontroller/src/main.cpp +++ b/blitzercontroller/src/main.cpp @@ -1,121 +1,12 @@ -#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 - + handleSpeedSetup(); + handleRemoteSetup(); 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 +#include +#endif \ No newline at end of file diff --git a/blitzercontroller/src/remote.cpp b/blitzercontroller/src/remote.cpp new file mode 100644 index 0000000..19e76ea --- /dev/null +++ b/blitzercontroller/src/remote.cpp @@ -0,0 +1,31 @@ +#include + +// Create a struct_message called myData +struct_message recvRemoteData; + +// callback function that will be executed when data is received +void OnDataRecv(uint8_t *mac, uint8_t *incomingData, uint8_t len) +{ + Serial.println("received data"); + String data = String((char *)incomingData); + if (data.equals("flash")) + { + flash(); + } +} + +void handleRemoteSetup() +{ + + // Init ESP-NOW + if (esp_now_init() != 0) + { + Serial.println("Error initializing ESP-NOW"); + return; + } + + // Once ESPNow is successfully Init, we will register for recv CB to + // get recv packer info + esp_now_set_self_role(ESP_NOW_ROLE_SLAVE); + esp_now_register_recv_cb(OnDataRecv); +} \ No newline at end of file diff --git a/blitzercontroller/src/remote.h b/blitzercontroller/src/remote.h new file mode 100644 index 0000000..5c1e336 --- /dev/null +++ b/blitzercontroller/src/remote.h @@ -0,0 +1,13 @@ +#ifndef remote_h +#define remote_h +#include +#include +#include + +typedef struct struct_message { + int action; +} struct_message; + + +void handleRemoteSetup(); +#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..9ea647f --- /dev/null +++ b/blitzercontroller/src/site.h @@ -0,0 +1,81 @@ +#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..6b1f138 --- /dev/null +++ b/blitzercontroller/src/speed.cpp @@ -0,0 +1,144 @@ +#include + +#define PIN_SW1 D6 +#define PIN_SW2 D5 +#define PIN_TRIGGER D7 + +unsigned long sw1_lastTime = 0; +unsigned long sw2_lastTime = 0; +unsigned long sw1_lastTime_e = 0; +unsigned long sw2_lastTime_e = 0; + +float flashspeed = 20; // in kmh +unsigned long flashdeadtime = 1000; // in ms +float calib_distance = 0.1; // distance of sensors in meters +float lastMeasuredSpeeds[10]; +float highscore = 0; + +unsigned long last_flash = 0; +bool flashNext = false; + +#define SWDEBOUNCE 1000000 + +ICACHE_RAM_ATTR void interrupt_sw1(); +ICACHE_RAM_ATTR void interrupt_sw2(); +float getLastSpeed(); + +void handleSpeedSetup() +{ + pinMode(PIN_SW1, INPUT_PULLUP); + pinMode(PIN_SW2, INPUT_PULLUP); + pinMode(PIN_TRIGGER, OUTPUT); + attachInterrupt(digitalPinToInterrupt(PIN_SW2), interrupt_sw2, FALLING); + attachInterrupt(digitalPinToInterrupt(PIN_SW1), interrupt_sw1, FALLING); + digitalWrite(PIN_TRIGGER, HIGH); // active low +} + +void handleSpeedLoop() +{ + // reset micros within the first half second to care for overflowing micros + if (micros() < 500000) + { + sw1_lastTime = 0; + sw2_lastTime = 0; + sw1_lastTime_e = 0; + sw2_lastTime_e = 0; + + } + if (millis() < 500) + { + last_flash = 0; + } + if (sw1_lastTime > 0 && sw2_lastTime > 0 && sw2_lastTime - sw1_lastTime > 1200 && sw2_lastTime - sw1_lastTime < 10000000) + { + // 0,036 km/h - 300 km/h und sw2 nach sw1 ausgelöst + doTrigger(getLastSpeed()); + sw2_lastTime = 0; + sw1_lastTime = 0; + } + { + /* code */ + } + + if (flashNext) + { + flashNext = false; + Serial.print("Flashing"); + pinMode(PIN_TRIGGER, INPUT); // high impedance + delay(100); + pinMode(PIN_TRIGGER, OUTPUT); + digitalWrite(PIN_TRIGGER, LOW); + Serial.println(".."); + } +} + +void doTrigger(float speed) +{ + 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"); + flash(); + } + else + { + Serial.print(">> Speed="); + Serial.print(speed); + Serial.println(" km/h"); + } + } +} + +void flash() +{ + flashNext = true; +} + +void addLastSpeed(float speed) +{ + for (int i = 0; i < 9; i++) + { + lastMeasuredSpeeds[i] = lastMeasuredSpeeds[i + 1]; + } + lastMeasuredSpeeds[9] = speed; + displaySpeed(speed); + if (highscore < speed) + { + highscore = speed; + } +} + +ICACHE_RAM_ATTR void interrupt_sw1() +{ + if (sw1_lastTime_e + SWDEBOUNCE < micros()) + { + sw1_lastTime_e = micros(); + sw1_lastTime = micros(); + Serial.print("SW1 - "); + Serial.println(micros()); + } +} + +ICACHE_RAM_ATTR void interrupt_sw2() +{ + if (sw2_lastTime_e + SWDEBOUNCE < micros()) + { + sw2_lastTime_e = micros(); + Serial.print("SW2 - "); + Serial.println(micros()); + if (sw1_lastTime > 0) + { + sw2_lastTime = micros(); + } + } +} + +float getLastSpeed() +{ + return calib_distance / ((sw2_lastTime - sw1_lastTime + 11404) / 1000000.0) * 3.6; // lichtschranke 1 kaputt, hat delay, brauchen 11404 microsekunden mehr, trust me. +} diff --git a/blitzercontroller/src/speed.h b/blitzercontroller/src/speed.h new file mode 100644 index 0000000..1f602a2 --- /dev/null +++ b/blitzercontroller/src/speed.h @@ -0,0 +1,17 @@ +#ifndef speed_h +#define speed_h +#include +#include + +extern float flashspeed; +extern float highscore; +extern unsigned long flashdeadtime; +extern float calib_distance; + +extern float lastMeasuredSpeeds[10]; +void flash(); +void handleSpeedLoop(); +void handleSpeedSetup(); +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..b8616b0 --- /dev/null +++ b/blitzercontroller/src/webserver.cpp @@ -0,0 +1,58 @@ +#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.print("ESP8266 Board MAC Address: "); + Serial.println(WiFi.macAddress()); + Serial.println(""); + Serial.print("ESP822 IP: "); + Serial.println(WiFi.localIP()); + Serial.println(WiFi.channel()); + 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(", \"threshold\": "); + response.concat(flashspeed); + 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