Overview

Skill Level: Any Skill Level

This is a school project.

Ingredients

Source code

https://github.com/leyondlee/LTD_SmartHome

 

Main server

  • Hardware - Any (AWS EC2, Virtual Machine, your laptop, Raspberry Pi, etc)
  • Operating System - Any (Preferably Ubuntu Server)
  • Applications:
    • python
    • python-pip
    • MySQL (optional if using other hosting solutions such as AWS RDS)
  • Python Libraries:
    • virtualenv
    • To be installed in virtual environment:
      • gevent
      • flask
      • flask-sqlalchemy
      • flask-wtf
      • flask-login
      • watson_developer_cloud
      • MySQL-python
      • AWSIoTPythonSDK
      • boto3
      • python-dateutil
      • flask-socketio

 

Camera server

  • Hardware - Any (AWS EC2, Virtual Machine, your laptop, Raspberry Pi, etc)
  • Operating System - Any (Preferably Ubuntu Server)
  • Applications:
    • python
    • python-pip
  • Python Libraries:
    • virtualenv
    • To be installed in virtual environment:
      • tornado
      • pillow
      • requests

 

Camera client

  • Hardware - Raspberry Pi 3
    • Additional Hardware:
      • 1x PiCamera
  • Operating System - Raspbian
  • Applications:
    • python
    • python-pip
  • Python Libraries:
    • Python Libraries

 

Sensor

  • Hardware - Raspberry Pi 3
    • Additional Hardware:
      • 1x Breadboard (with T-Cobbler)
      • 1x MCP3008
      • 1x Light Dependent Resistor (LDR) Sensor
      • 1x DHT11 Sensor
      • 1x PIR Motion Sensor
      • 1x 10kΩ Resistor
      • ~2x 220Ω Resistor
      • ~30x Jumper cables/wires
  • Operating System - Raspbian
  • Applications:
    • python
    • python-pip
  • Python Libraries:
    • AWSIoTPythonSDK
    • gpiozero
    • Adafruit_Python_DHT

 

Doorbell

  • Hardware - Raspberry Pi 3
    • Additional Hardware:
      • 1x Breadboard (with T-Cobbler)
      • 1x Buzzer
      • 1x Button
      • 1x 220Ω Resistor
      • ~20x Jumper cables/wires
  • Operating System - Raspbian
  • Applications:
    • python
    • python-pip
  • Python Libraries:
    • AWSIoTPythonSDK
    • gpiozero

 

LCD

  • Hardware - Raspberry Pi 3
    • Additional Hardware:
      • 1x Breadboard (with T-Cobbler)
      • 1x IC2 LCD Screen
      • 4x Jumper cables/wires
  • Operating System - Raspbian
  • Applications:
    • python
    • python-pip
  • Python Libraries:
    • AWSIoTPythonSDK
    • rpi-lcd

 

 

Step-by-step

  1. AWS and certificates setup

    AWS IoT

    1. Login to AWS and navigate to AWS IoT console.
    2. Register a device
      • On the navigation panel, click Registry to expand the choices, and then choose Things.
      • Click Register a thing or Create.
      • Enter a name and click Create thing.
    3. Create certificates
      • On the navigation panel, click Security to expand the choices, and then choose Certificates.
      • Click Create.
      • Click Create certificate for “One-click certificate creation (recommended)”.
      • Once the certificate is created, Download them (including RootCA), then click Activate.
      • Click Attach a policy then Create new policy.
      • For name, enter “Webapp”.
      • Under “Add statements” switch the mode to Advanced mode and enter the contents of the “webapp_iot_policy.txt” file.
      • Go back to Certificates and attach the policy.
      • Next, repeat the certification creation steps and create a new policy.
      • For name, enter “RPi”.
      • Under “Add statements” switch the mode to Advanced mode and enter the contents of the “rpi_iot_policy.txt” file.
      • Go back to Certificates and attach the policy.
    4. Place the certificates in the correct folders.
      • Note – Configure settings.ini
      • Certificate with Webapp policy:
        • server/aws
      • Certificate with RPi policy:
        • doorbell/aws
        • lcd/aws
        • sensor/aws

     

    AWS Lambda

    1. Login to AWS and navigate to AWS Lambda.
    2. In AWS Lambda Dashboard page, click Create function.
    3. For blueprint, click Author from scratch.
    4. For trigger, click Next.
    5. Under Basic information: 
      • Name – SensorData
      • Runtime – Node.js 6.10
    6. Under Lambda function code, enter the contents of the “sensordata_function.txt” file.
    7. Follow through till the end.
    8. Navigate to AWS IoT console.
    9. Click Rules, then click Create.
    10. Enter the following:
      • Name – SensorRule
      • Attribute – topic(2) as Room, Timestamp, DHT11, LDR
      • Topic filter – room/+/sensors
    11. Then click Add action and select Invoke a Lambda function passing the message data.
    12. Click Configure action and choose the function “SensorData”.
    13. Click Add action and then Create rule.

     

    AWS IAM

    • Lambda Role
      1. Login to AWS and navigate to AWS IAM console.
      2. On the navigation panel on the left, click on Policies.
      3. Then Create policy.
      4. Click Select, for “Create Your Own Policy” and enter the following:
        • Policy Name – Sensor_Lambda
        • For Policy Document, enter the contents of the “sensor_lambda_policy.txt” file.
      5. Click Create policy.
      6. Next, on the navigation panel on the left, click on Roles.
      7. Click on the role associated to the “SensorData” Lambda function.
      8. Click Attach Policy and select “Sensor_Lambda”.

     

    • Webapp user
      1. On the navigation panel on the left, click on Users.
      2. Click Add user.
      3. Enter the following:
        • Name – DynamoDB_Webapp
        • Access type – Programmatic access
      4. Click Next: Permissions.
      5. Select Attach existing policies directly, then click Create policy.
      6. Click Select, for “Create Your Own Policy” and enter the following:
        • Policy Name – Webapp_policy
        • For Policy Document, enter the contents of the “webapp_policy.txt” file.
      7. Click Create policy, then click Next: Review.
      8. Click Create user.
      9. Note down the “Access key ID” and “Secret access key” as it is needed in the server setup later.

     

    Server and Client certificates

    1. Download KeyStore Explorer
    2. Open KeyStore Explorer
    3. Click Create a new KeyStore.
      • Type – PKCS #12
    4. Click Tools -> Generate Key Pair.
      • Use the default values until “Name”.
      • For “Name”, specify your details.
      • Click OK.
      • For alias, use “rootca”
    5. In the main keystore page, right click on “rootca”. Sign -> Sign New Key Pair.
      • Follow the same steps as when creating “rootca”. Note: Do not use same common name
      • For alias, use “server”.
    6. Repeat step 5 to create a certificate for “server”.
      • For alias, use “client”.
    7. Export all certificates and private keys (excluding rootca).
      • Right click -> Export -> Export Certificate Chain.
        • Use the default values
      • Right click -> Export -> Export Private Key.
        • Private Key Type – OpenSSL
        • Encrypt
        • Encryption Algorithm PBE with 256 bit AES CBC
      • (Recommended) Rename the certificate and private key accordingly:
        • <type>_certificate.cer
        • <type>_private.key
    8. Place the certificates in the correct folders.
      • Note – Configure settings.ini
      • server/certs
        • rootca (certificate)
        • server (certificate and private key)
        • client (certificate and private key)
      • camera_server/certs
        • rootca (certificate)
        • server (certificate and private key)
      • camera_client/certs
        • rootca (certificate)
        • client (certificate and private key)
  2. Server setup

    MySQL Database

    • METHOD 1: Host locally on server
      1. Install MySQL-Server
        • sudo apt-get update
        • sudo apt-get install mysql-server
        • sudo mysql_secure_installation
      2. Move on to Configure Database.

     

    • METHOD 2: AWS RDS
      1. Sign in to the AWS Management Console and navigate to AWS RDS.
      2. In the top right corner of the Amazon RDS console, choose the region in which you want to create the DB instance.
      3. In the navigation pane, choose Instances.
      4. Choose Launch DB Instance. The Launch DB Instance Wizard opens on the Select Engine page.
      5. Choose MySQL and click Select.
      6. Configure the instance and follow through till the end.
      7. Move on to Configure Database.

     

    • Configure Database
      1. Connect to MySQL-Server
        • mysql -h <endpoint> -P <port> ‐u root -p
      2. Create database
        • mysql> CREATE DATABASE assignment;
      3. Create user for database
        • mysql> USE assignment;
        • mysql> CREATE USER ‘assignmentuser’@’localhost’ IDENTIFIED by ‘<NEW PASSWORD>’;
        • mysql> GRANT ALL PRIVILEGES ON assignment.* TO ‘assignmentuser’@’localhost’;
        • mysql> FLUSH PRIVILEGES;
      4. Exit MySQL console
        • mysql> quit

     

    Setup files

    • METHOD 1: RaspberryPi
      1. Access the RaspberryPi.
      2. Move on to Configure Files.

     

    • METHOD 2: AWS EC2
      1. Open the AWS EC2 console.
      2. From the console dashboard, click Launch Instance.
      3. Choose your desired image (preferably “Ubuntu Server”).
      4. Follow through till the end.
      5. Access the instance through SSH.
      6. Move on to Configure Files.

     

    • Configure files
      1. On the server, create a folder named “LTD_SmartHome”.
        • mkdir -p ~/LTD_SmartHome
      2. From the zip file, copy the folders “server” and “camera_server” (located within “source”) into the “LTD_SmartHome” folder.
      3. Configure settings.ini for “server” and “camera_server”

     

    Run servers

    1. Create virtual environments
      • server
        • cd ~/LTD_SmartHome/server
        • virtualenv env
        • env/bin/pip install -r requirements.txt
      • camera_server
        • cd ~/LTD_SmartHome/camera_server
        • virtualenv env
        • env/bin/pip install -r requirements.txt
    2. Start servers
      • server
        • cd ~/LTD_SmartHome/server
        • nohup env/bin/python run.py &
      • camera_server
        • cd ~/LTD_SmartHome/camera_server
        • nohup env/bin/python run.py &
    3. Additional information
      • Check processes
        • ps aux | grep python
      • Kill process
        • kill -9 <pid>
    4. Access servers
      • server
        • https://<server ip>:<server_port in “server”>
      • camera_server
        • https://<server ip>:<server_port in “camera_server”>
  3. RaspberryPi setup

    Camera client

    1. Create a folder named “LTD_SmartHome”.
      • mkdir ~/LTD_SmartHome
    2. Copy the folder “camera_client” (located within “source”) into “LTD_SmartHome” folder.
    3. Install python packages
      • cd ~/LTD_SmartHome/camera_client
      • pip install -r requirements.txt
    4. Configure settings.ini
    5. Start camera client script
      • cd ~/LTD_SmartHome/camera_client
      • python run.py

     

    Sensor

    1. Complete the Fritzing Diagram.
      diagram
    2. Create a folder named “LTD_SmartHome”.
      • mkdir ~/LTD_SmartHome
    3. Copy the folder “sensor” (located within “source”) into “LTD_SmartHome” folder.
    4. Install python packages
      • cd ~/LTD_SmartHome/sensor
      • pip install -r requirements.txt
    5. Configure settings.ini
    6. Start sensor script
      • cd ~/LTD_SmartHome/sensor
      • python run.py

     

    Doorbell

    1. Complete the Fritzing Diagram.
      diagram2
    2. Create a folder named “LTD_SmartHome”.
      • mkdir ~/LTD_SmartHome
    3. Copy the folder “doorbell” (located within “source”) into “LTD_SmartHome” folder.
    4. Install python packages
      • cd ~/LTD_SmartHome/doorbell
      • pip install -r requirements.txt
    5. Configure settings.ini
    6. Start doorbell script
      • cd ~/LTD_SmartHome/doorbell
      • python run.py

     

    LCD

    1. Complete the Fritzing Diagram.
      diagram2
    2. Create a folder named “LTD_SmartHome”.
      • mkdir ~/LTD_SmartHome
    3. Copy the folder “lcd” (located within “source”) into “LTD_SmartHome” folder.
    4. Install python packages
      • cd ~/LTD_SmartHome/lcd
      • pip install -r requirements.txt
    5. Configure settings.ini
    6. Start lcd script
      • cd ~/LTD_SmartHome/lcd
      • python run.py
  4. Additional information

    Server “manage.py”

    1. Access the main server
    2. cd ~/LTD_SmartHome/server
    3. env/bin/python manage.py
      • Options:
        • (1) Create User
        • (2) Delete User
        • (3) List Users
        • (4) Delete Sensor Table
        • (5) Delete Subscription Table
        • (q) Exit

     

    Recommended Setup

    • Ubuntu Server
      • Main server
      • Camera server

     

    • RaspberryPi – Sensor (at least 1)
      • Sensor
      • Camera client (only 1 required)

     

    • RaspberryPi
      • Doorbell
      • LCD

Join The Discussion