#include
#include
#include
#include
int readval;
Servo myservo;
int resultvalue;
int checktime=0;
String readpassword;
String Password;
String msg;
const int buttonPin = 12;
const int ledPin = 13;
boolean firstconn;
boolean changepass;
int Doorstatus;
int Dooraddress=100;
void setup() {
if(!LBTServer.begin((uint8_t*)"Sivam_LIO"))
{
return;
}
// writepass("123456");
Doorstatus=EEPROM.read(Dooraddress);
// Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
firstconn=false;
changepass=false;
readpassword="";
Password=readpass();
myservo.attach(9);
delay(100);
}
void servocommand()
{
if (Doorstatus==0)
{
myservo.write(0);
delay(100);
}
else
{
myservo.write(180);
delay(100);
}
}
void loop() {
uint8_t buf[1];
int bytesRead;
if(LBTServer.connected())
{
if (firstconn==false)
{
firstconn=true;
delay(200);
LBTServer.write("Hello Welcome");
delay(100);
if (Doorstatus==0)
{
LBTServer.write("Door is close");
delay(100);
LBTServer.write("Type Password to open");
delay(100);
}
else
{
LBTServer.write("Door is open");
delay(100);
LBTServer.write("Type Password to close");
delay(100);
}
}
if (digitalRead(buttonPin) == HIGH && Doorstatus==0 && changepass==false)
{
changepass=true;
LBTServer.write("Type new password");
readpassword="";
delay(100);
}
else if (digitalRead(buttonPin) == LOW && changepass==true)
{
changepass=false;
LBTServer.write("Exit from password change");
readpassword="";
delay(100);
}
while(true)
{
bytesRead = LBTServer.readBytes(buf, 1);
if(!bytesRead)
break;
readval = buf[0];
readpassword = readpassword + char(readval);
checktime=0;
// msg = "U type -" + readpassword;
// LBTServer.write(msg);
// Serial.println(readpassword);
if (readpassword.length()==6)
{
if (changepass==false)
{
if (readpassword.equals(Password))
{
if (Doorstatus==0)
{
Doorstatus=1;
servocommand();
EEPROM.write(Dooraddress,1);
LBTServer.write("Door Open");
}
else
{
Doorstatus=0;
servocommand();
EEPROM.write(Dooraddress,0);
LBTServer.write("Door Close");
}
}
else
{
LBTServer.write("Invalid Password");
}
}
else
{
writepass(readpassword);
Password=readpassword;
LBTServer.write("Password Changed");
delay(100);
LBTServer.write("Remove Your hand");
delay(100);
}
readpassword="";
}
}
delay(100);
if (readpassword!="")
{
checktime=checktime+1;
if (checktime>5)
{
readpassword="";
changepass=false;
checktime=0;
LBTServer.write("Time out. Try again");
delay(100);
}
}
}
else
{
readpassword="";
firstconn=false;
changepass=false;
LBTServer.accept(5);
}
// if (digitalRead(buttonPin) == HIGH) {
// Serial.println(2);
// digitalWrite(ledPin, HIGH);
// }
// else {
// Serial.println(3);
// digitalWrite(ledPin, LOW);
// }
delay(100);
}
String readpass()
{
String mempass;
int memval;
int memadd;
mempass="";
for(int i=0;i<6;i++)
{
memadd=200+i;
memval=EEPROM.read(memadd);
if (memval>9)
{
memval=0;
}
mempass=mempass + memval;
}
return mempass;
}
void writepass(String passw)
{
String mempass;
int memval;
int memadd;
for(int i=0;i<6;i++)
{
mempass=passw.substring(i,i+1);
memval=mempass.toInt();
memadd=200+i;
EEPROM.write(memadd,memval);
}
}