• Update 8 - Final Interface Screenshots

    Chris12/31/2017 at 13:31 0 comments

    Here are some pictures of the completed interface. It's all built with bootstrap, so it scales well to mobile screens.

  • Update 7 - Ruby on Rails Install and Configuration

    Chris12/29/2017 at 05:11 0 comments

    For the Ruby on Rails install there isn't really anything specific to a Raspberry Pi. I don’t need to copy something else's instructions here. Just follow steps 1-3 here to get started: http://parsun.com/2017/09/23/how-to-install-ruby-on-rails-on-raspberry-pi-3/

    I forget why, but I also needed to install the ruby development package so something could compile during the install. If things freak out just try

    sudo apt-get install ruby-dev 

    then continue.

    With that, you’re basically set except for some rails configuration. Download the repository by navigating to the folder you want the app stored in and running:

    git clone https://github.com/cmoulder/smartlok

     
    You will need to uncomment

    # gem 'rpi_gpio'

    in the gemfile if you are using a RPi. I’ve commented it out because it will only compile on a RPi, leading to one more problem for everyone else.

    Then run through the normal rails setup. Make sure you’re in the same folder you installed the site:

    bundle install
    rails db:migrate
    rails db:seed
    rails s 

    The development server should now be running on port 3000. Since we haven’t configured a firewall, we should be able to access this from another computer on the network with http://192.168.1.XX:3000, using whatever the local IP address is.

    For actual usage, you will definitely want to configure your rails serer to run development mode. Personally, this is a little annoying to set up manually (as opposed to Heroku that does everything for you), but there aren’t that many steps

    Start by making sure the server is shut down (Ctrl + C)

    For the server to start, you’ll need to make a secret key for both rails and devise. Best practice is to save these as environment variables and not hard code them in your code. I do no such thing, but if you wanted you could do something like this in your config file:

    config.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production? 


    Then set your environment variables, making sure you set them before we start rails during startup below.

    I hard code them by generating two secret keys with

    RAILS_ENV=production rake secret 

    in the console. One goes in config/secrets.yml as secret_key_base = … under the production section for rails, and the other goes in config/initializers/devise.rb as config.secret_key = ….

    More info on all of this is here: https://stackoverflow.com/questions/29187296/rails-production-how-to-set-secret-key-base

    Next, we you need to migrate the database to the production environment (more info here: https://stackoverflow.com/questions/1949229/change-a-rails-application-to-production). In the console:

    RAILS_ENV=production rake db:migrate
    RAILS_ENV=production rake db:seed

     
    To start your site in production your need to tell to tell rails to do so. I run it at boot by having rc.local run a script located in my home directory

    at the end of /etc/rc.local copy
    sudo sh /home/pi/run_server.sh

    sudo sh /home/pi/run_server.sh  


    and in /home/pi/run_server.sh copy

    echo 'Starting Rails'
    cd /home/pi/sites/smartlok && sudo rails s -e production -p 1234 

     
    The structure of the bottom line is 1. Navigate to my rails folder. 2. Start rails as a daemon in the production environment 3. Run it on port 1234 instead of a standard 80 (yours could default to 3000, but you should whatever port you want)

    In rc.local above my rails line I also have

    exec 2> /home/pi/rc.local.log # send stderr from rc.local to a log file
    exec 1>&2 # send stdout to the same log file
    set -x # tell sh to display commands before execution

     
    which will save all of the console messages at boot to a log file (for the inevitable debugging when something doesn’t work).

    I have already enable this for this site, but in general for all apps that you’re converting to production you either...

    Read more »

  • Update 6 - Headless Raspberry Pi Install

    Chris12/14/2017 at 20:45 0 comments

    A headless Raspberry Pi Zero W install isn’t terribly difficult, but I’m including a fairly comprehensive list of steps here because this process is always changing and many online guides are out of date.

    1. Download the latest Raspbian image (https://www.raspberrypi.org/downloads/raspbian/). I like the full image because it includes everything just in case I need something later. This build uses more memory and flash, but I’ve convinced myself it will save time later on if I need something. The clean image only uses abut 75MB of memory anyway, so the trade-off isn’t bad.
    2. Unzip the image. On Windows I use Etcher (https://etcher.io/) to write it my SD card. I use a 16GB card, but you can get away with 8GB. Same as above, I’m a fan of paying an extra $2 now to avoid any future space issues.
    3. Enable SSH by placing a blank file named “SSH” in the root directory of the boot partition. Before 2016 SSH was enabled by default. The boot partition is FAT32 formatted, so you will be no issues accessing the drive.
    4. Set your wireless settings (https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md). These are in the ext4 formatted partition (the big one that most stuff is stored on) which Windows cannot access on its own. I use the ext2fsd driver (http://www.ext2fsd.com/) for Windows to get around this. Always be sure to “flush cache to disk” and properly eject the SD card from Windows before taking the card out. Go to /wpa_supplicant/wpa_supplicant.conf and add to the bottom of the file
      network={
      ssid="YOUR SSID HERE"
      psk="YOUR PASSWORD HERE"
      }  
      You can have multiple sections like this if you will be moving your Pi to different networks. If you follow the link above, there are instruction to encrypt your password if you don’t want it saved in plain text.
    5. Once you pop the card in the Raspberry Pi, it should be on your wifi within 30 seconds. You can then SSH in with the default username: pi and default password: raspberry. I use Putty for this (and you should too) in Windows (http://www.putty.org/). The hostname raspberrypi will eventually show up on your network, but I normally find it faster to go to my router and find the IP address.
    6. For debugging, it’s always good to know you can start the RealVNC server to remotely view the desktop. Just run
      vncserver  
       to start the server. You can then use any VNC viewer (https://www.realvnc.com/en/connect/download/viewer/windows/) on another system to connect.
    7. I occasionally use X11 forwarding instead of VNC for remote viewing. On Windows you’ll need Xming (https://sourceforge.net/projects/xming/) installed and X11 forwarding enabled in Putty. In Putty the only change you need to make is to check the box in the settings at Connection -> SSH -> Enable X11 forwarding. You do not need to set any display locations. The standard Xming settings are fine. To start the desktop over SSH using Xming, use Putty to connect over SSH, then run
      lsession &
      For anyone very new to Linux the ‘&’ tells the system to run the previous command in the background, which lets you keep using you current SSH window. VNC generally performs better than X11 though.
    8. Once you’re in, you should perform a system update
      sudo apt-get update
      sudo apt-get dist-upgrade
    9. And at a minimum change your password
      passwd 
    10. That's it. Others will tell you to also create a new user account and enable a firewall. This really depends on your use case and if you’re going to maintain control of the device. If you’re behind a router, using it for a dedicated purpose, and no one else is touching your Pi, you’re probably fine.

  • Update 5 - More to Come

    Chris12/12/2017 at 18:06 0 comments

    Additional updates with instructions for a headless Raspberry Pi installation and setting up Rails are coming

  • Update 4 - Hardware

    Chris12/12/2017 at 17:58 0 comments

    The circuit here is straight forward. I’m using a basic MOSFET-as-a-digital-switch configuration like what is shown here (http://www.learningaboutelectronics.com/Articles/N-channel-MOSFET-switch-circuit.php). I have added a 10k Ohm resistor between the gate and ground to make sure it is pulled down when not activated. I also use 2 1000uF bypass capacitors on the power rails for good measure. This isn’t particularly necessary though because I’m using a separate power supply for the solenoids and the RPi. If I wanted to be particularly rigorous, I could put a few hundred ohm resistor between the RPi and the gate to prevent any weird oscillations due to the gate capacitance, but considering our switching speed is about 1Hz, as opposed to the 1Mhz these can operate at, I’m not concerned.

    We also need a ground connection between the RPi and the breakout boarding to make sure all parts of our circuit use the same ground reference.

    You’ll notice that I’m struggling super hard on the equipment here while I’m out of the states…only black, hand-cut jumpers on a black board that’s barely large enough and no socketing. Also, the mount to attach everything to the touchscreen is simply some cardboard I've glued together

    Since writing this, I've upgraded to a 12v power supply for the solenoids because you really need hammer on the touchscreen. 5v would be perfectly find for any modern capacitive touch screen or even a not crappy resistive touchscreen.

  • Update 3 - Web Interface

    Chris12/12/2017 at 17:32 0 comments

    The full feature set has expanded a little:

    Anti-Brute force (lock after 5 invalid attempts). Unlock button available when logged in

    Email notifications to the admin when a user attempts to entry

    Email notification to new users with their code and entry restrictions

    Geofencing

    Entry scheduling

    Access expiration date

    Entry count limit

    Logging for all attempted entries

    The email templates and smtp server info will have to be edited manually, as I have little interest in adding all of those fields into the website settings page.

    Side note: Sparkfun is out of the solenoids, but Digikey also stocks them for the same price from Sparkfun.

  • Update 2 - Web Interface

    Chris10/15/2017 at 16:46 0 comments

    The management interface is starting to take shape. Also occurred to me that I should add a geofencing option. Will probably just use the HTML5 location and call it a day.

  • Update 1 - A Start

    Chris10/13/2017 at 14:29 0 comments

    I am out of the country right now (and my parts supply is at home), so it will be the end of November before I have all the components necessary to finish. Nonetheless, most of the work here will be building a functional front end so the end result is more than just a hack.

    There general task here will be to build a Rails app that allows an administrator to create users, enforce scheduled access, and log all access. I will then use an additional Rails GEM to control the gpio pins on the RPi to actuate some solenoids to physically control the smart home system below:

    I will need two separate actuators because I need to press the "Intercom" button and then the "Open Door" button. I considered cheating here by pressing both locations with one actuator, but the system will only respond to one touchpoint.

    I'll have to MOSFETs between the RPi and the solenoids to keep anything from burning out...supposedly these solenoid can drawn up to 700mA. We also shouldn't have any issue with the 3.3 pins on the RPi triggering the MOSFETs.

    On the software side, I'll basically be running a stock Raspian image + Ruby on Rails + a Dynamic DNS Updater. I've never cared for setting these up with a screen and keyboard, so I'll plug in via USB directly and configure over SSH.

    On the coding front, I'm making liberal use of some generators from a prior app development course. Full info is at https://guides.firstdraft.com. I'm using a couple of tools here:

    1. "Starter" generators to create my basic controllers, views, and routes.
    2. https://ideas.firstdraft.com/ to develop my DB structure as below:

    3. Devise for the admin authentication.
    4. Bootstrap for the theme.