#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial ESP01(2, 3);
const byte ROWS = 4; // four rows
const byte COLS = 3; // three columns
char keys[ROWS][COLS] = { // keypad labels
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {12, 7, 8, 10}; //connect to keypad pins 12, 7, 8, 10
byte colPins[COLS] = {11, 13, 9}; // connect to keypad pins 11, 13, 9 column pinouts
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define Password_Length 5
#define DEBUG true
#define IP "184.106.153.149"
// define pin numbers
int buzzer = 5; // buzzer is connected to pin 5
int PIR = 6; // PIR is connected to pin 6
// intruder alarm password
char Data[Password_Length];
char Master[Password_Length] = "1234";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
int val = 0;
// thingspeak & WiFi
String apiKey = "xxxxxxxxxxxxxxxx"; // thingspeak API key
void setup() {
lcd.init();
lcd.backlight();
pinMode(buzzer, OUTPUT);
pinMode(PIR, INPUT);
Serial.begin(9600);
while (!Serial){
}
Serial.println("Starting...");
ESP01.begin(9600);
// Reset ESP8266, put it into mode 1 i.e. STA only, make it join hotspot / AP,
// establish single connection
ESP01.println();
sendData("AT+RST\r\n",2000,DEBUG);
sendData("AT+CWMODE=1\r\n",2000,DEBUG);
sendData("AT+CWJAP=\"AiPhone\",\"123889\"\r\n",20000,DEBUG);
sendData("AT+CIPMUX=0\r\n",4000,DEBUG);
// Make TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // Thingspeak.com's IP address
cmd += "\",80\r\n";
sendData(cmd,4000,DEBUG);
// Read PIR sensor value
val = digitalRead(PIR);
// check if there was a state transition
String val1 = String(val);
if (val == HIGH)
{
Serial.println("Motion detected!");
delay(10);
Serial.println(val1);
}
else
{
Serial.println("Asleep");
delay(10);
//String val1 = String(val);
Serial.println(val1);
}
//String val1 = String(val);
//Serial.println(val1);
//Prepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr += "&field1=";
getStr += val1;
getStr +="\r\n";
// Send data length & GET string
ESP01.print("AT+CIPSEND=");
ESP01.println (getStr.length());
Serial.print("AT+CIPSEND=");
Serial.println(getStr.length());
delay(500);
if(ESP01.find (">"))
{
Serial.print(">");
sendData(getStr, 2000, DEBUG);
}
//Close connection, wait a while before repeating
sendData("AT+CIPCLOSE", 16000, DEBUG); // 15 seconds delay between updates
}
if true
% code
endvoid loop() {
if (digitalRead(PIR) == HIGH)
{
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Password: ");
customKey = customKeypad.getKey();
if (customKey)
{
Data[data_count] = customKey;
lcd.setCursor(data_count, 1);
lcd.print(Data[data_count]);
data_count++;
}
if (data_count == Password_Length - 1)
{
lcd.clear();
if (!strcmp(Data, Master))
{
lcd.print("OK");
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
}
else
{
lcd.print("Wrong Password");
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
}
lcd.clear();
clearData();
}
}
else
{
lcd.noBacklight(); // turn backlight off
lcd.clear(); // clear display
delay(250);
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
ESP01.print(command);
long int time = millis();
while( (time+timeout) > millis())
{
while(ESP01.available())
{
// "Construct" response from ESP01 as follows
// - this is to be displayed on Serial Monitor.
char c = ESP01.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return (response);
}
void clearData() {
while (data_count != 0)
{
Data[data_count--] = 0;
}
return;
}