Showing posts with label Raspberry Pi. Show all posts
Showing posts with label Raspberry Pi. Show all posts

Wednesday, November 01, 2017

Magic Mirror - build part 4

We had been away on holiday. We returned to discover the magic mirror stuck in a reboot cycle. I hadn't backed anything up. Crisis!


Troubleshooting consisted of swapping out different components with my test unit to work out what had failed. After a few hours of exhaustive testing, it turns out that the Pi power brick had failed, and was not providing enough power to boot properly. The constant reboots and power failures also seem to have irreparably damaged the boot partition on the SD card, because that wouldn't boot in my test unit. Luckily though, I was still able to access the SD card with a freeware Windows program called 'DiskInternals Linux Reader', which could open and read the root partition, which is where all the custom scripts, magic mirror module configuration and timebonger code were stored. All relevant files were immediately copied off and backed up safely! I just had to reflash the SD card, order a new replacement power brick, and then reinstall everything.

I chose to use the latest Google AIY Projects image, meaning that I shouldn't have to worry about trying to reconfigure any audio settings because it should all work out of the box. All I would need to do would be configure the networking, retrieve the previously created AIY credentials from my Google account, reinstall the Magic Mirror software and modules, and copy back the timebonger script. I also decided to keep some notes on what I was doing, just in case I'd have to do it all again! Here they are:

1. Flash AIY Projects image to flash card with Etcher

2. Boot Pi with new image

3. Connect to home wifi using menu icon

4. Using 'Preferences'->'Raspberry Pi Configuration'
    System tab: Set hostname to 'magicmirror'
    Interfaces tab: Enable VNC server (then set admin password in VNC server config)
    Localisation: check settings for UK

5. Set fixed IP Address
    run:
        sudo leafpad /etc/network/interfaces
    comment out:
        iface wlan0 inet manual
    add:
        iface wlan0 inet static
                address 192.168.0.98
                subnet 255.255.255.0
                gateway 192.168.0.1

 6. Update everything
    sudo apt-get update && sudo apt-get -y upgrade && sudo reboot
    sudo rpi-update

7. Configure AIY Projects
    Follow instructions in AIY project installation to create the 'client_secrets.json' and 'credentials.json' files and put them in /home/pi/

8. Install 'xscreensaver' to stop the default screensaver kicking in
    run:
        sudo apt-get install xscreensaver
    configure by going to 'Preferences'->'Screensaver' and setting the blank screensaver

9. Install 'unclutter' to hide the mouse after a few moments of inactivity
    run:
        sudo apt-get install unclutter

10. Install my 'timebonger' script
    copy timebonger.py and cuckoo_clock1_x.wav to ~/timebonger
    create ~/timebonger/tb.sh with leafpad to contain:
        python /home/pi/timebonger/timebonger.py
    make executable:
        chmod +rwx tb.sh

11. Install MagicMirror core - https://magicmirror.builders/
    run:
        bash -c "$(curl -sL https://raw.githubusercontent.com/MichMich/MagicMirror/master/installers/raspberry.sh)"

12. Configure some default MagicMirror modules
    Add a personal Google Calendar using the ical link
    Add local weather and weather forecast using details from http://www.openweathermap.org
    Configure a couple of BBC News rss feeds

13. Install some additional MagicMirror modules.

random_quotes:
        cd ~/MagicMirror/modules
       
        git clone https://github.com/KirAsh4/random_quotes

        add to MagicMirror config.js:
       
            {
                module: 'random_quotes',
                position: 'middle_center',
                config: {
                    updateInterval: 300
                        // The config property is optional
                        // Without a config, a random quote is shown,
                        // selected from all of the categories available.
                }
            },

mmm-systemtemperature:
        cd ~/MagicMirror/modules

        git clone https://github.com/MichMich/mmm-systemtemperature.git

        add to MagicMirror config.js:
       
            {
                module: 'mmm-systemtemperature',
                position: 'bottom_right',    // This can be any of the regions.
                classes: 'small dimmed', // Add your own styling. Optional.
                config: {
                    // See 'Configuration options' for more information.
                }
            },

MMM-ping:
add ip addresses and hostnames of the devices I want to monitor inside my network to /etc/hosts so we can see names and not a list of ip addresses.

        leafpad /etc/hosts

        192.168.0.1    sky_router
        192.168.0.99    deepthought
        192.168.0.150    chromecast 
        192.168.0.151    firestick
        192.168.0.200    tplink_ext 
        192.168.0.209    hipsy00005
        192.168.0.210    retropie

        cd ~/MagicMirror/modules

        git clone https://github.com/fewieden/MMM-ping.git
       
        cd ~/MagicMirror/modules/MMM-ping
       
        npm install --productive
       
        sudo chmod u+s /bin/ping

        add to MagicMirror config.js:

            {
                module: 'MMM-ping',
                position: 'bottom_left',
                header: 'Ping',
                config: {
                    colored: true,
                    font: 'xsmall',
                    updateInterval: 1,
                    hosts: [
                        'sky_router',
                        'deepthought',
                        'chromecast',
                        'firestick',
                        'tplink_ext',
                        'retropie',
                        'hipsy00005' 
                            ]
                    }
            },

MMM-Trello:
useful online collaboration post it notes for teams (we use it at work)

        cd ~/MagicMirror/modules
       
        git clone https://github.com/Jopyth/MMM-Trello.git

        cd MMM-Trello

        npm install

        add to MagicMirror config.js:

            {
                module: 'MMM-Trello',
                position: 'bottom_right', // This can be any of the regions, best results in center regions.
                header: 'Trello',
                config: {
                    // See 'Configuration options' for more information.
                    showTitle: "true",
                    api_key: "<my api key>",
                    token: "<my token>",
                    list: "<my card id>"
                }
            },

14. Adjust MagicMirror CSS to reduce the large border and maximise screen area
    Edit custom.css:
        leafpad ~/MagicMirror/css/custom.css

    Insert this css into body to override those defaults in main.css

        body {
          margin: 10px;
          position: absolute;
          height: calc(100% - 20px);
          width: calc(100% - 20px);
        }

15. Set pm2 to autostart everything

I initially followed these instructions Auto Starting MagicMirror and then adapted them to start the other features.

start timebonger
    pm2 start /home/pi/timebonger/tb.sh --name "TimeBonger"

start Google Assistant - assistant_library_demo.py
    create gassist.sh (which is mostly a copy of ~/bin/AIY-voice-kit-shell.sh):
        leafpad ~/bin/gassist.sh

                #!/bin/bash --rcfile

                source /etc/bash.bashrc
                source ~/.bashrc

                cat /etc/aiyprojects.info

                cd ~/AIY-voice-kit-python
                source env/bin/activate

                /home/pi/AIY-voice-kit-python/src/assistant_library_demo.py

    make it executable
        chmod +rwx ~/bin/gassist.sh

    add it to pm2 startup:
        pm2 start /home/pi/bin/gassist.sh  

start MagicMirror
    pm2 start /home/pi/MagicMirror/installers/mm.sh  --name "MagicMirror"
      
save pm2 process list
    pm2 save
   
add pm2 startup to /etc/rc.local
    run:
        pm2 startup systemd
    which should add a startup command (like the one below) to /etc/rc.local
        # pm2 startup
        sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u pi --hp /home/pi

16. Reboot, and cross fingers!

So that's about it for my Magic Mirror!

It's especially useful in the mornings to have a clock and calendar available, and to quickly check the day's forecast for good or bad local weather. Having the random quotes is fun, but the rss news feed is less useful because it only shows one thing at a time. Having the Google VoiceHat installed is also cool, and when I find the time, I'll have a go at swapping out the simple Assistant with the more powerful Cloud Speech API to implement some of my own commands, and bring me a bit closer to the Star Trek Next Generation LCARS interface! Yes, I am a nerd.

All in all, the timebonger is the icing on the cake as far as I'm concerned, it amuses me all the time, and no-one's complained about it so far!

Thursday, October 26, 2017

Magic Mirror - build part 3


So, my magic mirror had been hanging in the living room above the fireplace, happily serving information for a few weeks, when May 2017's issue of the MagPi magazine (issue 57) was published, containing some rather spectacular electronic gubbins, the Google AIY Project kit (Artificial It Yourself)! A big speaker cone, wires, arcade style button, and a Voice HAT printed circuit board, enabling you to build your own version of Google's Home voice assistant, all free with the magazine. I had to have one!


Publication day came, and I tried at least 5 different stores, all of which did NOT have a copy or had already sold out. My wife eventually found and reserved a copy that was still available (thanks Sainsbury's Shrewsbury!) and we were able to pick it up the following Saturday from the customer service desk. Phew! We also found some additional copies on the shelves in the main store... I had them too!

I also ordered a brand new Pi3 and built it into the cardboard version. This was to become my test unit to see how to install and configure it, and to see if it really was actually worth the effort to cram it inside the magic mirror. The cardboard build seemed to go relatively smoothly, if a bit fiddly, however the instructions in the magazine didn't quite match with the necessities of reality (if I recall correctly, there was a file renaming step missing) but after some further troubleshooting, it eventually worked really well. Run the code, press the button, talk, and receive an answer. Super!


Next problem. If I was going to build this into the mirror, I didn't want to have to press the button to activate the assistant, nor worry about where to position or how to mount it. Luckily, there were additional instructions on the following pages to circumvent the button push, and move to a 'clap clap' trigger, which also worked. There also appeared to be further instructions to change the clap trigger to using the hot words "Hey Google", so I also tested that with similar success. A hands-off voice assistant it was to be then!


The major problem though, was with the additional height of the larger speaker cone, and the VoiceHAT board on top of the Raspberry Pi.


There are two little white plastic connectors (one for the microphone, and one for the pushbutton, which increased the height required beyond the 2cm of space available inside the mirror.


I considered a couple of solutions. Cutting more holes in the backing board for the components to poke out felt inelegant, would look messy and may affect how the mirror hangs on the wall. Equally, extending the GPIO ports with an IDE cable might work electronically, but I didn't even bother testing that because that configuration would still not have fit in the space available.

I really wasn't sure how to proceed, and found my mind wandering. My daughter was watching Dr Who, rather loudly in her bedroom next door. He wouldn't have this problem, because his Tardis is bigger on the inside... Therefore the only solution was to steal some Timelord technology and actually make the inside bigger...
...ah-ha!


Thanks to that flash of inspiration, it would be a relatively simple modification to tack on some additional 20mm strips of wood to make the 'walls' of the backing board higher.


Then, after carefully removing the brass clasps, do exactly the same with the outer frame. Wood glue the strips into place first, then drill pilot screw holes for extra security, and reattach brass hooks.



Hey presto, extra room!


New blocks of wood were cut and glued on top of the existing ones to increase the height of the screen mounts to again push the screen against the inside of the glass pane.


Some additional scraps were used to hold the new speaker cone in place. Unfortunately, it completely threw off all sense of symmetricality, but as it's all neatly hidden inside it only bothers me when I think about it... :)



The microphones are not permanently affixed, and are instead woven between other cables and left to dangle slightly, but not so much that they push up against the inside of the black card and glass. The mics are quite sensitive, so being inside a glass picture frame with a large, sound deadening bit of thick card in the way doesn't actually seem to be a problem at all.



Back on the wall, and configuring by remote was a bit more of a trial. There are instructions available for you to install the required software manually, and I followed those. The Google AIY stuff worked pretty much first time, but I had problems configuring regular audio output for my timebonger script. Again, I didn't leave myself any notes on what I did to eventually get this working, but it took many attempts and lots of Googling. I might have just chosen an alternate output device from the volume control's wiget on the menu... can't actually remember!

Finally, everything was working smoothly!

Coming up... Magic Mirror Build Part Four - Everything stopped working smoothly!

Thursday, October 19, 2017

Magic Mirror - build part 2

Quick modification this. I wanted my magic mirror to play a sound effect on the hour, but for that, it would need some speakers. I found the cheapest USB powered speakers I could find on eBay to experiment with, and if I broke them, it wouldn't be the end of the world.

  • Cheap USB speakers £2.69

They were surprisingly quite chunky, but were easy enough to open and unscrew...



However, I had to break the back end of each speaker box because the wires were threaded through a tiny hole, which wouldn't allow the plug ends through, and I also didn't want to have to desolder the wires. Two pairs of pliers made short work of breaking apart the moulded plastic, and removing the hot glue that held the volume knob PCB in place.


Next, after a few attempts to work out optimal positioning for the speakers, and to keep everything OCD symmetrical (very important) the mounting holes were marked and drilled and cut...



Superey dupery! Next, the plan required that I was to bolt the speaker cones inside the mirror, however... catastrophe! They were several millimetres too tall to fit inside the case! Daaahhh!

There really was no other option than to mount the speakers on the outside of the backing board, which meant that I had to desolder the wires anyway, taking care to remember which way round the wires went originally.


Some delicately balanced soldering later, the speakers were rewired, and even though they're mounted on the outside, they don't protrude too much, and don't affect how it hangs on the wall.



I first tried plugging the audio plug straight into the Pi's audio out, but it was way too hissy. Luckily, the screen driver board also has a standard audio out socket, and all that was required was to fiddle with some configuration files* to force audio out of the HDMI lead. Luckier still, it's no where near as hissy as the Pi direct audio connection.

(* This earlier incarnation of the magic mirror used an older vanilla version of Raspbian Jesse. I think I had to uncomment or add some specific HDMI options in /boot/config.txt to get the audio to work properly through the screen driver board, though I didn't leave myself any notes and can't remember exactly what I did)

So, now to the business of automating a python script to play audio on the hour.

I've only used python a little, but from what I have done, I was able to reuse some previously written functions to get things moving along. The hardest part, as with every software engineering project, is to come up with a suitable name. After thinking long and hard about it, it was named "timebonger.py"!
#!/usr/bin/env python

#import libraries
import sys
import time
import signal
import datetime
import subprocess

from time import sleep

#define global variables
blnStopped=False

#exit when received SIGTERM or SIGHUP
def stop(sig, frame):
    global blnStopped
    blnStopped=True
    print "caught signal ", sig, " exiting"

signal.signal(signal.SIGTERM, stop) #-15
signal.signal(signal.SIGHUP, stop) #-1

def runcmd(strCommand):
    print "runcmd() strCommand='" + strCommand + "'"
    try:
        output = subprocess.check_output(strCommand, shell=True)
        output = output.strip('\n')
        output = output.strip('\r')
        output = output.strip('\t')
        output = output.strip(' ')
        #print "output=" + output
    except Exception:
        return "error"

    return output

def mainloop():
    global blnStopped

    print "mainloop() started"

    dteNow=datetime.datetime.now()
    intHourNow=dteNow.hour
    intNextHour=intHourNow+1
    if(intNextHour>=24):
        intNextHour=0

    intMinuteNow=dteNow.minute
    intNextMinute=intMinuteNow+1
    if(intNextMinute>=60):
        intNextMinute=0

    try:
        while(not blnStopped):
            dteNow=datetime.datetime.now()
            intHourNow=dteNow.hour
            intMinuteNow=dteNow.minute
            #print "mainloop() dteNow=" + str(dteNow) + " intNextHour=" + str(intNextHour) + " intNextMinute=" + str(intNextMinute)

            if(intMinuteNow==intNextMinute or intHourNow==intNextHour):
                #print "mainloop() New minute!"

                #prepare time output
                strMinute=str(intMinuteNow)
                if(intMinuteNow==0):
                    strMinute="o'clock"

                strHour=str(intHourNow)
                if(intHourNow==0):
                    strHour="midnight"
                    strMinute=""
                if(intHourNow==12):
                    strHour="midday"
                    strMinute=""

                #do we run now?
                if(intHourNow>=7 and intHourNow<=23 and intMinuteNow==0):
                    print "mainloop() Bing Bong! The time is " + strHour + " " + strMinute
                    runcmd("aplay --device=sysdefault /home/pi/timebonger/cuckoo_clock1_x.wav")

                #calculate next minute
                if(intMinuteNow==intNextMinute):
                    intNextMinute=intNextMinute+1
                    if(intNextMinute>=60):
                        intNextMinute=0
                if(intHourNow==intNextHour):
                    intNextHour=intNextHour+1
                    if(intNextHour>=24):
                        intNextHour=0

            sleep(1)

        sys.exit(1)

    except KeyboardInterrupt:
        print "mainloop() Keyboard Interrupt!"
        sys.exit(1)

mainloop()


Not very elegant, I'm sure you'll agree, but it gets the job done, and makes me giggle EVERY SINGLE TIME. Can't remember where I got the sound effect from (it took me a while trying to find a suitably funny one, eventually I settled on a cartoon sounding cuckoo clock), but you could probably Google the filename to locate it. You may also notice that I've limited the script to only play the sound between the hours of 7am to 11pm, because I don't want it to 'bong' throughout the night and piss my family off!

To get the timebonger script to auto run on restart, I used the pm2 scheduler that I'd previously installed to get the Magic Mirror package to auto run. As the recommended method for the magic mirror suggests, I created a small shell script, /home/pi/timebonger/tb.sh which contained the single line:

python /home/pi/timebonger/timebonger.py

...which executes python and passes a fully qualified directory path to the timebonger script, then I made it executable with:

sudo chmod +rwx timebonger.py

...then added it to pm2 with:

pm2 start tb.sh
pm2 save

pm2 is itself already autostarted from /etc/rc.local (which I believe I setup by executing 'pm2 startup systemd' because Raspbian uses systemd) so that's job done! Everything auto runs on reboot without me having to touch anything.

So, the last part was to carefully measure out exactly where the wall hooks had to go so I could drill holes for wall plugs and mount it over the fireplace in the living room, leaving everything level and symmetrical. After much headscratching, measuring multiple times, umming, ahhhing, pencil marks, and aching arms, I managed to get it right first time!



Coming up in part three - the Google AIY (Artificial It Yourself) version!

Monday, October 16, 2017

Magic Mirror - build part 1

February 2017's issue of The MagPi magazine (issue 54) caught my eye in a supermarket, because it featured a 'magic mirror' build project. I thought it was a great idea, and since I had a couple of Pis sitting around, and just had to build one!

First, can I get it to work? I figured that I'd use the Pi Zero, because it's a very small device, and would have the best chance of fitting it in a suitable picture frame. I managed to get an image running, but it did seem to be terribly slow, and I hoped to be able to run a few other services on it in the future, so I plumped for getting a version running on the larger and more powerful Pi3. Here is the image, with a few configured modules, running on my 23" main monitor...




Configuring the modules was straigtforward enough, as long as you read their instructions.

So far so good, on to the difficult part of getting all the right bits.

I did all the standard things... find a cheap laptop screen on eBay, then Google the model number to find a compatible HDMI screen driver, and agonised over picking the correct power supply for it. I wanted to be a complete cheapskate in this build, and don't want to blow anything up in the process. Double and triple checking was a necessity! A few hours later, the main electronic bits are ordered.
  • 15.6" screen £25
  • HDMI Audio Controller Board Driver £13
  • Screen Power Supply £7
  • Short HDMI cable £3
  • Pi 5v 3A power supply £3

Once all the bits turned up, time to see if it all works...


...success!

Now, all this needs to be crammed into a picture frame. I needed one that would give me enough room to fit the 'tallest' component, which turns out to be the Pi itself, at just over 2cm, from the top of the USB ports to the bottom of the pins sticking out on the bottom. Plus this mirror was intended to replace the big mirror in our living room above the fireplace. I found just the right frame from B&Q for about £15. It's the sort that you have to bend a bunch of little metal tabs down to keep the picture sandwiched between the backing board and the glass, but it was deep enough that it would just about fit the Pi inside the frame with the backing board remaining flush with the back of the frame. A pair of pliers was required to remove the metal tabs.

B&Q have had a lot of my money thanks to this project! Some 2cm wide strips of wood were purchased, and glued to the inside of the backing board to make an inner frame. Some smaller and shorter bits were used to hold the screen display in place...



The screen is 4mm deep, so some other pieces that were 16mm high were used to prop the screen up from underneath, this allowed me to hold the screen right up against the glass to minimise internal reflections when finally complete. Clever, huh? ;)



 A few other scraps were used to help keep the driver board and Pi in position.


Here the backing board and inner frame is fitted inside the main frame 'in reverse' to show how much room is left for the glass. It all fits!


Next, more nerve wracking stuff... fixing the mirrored surface to the glass!

  • One Way Privacy Window Film £10
  • Brass plated side hooks £3
  • Heavy Duty Picture and Mirror Hanging Kit £4
  • M3 nuts and bolts £3.25

I had to re-read the instructions a number of times, to wrap my head around which sides of film and glass I was supposed to cover in 'slip solution' (soapy water) and to get the steps in the right order. I decided to trim the film before affixing to the glass, rather than afterwards as recommended, because I worried that I'd cause more damage to the reflective film by trimming it after sticking it than before.



Handy hint: I used a small scrap of film to test how to separate the backing from the film with a bit of sellotape. Also, you can see the definite difference between silvery 'front' and see-through 'back' sides of the one way film...


It was tricky to squeegie the trapped water out, and keep all the edges aligned. Here's the final squeegied result...


I planned to use the original picture frame card to separate the inner wood frame from the one-way film, and trim out the bits where the screen is, however, after putting it all together, the white card was actually quite visible through the one way film. Not good enough! Here it can be seen, with some A4 white paper taped to it, that I used to mark out a template for the exact size and position of the screen hole...


One trip to a local stationers later, I've obtained a large A2 piece of black card, easily trimmed to size using the backing board as a template. The paper template above worked perfectly to let me work out exactly where the hole for the screen display needed to be.


The only downside is that it was half a centimetre too short!


Again, some scraps of card were carefully trimmed to provide the last few millimetres and taped in place.

Even the cut inner edge of the black card reflected enough light back through the one way film to be visible, so a trusty Sharpie was employed to deal with the problem...


Some admittedly not-very-neatly placed holes were drilled to allow the screen control buttons to poke through the backing board...



Lastly, the heavy duty picture hanging loops were affixed to the backing board (being drilled through to the inner wooden frame) and the brass hooks were added with some little self tapping wood screws to hold everything together. It also became a necessity to drill a couple of holes for some more M3 bolts to hold the Pi and driver board down, otherwise, they'd keep falling out when ever I tried to put the inner frame into the outer frame.




And with a super pleased and self satisfied grin, here it is on the floor, finally assembled and booted!



That was build Phase One. It sat on the floor in the office for a month before I found the time to move on to Phase Two...