IOT Tresor mit Keypad oder RFID Verriegelung

In dit artikel laten wij zien hoe u een IOT-kluis met toetsenbord- of RFID-vergrendeling kunt bouwen. De volgende video is de instructie. We geven aanvullende informatie via de blogpost.


 Hier vindt u de gebruikte producten:

 Bedrading van het toetsenbord:


Code Toetsenbord:

/*
    ___ _____        ____       ___                      
   /   /__  /       / __ \___  / (_)   _____  _______  __
  / /| | / / ______/ / / / _ \/ / / | / / _ \/ ___/ / / /
 / ___ |/ /_/_____/ /_/ /  __/ / /| |/ /  __/ /  / /_/ / 
/_/  |_/____/    /_____/\___/_/_/ |___/\___/_/   \__, /  
                                                /____/   
 Product, data sheet en pinout onder:
  https://www.az-delivery.de/

 Project: kluis met toetsenbord en binnenverlichting
 Datum: 10/2022

 Toetsenbordbediening uit de voorbeeldschets van Chris--A's bibliotheek:
  https://github.com/Chris--A/Keypad/blob/master/examples/CustomKeypad/CustomKeypad.ino
*/
#include <Arduino.h>
#include <ESP32Servo.h>
#include <SPI.h>
// #include <MFRC522.h>
#include <fastLED.h>
#include <Keypad.h>

// motor
const int servoPin = 2;
Servo myservo;

// leds
const int NUM_LEDS = 12;
const int DATA_PIN = 25;
CRGB leds[NUM_LEDS];
int randomRed = 0;
int randomGreen = 0;
int randomBlue = 0;
const int LEDRed = 4;

bool dooropen = false;

// Keypad
const byte ROWS = 4;    // four rows
const byte COLUMNS = 4; // four columns
// define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLUMNS] = {
    {'1', '2', '3', 'A'},
    {'4', '5', '6', 'B'},
    {'7', '8', '9', 'C'},
    {'*', '0', '#', 'D'}};
byte rowPins[ROWS] = {16, 17, 21, 22};   // connect to the row pinouts of the keypad
byte colPins[COLUMNS] = {26, 18, 19, 23}; // connect to the column pinouts of the keypad


Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLUMNS);

const String password = "1234"; // change your password here
String input_password;

void setup()
{
  Serial.begin(115200);
  pinMode(LEDRed, OUTPUT);
  myservo.attach(servoPin);
  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  input_password.reserve(32); // maximum input characters is 33, change if needed
}

void loop()
{
  char key = keypad.getKey();

  if (key)
  {
    Serial.println(key);

    if (key == '#')
    {
      if (password == input_password)
      {
        Serial.println(" Password is correct");
        if (dooropen)
        {
          myservo.write(90); // move MG996R's shaft to angle 0°
          for (int i = 0; i < NUM_LEDS; i++)
          {
            leds[i].setRGB(0, 0, 0); // leds off
          }
          FastLED.setBrightness(20);
          FastLED.show();
          dooropen = false;
          delay(1000);
        }

        else if (dooropen == false)
        {
          myservo.write(0); // move MG996R's shaft to angle 90°
          randomRed = random(1, 256);
          randomBlue = random(1, 256);
          randomGreen = random(1, 256);

          for (int i = 0; i < NUM_LEDS; i++)
          {
            leds[i].setRGB(randomRed, randomGreen, randomBlue); // leds on with random colours
          }
          FastLED.setBrightness(20);
          FastLED.show();
          dooropen = true;
          delay(1000);
        }
      }
      else
      {
        digitalWrite(LEDRed, HIGH);
        delay(1000);
        digitalWrite(LEDRed, LOW);
      }
      input_password = ""; // clear input password
    }

    else if (key == '*')
    {
      input_password = ""; // clear input password
    }
    else
    {
      input_password += key; // append new character to input password string
    }
  }
}

RFID bedraden:


Code RFID:


/*
    ___ _____        ____       ___                      
   /   /__  /       / __ \___  / (_)   _____  _______  __
  / /| | / / ______/ / / / _ \/ / / | / / _ \/ ___/ / / /
 / ___ |/ /_/_____/ /_/ /  __/ / /| |/ /  __/ /  / /_/ / 
/_/  |_/____/    /_____/\___/_/_/ |___/\___/_/   \__, /  
                                                /____/   
 Product, datasheet en pinout onder:
  https://www.az-delivery.de/

 Project: Veilig met RFID en Node-RED verbinding via MQTT
 Datum: 10/2022

*/
#include <Arduino.h>
#include <ESP32Servo.h>
#include <SPI.h>
#include <MFRC522.h>
#include <fastLED.h>

// for MQTT
#include <PubSubClient.h>
#include <WiFi.h>
//für etwaigen späteren Gebrauch für MQTT etc. 
#include <ArduinoJson.h>
#include <AsyncTCP.h>
#include "credentials.h"

// MQTT
String clientId = "ESP32-";
const char *mqtt_server = "192.168.178.42";
const char *mqtt_user = "azdelivery";
const char *mqtt_password = "myhiddenpassword";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

// RFID
const int SS_PIN = 5;
const int RST_PIN = 27;
MFRC522 rfid(SS_PIN, RST_PIN);

// motor
const int servoPin = 2;
Servo myservo;

// leds
const int NUM_LEDS = 12;
const int DATA_PIN = 25;
CRGB leds[NUM_LEDS];
int randomRed = 0;
int randomGreen = 0;
int randomBlue = 0;
const int LEDRed = 4;

bool dooropen = false;

void reconnect()
{
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    clientId += String(random(0xffff), HEX);
    if (client.connect(clientId.c_str(), mqtt_user, mqtt_password))
    {
      Serial.println("connected");
      // for this example not necessary
      // client.subscribe("topic");
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

// only required if we get informations from mqtt

void callback(char *topic, byte *message, unsigned int length)
{
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;

  for (int i = 0; i < length; i++)
  {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  if (String(topic) == "topic")
  {
    // action
  }
}

void connectAP()
{

  Serial.println("Connecting to WiFi..");
  WiFi.begin(ssid, password);

  int cnt = 0;
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(1000);
    Serial.print(".");
    cnt++;

    if (cnt > 30)
      ESP.restart();
  }
  Serial.println(WiFi.localIP());
}

void setup()
{
  Serial.begin(115200);
  pinMode(LEDRed, OUTPUT);
  SPI.begin();     // start SPI bus
  rfid.PCD_Init(); // start RFID-Modul
  Serial.println("RFID-RC522 ist bereit");

  myservo.attach(servoPin);

  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);

  connectAP();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop()
{

  if (!client.connected())
  {
    reconnect();
  }
  if (!client.loop())
    client.connect("ESP32Tresor");

  if (!rfid.PICC_IsNewCardPresent())
  {
    return;
  }
  if (!rfid.PICC_ReadCardSerial())
  {
    return;
  }

  Serial.println();
  // read the RFID-tags
  Serial.print(" UID tag  :");
  String content = "";
  for (byte i = 0; i < rfid.uid.size; i++)
  {
    Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(rfid.uid.uidByte[i], HEX);
    content.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(rfid.uid.uidByte[i], HEX));
  }
  content.toUpperCase();
  Serial.println();
  Serial.print(" PICC type: ");
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  // the returned hex-value can be used for further actions

  Serial.println(rfid.PICC_GetTypeName(piccType));

  if (content.substring(1) == "AC 32 E6 8C")
  {

    if (dooropen)
    {
      myservo.write(90); // move MG996R's shaft to angle 0°
      for (int i = 0; i < NUM_LEDS; i++)
      {
        leds[i].setRGB(0, 0, 0); // leds off
      }
      FastLED.setBrightness(50);
      FastLED.show();
      dooropen = false;
      delay(1000);
    }

    else if (dooropen == false)
    {
      myservo.write(0); // move MG996R's shaft to angle 90°
      randomRed = random(1, 256);
      randomBlue = random(1, 256);
      randomGreen = random(1, 256);

      for (int i = 0; i < NUM_LEDS; i++)
      {
        leds[i].setRGB(randomRed, randomGreen, randomBlue); // leds on with random colours
      }
      FastLED.setBrightness(50);
      FastLED.show();
      client.publish("safe", "Your safe has been opened"); // E-Mail
      dooropen = true;
      delay(1000);
    }
  }
  else
  {
    digitalWrite(LEDRed, HIGH);
    delay(1000);
    digitalWrite(LEDRed, LOW);
    delay(1000);
  }
}

Geloofsbrieven hiervoor:

const char* ssid = "myhiddenSSID";
const char* password = "mytopsecreitpassword";


3 Kommentare

Tobias

Tobias

@ Paul Moranduzzo: Du brauchst die FastLED Bibliothek von Daniel Garcia.

Paul Moranduzzo

Paul Moranduzzo

Fehlermeldung: fastLED.h No such file or directory
Welche Bibliothek soll ich nehmen?

dd1uzu

dd1uzu

Sieht “interessant” aus. Ein Tresor aus Pappe!!!

1. Frage: Wie soll ich den schweren Metallriegel oder die Sperre eines ECHTEN Tresors mit dem Servo verbinden?
2. Frage: Wie soll das RFIF-Signal durch die dicke Tresorwand aus Metall kommen?

Wenn man statt des Servos ein Relais verwendet, ließe sich ein elektrischer Türöffner damit bauen. Das wäre sicher praxisnäher!
Bei Nutzung der WIFI- oder Bluetooth-Funktion des ESP könnte man alternativ auch eine drahtlose Fernbedienung realisieren.

Einen Kommentar hinterlassen

Alle Kommentare werden vor der Veröffentlichung moderiert