Miriam Lauter - Make your own Smart Air Conditioner

Miriam Lauter - Make your own Smart Air Conditioner

Looking for a fun, useful Raspberry Pi project? Want to connect your household appliances to the internet? Come learn how to build your own 'smart' air conditioner using a Raspberry Pi, a bit of hardware, and, of course, Python. Plus, you can save energy and never have to come home to a sweltering bedroom again.

https://us.pycon.org/2015/schedule/presentation/412/

D5710b3bca38f1233274b4cbc523dc4b?s=128

PyCon 2015

April 18, 2015
Tweet

Transcript

  1. MAKE YOUR OWN SMART AIR CONDITIONER Created by / Miriam

    Lauter @miriamlauter
  2. None
  3. None
  4. THE GOAL

  5. 0:17

  6. 0:20

  7. None
  8. OVERVIEW Getting Started Hardware A simple Flask application The code

    on the Pi Final Notes and Demo!
  9. OVERVIEW Getting Started What you'll need Resources Cost Hardware A

    simple Flask application The code on the Pi Final Notes and Demo!
  10. WHAT YOU'LL NEED

  11. ADAFRUIT.COM (Credit: Allie Brosh)

  12. AN AIR CONDITIONER window unit connects to AC power draws

    less than 15 amps you don't care about it that much
  13. A RASPBERRY PI (Photo: Raspberry Pi Foundation)

  14. EXTRA RASPBERRY PI COMPONENTS + (or whatever fits your R-Pi

    model) Micro USB charger wall adapter Micro SD card USB wifi (Photos: adafruit.com) mlauter.github.io/getting-started-with-raspberry-pi
  15. MOAR STUFF Male and female jumper wires Prototype PCB Paper

    Power Switch (relay) Digital temperature sensor 4.7K resistor
  16. TOOLS! Soldering iron and solder Very skinny phillips head screwdriver

    Hot glue gun
  17. FOR TESTING/DEBUGGING ONLY A breadboard An LED Multimeter - nice

    to have but not necessary
  18. None
  19. None
  20. OVERVIEW Getting Started Hardware Controlling the AC with the raspberry

    pi Measuring temperature with the raspberry pi A simple Flask application The code on the Pi Final Notes and Demo!
  21. TWO SIMPLE CIRCUITS

  22. Allow the Raspberry Pi to turn the air conditioner on

    and off #1 RASPBERRY PI | RELAY | AIR CONDITIONER
  23. RELAY

  24. CONNECT THE RASPBERRY PI Make sure the pi is not

    plugged in Find a GPIO (general purpose input output) pin Don't use GPIO4 (we'll need it later)
  25. None
  26. None
  27. None
  28. None
  29. WHILE YOU'RE TESTING

  30. Allow the Raspberry Pi to check temperature in your room

    #2 DIGITAL TEMPERATURE SENSOR
  31. None
  32. None
  33. None
  34. SOLDERED TO PCB PROTOTYPE

  35. None
  36. 0:14

  37. OVERVIEW Getting Started Hardware A simple Flask application Overview Intro

    to Flask 3 Routes Translating states The code on the Pi Final Notes and Demo!
  38. None
  39. APPLICATION REQUIREMENTS Raspberry Pi can: 1. Give information about room

    conditions 2. Receive instructions User can: 1. Receive info about room conditions 2. Give instructions Store information to be passed between User and R-Pi
  40. FLASK Flask is a microframework for Python based on Werkzeug,

    Jinja 2 and good intentions.
  41. FLASK RESOURCES and video series Miguel Grinberg's Flask Mega-Tutorial Real

    Python's Flask tutorial 'Discover Flask'
  42. FLASK BASICS $ mkdir app $ mkdir app/assets $ mkdir

    app/templates $ touch app.py $ cd app $ tree . ├── app.py ├── assets └── templates └── index.html
  43. APP.PY: HELLO WORLD from flask import Flask, render_template app =

    Flask(__name__) @app.route("/") def hello(): return render_template('index.html', name='Miriam') if __name__ == "__main__": app.run()
  44. @ROUTE DECORATORS @app.route("/") #route(rule, **options) def hello(): return render_template('index.html', name='Miriam')

    Add the following url rule: when there is a request from a url like my.site.com/ run the hello function. If you are testing you'll be running on localhost:5000
  45. JINJA TEMPLATES

  46. AN EXAMPLE TEMPLATE # index.html <html> <head> <title>Air conditioner remote</title>

    </head> <body> <h1>Hello World</h1> <div> Hello, {{name}} </div> </body> </html>
  47. None
  48. DEBUG MODE app.run(debug=True)

  49. 3 ENDPOINTS Homepage (GET) User Endpoint (POST) Pi Endpoint (POST)

  50. ENDPOINT 1: HOMEPAGE from flask import Flask, render_template app =

    Flask(__name__) @app.route('/', methods = ['GET']) @app.route('/index', methods = ['GET']) def homepage(): data = #Get stored room temp and AC state return render_template('index.html', data=data)
  51. None
  52. TRANSLATING STATES

  53. ENDPOINT 2: USER @app.route('/user', methods=['POST']) def user(): user_input = request.json

    current_temp = #Get most recently stored room temp desired_state = translate_state(user_input, current_temp) # Store the desired_state
  54. ENDPOINT 3: PI @app.route('/pi', methods=['POST']) def pi(): pi_input = request.json

    #store pi input #return ON or OFF return flask.jsonify(desired_state)
  55. OVERVIEW Getting Started Hardware A simple Flask application The code

    on the Pi RPi.GPIO Surviving unresponsive server Final Notes and Demo!
  56. THE CODE ON THE RASPBERRY PI 1. Make requests using

    the Pi endpoint 2. Set the switch pin high (on) or low (off) 3. Repeat
  57. MAKING REQUESTS

  58. import requests import json url = 'your.url.here/pi' data = {'info

    to send'} headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} r = requests.post(url, data=json.dumps(data), headers) # 0 or 1
  59. SETTING THE SWITCH PIN

  60. RPI.GPIO import RPi.GPIO as io io.setmode(io.BCM) switch_pin = 17 io.setup(switch_pin,

    io.OUT) io.output(switch_pin, False) def is_running(): return io.input(switch_pin)
  61. AND NOW FOR THE REAL MAGIC...

  62. def turn_on(): io.output('switch_pin', True) def turn_off(): io.output('switch_pin', False) r =

    requests.post(url, data=json.dumps(data), headers) # 0 or 1 if r == 1: turn_on() else: turn_off()
  63. NOW DO THIS OVER AND OVER AGAIN

  64. WHAT IF THE SERVER DOESN'T RESPOND?

  65. WHILE TRUE:

  66. None
  67. FINAL NOTES

  68. SECURITY Input validation Web form Raspberry Pi endpoint Login required

    decorators
  69. DEMO!

  70. None
  71. THE END - Blog: - Github: mlauter.github.io /mlauter