Hi all,
I am attempting to create a smart lock that can be unlocked through the android app.
I am using a Pi as the main system controller. I also want the system to notify a person via email when the attached PIR sensor is tripped, which then allows them to check the app and either leave the door locked or unlock it.
I am just not quite sure how to setup the code for such
Search this site and the wider internet for something like “Blynk nodejs”.
Okay. I’m understanding how the example email code is working I just am not sure how to adjust it from a button to a PIR sensor. Also the Lock should be simple as it is only open or closed so I would treat it just like a button correct?
Here is my code so far
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT stdout
#ifdef RASPBERRY
#include <BlynkApiWiringPi.h>
#else
#include <BlynkApiLinux.h>
#endif
#include <BlynkSocket.h>
#include <BlynkOptionsParser.h>
static BlynkTransportSocket _blynkTransport;
BlynkSocket Blynk(_blynkTransport);
#include <BlynkWidgets.h>
BLYNK_WRITE(V0)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// process received value
}
void emailOnMotion()
{
// *** WARNING: You are limited to send ONLY ONE E-MAIL PER 15 SECONDS! ***
// Let's send an e-mail when you press the button
// connected to digital pin 2 on your Arduino
int motion = !digitalRead(2); // Invert state, since button is "Active LOW"
if (motion) // You can write any condition to trigger e-mail sending
{
stdout.println("Motion Detected"); // This can be seen in the Serial Monitor
Blynk.email("your_email@mail.com", "Motion Detected", "Motion has been detected");
}
void setup()
{
// Send e-mail when your hardware gets connected to Blynk Server
// Just put the recepient's "e-mail address", "Subject" and the "message body"
Blynk.email("Pittss@chc.edu", "Intruder", "Someone is trying to enter your home");
// Setting the button
pinMode(2, INPUT_PULLUP);
// Attach pin 2 interrupt to our handler
attachInterrupt(digitalPinToInterrupt(2), emailOnButtonPress, CHANGE);
}
void loop()
{
Blynk.run();
}
int main(int argc, char* argv[])
{
const char *auth, *serv;
uint16_t port;
parse_options(argc, argv, auth, serv, port);
Blynk.begin(auth, serv, port);
setup();
while(true) {
loop();
}
return 0;
}
Yes.
Google “Arduino PIR” for a small piece of code.
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT stdout
#ifdef RASPBERRY
#include <BlynkApiWiringPi.h>
#else
#include <BlynkApiLinux.h>
#endif
#include <BlynkSocket.h>
#include <BlynkOptionsParser.h>
static BlynkTransportSocket _blynkTransport;
BlynkSocket Blynk(_blynkTransport);
#include <BlynkWidgets.h>
int pirState = Low
boolean motion = false
BLYNK_WRITE(V0)
{
int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable
}
void emailOnMotion()
{
// *** WARNING: You are limited to send ONLY ONE E-MAIL PER 15 SECONDS! ***
if (motion == true) // You can write any condition to trigger e-mail sending
{
stdout.println("Motion Detected"); // This can be seen in the Serial Monitor
Blynk.email("your_email@mail.com", "Motion Detected", "Motion has been detected");
}
void setup()
{
// Send e-mail when your hardware gets connected to Blynk Server
// Just put the recepient's "e-mail address", "Subject" and the "message body"
Blynk.email("Pittss@chc.edu", "Intruder", "Someone is trying to enter your home");
// Setting the button
pinMode(2, INPUT_PULLUP);
// Attach pin 2 interrupt to our handler
attachInterrupt(digitalPinToInterrupt(2), emailOnMotion, CHANGE);
}
void loop()
{
Blynk.run();
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
motion = true
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
int main(int argc, char* argv[])
{
const char *auth, *serv;
uint16_t port;
parse_options(argc, argv, auth, serv, port);
Blynk.begin(auth, serv, port);
setup();
while(true) {
loop();
}
return 0;
}
Would I be correct in thinking this should work?
I’m sorry I cannot test at the moment waiting on my PIR in the mail but wanna get the code setup
@Pittsie the code looks reasonable but you might want to rethink the subject and body of the Email message in setup() as it’s out of context. Something like subject “Alarm system” and body “Device rebooted”.
Will do. However I am receiving an error on this line any suggestions