Now that in the previous section we have understood how the learning works, it's time to use this concept to make a robot that will automatically understand how we function and make decisions. Based on our decisions, the system will judge what should be done. But this time, rather than giving a set of data by the user, let's make this program create the data for itself. Once the data seems sufficient for itself to function. So, without much explanation, let's get right into it:
import Adafruit_DHT
import datetime
import RPi.GPIO as GPIO
import time
import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
light = 22
fan = 23
sw1 = 13
sw2 = 14
GPIO.setup(light,GPIO.OUT)
GPIO.setup(fan,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 < 200:
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
Test_set = []
knn = KNeighborsClassifier(n_neighbors=5)
data = pd.read_csv('dataset.csv')
X = np.array(data[['Time', 'Temp']])
y = np.array(data[['State']]).ravel()
knn.fit(X,y)
While Count > 200:
time = ""
H = datetime.datetime.now().strftime('%H')
M = datetime.datetime.now().strftime('%M')
time = float(str(H)+"."+str(M))
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
temp = int(temperature)
test_set.append(time)
test_set.append(temp)
a = knn.predict([test_set]])
Out = a[0]
If out == 0:
GPIO.output(light,GPIO.LOW)
GPIO.output(fan,GPIO.LOW)
If out == 1:
GPIO.output(light,GPIO.LOW)
GPIO.output(fan,GPIO.HIGH)
If out == 2:
GPIO.output(light,GPIO.HIGH)
GPIO.output(fan,GPIO.LOW)
If out == 3:
GPIO.output(light,GPIO.HIGH)
GPIO.output(fan,GPIO.HIGH)
Now let's see what we have done here. In this program, the first part of the program inside the condition while count < 200: is exactly the same as what we have done in the last code. So, it is just doing the things according to the user, and at the same time, it's taking in the values from the users to understand their working behavior:
while count > 200:
Thereafter, we have the second part of the code that will start to execute when the count is beyond 200 that is inside the preceding loop:
time = ""
In this line, we are forming an empty string by the name of time where we would be storing the value of time:
H = datetime.datetime.now().strftime('%H')
M = datetime.datetime.now().strftime('%M')
We are storing the values of time into the variable named H and M:
time = float(str(H)+"."+str(M))
We are now storing the value of time in the string time. This would include both hours and minutes:
temp = int(temperature)
For the sake of ease of calculations and reducing the computing load on the system, we are reducing the size of the temperature variable . We are doing it by removing the decimal places. To do that TT.TT; we are simply eliminating the decimal point and converting it into integer. This is done by the function named int(). The value of temperature in int will be stored in the variable named temp:
test_set.append(time)
test_set.append(temp)
Here, we are adding the value of the time and the temperature to a list named test_set if you look in the program, then you will see the declaration of an empty set in the mid of the program. So, now this test_set has the value of time and temp, which can be further used by the prediction algorithm to predict the state:
a = knn.predict([test_set]])
Using the simple function named predict() from the knn function, we can predict the value of the state. All we need to do is to pass on the data or test_set list over to the predict function. The output of this function will be a list that will be stored in a variable named a:
Out = a[0]
The value of Out will be set to the first element of the list a:
If out == 0:
GPIO.output(light,GPIO.LOW)
GPIO.output(fan,GPIO.LOW)
If out == 1:
GPIO.output(light,GPIO.LOW)
GPIO.output(fan,GPIO.HIGH)
If out == 2:
GPIO.output(light,GPIO.HIGH)
GPIO.output(fan,GPIO.LOW)
If out == 3:
GPIO.output(light,GPIO.HIGH)
GPIO.output(fan,GPIO.HIGH)
Using the preceding code block, we are able to switch on the light and fans selectively based on the state predicted by the algorithm. Hence, using this, the program would be able to automatically predict and switch on or off the light and the fans without your intervention.