iot ca2 step-by-step tutorial€¦ · st0324 internet of things ca2 step-by-step tutorial school of...
TRANSCRIPT
ST0324 Internet of Things CA2 Step-by-step Tutorial
SCHOOL OF COMPUTING (SOC)
IOT CA2
Step-by-step Tutorial DIPLOMA IN BUSINESS INFORMATION TECHNOLOGY
DIPLOMA IN INFORMATION TECHNOLOGY
DIPLOMA IN INFOCOMM SECURITY MANAGEMENT
ST0324 Internet of Things (IOT)
2017/2018 Semester 1
Date of Submission: 17/2/2019 Prepared for: Ms Dora Chua
Class: DIT/FT/2B02
Submitted by:
Student ID Name 1749209 Tan Chang Yuan Jasper 1749155 Tong Qian En
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 1 of 84
Table of Contents
Section 1 Overview of project ........................................................................................................... 3
A. Where we have uploaded our tutorial ............................................................................ 3
B. What is the application about? ........................................................................................ 3
C. How does the final RPI set-up looks like? ........................................................................ 4
D. How does the web or mobile application look like? ....................................................... 5
Section 2 Hardware requirements .................................................................................................... 8
Hardware checklist..................................................................................................................... 8
Section 3 Setup Hardware on Raspberry Pi ...................................................................................... 9
Fritzing Diagram of the completed setup ........................................................................ 9
Section 4 Sign in to AWS IoT Console .............................................................................................. 10
Sign in to the AWS IoT Console ...................................................................................... 10
Create and register your “Thing” ................................................................................... 11
Create Certificates ......................................................................................................... 12
Create a Security Policy for your RPi.............................................................................. 14
Attach Security Policy and Thing to your Cert ............................................................... 15
Copy REST API endpoint of your “Thing” ....................................................................... 18
Section 5 Whatever your section header is .................................................................................... 19
Install the AWS Python library ....................................................................................... 19
Section 6 Create the DynamoDB tables/Rules ................................................................................ 19
Create DynamoDB tables ............................................................................................... 19
Create rules to store MQTT message to DB .................................................................. 20
Section 7 Python code for main user control .................................................................................. 23
A. Pi2_two.py ..................................................................................................................... 23
Section 8 Python code for motion sensing ..................................................................................... 29
A. Pi2_one.py ..................................................................................................................... 29
Section 9 Python code for Telegram controls/DHT/LDR ................................................................. 33
A. Pi1_two.py ..................................................................................................................... 33
Section 10 Python code for server .................................................................................................. 36
A. Pi1_one.py ..................................................................................................................... 36
Section 11 Supporting python codes .............................................................................................. 43
A. dynamodb.py ................................................................................................................. 43
B. jsonconverter.py ............................................................................................................ 45
C. picam.py ......................................................................................................................... 46
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 2 of 84
D. base_camera.py ............................................................................................................. 47
E. MFRC522.py ................................................................................................................... 50
Section 12 Installing Telegram bot .................................................................................................. 61
A. telegrambot.py .............................................................................................................. 61
Section 13 Python code for webpage ............................................................................................. 62
A. Index.html ............................................................................................................................ 62
B. img.html ............................................................................................................................... 74
Section 14 Move files over to the Raspberry Pi and run them ....................................................... 82
A. Transfer files to RPI ........................................................................................................ 82
B. Run the programs .......................................................................................................... 82
Section 15 View your Smart Home Dashboard ............................................................................... 83
A. View dashboard(index.html) in browser ....................................................................... 83
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 3 of 84
Section 1
Overview of project
A. Where we have uploaded our tutorial Youtube Link : https://youtu.be/AlfqVZ5sbLQ Online tutorial : https://www.hackster.io/jasper/jttqe-smart-home-b6ae2f
B. What is the application about?
This application is a smart home control system which allows the user to monitor their house activities. The user will be able to lock and unlock their door using RFID cards and the system will
recognize the user based on the card that they use to tap in. To begin scanning, the user will press a button and there will be a prompt on the LCD to tell the user to tap their card, after tapping the card the system will check if this user is a valid user if the user is valid a welcome message will
appear on the LCD and the system will know that the user is home, if the card that is tapped is not a valid one the system will then send a message together with a picture using Telegram to the user. When the house gets dark lights will automatically turn on and when the house is bright the light
will turn off. There is also a motion sensor that will send a video taken using the camera to the user's Telegram when ever motion is detected, this function can be turn on and off by the user.
There will also be a web interface that works on the desktop and mobile phone for the user to
monitor and control the status of their house, user will be able to control the lights of the rooms and view past activities of all users, adding of new RFID cards and lights is also done here on the
web page. The user will also be able to see a live stream of their house and view current temperature of the house.
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 4 of 84
C. How does the final RPI set-up looks like?
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 5 of 84
D. How does the web or mobile application look
like? Webpage
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 6 of 84
Telegram Bot
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 7 of 84
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 8 of 84
Section 2
Hardware requirements
Hardware checklist 1. Raspberry Pi x2 2. Raspberry Pi Camera Module x2 3. PIR Motion Sensor 4. MCP3008 Analog-Digital Converter 5. Light-Resistant Diode (LDR) 6. DHT11 Temperature and Humidity Sensor 7. LED x4 8. 10K Ω resistor x2 (For DHT11 & LDR) 9. 330 Ω resistor x4 (For LED) 10. Buzzer 11. LCD 12. RFID Reader (MFRC522) x2 13. RFID Cards 14. Pushbutton switch
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 9 of 84
Section 3
Setup Hardware on Raspberry Pi This section will teach you how to connect the sensors to the Raspberry Pi. Part A shows you how the complete circuit should look like. Start from part B and connect the sensors one by one as shown in the diagrams.
Fritzing Diagram of the completed setup Raspberry Pi 1
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 10 of 84
Raspberry Pi 2
Section 4
Sign in to AWS IoT Console Log into the AWS IoT console.
Sign in to the AWS IoT Console
No Task
a) Turn on your Raspberry Pi and confirm you have an Internet connection.
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 11 of 84
No Task
b) Sign in with your AWS console at https://aws.amazon.com
c) In the AWS dashboard, type “AWS IoT Core” to access the AWS IoT service.
d) On the
Welcome page,
choose Get
started
In this section, you will learn how to create and register your Raspberry Pi as a “Thing” with AWS IoT.
Create and register your “Thing”
No
Task
a) In the left navigation pane, click “Manage” to expand it,
then choose “Things”.
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 12 of 84
No
Task
b) On the page that says “You don't
have any things yet”, choose
“Register a thing”.
If you have created a thing before,
choose Create.
c) A thing represents a device whose status or data is stored in the AWS cloud. The Thing
Shadows is the state of the device, e.g. is it “on” or “off”, is it “red” or “green” etc.
Our “thing” here is our RPi, so let’s type “MyRaspberryPi” for the name.
Click “Create a single thing”
Create Certificates
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 13 of 84
No
Task
a) After you have completed
the previous section, you
might be presented with
this screen.
b) Choose “One-click certificate creation” to generate an X.509 certificate and key pair.
c) After a while, you should
see the following screen,
where there are a total of
four download links.
For the root CA, you can
choose CA 1
d) Create a working directory called deviceSDK and
download all the 4 files above in this deviceSDK
directory, renaming them with friendly names like
what I have done.
e) Next, click the “Activate” button.
Almost immediately, you should see
“Successfully activated certificate” and the
Activate button changes to “Deactivate”
f) Click to the next page, and click “Register
Thing”
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 14 of 84
No
Task
g) You are brought to this page.
Continue with the next section to attach a security
policy to your Thing.
Create a Security Policy for your RPi
Task
a) On the left IOT Core dashboard, select Policies under the
Secure sub-menu
b) On the next page, choose “Create new policy”
c) On the Create a policy page, key in
the following configuration
Field Type this in
Name MyRaspberryPiSecurityPolicy
Action iot:*
Resource ARN *
Allow Checked
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 15 of 84
Task
d) Click “Create”.
You now have a Security Policy that
allows all access to IOT Core
services
Attach Security Policy and Thing to your Cert In this section, you will attach both your security policy and your Thing to your X.509 certificate
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 16 of 84
Task
a) On the left nav bar, click “Security, Certificates”
b) The X.509 certificate you
created earlier is shown.
Click the checkbox beside
it, then click “Actions”
button and choose
“Attach Policy”
c) Check the
“MyRaspberryPiSecurityPolicy”
you created earlier and click
“Attach” button.
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 17 of 84
Task
d) Let’s attach the “Thing” to this
certificate.
Click “Actions” button and choose
“Attach Thing”
e) In the Attach things to
certificate(s) dialog box, select the
check box next to the thing you
created to represent your
Raspberry Pi, and then choose
Attach
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 18 of 84
Copy REST API endpoint of your “Thing”
No Task
a) Click “Manage->Things” and choose
“MyRaspberryPi”.
On the next screen, choose “Interact”
b) Copy and paste
the REST API
endpoint into a
Notepad.
You will need this
value later.
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 19 of 84
Section 5
Whatever your section header is By now, you have completed the first part of the process to connect your device to AWS cloud. In the next few sections, you will begin to code your app. However, before you can start coding an IoT app on the AWS Cloud, you will need to install the AWS Python software libraries first.
Install the AWS Python library
No Task
a) Install the AWS Python library with this command
sudo pip install AWSIoTPythonSDK
Section 6
Create the DynamoDB tables/Rules
Create DynamoDB tables
No Task
a) Open the Amazon DynamoDB console and click “Create Table”
b) Create the table using the attributes as shown below
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 20 of 84
No Task
Table Name Partition Key Sort Key
1. JTTQE-RfidUser deviceid nil
2. JTTQE-RfidUserLog uid nil
3. JTTQE-temp deviceid datetime
4. JTTQE-trip deviceid time
Create rules to store MQTT message to DB In this step, you will create and configure a rule to send the data received from a device to the AWS DynamoDB table you created in Step A of this section
No
Task
a) In the AWS IoT console, in the left navigation pane, choose “Act”,
then “Create a rule”
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 21 of 84
No
Task
b) On the Create a rule page, Create 4
rules.
1. JTTQE_uidLog
2. JTTQE_Trip
3. JTTQE_temp
4. JTTQE_uid
c) Scroll down to Message
source. Choose the latest
version from the Using SQL
version drop-down list. In
the Attribute field, type *.
This specifies that you want
to send the entire MQTT
message that triggered the
rule.
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 22 of 84
No
Task
d) The rules engine uses the
topic filter to determine
which rules to trigger when
an MQTT message is
received. For the 4 rules
1. “sensors/uidLog”
2. “sensors/trip”
3. “sensors/temp”
4. “sensors/uid”
e) In Set one or more actions, choose Add action.
f) On the Select an action page, select the action below. Next, choose Configure action.
g) On the Configure action page,
from the SNS target drop-down
list, choose the DynamoDB table
you created earlier.
1. JTTQE-RfidUserLog
2. JTTQE-trip
3. JTTQE-temp
4. JTTQE-RfidUser
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 23 of 84
No
Task
h) Click “Create” at the bottom right hand side of the screen.
i) The previous action brings you back to the “Create a Rule” page.
Click “Create Rule” at the bottom right hand side of the screen to finalise the steps in
this section
Section 7
Python code for main user control
A. Pi2_two.py
No Task
a) • Next, we will create a Python program that will manage the RFID cards that are
being tapped, this program will check if the card tapped is authorized and if the
card is not the program will send a message together with a picture the user’s
Telegram and also send the picture to S3 and if the card is authorized the system
will publish a mqtt message.
• On your laptop, create a new Python code file named pi2_two.py
b) • Copy and paste the code below to the newly created file
import boto3
import botocore
import json
import decimal
import MFRC522
import time
import telepot
import os
import datetime
from picamera import PiCamera
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 24 of 84
No Task
from gpiozero import Button, Buzzer
from rpi_lcd import LCD
from signal import pause
from boto3.dynamodb.conditions import Key, Attr
lcd = LCD()
button = Button(21)
bz = Buzzer(18)
# Image uploading
# Create an S3 resource
s3 = boto3.resource('s3')
# Start AWS
# Import SDK packages
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
# Custom MQTT message callback
def customCallback(client, userdata, message):
print("Received a new message: ")
print(message.payload)
print("from topic: ")
print(message.topic)
print("--------------\n\n")
host = "abpl27ba00qyj-ats.iot.us-west-2.amazonaws.com"
rootCAPath = "rootca.pem"
certificatePath = "certificate.pem.crt"
privateKeyPath = "private.pem.key"
my_rpi = AWSIoTMQTTClient("basicPubSub")
my_rpi.configureEndpoint(host, 8883)
my_rpi.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
my_rpi.configureOfflinePublishQueueing(-1) # Infinite offline Publish
queueing
my_rpi.configureDrainingFrequency(2) # Draining: 2 Hz
my_rpi.configureConnectDisconnectTimeout(10) # 10 sec
my_rpi.configureMQTTOperationTimeout(5) # 5 sec
# Connect and subscribe to AWS IoT
my_rpi.connect()
time.sleep(2)
# End AWS Image uploading
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 25 of 84
No Task
# Set the filename and bucket name
s3 = boto3.resource('s3',
aws_access_key_i=your key,
aws_secret_access_key=your key)
bucket_name = 'iot-ay1819s2'
exists = True
try:
s3.meta.client.head_bucket(Bucket=bucket_name)
except botocore.exceptions.ClientError as e:
error_code = int(e.response['Error']['Code'])
if error_code == 404:
exists = False
if exists == False:
s3.create_bucket(Bucket=bucket_name,CreateBucketConfiguration={
'LocationConstraint': 'us-west-2'})
def beep1():
bz.on()
time.sleep(0.3)
bz.off()
def beep2():
bz.on()
time.sleep(0.3)
bz.off()
time.sleep(0.1)
bz.on()
time.sleep(0.3)
bz.off()
def beep3():
bz.on()
time.sleep(0.3)
bz.off()
time.sleep(0.1)
bz.on()
time.sleep(0.3)
bz.off()
time.sleep(0.1)
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 26 of 84
No Task
bz.on()
time.sleep(0.3)
bz.off()
def displayLcd(line1, line2):
lcd.text(line1, 1)
lcd.text(line2, 2)
def displayLcdC(line1, line2):
lcd.text(line1, 1)
lcd.text(line2, 2)
time.sleep(5)
lcd.clear()
def telePic(): # Send image captured from pi cam to telegram
camera = PiCamera()
print("Tele bot sending pic")
my_bot_token = '791169392:AAED_dJSbR1y2epLsinLUcSVU2vUThK4l0I'
bot = telepot.Bot(my_bot_token)
#Take pic and upload to S3
time1 = datetime.datetime.now()
fileName = time1.strftime("%y%m%d_%H-%M-%S") + '.jpg'
full_path = "/home/pi/Desktop/pic/{}".format(fileName)
camera.capture(full_path)
time.sleep(3)
camera.close()
print("Uploading to S3 / Telegram")
bot.sendPhoto(chat_id=130237501, photo=open(full_path, 'rb'))
s3.Object(bucket_name, 'home/1819s2_iot_JT-
TQE/'+fileName).put(Body=open(full_path, 'rb'),ACL='public-read')
#bot.sendPhoto(chat_id=314361200,
photo=open('/home/pi/Desktop/image1.jpg', 'rb'))
print("Uploaded to S3 / Telegram")
def teleText(text): # Send text from pi to telegram
print("Tele bot sending text")
my_bot_token = '791169392:AAED_dJSbR1y2epLsinLUcSVU2vUThK4l0I'
bot = telepot.Bot(my_bot_token)
bot.sendMessage('130237501', text)
#bot.sendMessage('314361200', text)
print("Message sent")
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 27 of 84
No Task
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
if o % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
dynamodb = boto3.resource('dynamodb', region_name='us-west-2',
aws_access_key_id="YOUR KEY", aws_secret_access_key="your key")
table = dynamodb.Table('JTTQE-RfidUserLog')
print("waiting for press")
displayLcd("Kindly press","button to scan")
def updateUid():
print("Starting")
displayLcd("Please scan card","")
global uid
nameList = []
nameList2 = []
update = True
while update:
#Scan uid
MIFAREReader = MFRC522.MFRC522()
(status,TagType) =
MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
(status,uid) = MIFAREReader.MFRC522_Anticoll()
if status == MIFAREReader.MI_OK:
displayLcd("Scanning card","Please wait")
print("UID of tapped card:
"+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
uid = str(uid[0]) + str(uid[1]) + str(uid[2]) + str(uid[3])
time.sleep(2)
#Scan table and put in nameList2
response = table.scan()
for i in response['Items']:
nameList2.append(i['uid'])
print(nameList2)
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 28 of 84
No Task
#Check if tapped uid is inside db if inside update table if
not inside reject
if uid in nameList2:
response = table.query(
KeyConditionExpression=Key('uid').eq(uid)
)
for i in response['Items']:
nameList.append(i['stat'])
print(nameList)
if 'out' in nameList:
response = table.update_item(
Key={
'uid': uid
},
UpdateExpression="set stat = :r",
ExpressionAttributeValues={
':r': 'in',
},
ReturnValues="UPDATED_NEW"
)
nameList = []
update = False
beep1()
displayLcdC("Welcome Home :)","")
displayLcd("Kindly press","button to scan")
print("Updated to in")
else:
response = table.update_item(
Key={
'uid': uid
},
UpdateExpression="set stat = :r",
ExpressionAttributeValues={
':r': 'out',
},
ReturnValues="UPDATED_NEW"
)
nameList = []
update = False
beep2()
displayLcdC("Goodbye :)","")
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 29 of 84
No Task
displayLcd("Kindly press","button to scan")
print("Updated to out")
else:
print("Card not in db")
update = False
beep3()
teleText("Unauthorized card tapped, sending picture!")
telePic()
displayLcdC("Unauthorize User","")
displayLcd("Kindly press","button to scan")
#Press button to start scanning
button.when_pressed = updateUid
pause()
Section 8
Python code for motion sensing
A. Pi2_one.py
No Task
B. • Next, we will create a Python program that will sense for motion using the motion
sensor and when the sensor is triped the program will take a video and send it to
the user’s Telegram and also publish a mqtt message
• On your laptop, create a new Python code file named local.py
C. – • Copy and paste the code below to the newly created file
import dynamodb
import jsonconverter as jsonc
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 30 of 84
No Task
import time
import datetime
import decimal
import gevent
import gevent.monkey
import os
import sys
import json
import numpy
import telepot
from subprocess import call
from picamera import PiCamera
from gevent.pywsgi import WSGIServer
from gpiozero import MotionSensor, LED
from rpi_lcd import LCD
# Declarations
pir = MotionSensor(17, sample_rate=5, queue_len=1)
led = LED(6)
# Start AWS
# Import SDK packages
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
# Custom MQTT message callback
def customCallback(client, userdata, message):
print("Received a new message: ")
print(message.payload)
print("from topic: ")
print(message.topic)
print("--------------\n\n")
host = "abpl27ba00qyj-ats.iot.us-west-2.amazonaws.com"
rootCAPath = "rootca.pem"
certificatePath = "certificate.pem.crt"
privateKeyPath = "private.pem.key"
my_rpi = AWSIoTMQTTClient("basicPubSub")
my_rpi.configureEndpoint(host, 8883)
my_rpi.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
my_rpi.configureOfflinePublishQueueing(-1) # Infinite offline Publish
queueing
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 31 of 84
No Task
my_rpi.configureDrainingFrequency(2) # Draining: 2 Hz
my_rpi.configureConnectDisconnectTimeout(10) # 10 sec
my_rpi.configureMQTTOperationTimeout(5) # 5 sec
# Connect and subscribe to AWS IoT
my_rpi.connect()
my_rpi.subscribe("sensors/trip", 1, customCallback)
time.sleep(2)
# End AWS
def getTime():
currentDt = datetime.datetime.now()
fCurrentDt = currentDt.strftime("%I:%M:%S %p")
return fCurrentDt
def getDate():
currentDt = datetime.datetime.now()
fCurrentDt = currentDt.strftime("%Y-%m-%d")
return fCurrentDt
def pubTrip(time, date):
message={}
message["deviceid"] = "pi"
message["time"] = time
message["date"] = date
message["trip"] = "1"
import json
my_rpi.publish("sensors/trip", json.dumps(message), 1)
def teleVid(): # Send video captured from pi cam to telegram
print("Recording Vid")
camera = PiCamera()
camera.resolution = (640, 480)
time.sleep(5)
camera.start_recording('/home/pi/Desktop/video1.h264')
camera.wait_recording(8)
camera.stop_recording()
command = "MP4Box -add /home/pi/Desktop/video1.h264
/home/pi/Desktop/out.mp4"
call([command], shell=True)
my_bot_token = '791169392:AAED_dJSbR1y2epLsinLUcSVU2vUThK4l0I'
bot = telepot.Bot(my_bot_token)
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 32 of 84
No Task
bot.sendMessage('130237501', "Motion detected, sending video!")
#bot.sendMessage('314361200', "Motion detected")
bot.sendVideo(chat_id=130237501,
video=open('/home/pi/Desktop/out.mp4', 'rb'))
#bot.sendVideo(chat_id=314361200,
video=open('/home/pi/Desktop/out.mp4', 'rb'))
os.remove("/home/pi/Desktop/out.mp4")
camera.close()
print("Video sent")
# Start of Motion Recording System
def ledOn():
#led.on()
global ledStat
ledStat = 1
while (ledStat < 2):
pir.wait_for_motion()
if ledStat > 1:
led.off()
break
timestring = time.strftime("%d-%m-%Y_%H:%M:%S",
time.localtime())
datenow = datetime.datetime.today().strftime('%Y-%m-%d')
timenow = time.strftime("%H:%M:%S", time.localtime())
trip = str(1)
print("Motion detected, recording video at "+timestring)
# Begin storing values to MQTT
try:
print("Debug 1:", datenow)
print("Debug 2:", timenow)
print("Debug 3:", trip)
pubTrip(getTime(), getDate()) #publish trip to AWS MQTT
teleVid()
except:
print("Error while inserting data...")
print(sys.exc_info()[0])
print(sys.exc_info()[1])
# End storing values to MQTT
print("Recording completed, waiting for motion")
time.sleep(5)
continue
return
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 33 of 84
No Task
ledOn()
Section 9
Python code for Telegram controls/DHT/LDR
A. Pi1_two.py
No Task
B. • Next, we will create a Python program that will allow the user to send commands
to the Telegram bot which will then turn on and off the light in the house
depedning on which light the user choose, this program will also turn on and off a
light when the LDR senses low light and lastly it will also publish the temp of the
DHT sensor
• On your laptop, create a new Python code file named pi2_two.py
C. • Copy and paste the code below to the newly created file
import telepot
import time
import datetime
import Adafruit_DHT
from gpiozero import LED, MCP3008
# Start AWS
# Import SDK packages
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
# Custom MQTT message callback
def customCallback(client, userdata, message):
print("Received a new message: ")
print(message.payload)
print("from topic: ")
print(message.topic)
print("--------------\n\n")
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 34 of 84
No Task
host = "abpl27ba00qyj-ats.iot.us-west-2.amazonaws.com"
rootCAPath = "rootca.pem"
certificatePath = "certificate.pem.crt"
privateKeyPath = "private.pem.key"
my_rpi = AWSIoTMQTTClient("basicPubSub")
my_rpi.configureEndpoint(host, 8883)
my_rpi.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
my_rpi.configureOfflinePublishQueueing(-1) # Infinite offline Publish
queueing
my_rpi.configureDrainingFrequency(2) # Draining: 2 Hz
my_rpi.configureConnectDisconnectTimeout(10) # 10 sec
my_rpi.configureMQTTOperationTimeout(5) # 5 sec
# Connect and subscribe to AWS IoT
my_rpi.connect()
my_rpi.subscribe("sensors/temp", 1, customCallback)
time.sleep(2)
# End AWS
my_bot_token = '791169392:AAED_dJSbR1y2epLsinLUcSVU2vUThK4l0I'
led1 = LED(13)
led2 = LED(19)
led3 = LED(26)
mcp3008 = MCP3008(channel=0)
light1 = LED(6)
def getDateTime():
currentDt = datetime.datetime.now()
fCurrentDt = currentDt.strftime("%Y-%m-%d %I:%M:%S:%p")
return fCurrentDt
def pubTemp(datetime, temp):
message={}
message["deviceid"] = "pi"
message["datetime"] = datetime#.isoformat()
message["temp"] = temp
import json
my_rpi.publish("sensors/temp", json.dumps(message), 1)
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 35 of 84
No Task
def onLED1():
led1.on()
return "Turning on Room 1 light"
def offLED1():
led1.off()
return "Turning off Room 1 light"
def onLED2():
led2.on()
return "Turning on Room 2 light"
def offLED2():
led2.off()
return "Turning off Room 2 light"
def onLED3():
led3.on()
return "Turning on Room 3 light"
def offLED3():
led3.off()
return "Turning off Room 3 light"
def respondToMsg(msg):
chat_id = msg['chat']['id']
command = msg['text']
print('Got command: {}'.format(command))
if command.upper() == '/ONROOM1':
bot.sendMessage(chat_id, onLED1())
elif command.upper() =='/OFFROOM1':
bot.sendMessage(chat_id, offLED1())
elif command.upper() == '/ONROOM2':
bot.sendMessage(chat_id, onLED2())
elif command.upper() == '/OFFROOM2':
bot.sendMessage(chat_id, offLED2())
elif command.upper() == '/ONROOM3':
bot.sendMessage(chat_id, onLED3())
elif command.upper() == '/OFFROOM3':
bot.sendMessage(chat_id, offLED3())
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 36 of 84
No Task
bot = telepot.Bot(my_bot_token)
bot.message_loop(respondToMsg)
print('Listening for RPi commands...')
while True:
time.sleep(5)
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
pubTemp(getDateTime(), temperature)
sensor_value = (1024*mcp3008.value)
sensor_value = round(sensor_value)
if sensor_value > 700:
light1.on()
else:
light1.off()
Section 10
Python code for server
A. Pi1_one.py
No Task
B. • Next, we will create a Python program that will run the sever for the web page and
retrieve data from DynamoDB
• On your laptop, create a new Python code file named pi1_one.py
C. • Copy and paste the code below to the newly created file
from flask import Flask, render_template, jsonify, request, Response
from picam import Camera
from gpiozero import LED
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 37 of 84
No Task
app = Flask(__name__)
import dynamodb
import jsonconverter as jsonc
import boto3
import json
import decimal
import MFRC522
import time
light3 = LED(13) #green
light2 = LED(19) #yellow
light1 = LED(26) #red
@app.route("/api/switchRed",methods=['POST','GET'])
def switchRed():
try:
command = request.form["command"]
if command == "on":
light1.on()
message = "Room light is turned on"
else:
light1.off()
message = "Room light is turned off"
except:
message = "Error changing red"
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
data = {"message" : message}
return jsonify(data)
@app.route("/api/switchYellow",methods=['POST','GET'])
def switchYellow():
try:
command = request.form["command"]
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 38 of 84
No Task
if command == "on":
light2.on()
message = "Room light is on"
else:
light2.off()
message = "Room light is off"
except:
message = "Error changing yellow"
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
data = {"message" : message}
return jsonify(data)
@app.route("/api/switchGreen",methods=['POST','GET'])
def switchGreen():
try:
command = request.form["command"]
if command == "on":
light3.on()
message = "Room light is on"
else:
light3.off()
message = "Room light is off"
except:
message = "Error changing green"
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
data = {"message" : message}
return jsonify(data)
#aws
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
# Custom MQTT message callback
def customCallback(client, userdata, message):
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 39 of 84
No Task
print("Received a new message: ")
print(message.payload)
print("from topic: ")
print(message.topic)
print("--------------\n\n")
host = "abpl27ba00qyj-ats.iot.us-west-2.amazonaws.com"
rootCAPath = "rootca.pem"
certificatePath = "certificate.pem.crt"
privateKeyPath = "private.pem.key"
my_rpi = AWSIoTMQTTClient("basicPubSub")
my_rpi.configureEndpoint(host, 8883)
my_rpi.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
my_rpi.configureOfflinePublishQueueing(-1) # Infinite offline Publish
queueing
my_rpi.configureDrainingFrequency(2) # Draining: 2 Hz
my_rpi.configureConnectDisconnectTimeout(10) # 10 sec
my_rpi.configureMQTTOperationTimeout(5) # 5 sec
# Connect and subscribe to AWS IoT
my_rpi.connect()
my_rpi.subscribe("sensors/uid", 1, customCallback)
time.sleep(2)
# End AWS
def pubUid(uid, name):
message={}
message["deviceid"] = "pi"
message["uid"] = uid
message["name"] = name
#import json
my_rpi.publish("sensors/uid", json.dumps(message), 1)
def pubUserLog1(uid, name):
message={}
message["uid"] = uid
message["name"] = name
message["stat"] = "out"
my_rpi.publish("sensors/uidLog", json.dumps(message), 1)
@app.route("/api/getdata",methods=['POST','GET'])
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 40 of 84
No Task
def apidata_getdata():
if request.method == 'POST' or request.method == 'GET':
try:
data = {'chart_data':
jsonc.data_to_json(dynamodb.get_data_from_dynamodb()), 'title': "IOT
Data"}
return jsonify(data)
except:
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
@app.route("/api/getdata2",methods=['POST','GET'])
def apidata_getdata2():
if request.method == 'POST' or request.method == 'GET':
try:
dldata = {'chart_data':
jsonc.data_to_json(dynamodb.get_data_from_dynamodb()), 'title': "IOT
Data"}
return jsonify(dldata)
except:
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
@app.route("/api/getdata3",methods=['POST','GET'])
def apidata_getdata3():
if request.method == 'POST' or request.method == 'GET':
try:
actdata = {'chart_data':
jsonc.data_to_json(dynamodb.get_data_from_dynamodb3()), 'title': "Act
Data"}
return jsonify(actdata)
except:
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 41 of 84
No Task
def default(self, o):
if isinstance(o, decimal.Decimal):
if o % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
@app.route("/api/currentTemp",methods=['POST','GET'])
def currentTemp():
if request.method == 'POST' or request.method == 'GET':
try:
dynamodb = boto3.resource('dynamodb', region_name='us-west-
2', aws_access_key_id="YOUR KEY", aws_secret_access_key="your key")
table = dynamodb.Table('JTTQE-temp')
update = True
while update:
# Table scan
global temp
response = table.scan()
for i in response['Items']:
# get all the table entries in json format
json_str = json.dumps(i, cls=DecimalEncoder)
resp_dict = json.loads(json_str)
temp = resp_dict.get('temp')
return jsonify(temp)
except:
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
@app.route("/api/uid",methods=['POST','GET'])
def uid():
try:
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
command = request.form["command"]
update = True
print("Please scan card")
while update:
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 42 of 84
No Task
#Scan card
(status,TagType) =
MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
#Get card UID
(status,uid) = MIFAREReader.MFRC522_Anticoll()
#If UID detacted
if status == MIFAREReader.MI_OK:
print("UID of tapped card:
"+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
uid = str(uid[0]) + str(uid[1]) + str(uid[2]) +
str(uid[3])
pubUid(uid,command)
pubUserLog1(uid, command)
print(uid,command)
break
except:
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
data = {"message" : "Added"}
return jsonify(data)
@app.route("/api/getImages",methods=['POST','GET'])
def getImages():
if request.method == 'POST' or request.method == 'GET':
try:
s3 = boto3.resource('s3', aws_access_key_id="YOUR KEY",
aws_secret_access_key="your key")
bucket_name = 'iot-ay1819s2'
my_bucket = s3.Bucket(bucket_name)
objs = my_bucket.objects.filter(Prefix ='home/1819s2_iot_JT-
TQE/')
url = []
for obj in objs:
if str(obj.key).find(".jpg") >= 0:
url.append(obj.key)
return jsonify(url)
except:
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 43 of 84
No Task
@app.route("/")
def home():
return render_template("index.html")
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(Camera()),
mimetype='multipart/x-mixed-replace;
boundary=frame')
app.run(debug=True,threaded=True,host="0.0.0.0")
Section 11
Supporting python codes In this section there will be files that are needed for the above programs to run properly, these need to be in the same folders as the above files
A. dynamodb.py
No Task
B. • Next, we will create a Python program is used by the server.py to get data
• On your laptop, create a new Python code file named dynamodb.py
C. • Copy and paste the code below to the newly created file
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 44 of 84
No Task
def get_data_from_dynamodb():
try :
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb', region_name='us-west-
2', aws_access_key_id="YOUR KEY", aws_secret_access_key="your key")
table = dynamodb.Table('JTTQE-trip')
#Get all data in table
response = table.scan()
items = response['Items']
return items
except:
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
def get_data_from_dynamodb2():
try :
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb', region_name='us-west-
2', aws_access_key_id="YOUR KEY", aws_secret_access_key="your key")
table = dynamodb.Table('JTTQE-temp')
update = True
while update:
# Table scan
global temp
response = table.scan()
items = response['Items']
#temp = items.get('temp')
return items
except:
import sys
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 45 of 84
No Task
print(sys.exc_info()[0])
print(sys.exc_info()[1])
def get_data_from_dynamodb3():
try :
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb', region_name='us-west-
2', aws_access_key_id="YOUR KEY", aws_secret_access_key="your key")
table = dynamodb.Table('JTTQE-RfidUserLog')
#Get all data in table
response = table.scan()
items = response['Items']
return items
except:
import sys
print(sys.exc_info()[0])
print(sys.exc_info()[1])
if __name__ == "__main__":
query_data_from_dynamodb()
B. jsonconverter.py
No Task
D. • Next, we will create a Python program that is used by the server.py to convert data
• On your laptop, create a new Python code file named jsonconverter.py
E. • Copy and paste the code below to the newly created file
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 46 of 84
No Task
from decimal import Decimal
import json
import datetime
import numpy
class GenericEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, numpy.generic):
return numpy.asscalar(obj)
elif isinstance(obj, Decimal):
return str(obj)
elif isinstance(obj, datetime.date):
return obj.strftime('%Y-%m-%d')
elif isinstance(obj, datetime.time):
return obj.strftime('%I:%M:%S %p')
elif isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%d %I:%M:%S %p')
elif isinstance(obj, Decimal):
if obj % 1 > 0:
return float(obj)
else:
return int(obj)
else:
return json.JSONEncoder.default(self, obj)
def data_to_json(data):
json_data = json.dumps(data,cls=GenericEncoder)
#print(json_data)
return json_data
def data_to_jsoni(data):
json_data = json.dumps(data,cls=GenericEncoder)
resp_dict = json.loads(json_data)
return resp_dict
C. picam.py
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 47 of 84
No Task
F. • Next, we will create a Python program that is used by the server.py to use the
picam
• On your laptop, create a new Python code file named picam.py
G. • Copy and paste the code below to the newly created file
import io
import time
import picamera
from base_camera import BaseCamera
class Camera(BaseCamera):
@staticmethod
def frames():
with picamera.PiCamera() as camera:
# let camera warm up
time.sleep(2)
stream = io.BytesIO()
for _ in camera.capture_continuous(stream, 'jpeg',
use_video_port=True):
# return current frame
stream.seek(0)
yield stream.read()
# reset stream for next frame
stream.seek(0)
stream.truncate()
D. base_camera.py
No Task
H. • Next, we will create a Python program that is used by the server.py to use the
picam
• On your laptop, create a new Python code file named base_camera.py
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 48 of 84
No Task
I. • Copy and paste the code below to the newly created file
import time
import threading
try:
from greenlet import getcurrent as get_ident
except ImportError:
try:
from thread import get_ident
except ImportError:
from _thread import get_ident
class CameraEvent(object):
"""An Event-like class that signals all active clients when a new
frame is
available.
"""
def __init__(self):
self.events = {}
def wait(self):
"""Invoked from each client's thread to wait for the next
frame."""
ident = get_ident()
if ident not in self.events:
# this is a new client
# add an entry for it in the self.events dict
# each entry has two elements, a threading.Event() and a
timestamp
self.events[ident] = [threading.Event(), time.time()]
return self.events[ident][0].wait()
def set(self):
"""Invoked by the camera thread when a new frame is
available."""
now = time.time()
remove = None
for ident, event in self.events.items():
if not event[0].isSet():
# if this client's event is not set, then set it
# also update the last set timestamp to now
event[0].set()
event[1] = now
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 49 of 84
No Task
else:
# if the client's event is already set, it means the
client
# did not process a previous frame
# if the event stays set for more than 5 seconds, then
assume
# the client is gone and remove it
if now - event[1] > 5:
remove = ident
if remove:
del self.events[remove]
def clear(self):
"""Invoked from each client's thread after a frame was
processed."""
self.events[get_ident()][0].clear()
class BaseCamera(object):
thread = None # background thread that reads frames from camera
frame = None # current frame is stored here by background thread
last_access = 0 # time of last client access to the camera
event = CameraEvent()
def __init__(self):
"""Start the background camera thread if it isn't running
yet."""
if BaseCamera.thread is None:
BaseCamera.last_access = time.time()
# start background frame thread
BaseCamera.thread = threading.Thread(target=self._thread)
BaseCamera.thread.start()
# wait until frames are available
while self.get_frame() is None:
time.sleep(0)
def get_frame(self):
"""Return the current camera frame."""
BaseCamera.last_access = time.time()
# wait for a signal from the camera thread
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 50 of 84
No Task
BaseCamera.event.wait()
BaseCamera.event.clear()
return BaseCamera.frame
@staticmethod
def frames():
""""Generator that returns frames from the camera."""
raise RuntimeError('Must be implemented by subclasses.')
@classmethod
def _thread(cls):
"""Camera background thread."""
print('Starting camera thread.')
frames_iterator = cls.frames()
for frame in frames_iterator:
BaseCamera.frame = frame
BaseCamera.event.set() # send signal to clients
time.sleep(0)
# if there hasn't been any clients asking for frames in
# the last 10 seconds then stop the thread
if time.time() - BaseCamera.last_access > 10:
frames_iterator.close()
print('Stopping camera thread due to inactivity.')
break
BaseCamera.thread = None
E. MFRC522.py
No Task
J. • Next, we will create a Python program that is used by the RFID reader
• On your laptop, create a new Python code file named MFRC522.py
K. • Copy and paste the code below to the newly created file
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 51 of 84
No Task
#!/usr/bin/env python
# -*- coding: utf8 -*-
#
# Copyright 2014,2018 Mario Gomez <[email protected]>
#
# This file is part of MFRC522-Python
# MFRC522-Python is a simple Python implementation for
# the MFRC522 NFC Card Reader for the Raspberry Pi.
#
# MFRC522-Python is free software: you can redistribute it and/or
modify
# it under the terms of the GNU Lesser General Public License as
published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MFRC522-Python is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
License
# along with MFRC522-Python. If not, see
<http://www.gnu.org/licenses/>.
#
import RPi.GPIO as GPIO
import spi
import signal
import time
class MFRC522:
NRSTPD = 22
MAX_LEN = 16
PCD_IDLE = 0x00
PCD_AUTHENT = 0x0E
PCD_RECEIVE = 0x08
PCD_TRANSMIT = 0x04
PCD_TRANSCEIVE = 0x0C
PCD_RESETPHASE = 0x0F
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 52 of 84
No Task
PCD_CALCCRC = 0x03
PICC_REQIDL = 0x26
PICC_REQALL = 0x52
PICC_ANTICOLL = 0x93
PICC_SElECTTAG = 0x93
PICC_AUTHENT1A = 0x60
PICC_AUTHENT1B = 0x61
PICC_READ = 0x30
PICC_WRITE = 0xA0
PICC_DECREMENT = 0xC0
PICC_INCREMENT = 0xC1
PICC_RESTORE = 0xC2
PICC_TRANSFER = 0xB0
PICC_HALT = 0x50
MI_OK = 0
MI_NOTAGERR = 1
MI_ERR = 2
Reserved00 = 0x00
CommandReg = 0x01
CommIEnReg = 0x02
DivlEnReg = 0x03
CommIrqReg = 0x04
DivIrqReg = 0x05
ErrorReg = 0x06
Status1Reg = 0x07
Status2Reg = 0x08
FIFODataReg = 0x09
FIFOLevelReg = 0x0A
WaterLevelReg = 0x0B
ControlReg = 0x0C
BitFramingReg = 0x0D
CollReg = 0x0E
Reserved01 = 0x0F
Reserved10 = 0x10
ModeReg = 0x11
TxModeReg = 0x12
RxModeReg = 0x13
TxControlReg = 0x14
TxAutoReg = 0x15
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 53 of 84
No Task
TxSelReg = 0x16
RxSelReg = 0x17
RxThresholdReg = 0x18
DemodReg = 0x19
Reserved11 = 0x1A
Reserved12 = 0x1B
MifareReg = 0x1C
Reserved13 = 0x1D
Reserved14 = 0x1E
SerialSpeedReg = 0x1F
Reserved20 = 0x20
CRCResultRegM = 0x21
CRCResultRegL = 0x22
Reserved21 = 0x23
ModWidthReg = 0x24
Reserved22 = 0x25
RFCfgReg = 0x26
GsNReg = 0x27
CWGsPReg = 0x28
ModGsPReg = 0x29
TModeReg = 0x2A
TPrescalerReg = 0x2B
TReloadRegH = 0x2C
TReloadRegL = 0x2D
TCounterValueRegH = 0x2E
TCounterValueRegL = 0x2F
Reserved30 = 0x30
TestSel1Reg = 0x31
TestSel2Reg = 0x32
TestPinEnReg = 0x33
TestPinValueReg = 0x34
TestBusReg = 0x35
AutoTestReg = 0x36
VersionReg = 0x37
AnalogTestReg = 0x38
TestDAC1Reg = 0x39
TestDAC2Reg = 0x3A
TestADCReg = 0x3B
Reserved31 = 0x3C
Reserved32 = 0x3D
Reserved33 = 0x3E
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 54 of 84
No Task
Reserved34 = 0x3F
serNum = []
def __init__(self, dev='/dev/spidev0.1', spd=1000000):
spi.openSPI(device=dev,speed=spd)
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.NRSTPD, GPIO.OUT)
GPIO.output(self.NRSTPD, 1)
self.MFRC522_Init()
def MFRC522_Reset(self):
self.Write_MFRC522(self.CommandReg, self.PCD_RESETPHASE)
def Write_MFRC522(self, addr, val):
spi.transfer(((addr<<1)&0x7E,val))
def Read_MFRC522(self, addr):
val = spi.transfer((((addr<<1)&0x7E) | 0x80,0))
return val[1]
def SetBitMask(self, reg, mask):
tmp = self.Read_MFRC522(reg)
self.Write_MFRC522(reg, tmp | mask)
def ClearBitMask(self, reg, mask):
tmp = self.Read_MFRC522(reg);
self.Write_MFRC522(reg, tmp & (~mask))
def AntennaOn(self):
temp = self.Read_MFRC522(self.TxControlReg)
if(~(temp & 0x03)):
self.SetBitMask(self.TxControlReg, 0x03)
def AntennaOff(self):
self.ClearBitMask(self.TxControlReg, 0x03)
def MFRC522_ToCard(self,command,sendData):
backData = []
backLen = 0
status = self.MI_ERR
irqEn = 0x00
waitIRq = 0x00
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 55 of 84
No Task
lastBits = None
n = 0
i = 0
if command == self.PCD_AUTHENT:
irqEn = 0x12
waitIRq = 0x10
if command == self.PCD_TRANSCEIVE:
irqEn = 0x77
waitIRq = 0x30
self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)
self.ClearBitMask(self.CommIrqReg, 0x80)
self.SetBitMask(self.FIFOLevelReg, 0x80)
self.Write_MFRC522(self.CommandReg, self.PCD_IDLE);
while(i<len(sendData)):
self.Write_MFRC522(self.FIFODataReg, sendData[i])
i = i+1
self.Write_MFRC522(self.CommandReg, command)
if command == self.PCD_TRANSCEIVE:
self.SetBitMask(self.BitFramingReg, 0x80)
i = 2000
while True:
n = self.Read_MFRC522(self.CommIrqReg)
i = i - 1
if ~((i!=0) and ~(n&0x01) and ~(n&waitIRq)):
break
self.ClearBitMask(self.BitFramingReg, 0x80)
if i != 0:
if (self.Read_MFRC522(self.ErrorReg) & 0x1B)==0x00:
status = self.MI_OK
if n & irqEn & 0x01:
status = self.MI_NOTAGERR
if command == self.PCD_TRANSCEIVE:
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 56 of 84
No Task
n = self.Read_MFRC522(self.FIFOLevelReg)
lastBits = self.Read_MFRC522(self.ControlReg) & 0x07
if lastBits != 0:
backLen = (n-1)*8 + lastBits
else:
backLen = n*8
if n == 0:
n = 1
if n > self.MAX_LEN:
n = self.MAX_LEN
i = 0
while i<n:
backData.append(self.Read_MFRC522(self.FIFODataReg))
i = i + 1;
else:
status = self.MI_ERR
return (status,backData,backLen)
def MFRC522_Request(self, reqMode):
status = None
backBits = None
TagType = []
self.Write_MFRC522(self.BitFramingReg, 0x07)
TagType.append(reqMode);
(status,backData,backBits) =
self.MFRC522_ToCard(self.PCD_TRANSCEIVE, TagType)
if ((status != self.MI_OK) | (backBits != 0x10)):
status = self.MI_ERR
return (status,backBits)
def MFRC522_Anticoll(self):
backData = []
serNumCheck = 0
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 57 of 84
No Task
serNum = []
self.Write_MFRC522(self.BitFramingReg, 0x00)
serNum.append(self.PICC_ANTICOLL)
serNum.append(0x20)
(status,backData,backBits) =
self.MFRC522_ToCard(self.PCD_TRANSCEIVE,serNum)
if(status == self.MI_OK):
i = 0
if len(backData)==5:
while i<4:
serNumCheck = serNumCheck ^ backData[i]
i = i + 1
if serNumCheck != backData[i]:
status = self.MI_ERR
else:
status = self.MI_ERR
return (status,backData)
def CalulateCRC(self, pIndata):
self.ClearBitMask(self.DivIrqReg, 0x04)
self.SetBitMask(self.FIFOLevelReg, 0x80);
i = 0
while i<len(pIndata):
self.Write_MFRC522(self.FIFODataReg, pIndata[i])
i = i + 1
self.Write_MFRC522(self.CommandReg, self.PCD_CALCCRC)
i = 0xFF
while True:
n = self.Read_MFRC522(self.DivIrqReg)
i = i - 1
if not ((i != 0) and not (n&0x04)):
break
pOutData = []
pOutData.append(self.Read_MFRC522(self.CRCResultRegL))
pOutData.append(self.Read_MFRC522(self.CRCResultRegM))
return pOutData
def MFRC522_SelectTag(self, serNum):
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 58 of 84
No Task
backData = []
buf = []
buf.append(self.PICC_SElECTTAG)
buf.append(0x70)
i = 0
while i<5:
buf.append(serNum[i])
i = i + 1
pOut = self.CalulateCRC(buf)
buf.append(pOut[0])
buf.append(pOut[1])
(status, backData, backLen) =
self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buf)
if (status == self.MI_OK) and (backLen == 0x18):
print "Size: " + str(backData[0])
return backData[0]
else:
return 0
def MFRC522_Auth(self, authMode, BlockAddr, Sectorkey, serNum):
buff = []
# First byte should be the authMode (A or B)
buff.append(authMode)
# Second byte is the trailerBlock (usually 7)
buff.append(BlockAddr)
# Now we need to append the authKey which usually is 6 bytes of 0xFF
i = 0
while(i < len(Sectorkey)):
buff.append(Sectorkey[i])
i = i + 1
i = 0
# Next we append the first 4 bytes of the UID
while(i < 4):
buff.append(serNum[i])
i = i +1
# Now we start the authentication itself
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 59 of 84
No Task
(status, backData, backLen) =
self.MFRC522_ToCard(self.PCD_AUTHENT,buff)
# Check if an error occurred
if not(status == self.MI_OK):
print "AUTH ERROR!!"
if not (self.Read_MFRC522(self.Status2Reg) & 0x08) != 0:
print "AUTH ERROR(status2reg & 0x08) != 0"
# Return the status
return status
def MFRC522_StopCrypto1(self):
self.ClearBitMask(self.Status2Reg, 0x08)
def MFRC522_Read(self, blockAddr):
recvData = []
recvData.append(self.PICC_READ)
recvData.append(blockAddr)
pOut = self.CalulateCRC(recvData)
recvData.append(pOut[0])
recvData.append(pOut[1])
(status, backData, backLen) =
self.MFRC522_ToCard(self.PCD_TRANSCEIVE, recvData)
if not(status == self.MI_OK):
print "Error while reading!"
i = 0
if len(backData) == 16:
print "Sector "+str(blockAddr)+" "+str(backData)
def MFRC522_Write(self, blockAddr, writeData):
buff = []
buff.append(self.PICC_WRITE)
buff.append(blockAddr)
crc = self.CalulateCRC(buff)
buff.append(crc[0])
buff.append(crc[1])
(status, backData, backLen) =
self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buff)
if not(status == self.MI_OK) or not(backLen == 4) or
not((backData[0] & 0x0F) == 0x0A):
status = self.MI_ERR
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 60 of 84
No Task
print "%s backdata &0x0F == 0x0A %s" % (backLen, backData[0]&0x0F)
if status == self.MI_OK:
i = 0
buf = []
while i < 16:
buf.append(writeData[i])
i = i + 1
crc = self.CalulateCRC(buf)
buf.append(crc[0])
buf.append(crc[1])
(status, backData, backLen) =
self.MFRC522_ToCard(self.PCD_TRANSCEIVE,buf)
if not(status == self.MI_OK) or not(backLen == 4) or
not((backData[0] & 0x0F) == 0x0A):
print "Error while writing"
if status == self.MI_OK:
print "Data written"
def MFRC522_DumpClassic1K(self, key, uid):
i = 0
while i < 64:
status = self.MFRC522_Auth(self.PICC_AUTHENT1A, i, key, uid)
# Check if authenticated
if status == self.MI_OK:
self.MFRC522_Read(i)
else:
print "Authentication error"
i = i+1
def MFRC522_Init(self):
GPIO.output(self.NRSTPD, 1)
self.MFRC522_Reset();
self.Write_MFRC522(self.TModeReg, 0x8D)
self.Write_MFRC522(self.TPrescalerReg, 0x3E)
self.Write_MFRC522(self.TReloadRegL, 30)
self.Write_MFRC522(self.TReloadRegH, 0)
self.Write_MFRC522(self.TxAutoReg, 0x40)
self.Write_MFRC522(self.ModeReg, 0x3D)
self.AntennaOn()
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 61 of 84
No Task
Section 12
Installing Telegram bot
A. telegrambot.py
Next, we will create a telegram bot that will let you turn on and off the room lights using
telegram. While the light turns off/on the LCD will also show the status of the light.
a) Open Web/App Telegram in your laptop or mobile and start “BotFather”
b) Type /newbot to create a new bot
c) Give a name to the bot.
d) Ensure it ends with _bot
e) Copy the access token
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 62 of 84
Task
Install the Telegram API on your Raspberry Pi sudo pip install telepot
Section 13
Python code for webpage
A. Index.html
No Task
B. • On your laptop, create 2 sub-folder named templates and static under the same
folder where you saved server.py
• Ensure that inside the static folder there is assets and images, if not pleae copy it in
from the file provided
• Create a new HTML file under the templates folder and name it index.html
C. • Copy and paste the code below to templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1,
shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>JTTQE Smart Home - Dashboard</title>
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 63 of 84
No Task
<!-- Custom fonts for this template-->
<link href="../static/vendor/fontawesome-free/css/all.min.css"
rel="stylesheet" type="text/css">
<link
href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,4
00,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">
<!-- Custom styles for this template-->
<link href="../static/css/sb-admin-2.min.css" rel="stylesheet">
<!-- Bootstrap core JavaScript-->
<script src="../static/vendor/jquery/jquery.min.js"></script>
<script
src="../static/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Google chart JavaScript-->
<script type="text/javascript"
src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="../static/js/gchart.js"></script>
<!-- Export SQL JavaScript-->
<script type="text/javascript"
src="../static/js/exporttoexcel.js"></script>
<!-- Page level custom scripts -->
<script type="text/javascript" src="../static/js/app.js"></script>
</head>
<body id="page-top">
<!-- Page Wrapper -->
<div id="wrapper">
<!-- Sidebar -->
<ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark
accordion" id="accordionSidebar">
<!-- Sidebar - Brand -->
<a class="sidebar-brand d-flex align-items-center justify-content-
center" href="/#"">
<div class="sidebar-brand-icon rotate-n-15">
<i class="fas fa-laugh-wink"></i>
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 64 of 84
No Task
</div>
<div class="sidebar-brand-text mx-3">JQTTQE Smart Home</div>
</a>
<!-- Divider -->
<hr class="sidebar-divider my-0">
<!-- Nav Item - Dashboard -->
<li class="nav-item active">
<a class="nav-link" href="/#">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Dashboard</span></a>
</li>
<!-- Divider -->
<hr class="sidebar-divider">
<!-- Heading -->
<div class="sidebar-heading">
Interface
</div>
<!-- Nav Item - Modules Collapse Menu -->
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse"
data-target="#collapseTwo" aria-expanded="true" aria-
controls="collapseTwo">
<i class="fas fa-fw fa-cog"></i>
<span>Modules</span>
</a>
<div id="collapseTwo" class="collapse" aria-
labelledby="headingTwo" data-parent="#accordionSidebar">
<div class="bg-white py-2 collapse-inner rounded">
<h6 class="collapse-header">Add Module:</h6>
<a class="collapse-item" href="#" id="addLight">Lights</a>
<a class="collapse-item" href="#" data-toggle="modal" data-
target="#logoutModal">RFID Cards</a>
</div>
</div>
</li>
<!-- Nav Item - Images Collapse Menu -->
<li class="nav-item">
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 65 of 84
No Task
<a class="nav-link collapsed" href="#" data-toggle="collapse"
data-target="#collapsePages" aria-expanded="true" aria-
controls="collapsePages">
<i class="fas fa-fw fa-folder"></i>
<span>Images</span>
</a>
<div id="collapsePages" class="collapse" aria-
labelledby="headingPages" data-parent="#accordionSidebar">
<div class="bg-white py-2 collapse-inner rounded">
<h6 class="collapse-header">View:</h6>
<a class="collapse-item" href="static/img.html">Images</a>
</div>
</div>
</li>
<!-- Divider -->
<hr class="sidebar-divider">
<!-- Sidebar Toggler (Sidebar) -->
<div class="text-center d-none d-md-inline">
<button class="rounded-circle border-0"
id="sidebarToggle"></button>
</div>
</ul>
<!-- End of Sidebar -->
<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">
<!-- Main Content -->
<div id="content">
<!-- Topbar -->
<nav class="navbar navbar-expand navbar-light bg-white topbar
mb-4 static-top shadow">
<!-- Sidebar Toggle (Topbar) -->
<button id="sidebarToggleTop" class="btn btn-link d-md-none
rounded-circle mr-3">
<i class="fa fa-bars"></i>
</button>
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 66 of 84
No Task
<!-- Topbar Search -->
<form class="d-none d-sm-inline-block form-inline mr-auto ml-
md-3 my-2 my-md-0 mw-100 navbar-search">
<div class="input-group">
<input type="text" id="txtSearch" class="form-control bg-
light border-0 small" placeholder="Search for..." aria-label="Search"
aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-primary" type="button"
onclick="searchFunc()">
<i class="fas fa-search fa-sm"></i>
</button>
</div>
</div>
</form>
<!-- Topbar Navbar -->
<ul class="navbar-nav ml-auto">
<!-- Nav Item - Search Dropdown (Visible Only XS) -->
<li class="nav-item dropdown no-arrow d-sm-none">
<a class="nav-link dropdown-toggle" href="#"
id="searchDropdown" role="button" data-toggle="dropdown" aria-
haspopup="true" aria-expanded="false">
<i class="fas fa-search fa-fw"></i>
</a>
<!-- Dropdown - Messages -->
<div class="dropdown-menu dropdown-menu-right p-3 shadow
animated--grow-in" aria-labelledby="searchDropdown">
<form class="form-inline mr-auto w-100 navbar-search">
<div class="input-group">
<input type="text" class="form-control bg-light
border-0 small" placeholder="Search for..." aria-label="Search" aria-
describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-primary" type="button">
<i class="fas fa-search fa-sm"></i>
</button>
</div>
</div>
</form>
</div>
</li>
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 67 of 84
No Task
<div class="topbar-divider d-none d-sm-block"></div>
<!-- Nav Item - User Information -->
<li class="nav-item dropdown no-arrow">
<a class="nav-link dropdown-toggle" href="#"
id="userDropdown" role="button" data-toggle="dropdown" aria-
haspopup="true" aria-expanded="false">
<span class="mr-2 d-none d-lg-inline text-gray-600
small">
Sarah Tan
</span>
<img class="img-profile rounded-circle"
src="https://source.unsplash.com/QAB-WJcbgJk/60x60">
</a>
</li>
</ul>
</nav>
<!-- End of Topbar -->
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-
between mb-4">
<h1 class="h3 mb-0 text-gray-800">Dashboard</h1>
<a id="downloadAnchorElem" onclick="loadDL()" href="#"
class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i
class="fas fa-download fa-sm text-white-50"></i> Generate Report</a>
</div>
<!-- Content Row -->
<div class="row">
<!-- Current Temperature -->
<div class="col-xl-3 col-md-6 mb-4" id="divTemp">
<div class="card border-left-primary shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 68 of 84
No Task
<div class="text-xs font-weight-bold text-primary
text-uppercase mb-1">Temperature</div>
<div class="mb-0">
<div id="currentTemp">Loading temperature..
</div>
<div class="progress progress-sm mr-2">
<div id="tempbar" class="progress-bar bg-
primary" role="progressbar" style="width: 0%"></div>
</div>
</div>
</div>
<div class="col-auto">
<img id="tempPic" src="static/img/cloud.png" />
</div>
</div>
</div>
</div>
</div>
<!-- Light 1 -->
<div class="col-xl-3 col-md-6 mb-4" id="divLight1">
<div class="card border-left-success shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-success
text-uppercase mb-1">Light 1</div>
<div class="mb-0">
<div id="message2">Room light is off</div>
<button class="btn btn-primary"
onclick="switchGreen('on');greenOn();">ON</button>
<button class="btn btn-secondary"
onclick="switchGreen('off');greenOff();">OFF</button>
</div>
</div>
<div class="col-auto">
<img id="greenLed"
src="static/img/led_green_off.png" />
</div>
</div>
</div>
</div>
</div>
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 69 of 84
No Task
<!-- Light 2 -->
<div class="col-xl-3 col-md-6 mb-4" id="divLight2">
<div class="card border-left-info shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-info
text-uppercase mb-1">Light 2</div>
<div class="mb-0">
<div id="message1">Room light is off</div>
<button class="btn btn-primary"
onclick="switchYellow('on');yellowOn();">ON</button>
<button class="btn btn-secondary"
onclick="switchYellow('off');yellowOff();">OFF</button>
</div>
</div>
<div class="col-auto">
<img id="yellowLed"
src="static/img/led_yellow_off.png" />
</div>
</div>
</div>
</div>
</div>
<!-- Light 3 -->
<div id="lightfour" class="col-xl-3 col-md-6 mb-4">
<div class="card border-left-warning shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-warning
text-uppercase mb-1">Light 3</div>
<div class="mb-0">
<div id="message0">Room light is off</div>
<button class="btn btn-primary"
onclick="switchRed('on');redOn();">ON</button>
<button class="btn btn-secondary"
onclick="switchRed('off');redOff();">OFF</button>
</div>
</div>
<div class="col-auto">
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 70 of 84
No Task
<img id="redLed" src="static/img/led_red_off.png"
/>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Content Row -->
<div class="row">
<!-- Trip Chart -->
<div class="col-xl-8 col-lg-7" id="divGraph">
<div class="card shadow mb-4">
<!-- Card Header - Dropdown -->
<div class="card-header py-3 d-flex flex-row align-
items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-primary">Number
of trips</h6>
<div class="dropdown no-arrow">
<a class="dropdown-toggle" href="#" role="button"
id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-
expanded="false">
<i class="fas fa-ellipsis-v fa-sm fa-fw text-gray-
400"></i>
</a>
<div class="dropdown-menu dropdown-menu-right shadow
animated--fade-in" aria-labelledby="dropdownMenuLink">
<div class="dropdown-header">Actions:</div>
<a class="dropdown-item" id="buttonloadchart"
onclick="loadChart()" href="#">Refresh chart</a>
</div>
</div>
</div>
<!-- Card Body -->
<div class="card-body">
<div class="chart-area pt-1 pb-1">
<!-- Start Graph Chart -->
<div id="status"></div>
<div id="chart_div" style="width:100%"></div>
<!-- End Graph Chart
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 71 of 84
No Task
<canvas id="myAreaChart"></canvas> -->
</div>
</div>
</div>
</div>
<!-- Table of information -->
<div class="col-xl-4 col-lg-5" id="divTable">
<div class="card shadow mb-4">
<!-- Card Header - Dropdown -->
<div class="card-header py-3 d-flex flex-row align-
items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-primary">Table of
information</h6>
</div>
<!-- Card Body -->
<div class="card-body">
<div class="chart-pie pt-4 pb-2">
<!-- Start Table -->
<div id="table_div" style="width:100%"></div>
<!-- End Table
<canvas id="myPieChart"></canvas> -->
</div>
</div>
</div>
</div>
</div>
<!-- Content Row -->
<div class="row">
<!-- Content Column -->
<div class="col-lg-6 mb-4" id="divActivity">
<!-- Recent Activity -->
<div class="card shadow mb-4">
<div class="card-header py-3 d-flex flex-row align-
items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-primary">Recent
Activities</h6>
<div class="dropdown no-arrow">
<a class="dropdown-toggle" href="#" role="button"
id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-
expanded="false">
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 72 of 84
No Task
<i class="fas fa-ellipsis-v fa-sm fa-fw text-gray-
400"></i>
</a>
<div class="dropdown-menu dropdown-menu-right shadow
animated--fade-in" aria-labelledby="dropdownMenuLink">
<div class="dropdown-header">Actions:</div>
<a class="dropdown-item" id="buttonloadchart"
onclick="loadChart()" href="#">Refresh table</a>
</div>
</div>
</div>
<div class="card-body">
<!-- Start Table -->
<div id="table2_div" style="width:100%"></div>
<!-- End Table -->
</div>
</div>
</div>
<!-- Content Column -->
<div class="col-lg-6 mb-4" id="divStream">
<!-- Live Stream -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Live
Stream</h6>
</div>
<div class="card-body">
<img class="img-fluid px-3 px-sm-4 mt-3 mb-4"
height="1200" width="800" src="{{ url_for('video_feed') }}">
</div>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
<!-- Footer -->
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 73 of 84
No Task
<footer class="sticky-footer bg-white">
<div class="container my-auto">
<div class="copyright text-center my-auto">
<span>Copyright © JTTQE | Smart Home 2019</span>
</div>
</div>
</footer>
<!-- End of Footer -->
</div>
<!-- End of Content Wrapper -->
</div>
<!-- End of Page Wrapper -->
<!-- Scroll to Top Button-->
<a class="scroll-to-top rounded" href="#page-top">
<i class="fas fa-angle-up"></i>
</a>
<!-- Logout Modal-->
<div class="modal fade" id="logoutModal" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add a new RFID
Record</h5>
<button class="close" type="button" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Input your name below and select OK to register a new card
<div><input type="text" id="rfidname" class="form-control bg-light
border-0 small" placeholder="Your name.."></div>
<p id="rfidscan" hidden="true">Please scan your new card on the card
reader</p>
</div>
<div class="modal-footer">
<a class="btn btn-primary" href="#"
onclick="addRFID();">OK</a>
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 74 of 84
No Task
<button class="btn btn-secondary" type="button" data-
dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
<!-- Core plugin JavaScript-->
<script src="../static/vendor/jquery-
easing/jquery.easing.min.js"></script>
<!-- Custom scripts for all pages-->
<script src="../static/js/sb-admin-2.min.js"></script>
</body>
</html>
B. img.html
No Task
D. • Create a new HTML file under the templates folder and name it img.html
E. • Copy and paste the code below to templates/img.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1,
shrink-to-fit=no">
<meta name="description" content="">
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 75 of 84
No Task
<meta name="author" content="">
<title>JTTQE Smart Home - Dashboard</title>
<!-- Custom fonts for this template-->
<link href="../static/vendor/fontawesome-free/css/all.min.css"
rel="stylesheet" type="text/css">
<link
href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,4
00,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">
<!-- Custom styles for this template-->
<link href="../static/css/sb-admin-2.min.css" rel="stylesheet">
<!-- Bootstrap core JavaScript-->
<script src="../static/vendor/jquery/jquery.min.js"></script>
<script
src="../static/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Google chart JavaScript-->
<script type="text/javascript"
src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="../static/js/gchart.js"></script>
<!-- Export SQL JavaScript-->
<script type="text/javascript"
src="../static/js/exporttoexcel.js"></script>
<!-- Page level custom scripts -->
<script type="text/javascript" src="../static/js/s3.js"></script>
</head>
<body id="page-top">
<!-- Page Wrapper -->
<div id="wrapper">
<!-- Sidebar -->
<ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark
accordion" id="accordionSidebar">
<!-- Sidebar - Brand -->
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 76 of 84
No Task
<a class="sidebar-brand d-flex align-items-center justify-content-
center" href="/"">
<div class="sidebar-brand-icon rotate-n-15">
<i class="fas fa-laugh-wink"></i>
</div>
<div class="sidebar-brand-text mx-3">JQTTQE Smart Home</div>
</a>
<!-- Divider -->
<hr class="sidebar-divider my-0">
<!-- Nav Item - Dashboard -->
<li class="nav-item active">
<a class="nav-link" href="/">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Dashboard</span></a>
</li>
<!-- Divider -->
<hr class="sidebar-divider">
<!-- Heading -->
<div class="sidebar-heading">
Interface
</div>
<!-- Nav Item - Modules Collapse Menu -->
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse"
data-target="#collapseTwo" aria-expanded="true" aria-
controls="collapseTwo">
<i class="fas fa-fw fa-cog"></i>
<span>Modules</span>
</a>
<div id="collapseTwo" class="collapse" aria-
labelledby="headingTwo" data-parent="#accordionSidebar">
<div class="bg-white py-2 collapse-inner rounded">
<h6 class="collapse-header">Add Module:</h6>
<a class="collapse-item" href="#" id="addLight">Lights</a>
<a class="collapse-item" href="#" data-toggle="modal" data-
target="#logoutModal">RFID Cards</a>
</div>
</div>
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 77 of 84
No Task
</li>
<!-- Nav Item - Images Collapse Menu -->
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse"
data-target="#collapsePages" aria-expanded="true" aria-
controls="collapsePages">
<i class="fas fa-fw fa-folder"></i>
<span>Images</span>
</a>
<div id="collapsePages" class="collapse" aria-
labelledby="headingPages" data-parent="#accordionSidebar">
<div class="bg-white py-2 collapse-inner rounded">
<h6 class="collapse-header">View:</h6>
<a class="collapse-item" href="#"
onclick="showS3()">Images</a>
</div>
</div>
</li>
<!-- Divider -->
<hr class="sidebar-divider">
<!-- Sidebar Toggler (Sidebar) -->
<div class="text-center d-none d-md-inline">
<button class="rounded-circle border-0"
id="sidebarToggle"></button>
</div>
</ul>
<!-- End of Sidebar -->
<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">
<!-- Main Content -->
<div id="content">
<!-- Topbar -->
<nav class="navbar navbar-expand navbar-light bg-white topbar
mb-4 static-top shadow">
<!-- Sidebar Toggle (Topbar) -->
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 78 of 84
No Task
<button id="sidebarToggleTop" class="btn btn-link d-md-none
rounded-circle mr-3">
<i class="fa fa-bars"></i>
</button>
<!-- Topbar Search -->
<form class="d-none d-sm-inline-block form-inline mr-auto ml-
md-3 my-2 my-md-0 mw-100 navbar-search">
<div class="input-group">
<input type="text" id="txtSearch" class="form-control bg-
light border-0 small" placeholder="Search for..." aria-label="Search"
aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-primary" type="button"
onclick="searchFunc()">
<i class="fas fa-search fa-sm"></i>
</button>
</div>
</div>
</form>
<!-- Topbar Navbar -->
<ul class="navbar-nav ml-auto">
<!-- Nav Item - Search Dropdown (Visible Only XS) -->
<li class="nav-item dropdown no-arrow d-sm-none">
<a class="nav-link dropdown-toggle" href="#"
id="searchDropdown" role="button" data-toggle="dropdown" aria-
haspopup="true" aria-expanded="false">
<i class="fas fa-search fa-fw"></i>
</a>
<!-- Dropdown - Messages -->
<div class="dropdown-menu dropdown-menu-right p-3 shadow
animated--grow-in" aria-labelledby="searchDropdown">
<form class="form-inline mr-auto w-100 navbar-search">
<div class="input-group">
<input type="text" class="form-control bg-light
border-0 small" placeholder="Search for..." aria-label="Search" aria-
describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-primary" type="button">
<i class="fas fa-search fa-sm"></i>
</button>
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 79 of 84
No Task
</div>
</div>
</form>
</div>
</li>
<div class="topbar-divider d-none d-sm-block"></div>
<!-- Nav Item - User Information -->
<li class="nav-item dropdown no-arrow">
<a class="nav-link dropdown-toggle" href="#"
id="userDropdown" role="button" data-toggle="dropdown" aria-
haspopup="true" aria-expanded="false">
<span class="mr-2 d-none d-lg-inline text-gray-600
small">
Sarah Tan
</span>
<img class="img-profile rounded-circle"
src="https://source.unsplash.com/QAB-WJcbgJk/60x60">
</a>
</li>
</ul>
</nav>
<!-- End of Topbar -->
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-
between mb-4">
<h1 class="h3 mb-0 text-gray-800">View Images</h1>
</div>
<!-- Content Row -->
<div class="row" id="s3pics">
<!-- Content Column -->
<div class="col-lg-6 mb-4" id="divActivity">
<!-- Recent Activity -->
<div class="card shadow mb-4">
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 80 of 84
No Task
<div class="card-header py-3 d-flex flex-row align-
items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-
primary">Pictures</h6>
</div>
<div class="card-body">
<div id="s3img"></div>
</div>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
<!-- Footer -->
<footer class="sticky-footer bg-white">
<div class="container my-auto">
<div class="copyright text-center my-auto">
<span>Copyright © JTTQE | Smart Home 2019</span>
</div>
</div>
</footer>
<!-- End of Footer -->
</div>
<!-- End of Content Wrapper -->
</div>
<!-- End of Page Wrapper -->
<!-- Scroll to Top Button-->
<a class="scroll-to-top rounded" href="#page-top">
<i class="fas fa-angle-up"></i>
</a>
<!-- Logout Modal-->
<div class="modal fade" id="logoutModal" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 81 of 84
No Task
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add a new RFID
Record</h5>
<button class="close" type="button" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Input your name below and select OK to register a new card
<div><input type="text" id="rfidname" class="form-control bg-light
border-0 small" placeholder="Your name.."></div>
<p id="rfidscan" hidden="true">Please scan your new card on the card
reader</p>
</div>
<div class="modal-footer">
<a class="btn btn-primary" href="#"
onclick="addRFID();">OK</a>
<button class="btn btn-secondary" type="button" data-
dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
<!-- Core plugin JavaScript-->
<script src="../static/vendor/jquery-
easing/jquery.easing.min.js"></script>
<!-- Custom scripts for all pages-->
<script src="../static/js/sb-admin-2.min.js"></script>
</body>
</html>
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 82 of 84
Section 14
Move files over to the Raspberry Pi and run them
A. Transfer files to RPI
a) • Using Filezilla, transfer pi2_one.py,
MFRC522.py, pi2_two.py and the 4 certs
download in 4C to your Raspberry Pi 1
~/labs/ca2
• Using Filezilla, transfer pi1_one.py,
pi1_two.py, base_camera.py, dynamodb.py,
jsonconverter.py, MFRC522.py, picam.py,
templates folder, static folder and the 4 certs
download in 4C to your Raspberry Pi 2
~/labs/ca2
B. Run the programs
No. Task
a) Ensure you are still in the ~/labs/ca2 directory.
cd ~/labs/ca2
b) Run the server.py file
sudo python pi1_one.py file
c) Run the updateUserLog.py
d) Run the local.py
sudo python pi1_two.py file
sudo python pi2_one.py
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 83 of 84
No. Task
e) Run the teleLight.py
f) Press Ctrl-Z to terminate the program
Section 15
View your Smart Home Dashboard
A. View dashboard(index.html) in browser
Task
You are now ready to viewy our webpage.
Type the following code in your laptop’s browser
(x.x.x.x refers to your RPI’s IP address, e.g. 10.10.10.200)
http://x.x.x.x:5000
You should see your home dashboard in your browser
sudo python pi2_two.py
<Jasper Tan, Tong Qian En > ST0324 Internet of Things CA2 Step-by-step Tutorial
Created by Dora Page 84 of 84
Task
-- End of CA2 Step-by-step tutorial --
Recent activites of users
Live stream of house
Real time temperature and icon will change depending on the temperature Control light status
Table and graph of motion trips
Add new rfid or light
Download excel file of motion trips
Search for specfic modules