Smart Mirror complete walkthrough

Required Equipment:

Step 1: Setting up the Pi

Meteor:

The Raspberry Pi came with instructions on getting it up and running but to install the Meteor Platform I used for creating the app I had to follow this guide. Most other smart mirror tutorials don’t use Meteor so don’t have this step.

Chromium:

You need this web browser because it is easy to get it set up in “kiosk” (full screen) mode. It’s also useful since it has a javascript console so that made it easy to add documents into the Mongo DB. The link above was the best way I could find to get it installed. Next I needed to get Chromium to launch in kiosk mode when the Raspberry Pi boots up (without the spaces, wordpress won’t let me post that string of text for some reason):

 

sudo nano / etc / xdg / lxsession /  LXDE-pi / autostart

 

Disable the screen saver by commenting out the following line:

#screensaver -no-splash

Then add the following line to the end of the file:

@xset s off @xset -dpms @xset s noblank / usr / bin / chromium --noerrdialogs --kiosk --incognito "http://localhost:3000"

To get rid of the mouse cursor:

 sudo apt-get install unclutter

Rotating the display:

 sudo nano / boot / config.txt

At the bottom of this file add:

 DISPLAY_ROTATE=X

Where X is: 0, 1, 2, 3 corresponding to no rotation, 90 degrees, 180 degrees, 270 degrees of rotation.

Step 2: The application

The code for my application was based on the code from github user lambtron. The biggest change I made was to add the random compliment at the bottom of the mirror. The javascript to do this:


if (Meteor.isClient){
   var ONE_MIN = 1 * 60 * 1000;
   var chooseRandom = function() {
   var compliments = Compliments.find().fetch();
   var randomComp = Random.choice(compliments);
   Session.set('currentComp', randomComp);
};
Template.compliments.helpers({
   compliment: function() {
      var c = Compliments.findOne(Session.get('currentComp'));
      return c.text;
      }
});
Template.compliments.created = function() {
   this.handle = Meteor.setInterval(chooseRandom, ONE_MIN);
};
Template.compliments.destroyed = function() {
   Meteor.clearInterval(this.handle);
   };
}

I’ve been running the app locally on the raspberry pi and just pointing the browser to localhost:3000. I can update the app via ssh when I am at home on my local wifi but I don’t seem to be able to ssh to the pi from anywhere else. I’ve also had problems adding content to the database from a command prompt. I can however attach a mouse and keyboard and update the database through a javascript console in the browser.

So far this has been working well but I have since updated the app to include an admin panel and I could move it to a digital ocean droplet for $5/month which would give me better access to the database.

Motion sensor

I purchased a motion sensor from Lee’s Electronics in Vancouver for ~$15.

Connecting the sensor to the Pi was simple enough with some female-to-female connectors. The three wires plug into Pin 2 (5V), Pin 6 (ground) and Pin 7 (GPIO 4). GPIO 4 is then monitored by the Pi to detect motion. I followed an online tutorial for creating a Python script to detect motion and turn on/off the monitor. I made a few modifications to the script for my own purposes:

import sys

import time

import RPi.GPIO as io

import subprocess

io.setmode(io.BCM)

SHUTOFF_DELAY = 60 # seconds

PIR_PIN = 4

def main():

io.setup(PIR_PIN, io.IN)

io.setup(LED_PIN, io.OUT)

turned_off = False

last_motion_time = time.time()

while True:

if io.input(PIR_PIN):

last_motion_time = time.time()

sys.stdout.flush()

if turned_off:

turned_off = False

turn_on()

else:

if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY):

turned_off = True

turn_off()

if not turned_off and time.time() > (last_motion_time + 1):

io.output(LED_PIN, io.HIGH)

time.sleep(.1)

def turn_on():

subprocess.call(“sh /home/pi/mirror/monitor_on.sh”, shell=True)

def turn_off():

subprocess.call(“sh /home/pi/mirror/monitor_off.sh”, shell=True)

if __name__ == ‘__main__’: try: main() except KeyboardInterrupt:

io.cleanup()

SSH and a Monitor

I had been having problems setting up SSH on the Raspberrypi but then one day it started working. Sort of. I can ssh into the pi as long as I am at home on the same network. Trying from work or anywhere else and it times out. Oh well at least being able to get into the pi from my computer at home will allow me to update the app and reboot if needed. I also found a handy app that will let me ssh from my phone:

IMG_1665I also found a relatively cheap ($65) monitor from a local used computer part store called Free Geek.

IMG_1666Finally I also ordered the mirror online from Tap Plastics. I wasn’t able to find anywhere local that sold two-way mirrors. With the shipping and exchange rate the mirror turned out to be about $120. Once the mirror arrives I just need to build a frame.

 

Setting up the Pi

Over the last little while I have managed to get the Raspberry Pi setup and got Meteor installed and running. I moved the app over to the Pi and initially it ran but wasn’t updating the weather. I eventually realized that it was the Epiphany (Gnome) web browser that was the culprit. After a few failed attempts I was eventually able to get Chromium installed and viola the app worked perfectly. Using Chromium also solved another problem where I was unable to access the database from the console. Running meteor mongo just keeps giving me an error. At least with Chromium I can use the javascript console to edit the database.

I’ve also got the browser to autostart in kiosk mode with the localhost page but still need to get the meteor app to run automatically as well.

Mirror Website

I’ve managed to make a Meteor application that displays the current day, time, weather and a random compliment:

Screen Shot 2016-01-07 at 3.37.12 PMGetting the random compliment took the most time. I spent a lot of time trying to find out how to return a random document from the compliments collection I created. I found quite a few posts online but none seemed to work. I could return an object but it was not displaying the text of that object. In the end it was as simple as changing from find() to findOne().

Next I want to have it check the date and display custom messages such as “Happy Birthday”, “Happy Anniversary”, “Merry Christmas”, etc.

I also set up the Raspberry Pi this week. I want to see if I can get Meteor running on it. If so I can just have the application running locally on the Pi and point its browser to the localhost. I did find this walk-through online.  If not I can deploy it to Digital Ocean which may be the better choice in the end as I can update the database easily that way.

Raspberry Pi, Littlebits and Meteor

So I haven’t been teaching for the past few years due to lack of enrolment in continuing education (SD38 continuing ed has stopped offering all math and science based night school courses!) hence the lack of any new posts on this blog. Since I haven’t been teaching I started learning new skills, did some online Python tutorials and then got interested in Meteor. When I first started teaching full time in Powell River one of the classes I was given was Information Technology 11/12. Part of this was teaching basic HTML which I was comfortable with but the other part was Javascript which I wasn’t. It was a quick and steep learning curve. Since meteor is based on JS I thought it would be a good way to revive that forgotten skill set. So far it has been going well and I’ve even made a few apps.

Then for Christmas I was given a Rasperry Pi and some Littlebits. After looking through some possible projects I’ve decided to try and build a Magic Mirror. I can build the site using Meteor and it can run on the Pi and perhaps make use of some of the Littlebits to act as motion sensor to activate it.

This project should be a lot of fun and documenting it here will be a good excuse to revive this blog.