Difference between revisions of "Nest Model"

From ESE205 Wiki
Jump to navigation Jump to search
Line 178: Line 178:
 
==== Sensors & Hardware ====
 
==== Sensors & Hardware ====
 
<br>Sensors to Transmit Data: We decided to add sensors to collect data that can be displayed on the main web page. For our project, we were only able to add a temperature and light sensor, but many more features could be added in the future. We had to wire an AD converter into the Raspberry Pi because it does not have one built in. The AD converter reads a voltage from the device and outputs a meaningful value.  
 
<br>Sensors to Transmit Data: We decided to add sensors to collect data that can be displayed on the main web page. For our project, we were only able to add a temperature and light sensor, but many more features could be added in the future. We had to wire an AD converter into the Raspberry Pi because it does not have one built in. The AD converter reads a voltage from the device and outputs a meaningful value.  
*insert fritz diagram of MCP3008 and sensors/pi*
 
  
 
<gallery>
 
<gallery>

Revision as of 20:41, 26 April 2019

Overview

Our project is to create a smart outlet consisting of a raspberry pi zero, a relay, and a container that works between an AC outlet and a device. We'd like to be able to communicate between a web interface and the outlet to be able to turn the device on and off. We would also like to add timing control features through the web interface. If we are successful with the initial project, we will add additional modules to the web interface to be able to control multiple devices.

Team Members

Amanda Hua
Tricia Brown
TA: Keith Kamons
Professor: James Feher

Links

[View Project Log]
[View Github]
[View Preliminary Presentation]
[Tutorial]
[Poster]

Objectives and Goals

Build a module that can relay between an AC outlet and device that we want to control
Build a web interface that successfully communicates with the built module in at least a binary fashion
Set up a server to host the web interface

Challenges

  • A majority of our programming expertise is in Java, and learning more about web development languages, especially Python, will be time consuming.
  • We have minimal background in hardware and circuitry, so a big challenge will be ensuring that we will account for all of the details necessary for our module.
  • We have no background in web security, so we need to be very careful in allowing access to the devices and files that we will be using.
  • We have never worked with Raspberry Pi before.
  • Linux is an unfamiliar operating system.


Budget

Total: -----needs to be updated ----

Useful Resources

RasPi Resources

GPIO Diagram
Blink Code

Temp Sensor Resources

132 Assignment (More Context)
Spec Sheet

DevOps and Server Side Resources

Websockets Tutorial

Project Results

Gantt Chart

https://docs.google.com/spreadsheets/d/10Ftzdzjh8UOGXznYvhBb2OvnZ2Exib2vUMbfKiH339s/edit?usp=sharing
Media:GanttChart2-16.png

Design and Solution

Website

The website uses websockets to communicate with the raspberry pi hardware. The html buttons on the website will run a script on the pi to turn on/off the smart outlet.

On boot, the pi should run the server side websocket code(works on pi3 but not pi0, pi0 requires manual start, debugging). This starts the server that listens for emissions that are communicating with the IP address of the pi. The EC2 instance (continually running) connects to the server through a javascript script embedded in the website's html. This is kept track of by the Websocket Status shown at the bottom of the page. When connected, the client side (EC2) will send input from buttons on the page as messages to the pi, which interprets the incoming messages and converts them into instructions for its GPIO pins.

#SERVER SIDE SCRIPT server.py on Raspberry Pi
#! /usr/bin/python
import os.path 
import tornado.httpserver 
import tornado.websocket 
import tornado.ioloop 
import tornado.web 
import RPi.GPIO as GPIO

#Initialize Raspberry PI GPIO
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(16, GPIO.OUT) 
GPIO.setup(18, GPIO.OUT)
#Tornado Folder Paths
settings = dict(
	template_path = os.path.join(os.path.dirname(__file__), "templates"),
	static_path = os.path.join(os.path.dirname(__file__), "static")
	)
#Tornado server port
PORT = 8888 
class MainHandler(tornado.web.RequestHandler):
  def get(self):
     print "[HTTP](MainHandler) User Connected."
     self.render("index.html")
	
class WSHandler(tornado.websocket.WebSocketHandler):
  def check_origin(self, origin):
    return True

  def open(self):
    print '[WS] Connection was opened.'
 
  def on_message(self, message):
    print '[WS] Incoming message:', message
    if message == "on_outlet":
      GPIO.output(18, True)
    if message == "off_outlet":
      GPIO.output(18, False)
  def on_close(self):
    print '[WS] Connection was closed.' 

application = tornado.web.Application([
  (r'/', MainHandler),
  (r'/ws', WSHandler),
  ]) 
if __name__ == "__main__":
    try:
        http_server = tornado.httpserver.HTTPServer(application)
        http_server.listen(PORT)
        main_loop = tornado.ioloop.IOLoop.instance()
        print "Tornado Server started"
        main_loop.start()
    except:
        print "Exception triggered - Tornado Server stopped."
        GPIO.cleanup()
#End of Program
//CLIENT SIDE SCRIPT ws-client.js on EC2 instance
$(document).ready(function(){

        var WEBSOCKET_ROUTE = ":8888/ws";
        var PIIP = #INSERT PI IP ADDRESS HERE;

        if(window.location.protocol == "http:"){
            //localhost
            var ws = new WebSocket("ws://" + PIIP + WEBSOCKET_ROUTE);
	    console.log("http case");
            }
        else if(window.location.protocol == "https:"){
            //Dataplicity
	    console.log("in the https state");
            var ws = new WebSocket("wss://" + PIIP + WEBSOCKET_ROUTE);
            }

        ws.onopen = function(evt) {
            $("#ws-status").html("Connected");
            };

        ws.onmessage = function(evt) {
            };

        ws.onclose = function(evt) {
            $("#ws-status").html("Disconnected");
            };

        $("#outlet_on").click(function(){
            ws.send("on_outlet");
            });

        $("#outlet_off").click(function(){
            ws.send("off_outlet");
            });

      });

2-16-19Indexpage.png Figure 1. Rough design of website home page

Communicationflow.png Figure 2. Communication depicted between the AWS web interface and the user, the database with user data, and the pi module.

FSM.png Figure 3. Original plan for configuration of the website pages and link paths

Sensors & Hardware


Sensors to Transmit Data: We decided to add sensors to collect data that can be displayed on the main web page. For our project, we were only able to add a temperature and light sensor, but many more features could be added in the future. We had to wire an AD converter into the Raspberry Pi because it does not have one built in. The AD converter reads a voltage from the device and outputs a meaningful value.


Power Supply: Our device plugs directly into the wall, however a typical outlet supplies 120 volts. The Raspberry Pi can only handle 5 volts at a time so we had to include a relay to the module in order to separate the pi from the wall outlet voltage unless the device needs to be turned on. *need to add explanation about how the relay works*

  • insert relay circuit diagram*

Results and Solution

Software

Hardware