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

Escape game - Webclient - Arduino


Hardware description:

  • Arduino Nano / Uno / Arduino Mega 2560
  • Ethernet shield Wiznet W5100
  • 5x switch button
  • LCD display 16x2 / 20x4
  • I2C converter for LCD display
  • Electromagnetic Relay SRD-05VDC-SL-C / SSR Relay OMRON G3MB-202P
  • Solenoid door lock 12 / 24V
  • Functionality description:

  • The player has 5 numeric input buttons.
  • With four buttons the player increments the number in each of the four positions.
  • The fifth button is data sent to the webserver, which returns payload Access / Problem.
  • In the case of an "Access" payload, the relay is activated and the solenoid door lock opens - allowing players to move further in the game.
  • In case of payload "Problem" the relay remains open, the lock does not open. The door cannot be opened.
  • The player can also be informed of the failure by a buzzer or LED with indication.
  • Implementations:

  • One Number: - Use the 1-5 buttons to send a number (1,2,3,4,5), one of which is correct. Max. 5 combination does not include the enter button.
  • Four Repeating Numbers: - Use the 1-4 buttons to increment a number between 0-9 for each of the four repeating positions. Max. 10,000 combinations.
  • Four Numbers Without Repetition: - Implement without Numbers Repeat (functionality as with Four Numbers Repeat), max 5040 combination.
  • Web interface:

  • The web interface offers the ability to modify the search number in each of the three implementations (can be used simultaneously).
  • It takes into account the limitations of all implementations (repetition, numeric code length).
  • Login data can be changed. It is possible to enable logging and statistics of received data from the Arduino microcontroller. Statistics of players success with record of received numeric combinations, times of acceptance.
  • Wiring diagram:

    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!";
    }   
    ?>