Making your home learn

Once this constitution is done, go ahead and wire it up, as shown here:

Once that is set, it is time for us to write the following code on to our Raspberry Pi:

import Adafruit_DHT
import datetime
import RPi.GPIO as GPIO
import time
import numpy as np
import pandas as pd
import Adafruit_DHT
from sklearn.neighbors import KNeighborsClassifier

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

fan = 22
light = 23
sw1 = 13
sw2 = 14

GPIO.setup(led1,GPIO.OUT)
GPIO.setup(led2,GPIO.OUT)
GPIO.setup(sw1,GPIO.IN)
GPIO.setup(sw2,GPIO.IN)

sensor = 11
pin = 2


f = open("dataset.csv","a+")
count = 0
while count < 50:

data = ""

H = datetime.datetime.now().strftime('%H')
M = datetime.datetime.now().strftime('%M')

data = str(H)+"."+str(M)
humidity,temperature = Adafruit_DHT.read_retry(sensor,pin)
data = data + "," + str(temperature)

prev_state = state

if (GPIO.input(sw1) == 0) and (GPIO.input(sw2) == 0):
state = 0
GPIO.output(light,GPIO.LOW)
GPIO.output(fan,GPIO.LOW)

elif (GPIO.input(sw1) == 0) and (GPIO.input(sw2) == 1):
state = 1
GPIO.output(light,GPIO.HIGH)
GPIO.output(fan,GPIO.LOW)

elif (GPIO.input(sw1) == 1) and (GPIO.input(sw2) == 0):
state = 2
GPIO.output(light,GPIO.LOW)
GPIO.output(fan,GPIO.HIGH)

elif (GPIO.input(sw1) == 1) and (GPIO.input(sw2) == 1):
state = 3
GPIO.output(light,GPIO.HIGH)
GPIO.output(fan,GPIO.HIGH)

data = ","+str(state)

if prev_state =! state:

f.write(data)
count = count+1


f.close()

Now, let's see what we have done here:

f = open("dataset.csv","a+")

In this line of the code, we have assigned the value open("dataset.csv", "a+") to the variable f. Thereafter, the open() function will open the file that is passed on to its argument, which in our case is dataset.csv; the argument a+ stands for appending the value at the end of the CSV file. Hence, what this line will do is to open the file dataset.csv and add a value that we will pass later on:

 data = ""

We are declaring an empty string by the name of data:

 data = str(H)+"."+str(M)

We are adding values of hours and minutes to the string, separated by a dot in between for differentiation. Hence, the data will look like HH.MM:

 humidity,temperature = Adafruit_DHT.read_retry(sensor,pin)

We are using this line to read the humidity and temperature reading from the DHT 11 sensor and the values that would be passed on to the variables humidity and temperature:

data = data + "," + str(temperature)

Once the data is read, we are adding temperature to the variable data as well. Hence, now the data would look like this HH.MM and TT.TT:

 if (GPIO.input(sw1) == 0) and (GPIO.input(sw2) == 0):
state = 0
elif (GPIO.input(sw1) == 0) and (GPIO.input(sw2) == 1):
state = 1
elif (GPIO.input(sw1) == 1) and (GPIO.input(sw2) == 0):
state = 2
elif (GPIO.input(sw1) == 1) and (GPIO.input(sw2) == 1):
state = 3

Here, we have defined different types of states which are corresponding to the switch combinations. The table for it is as follows:

Switch 1 Switch 2 State
0 0 0
0 1 1
1 0 2
1 1 3

 

Hence, by the value of state, we can understand which switch would be turned on and which would be turned off:

 data = ","+str(state)

Finally, the value of state is also added to the variable named data. Now, finally, the data would look like HH.MM, TT.TT, and S:

f.write(data)

Now, using the write() function, we are writing the value of data to the file that we have already defined by the value f earlier. 

Hence, with every single switch on or off, the data would be collected, and the value would be recorded with the time stamp in that file. This data can then be used to predict the state of the home at any given time without any intervention:

if prev_state =! state:

f.write(data)
count = count+1

Here, we are comparing the state with the prev_state as you can see in our program. The previous state is calculated at the start of our program. So, if there is any change in the state of the system, then the value of prev_state and state would be different. This will lead to the if statement to be true. When that happens, the data would be written onto our file using the write() function. The argument passed is the value that needs to be written. Finally, the value of count is increased by 1.

Once this is left running for a few hours or may be days, then it would collect some really useful data regarding your switching pattern of the lights and fan. Thereafter, this data can be fetched to the previous program wherein it would be able to to take its own decision based on the time and temperature. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.172.146