$30 off During Our Annual Pro Sale. View Details »

DIY: Home monitoring with Rails & RaspberryPi

DIY: Home monitoring with Rails & RaspberryPi

In a world we teach ourselves a lot of things, why not do it Ruby-on-Rails-on-Pi way? Let’s get our hands on a piece of hardware, understand what it takes to setup one up, install a couple of libraries, Ruby, RVM, RBENV, a GUI. Let’s get a couple of sensors: motion, temperature, humidity, tsunami, ectoplasm and see if we detect a cool thing or two. Let's discuss how we can use what we know about Rails to explore things on a RaspberryPi. From a practical point of view, we will go through installation, setup, and build a monitoring Motion Sensor application on a RaspberryPi.

Helio Cola

April 25, 2017
Tweet

More Decks by Helio Cola

Other Decks in Programming

Transcript

  1. RailsConf 2017 - Phoenix, AZ
    Helio Cola
    @hacrods
    DIY: Home monitoring
    with Ruby & Pi

    View Slide

  2. RailsConf 2017 - Phoenix, AZ
    hac-rods.me
    Hi, I am Helio Cola!
    • ~16 years developing SW
    • ~7 years since I found happiness
    • ~6 years full time happiness
    • I go by @hacrods on the internet…

    View Slide

  3. RailsConf 2017 - Phoenix, AZ
    Agenda
    • OS installation
    • Why Raspberry Pi
    • What & Where to buy
    • Motion Sensor app
    • List of Sensors
    • Building Blocks
    • Exercises

    View Slide

  4. RailsConf 2017 - Phoenix, AZ
    Exercises
    • OS installation: Raspbian
    • Capture images
    • Capture videos
    • Motion & Digital touch sensor
    • Buzzer Lite & Buzzer
    • Detect presence & record a video
    • Other sensors

    View Slide

  5. RailsConf 2017 - Phoenix, AZ
    References
    • This presentation: https://speakerdeck.com/heliocola
    • Repo with building blocks: https://github.com/heliocola/rpi-bb

    View Slide

  6. RailsConf 2017 - Phoenix, AZ
    OS installation

    View Slide

  7. RailsConf 2017 - Phoenix, AZ
    OS installation
    Options
    • Raspbian
    • Ubuntu Core
    • Ubuntu Mate
    • Others: like Windows IOT

    View Slide

  8. RailsConf 2017 - Phoenix, AZ
    OS installation
    Steps for Raspbian:
    • Format SDHC
    SD formatter tool: https://www.sdcard.org/downloads/formatter_4/
    • Copy NOOBS to the SDHC
    Download link: https://downloads.raspberrypi.org/NOOBS_latest
    • Put SDHC in the PI, power up, and follow the instructions
    Instructions can be found: /INSTRUCTIONS-README.txt
    So, let’s do that!!! :)

    View Slide

  9. RailsConf 2017 - Phoenix, AZ
    OS installation
    References for Ubuntu Core/Mate/Windows
    • Ubuntu Core: https://developer.ubuntu.com/core/get-started/raspberry-pi-2-3
    • Ubuntu Mate: https://ubuntu-mate.org/download/
    • Windows: http://ms-iot.github.io/content/en-US/Downloads.htm
    * If you are interested in one of these OSes, I have some more info I can share: @hacrods

    View Slide

  10. RailsConf 2017 - Phoenix, AZ
    OS setup (raspbian)
    Setup steps:
    1. Turn off bluetooth
    2. Change keyboard to US
    3. Enable SSH & Camera
    4. Install MP4 Video format:
    $ sudo apt-get install -y gpac
    5. Set static IP address:
    5.1. sudo vi /etc/dhcpcd.conf
    5.2. Paste at the end
    interface wlan0
    static ip_address=192.168.1.205/24
    static routers=192.168.1.1
    static domain_name_servers=192.168.1.1

    View Slide

  11. RailsConf 2017 - Phoenix, AZ
    Why Raspberry Pi

    View Slide

  12. RailsConf 2017 - Phoenix, AZ
    Why RaspberryPi & Parallella
    • Because of Ray Hightower (@RayHightower), Julian Cheal
    (@juliancheal), and Chris (@crsexton) and Leah Sexton
    • Because they put a curiosity bug on my head
    • Because I was bored
    • And I’ve been doing some renovations at my house

    View Slide

  13. RailsConf 2017 - Phoenix, AZ
    What & Where to buy

    View Slide

  14. RailsConf 2017 - Phoenix, AZ
    What to buy
    • Depend of what you are trying to do
    • The basics:
    Starter kit (Pi 3, case, power supply, heatsink) and Micro SDHC
    • Extras:
    Camera, sensors, 7” Touch screen display
    Other gadgets

    View Slide

  15. RailsConf 2017 - Phoenix, AZ
    What to buy: RaspberryPi
    RaspberryPi: https://www.raspberrypi.org/
    • Raspberry Pi Zero W
    • Raspberry Pi 1 Model A+
    • Raspberry Pi 2 Model B
    • Raspberry Pi 3 Model B

    View Slide

  16. RailsConf 2017 - Phoenix, AZ
    Pi Zero
    Pi 1 A+
    Pi 2 B Pi 3 B

    View Slide

  17. RailsConf 2017 - Phoenix, AZ
    Pi Zero vs Quarter

    View Slide

  18. RailsConf 2017 - Phoenix, AZ
    Pi 3 Case vs Quarter

    View Slide

  19. RailsConf 2017 - Phoenix, AZ
    Differences between the Pi’
    CPU RAM Display USB Others
    Pi Zero
    W
    1Ghz,
    Single-core CPU
    512MB RAM
    CSI (v1.2 only)
    and Mini HDMI
    USB On-The-Go
    ports
    Half the size of a
    Model A+, with
    twice the utility
    Pi 1 A+ 700Mhz ARM CPU 512MB RAM HDMI
    One USB 2.0
    Port
    No Ethernet
    Low power 0.5W
    and 1W
    Pi 2 B
    A 900MHz quad-
    core ARM Cortex-
    A7 CPU
    1GB RAM
    CSI, DSI, Full
    HDMI
    4 USB 2.0 ports Ethernet
    Pi 3 B
    1.2GHz 64-bit
    quad-core ARMv8
    CPU
    1GB RAM
    CSI, DSI, Full
    HDMI
    4 USB ports
    Ethernet,
    802.11n,
    Bluetooth & BLE

    View Slide

  20. RailsConf 2017 - Phoenix, AZ
    Differences between the Pi’
    Price Dimensions GPIO
    Pi Zero W $10
    65mm long x 30mm wide x
    5mm thick
    HAT-compatible 40-pin
    header
    Pi 1 A+ $24.95 65mm x 56mm 40 GPIO pins
    Pi 2 B $39.95 40 GPIO pins
    Pi 3 B $39.95 86.9mm x 58.5mm x 19.1mm 40 GPIO pins

    View Slide

  21. RailsConf 2017 - Phoenix, AZ
    What to buy: Parallella
    • Parallella: https://www.parallella.org/
    Parallella Microserver (P1600)
    Parallella Desktop (P1601)
    Parallella Embedded (P1602)

    View Slide

  22. RailsConf 2017 - Phoenix, AZ
    The differences
    CPU Price RAM HDMI USB GPIO
    Microserver
    18 cores
    Zynq Z7010
    667 MHz
    (max 1 GHz)
    $99 1GB RAM No 1 port No
    Desktop
    18 cores
    Zynq Z7010
    667 MHz
    (max 1 GHz)
    $149 1GB RAM Yes 1 port
    24
    ports
    Embedded
    18 cores
    Zynq Z7020
    667 MHz
    (max 1 GHz)
    $264.94 1GB RAM Yes 1 port
    48
    ports

    View Slide

  23. RailsConf 2017 - Phoenix, AZ
    Buy: where
    • Micro center near you
    • else if you are near Element14: buy it
    • else if you are near Adafruit: buy it
    • else (if) you are near Amazon: buy it

    View Slide

  24. RailsConf 2017 - Phoenix, AZ
    Pi 3: On a TV

    View Slide

  25. RailsConf 2017 - Phoenix, AZ
    Pi 3: On a touch screen

    View Slide

  26. RailsConf 2017 - Phoenix, AZ
    Motion Sensor App

    View Slide

  27. RailsConf 2017 - Phoenix, AZ
    Motion Sensor App
    • My Hello World for RaspberryPi
    • Detect “movement”
    • Do something:
    • Call an API
    • Sound an alarm
    • Turn on the lights and play a music
    • Turn on the Camera & record it until
    no more presence (or 5-10s)
    • Report event + video

    View Slide

  28. RailsConf 2017 - Phoenix, AZ
    Motion Sensor App
    Pi 0 W: front door
    Pi 0 W: back door
    Pi 0 W: game room Or Heroku, AWS,
    Digital Ocean, etc
    Pi 3: local server

    View Slide

  29. RailsConf 2017 - Phoenix, AZ
    Motion Sensor App
    • Ruby as usual: rvm, ruby, rack, rails, rails-api, postgres, etc.
    Data model

    View Slide

  30. RailsConf 2017 - Phoenix, AZ
    PIR Motion Sensor

    View Slide

  31. RailsConf 2017 - Phoenix, AZ
    PIR Motion Sensor
    • Reference: https://cdn-learn.adafruit.com/downloads/pdf/pir-passive-infrared-proximity-motion-sensor.pdf
    Passive infrared
    Pyroelectric
    IR motion

    View Slide

  32. RailsConf 2017 - Phoenix, AZ
    Monitoring a PIR sensor
    Reference: https://github.com/jwhitehorn/pi_piper

    View Slide

  33. RailsConf 2017 - Phoenix, AZ
    List of Sensors

    View Slide

  34. RailsConf 2017 - Phoenix, AZ
    PIR Motion Sensor

    View Slide

  35. RailsConf 2017 - Phoenix, AZ
    Smoke and
    Combustible
    Gas

    View Slide

  36. RailsConf 2017 - Phoenix, AZ
    Buzzer

    View Slide

  37. RailsConf 2017 - Phoenix, AZ
    Carbon
    Monoxide

    View Slide

  38. RailsConf 2017 - Phoenix, AZ
    Digital
    touch

    View Slide

  39. RailsConf 2017 - Phoenix, AZ
    Flame

    View Slide

  40. RailsConf 2017 - Phoenix, AZ
    Vibration

    View Slide

  41. RailsConf 2017 - Phoenix, AZ
    Temperature
    and Humidity

    View Slide

  42. RailsConf 2017 - Phoenix, AZ
    Sound

    View Slide

  43. RailsConf 2017 - Phoenix, AZ
    Water level

    View Slide

  44. RailsConf 2017 - Phoenix, AZ
    Exercises

    View Slide

  45. RailsConf 2017 - Phoenix, AZ
    Exercises: building blocks
    • General Purpose Input and Output pins: aka GPIO
    • (binary) raspistill: capture photos
    • (binary) raspivid: capture videos
    • (gem) pi_piper: read and write from/to GPIO
    Reference: https://github.com/heliocola/rpi-bb

    View Slide

  46. RailsConf 2017 - Phoenix, AZ
    Building block: GPIO
    • It is a generic pin in the raspberry board that allows you to control and integrate with the
    outside world at run time.
    • Read something from the world
    • Or Write something to the world
    Reference: https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/README.md

    View Slide

  47. RailsConf 2017 - Phoenix, AZ
    Building block: GPIO
    • Reference: http://blog.mcmelectronics.com/
    post/Raspberry-Pi-3-GPIO-Pin-Layout#.WP5-
    u1PyslU

    View Slide

  48. RailsConf 2017 - Phoenix, AZ
    Building block: raspistill
    • Reference: https://www.raspberrypi.org/documentation/usage/camera/raspicam/raspistill.md

    View Slide

  49. RailsConf 2017 - Phoenix, AZ
    Building block: raspivid
    • Reference: https://www.raspberrypi.org/documentation/usage/camera/raspicam/raspivid.md
    MP4Box:
    Install: $ sudo apt-get install -y gpac
    Run: $ MP4Box -add pivideo.h264
    pivideo.mp4

    View Slide

  50. RailsConf 2017 - Phoenix, AZ
    Building block: pi_piper
    • Reference: https://github.com/jwhitehorn/pi_piper
    Read from a GPIO

    View Slide

  51. RailsConf 2017 - Phoenix, AZ
    Building block: pi_piper
    • Reference: https://github.com/jwhitehorn/pi_piper
    Write to a GPIO

    View Slide

  52. RailsConf 2017 - Phoenix, AZ
    Army of Pi Zero W & a Pi3
    • Pi Zero 1.3 (raspberry):
    ssh [email protected]
    • Pi 3 (raspberry): monitor/mouse/keyboard
    ssh [email protected]
    • List of Pi Zero's (W): raspberry
    ssh [email protected]
    ssh [email protected]
    ssh [email protected]
    ssh [email protected]
    ssh [email protected]
    ssh [email protected] (case)

    View Slide

  53. RailsConf 2017 - Phoenix, AZ
    Rules for accessing the Pis
    • Create folder with your name
    • If there is already one, add _1, _2, etc
    • Do not delete folder that you haven’t created
    • The Pi’s don’t have access to the internet

    View Slide

  54. RailsConf 2017 - Phoenix, AZ
    Army of Pi Zero W & a Pi3
    Wifi to access the Raspberry Pi’s:
    SSID: RailsConf2017_Pi
    Security: WPA/WPA2 Personal
    Passphrase: raspberrypi2017

    View Slide

  55. RailsConf 2017 - Phoenix, AZ
    Exercise: capture a photo
    Use raspistill utility to capture a photo from the camera
    • Building block:
    Raspistill
    • Reference: https://www.raspberrypi.org/documentation/usage/camera/raspicam/raspistill.md

    View Slide

  56. RailsConf 2017 - Phoenix, AZ
    Exercise: capture a video
    Use raspivid utility to capture a video from the camera
    Limit the video to 5 seconds as the test Raspberry PI only have 32Gb micro disk
    • Building block:
    Raspivid
    • Reference: https://www.raspberrypi.org/documentation/usage/camera/raspicam/raspivid.md

    View Slide

  57. RailsConf 2017 - Phoenix, AZ
    Exercise: Motion & Digital touch sensor
    Use the motion (PIR) or digital touch sensor to trigger a presence event
    • Building block:
    GPIO
    Use GPIO 4 to read input sensor
    • Reference: https://github.com/jwhitehorn/pi_piper

    View Slide

  58. RailsConf 2017 - Phoenix, AZ
    Exercise: Lite Buzzer & Buzzer
    Lite Buzzer: turn on and off the buzzer by writing to a GPIO
    Buzzer: read value from motion (or digital touch) sensor and turn on/off the buzzer
    • Building block
    GPIO
    Use GPIO 4 to read from a sensor
    Use GPIO 17 to write to the buzzer
    • Reference: https://github.com/jwhitehorn/pi_piper

    View Slide

  59. RailsConf 2017 - Phoenix, AZ
    Exercise: Detect Presence & record a video
    When you detect a presence (motion or digital touch) start recording a video for a small period
    of time
    • Building blocks:
    Raspivid
    GPIO
    Use GPIO 4 to read from a sensor
    Use GPIO 17 to write to the buzzer

    View Slide

  60. RailsConf 2017 - Phoenix, AZ
    References
    https://github.com/heliocola/rpi-bb
    http://rayhightower.com/
    http://juliancheal.co.uk
    http://www.codeography.com/
    https://www.raspberrypi.org/
    https://www.parallella.org/
    https://developer.ubuntu.com/core/get-started/installation-medias
    https://ubuntu-mate.org/raspberry-pi/
    https://developer.ubuntu.com/core/get-started/raspberry-pi-2-3
    https://ubuntu-mate.org/download/
    http://ms-iot.github.io/content/en-US/Downloads.htm
    https://www.element14.com/community/docs/DOC-78156/l/raspberry-pi-7-touchscreen-display
    https://cdn-learn.adafruit.com/downloads/pdf/pir-passive-infrared-proximity-motion-sensor.pdf
    https://github.com/jwhitehorn/pi_piper
    http://bit.ly/k8s-rpi

    View Slide

  61. RailsConf 2017 - Phoenix, AZ
    http://bit.ly/k8s-rpi
    Helio Cola
    @hacrods
    Thank you!
    Any questions?

    View Slide