Arduino Ethernet Escape Room Numbers LCD Combination Interactive

Hľadaný kód pre miestnosť KE-012

Escape Room - Arduino + Ethernet, Webclient - výhra hráča

Úniková hra - Webclient - Arduino


Popis hardvéru:

  • Arduino Nano / Uno / Arduino Mega 2560
  • Ethernet shield Wiznet W5100
  • 5x spínacie tlačidlo
  • LCD displej 16x2 / 20x4
  • I2C prevodník pre LCD displej
  • Elektromagnetické relé SRD-05VDC-SL-C / SSR relé OMRON G3MB-202P
  • Solenoidový zámok dverí 12/24V
  • Popis funkčnosti:

  • Hráč má k dispozícii 5 tlačidiel pre číselný vstup.
  • Štyrmi tlačidlami hráč inkrementuje číslo na každej zo štyroch pozícii.
  • Piatym tlačidlom sú dáta odoslané na webserver, ktorý vráti payload Access / Problem.
  • V prípade "Access" payloadu (odpovede) sa aktivuje relé a solenoidový zámok dverí sa otvorí - hráčom sa umožní pohyb ďalej v hre, indikuje zelená dióda.
  • V prípade payloadu "Problem" ostáva relé v rozpojenom stave, zámok sa neotvorí. Dvere nie je možné otvoriť.
  • Hráč môže byť informovaný o neúspechu aj bzučiakom, alebo LED diódou s indikáciou.
  • Hráč dokáže zadať číslo maximálne 10 krát. Pokusy sa rátajú client-side.
  • Implementácie:

  • Jedno číslo: - tlačidlami 1-5 sa odošle číslo (1,2,3,4,5), pričom jedno z nich je správne. Max. 5 kombinácii, neobsahuje tlačidlo enter.
  • Štyri čísla s opakovaním: - tlačidlami 1-4 sa inkrementuje číslo v rozsahu 0-9 pre každú zo štyroch pozícii s možnosťou opakovania. Max. 10 000 kombinácii.
  • Štyri čísla bez opakovania: - Implementácia bez opakovania čísel (funkčnosť ako pri Štyri čísla s opakovaním), max 5040 kombinácii.
  • Webové rozhranie:

  • Webové rozhranie ponúka možnosť upraviť hľadané číslo v každej z troch implementácii (možno používať súčasne).
  • Zohľadňuje obmedzenie všetkých implementácii (opakovanie, dĺžka číselného kódu).
  • Možno zmeniť prihlasovacie údaje. Možno zapnúť logovanie a štatistiku prijatých dát od mikrokontroléru Arduino. Štatistika úspešnosti hráčov so záznamom prijatých číselných kombinácii, časov prijatia.
  • Schéma zapojenia:

    Schéma zapojenia - Escape Room - Arduino + Ethernet, Webclient

    Block scheme

    Bloková schéma - Escape Room - Arduino + Ethernet, Webclient

    Arduino + Ethernet W5100 program:

    /*|----------------------------------------------------------|*/
    /*|SKETCH FOR ESCAPE ROOM - Arduino + Ethernet W5100         |*/
    /*|Author: Bc. MARTIN CHLEBOVEC                              |*/
    /*|FB: https://www.facebook.com/martin.s.chlebovec           |*/
    /*|EMAIL: martinius96@gmail.com                              |*/
    /*|WEB: https://arduino.php5.sk?lang=en                      |*/
    /*|----------------------------------------------------------|*/
    #include <SPI.h>
    #include <Ethernet.h>
    
    byte mac[] = { 0xAA, 0xBB, 0xCC, 0xAA, 0xBB, 0xCC };
    IPAddress ip(192, 168, 1, 101);
    IPAddress gateway(192, 168, 1, 1);
    IPAddress subnet(255, 255, 255, 0);
    char serverName[] = "192.168.1.254"; //or hostname
    int serverPort = 80;
    
    const int buttonPin1 = 9;
    const int buttonPin2 = 8;
    const int buttonPin3 = 7;
    const int buttonPin4 = 6;
    const int buttonPin5 = 5;
    const int relay = 3;
    
    int buttonState1 = HIGH;
    int buttonState2 = HIGH;
    int buttonState3 = HIGH;
    int buttonState4 = HIGH;
    int buttonState5 = HIGH;
    
    int lastButtonState1 = HIGH;
    int lastButtonState2 = HIGH;
    int lastButtonState3 = HIGH;
    int lastButtonState4 = HIGH;
    int lastButtonState5 = HIGH;
    
    unsigned long lastDebounceTime1 = 0;
    unsigned long lastDebounceTime2 = 0;
    unsigned long lastDebounceTime3 = 0;
    unsigned long lastDebounceTime4 = 0;
    unsigned long lastDebounceTime5 = 0;
    unsigned long debounceInterval = 50;
    
    
    
    EthernetClient client;
    int number = 0;
    String url = "/sendnumber.php";
    void setup() {
      Serial.begin(115200);
      pinMode(relay, OUTPUT);
      digitalWrite(relay, HIGH); //lock door
      pinMode(buttonPin1, INPUT_PULLUP);
      pinMode(buttonPin2, INPUT_PULLUP);
      pinMode(buttonPin3, INPUT_PULLUP);
      pinMode(buttonPin4, INPUT_PULLUP);
      pinMode(buttonPin5, INPUT_PULLUP);
      Ethernet.begin(mac, ip, gateway, gateway, subnet);
      Serial.println("IP address set to Ethernet shield:");
      Serial.println(Ethernet.localIP());
      delay(2000);
      Serial.println("Ready");
    }
    
    void send_datas() {
      String my_datas = String(number);
      String data = "number=" + my_datas;
      if (client.connect(serverName, serverPort)) {
        client.println("POST " + url + " HTTP/1.0");
        client.println("Host: " + (String)serverName);
        client.println("User-Agent: ArduinoEthernetWiznet");
        client.println("Connection: close");
        client.println("Content-Type: application/x-www-form-urlencoded;");
        client.print("Content-Length: ");
        client.println(data.length());
        client.println();
        client.println(data);
        while (client.connected()) {
          //HTTP HEADER
          String line = client.readStringUntil('\n'); //HTTP header
          if (line == "\r") {
            break; //blank line between HTTP header and payload
          }
        }
        String line = client.readStringUntil('\n'); //Payload first line
        if (line == "Access") {
          Serial.println("Access gained!");
          digitalWrite(relay, LOW); //unlock door to next level
        } else {
          Serial.println("Access denied!");
          digitalWrite(relay, HIGH); //lock door
        }
      } else {
        Serial.println("Error connecting to webserver!");
      }
      client.stop();
    }
    
    void loop() {
      int reading1 = digitalRead(buttonPin1);
      int reading2 = digitalRead(buttonPin2);
      int reading3 = digitalRead(buttonPin3);
      int reading4 = digitalRead(buttonPin4);
      int reading5 = digitalRead(buttonPin5);
    
      if (reading1 != lastButtonState1) {
        lastDebounceTime1 = millis();
      }
    
      if (reading2 != lastButtonState2) {
        lastDebounceTime2 = millis();
      }
    
      if (reading3 != lastButtonState3) {
        lastDebounceTime3 = millis();
      }
    
      if (reading4 != lastButtonState4) {
        lastDebounceTime4 = millis();
      }
    
      if (reading5 != lastButtonState5) {
        lastDebounceTime5 = millis();
      }
      if ((millis() - lastDebounceTime1) > debounceInterval) {
        if (reading1 != buttonState1) {
          buttonState1 = reading1;
          if (buttonState1 == HIGH) {
            number = 1;
            send_datas();
          }
        }
      }
    
      if ((millis() - lastDebounceTime2) > debounceInterval) {
        if (reading2 != buttonState2) {
          buttonState2 = reading2;
          if (buttonState2 == HIGH) {
            number = 2;
            send_datas();
          }
        }
      }
    
      if ((millis() - lastDebounceTime3) > debounceInterval) {
        if (reading3 != buttonState3) {
          buttonState3 = reading3;
          if (buttonState3 == HIGH) {
            number = 3;
            send_datas();
          }
        }
      }
    
      if ((millis() - lastDebounceTime4) > debounceInterval) {
        if (reading4 != buttonState4) {
          buttonState4 = reading4;
          if (buttonState4 == HIGH) {
            number = 4;
            send_datas();
          }
        }
      }
      if ((millis() - lastDebounceTime5) > debounceInterval) {
        if (reading5 != buttonState5) {
          buttonState5 = reading5;
          if (buttonState5 == HIGH) {
            number = 5;
            send_datas();
          }
        }
      }
      lastButtonState1 = reading1;
      lastButtonState2 = reading2;
      lastButtonState3 = reading3;
      lastButtonState4 = reading4;
      lastButtonState5 = reading5;
    
    }
    

    Arduino + Ethernet W5500 program:

    /*|----------------------------------------------------------|*/
    /*|SKETCH FOR ESCAPE ROOM - Arduino + Ethernet W5500         |*/
    /*|Author: Bc. MARTIN CHLEBOVEC                              |*/
    /*|FB: https://www.facebook.com/martin.s.chlebovec           |*/
    /*|EMAIL: martinius96@gmail.com                              |*/
    /*|WEB: https://arduino.php5.sk?lang=en                      |*/
    /*|----------------------------------------------------------|*/
    #include <SPI.h>
    #include <Ethernet2.h>
    
    byte mac[] = { 0xAA, 0xBB, 0xCC, 0xAA, 0xBB, 0xCC };
    IPAddress ip(192, 168, 1, 101);
    IPAddress gateway(192, 168, 1, 1);
    IPAddress subnet(255, 255, 255, 0);
    char serverName[] = "192.168.1.254"; //or hostname
    int serverPort = 80;
    
    const int buttonPin1 = 9;
    const int buttonPin2 = 8;
    const int buttonPin3 = 7;
    const int buttonPin4 = 6;
    const int buttonPin5 = 5;
    const int relay = 3;
    
    int buttonState1 = HIGH;
    int buttonState2 = HIGH;
    int buttonState3 = HIGH;
    int buttonState4 = HIGH;
    int buttonState5 = HIGH;
    
    int lastButtonState1 = HIGH;
    int lastButtonState2 = HIGH;
    int lastButtonState3 = HIGH;
    int lastButtonState4 = HIGH;
    int lastButtonState5 = HIGH;
    
    unsigned long lastDebounceTime1 = 0;
    unsigned long lastDebounceTime2 = 0;
    unsigned long lastDebounceTime3 = 0;
    unsigned long lastDebounceTime4 = 0;
    unsigned long lastDebounceTime5 = 0;
    unsigned long debounceInterval = 50;
    
    
    
    EthernetClient client;
    int number = 0;
    String url = "/sendnumber.php";
    void setup() {
      Serial.begin(115200);
      pinMode(relay, OUTPUT);
      digitalWrite(relay, HIGH); //lock door
      pinMode(buttonPin1, INPUT_PULLUP);
      pinMode(buttonPin2, INPUT_PULLUP);
      pinMode(buttonPin3, INPUT_PULLUP);
      pinMode(buttonPin4, INPUT_PULLUP);
      pinMode(buttonPin5, INPUT_PULLUP);
      Ethernet.begin(mac, ip, gateway, gateway, subnet);
      Serial.println("IP address set to Ethernet shield:");
      Serial.println(Ethernet.localIP());
      delay(2000);
      Serial.println("Ready");
    }
    
    void send_datas() {
      String my_datas = String(number);
      String data = "number=" + my_datas;
      if (client.connect(serverName, serverPort)) {
        client.println("POST " + url + " HTTP/1.0");
        client.println("Host: " + (String)serverName);
        client.println("User-Agent: ArduinoEthernetWiznet");
        client.println("Connection: close");
        client.println("Content-Type: application/x-www-form-urlencoded;");
        client.print("Content-Length: ");
        client.println(data.length());
        client.println();
        client.println(data);
        while (client.connected()) {
          //HTTP HEADER
          String line = client.readStringUntil('\n'); //HTTP header
          if (line == "\r") {
            break; //blank line between HTTP header and payload
          }
        }
        String line = client.readStringUntil('\n'); //Payload first line
        if (line == "Access") {
          Serial.println("Access gained!");
          digitalWrite(relay, LOW); //unlock door to next level
        } else {
          Serial.println("Access denied!");
          digitalWrite(relay, HIGH); //lock door
        }
      } else {
        Serial.println("Error connecting to webserver!");
      }
      client.stop();
    }
    
    void loop() {
      int reading1 = digitalRead(buttonPin1);
      int reading2 = digitalRead(buttonPin2);
      int reading3 = digitalRead(buttonPin3);
      int reading4 = digitalRead(buttonPin4);
      int reading5 = digitalRead(buttonPin5);
    
      if (reading1 != lastButtonState1) {
        lastDebounceTime1 = millis();
      }
    
      if (reading2 != lastButtonState2) {
        lastDebounceTime2 = millis();
      }
    
      if (reading3 != lastButtonState3) {
        lastDebounceTime3 = millis();
      }
    
      if (reading4 != lastButtonState4) {
        lastDebounceTime4 = millis();
      }
    
      if (reading5 != lastButtonState5) {
        lastDebounceTime5 = millis();
      }
      if ((millis() - lastDebounceTime1) > debounceInterval) {
        if (reading1 != buttonState1) {
          buttonState1 = reading1;
          if (buttonState1 == HIGH) {
            number = 1;
            send_datas();
          }
        }
      }
    
      if ((millis() - lastDebounceTime2) > debounceInterval) {
        if (reading2 != buttonState2) {
          buttonState2 = reading2;
          if (buttonState2 == HIGH) {
            number = 2;
            send_datas();
          }
        }
      }
    
      if ((millis() - lastDebounceTime3) > debounceInterval) {
        if (reading3 != buttonState3) {
          buttonState3 = reading3;
          if (buttonState3 == HIGH) {
            number = 3;
            send_datas();
          }
        }
      }
    
      if ((millis() - lastDebounceTime4) > debounceInterval) {
        if (reading4 != buttonState4) {
          buttonState4 = reading4;
          if (buttonState4 == HIGH) {
            number = 4;
            send_datas();
          }
        }
      }
      if ((millis() - lastDebounceTime5) > debounceInterval) {
        if (reading5 != buttonState5) {
          buttonState5 = reading5;
          if (buttonState5 == HIGH) {
            number = 5;
            send_datas();
          }
        }
      }
      lastButtonState1 = reading1;
      lastButtonState2 = reading2;
      lastButtonState3 = reading3;
      lastButtonState4 = reading4;
      lastButtonState5 = reading5;
    
    }
    

    Webserver .php program (sendnumber.php):

    <?php 
    $secret_number = 3;
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      $number = $_POST['number'];
      $number = trim( $number );
      if (is_numeric($number)){
        if($number==$secret_number){
          echo "Access";
        }else{
          echo "Problem";
        }    
      }
    }else{
      echo "Unsupported METHOD! Use POST instead of GET/PUT/PATCH!";
    }   
    ?>