Skip to content

Instantly share code, notes, and snippets.

@beyazitkolemen
Created November 27, 2020 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beyazitkolemen/928bb435da28acf59ff0f7cebd65980e to your computer and use it in GitHub Desktop.
Save beyazitkolemen/928bb435da28acf59ff0f7cebd65980e to your computer and use it in GitHub Desktop.
HZ-1050-KEYPAD-MQTT-ESP32-SMARTDOOR
// by Joël Gähwiler
// https://github.com/256dpi/arduino-mqtt
#include <WiFi.h>
#include <MQTT.h>
#include <Wiegand.h>
#include <Keypad.h>
#include <esp_int_wdt.h>
#include <esp_task_wdt.h>
// use for no hardreset at the fist loop
bool newstart = 0;
int i, j;
int wifidelay = 0;
long value;
WIEGAND wg;
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
const String passwordnumpad = "121234"; // change your password here
String input_password;
byte rowPins[ROWS] = {26, 25, 33, 32}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {13, 12, 14, 27}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const char ssid[] = "ArtF4";
const char pass[] = "***";
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
void WIFI_Connect()
{
for (int k = 0; k < 25; k++)
{
if ( WiFi.status() != WL_CONNECTED )
{
wifidelay = wifidelay + 250;
WiFi.disconnect();
delay(wifidelay);
Serial.println("XXXXXXXXXXXXXXXX---------Connecting to WiFi...-------------XXXXXXXXXXXXX");
WiFi.mode(WIFI_STA);
delay(wifidelay);
WiFi.begin(ssid, pass);
delay(wifidelay);
delay(wifidelay);
}
}
if ( WiFi.status() != WL_CONNECTED )
{
Serial.println("Connecting to WiFi fail.");
delay (10000);
hard_restart();
// I think that is no longer necessary, but it does not hurt either.
delay (2000);
}
if ( WiFi.status() == WL_CONNECTED )
{
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
}
void reconnect() {
Serial.println("mqtt reconnect start");
// test is mqtt connected
if (!client.connected()) {
// test is wifi connected
if ( WiFi.status() != WL_CONNECTED )
{
WIFI_Connect();
}
if ( newstart == 0 )
{
hard_restart();
}
Serial.println("Attempting MQTT connection...");
// insert unique id
if (client.connect("kapi", "try", "try")) {
Serial.println("connected");
client.subscribe("/kapi");
} else {
Serial.print("failed, rc=");
Serial.println(" wait 0.5 seconds");
delay(500);
}
}
Serial.println("mqtt reconnect ende");
}
void hard_restart() {
esp_task_wdt_init(1, true);
esp_task_wdt_add(NULL);
while (true);
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
if (payload == "'kapiac'")
{
digitalWrite(5, LOW);
Serial.println("ozel kod geldi ");
delay(500);
digitalWrite(5, HIGH);
}
if (payload == "'tabelaac'")
{
digitalWrite(18, LOW);
Serial.println("tabela acilcak ");
}
if (payload == "'tabelakapat'")
{
digitalWrite(18, HIGH);
Serial.println("tabela kapatilacak");
}
}
void setup() {
Serial.begin(115200);
pinMode(5, OUTPUT); // sets the digital pin 13 as output
pinMode(18, OUTPUT); // sets the digital pin 13 as output
digitalWrite(5, HIGH);
digitalWrite(18, HIGH);
WiFi.begin(ssid, pass);
wg.begin(22, 23);
newstart = 1;
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
// You need to set the IP address directly.
client.begin("192.168.0.39", 1883, net);
client.onMessage(messageReceived);
reconnect();
}
void loop() {
newstart = 0;
client.loop();
value = NULL;
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '*') {
input_password = ""; // clear input password
} else if (key == 'A') {
if (passwordnumpad == input_password) {
Serial.println("password is correct");
// DO YOUR WORK HERE
digitalWrite(5, HIGH);
delay(200);
digitalWrite(5, LOW);
} else {
Serial.println("password is incorrect, try again");
}
input_password = ""; // clear input password
} else {
input_password += key; // append new character to input password string
}
}
if (wg.available())
{
if (wg.getCode() == 4537453 or wg.getCode() == 4522429 or wg.getCode() == 1053226 or wg.getCode() == 1238786 or wg.getCode() == 1003423 or wg.getCode() == 1384779 or wg.getCode() == 1476491 or wg.getCode() == 3589326 or wg.getCode() == 1389113 or wg.getCode() == 1365324)
{
digitalWrite(5, HIGH);
delay(200);
digitalWrite(5, LOW);
delay(2000);
Serial.println("Başarılı.");
}
else
{
Serial.println("Geçersiz kart okutuldu");
Serial.println(wg.getCode());
}
}
if (!client.connected()) {
reconnect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment