One of the basic functionalities of a smart home is to turn on the lights for you whenever you are around. It is one of the most basic things that any system can do for you. We will start off by turning on the light as soon as you come inside the room, thereafter, we will make the system more and more intelligent.
So, the first thing we need to do is recognize whether you are in a room or not. There are multiple ways to do that. One important characteristic of life is the presence of movement. You may say plants don't move, well they do; they grow, don't they? So detecting movement can be a key step in detecting whether someone is there or not!
This step will not be so difficult for you, as we have already interfaced this sensor previously. We are talking about the good old PIR sensor. So the sensor will sense any movement in the area. If there is any movement, then Jarvis will switch on the lights. I am sure this is something you can do by yourself by now. You can still refer to the code and the circuit diagram here:
Now upload the following code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
PIR = 24
LIGHT = 23
GPIO.setup(DOPPLER,GPIO.IN)
GPIO.setup(BUZZER,GPIO.OUT)
While True:
if GPIO.input(PIR) == 1:
GPIO.output(LIGHT,GPIO.HIGH)
if GPIO.input(PIR) == 0:
GPIO.output(LIGHT,GPIO.LOW)
In the preceding code, we are simply turning on the light as soon as the motion is detected, but the problem is that it will only switch on the light for the time the motion is there. What does that mean? Simple, while there is some movement, will keep the lights on and as soon as the movement stops, it will switch off the light.
This can be a very good code for a person who wants to lose weight, but for most of us, it will be annoying. So, let's include a small loop, which we have used in the previous chapter and make this a little better:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
PIR = 24
LIGHT = 23
TIME = 5
GPIO.setup(PIR,GPIO.IN)
GPIO.setup(BUZZER,GPIO.OUT)
While True:
If GPIO.input(PIR) == 1:
M = datetime.datetime.now().strftime('%M')
M_final= M + TIME
for M < M_final:
GPIO.output(LIGHT,GPIO.HIGH)
M = datetime.datetime.now().strftime('%M')
if GPIO.input(PIR) == 1:
M_final = M_final + 1
if GPIO.input(PIR) = 0:
GPIO.output(LIGHT, GPIO.LOW)}
So, in this program, all we have done is we have added a for loop, which switches on the light for a set amount of time. How long that time will be can be toggled by changing the value of the variable TIME.
There is one more interesting part in that loop which is as follows:
if GPIO.input(PIR) == 1
M_final = M_final + 1
Why did we do this you might wonder? Whenever the light will be switched on, it will remain on for 5 minutes. Then, it will switch off and wait for movement to occur. So, essentially, the problem with this code will be that if you are in the room and the light switches on, then for 5 minutes it will see if there is any motion detected or not. There is a chance that you will be in motion when it searches for the motion after 5 minutes. But for most of the time, it won't be the case. So we are detecting the movement using the PIR sensor. Whenever movement is detected, the value of M_final is incremented using the line M_final = M_final + 1, thereby increasing the time until which the light will be switched on.