#include <WiFi.h>
#include <HTTPClient.h>
#include <Keypad.h>
#include <Servo.h>
const uint8_t LEDS = 12;
#include <LiquidCrystal.h>
#define codeLength 5
Servo myservo;
char Code[codeLength];
char PassW[codeLength] = "1234";
byte keycount = 0;
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
LiquidCrystal lcd(13,11,9,8,7,6);
uint8_t rowPins[ROWS] = { 26, 22, 21, 20 };
uint8_t colPins[COLS] = { 19, 18, 17, 16 };
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// WiFi and ThinkSpeak setup
const char* ssid = "Aryan's iPhone";
const char* password = "Hn@123459";
String apiKey = "PZ8I3H2QPJ8OW1SX"; // Get this from ThinkSpeak
void setup()
{
lcd.begin(16,2);
lcd.print("Enter a Key:-");
myservo.attach(28);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop()
{
int pos;
char customKey = customKeypad.getKey();
if(customKey)
{
Code[keycount] = customKey;
Serial.print(Code[keycount]);
lcd.setCursor(5,1);
lcd.print(Code[keycount]);
keycount++;
}
if(keycount == codeLength - 1)
{
Serial.println("");
if(!strcmp(Code, PassW))
{
Serial.println("Correct Password");
lcd.clear();
lcd.setCursor(0,1);
lcd.print("correct Password");
// Unlock door with servo
for(pos = 90; pos <= 180; pos++) {
myservo.write(pos);
delay(40);
}
delay(1000);
for(pos = 180; pos >= 90; pos--) {
myservo.write(pos);
delay(40);
}
// Send correct password event to ThinkSpeak
sendToThinkSpeak("Correct");
}
else
{
Serial.println("Incorrect Password");
delay(1000);
// Send incorrect password event to ThinkSpeak
sendToThinkSpeak("Incorrect");
}
deletecount();
}
}
void deletecount()
{
while(keycount != 0)
{
Code[keycount--] = 0;
}
return;
}
// Function to send data to ThinkSpeak
void sendToThinkSpeak(String status)
{
if(WiFi.status() == WL_CONNECTED)
{
HTTPClient http;
String serverPath = "http://api.thingspeak.com/update?api_key=" + apiKey + "&field1=" + status;
http.begin(serverPath.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThinkSpeak successfully.");
} else {
Serial.println("Error sending data to ThinkSpeak.");
}
http.end();
}
}