Monday, October 8, 2018

Picture Frame - gets PIR and saves energy


My RPiPF is doing good. So now the goal is to shut down the TV when nobody is watching.

This was done. Many times. Even here.

A simple PIR (passive infrared) connected to Rpi pin can be monitored and when no motion is detected for some time the TV gets turned off. This saves energy and potentially extends the TV life span.

The problem with (most) PIRs

The PIR devices are contain two main components: the actual sensor that converts light into electric signals and a very high-gain amplifier. And here the problem begins. The sensor gives a very weak signal that need to be amplified so it can be detected. The amplifier however will happily amplify anything on its input, including any interference. And RPI being a decently strong EM signal  emitter (WiFi radio) when placed close to the sensor will cause unwanted tripping.

I have tried the $5 sensors that worked great in the past, both with arduino and other laptops-picture-frames. These worked great as there was no interference source nearby.

The aluminium foil shield did not work. Neither did attempt to decouple power supply with capacitors (apparently it is an inducted interference rather than a noise conducted via supply/ground lines).

However, I found this particular mini PIR sensor resilient to the interference. The cost being the sensitivity - it's detecting motion only in less then 3-4 meters away, while the other sensors were  double this.

Bash the code

The complete code (bash script) is on github here:
https://github.com/michkrom/pictureframe

The main parts to make PIR work are:

  • script to read the GPIO pin state: gpiord  
  • scripts to turn TV on and off
  • main controller script monitoring the PIR and deciding when to turn the TV on or off: pf.sh

Bit and nuts of the script:

Keep time: these two lines store current time int the form of number of seconds since 'epoh'. This is convenient when comparing them later.

lastmotion=`date +%s`
...
nowts=`date +%s`

Here we read the pin #18, it's either 0 or 1.

pinstate=`gpiord 18`

And here we observe detected motion ($pinstate equal 1) and note when this happened.

  if [ $pinstate -eq 1 ]; then
    lastmotion=$nowts
  fi   

Then compute how long it was since last time something moved


  let lastmotionage=$nowts-$lastmotion

Then decide to show or not (by extending existing schedule condition)

  if [ $lastmotionage -lt 600 ] && [ "$timeok" = true ]; then
    show=true

  fi

That's it.

Monday, August 20, 2018

Picture Frame - a RPiPF on the schedule

The previous builds were smaller but smarter. They actually turned off when nobody was around to see. The RPiPF (RapsberyPi Picture Frame) is not there yet.

A small step forward: turn off when nobody's around to watch and turn on when there may be - on the schedule. Turn on at 7am, turn off 11pm.

To control the TV from RPI - CEC


Consumer Electronics Control (CEC) is a control channel over the HDMI cable.

Install cec-utils

sudo apt-get install cec-utils

Turn the TV off:

echo "standby 0" | cec-client -s -d 1 

Turn the TV on

echo "on 0" | cec-client -s -d 1


Don't mind the CLI plumbing - these command are like magic: '-s' means execute single command while '-d 1' limits the damage to you eyes by cutting down the logging spewed back at ya. Note that the echo just pipes the actual command to the cec-client - an interactive program by birth.

Flying on a Magic Script

The ph.sh script that was previously used to just start feh. Now it does the scheduling.
The script loops and:
  • checks the time
  • decides if the slide show should or should not be running
  • depending on that decision either 
    • kills feh and turns off the TV or 
    • starts feh and turns on the TV
  • sleeps for 1 minute and repeats

logger "PF started"
PATH=$HOME/bin:$PATH
while true; do
  now=`date +%H%M`
  fehpid=`pidof feh`
  if [ $now -gt 700 ] && [ $now -lt 2300 ]; then
    if [ -z $fehpid ]; then
      logger "PF now=$now fehpid=$fehpid ON"
      tvon
      gofeh &
    fi
  else
    if [ ! -z $fehpid ]; then
      logger "PF now=$now fehpid=$fehpid OFF"
      skill feh
      tvoff
    fi
  fi
  sleep 1m 
done

Taking it apart

  • The lines with 'logger ...' just put entries in /var/log/user.log so I know what was happening.
  • The line "PATH=" just adds /home/pi/bin (user's pi bin directory) to the path. Normally this should be done the .profile.
  • Then we loop forever...
  • The line 'now=... ' captures current time into the 'now' variable. The time is formatted as hhmm (in 24 hour format) for easier comparing later.
  • The 'fehpid=...'  captures the process ID of feh, the slide show process. If it is not running, the fehpid is empty string.
  • Then in the first 'if' the script compares the $now to fit between 700 and 2300, military style.
  • And finally, the nested 'ifs' decide to turn on the show or to stop it. The '-z' checks for empty, while the '! -z' checks for not empty.
  • The small command scripts:  tvon, tvoff, gofeh got created in ~/bin (and chmod +x them so they can be executed).
  • The gofeh is run in the background (with '&') so the script can continue monitoring the time after starting the slide show.

References

Monday, July 30, 2018

40" Picture Frame For under $100

Yup, another version of my picture frame obsession (YAPF). I have turned several laptops into picture frames in the past. The Mark I, a ca'95 HP laptop is still running strong, 8 years non stop. The Mark II ca'99 HP laptops were not that strong. These died after few years...something changed in HP's quality about the new millenum...

So here we go again! This time I am spending real $$$ on it!

The ingredients:

  1.  A used HDTV from craigslist.org.  Best, a TV with LED back-light and 1080p resolution (a 720p works too but the picture quality is not that great unless they are small.). I got a 4yr old Emerson LC401EM3F.for $75
  2. Rapsberry Pi Zero $5 or better RPiZeroW $10 (much more fun to access remotely)
  3. miniSD card, greater than 4GB capacity $5
  4. 5V USB power supply (a charger better than 500mA), USB cable (to power RPi) $10
  5. High-Speed Mini-HDMI to HDMI Cable, such as this (Amazon Basic) $5
Total about $95.

Quick steps to the finish

Here are the steps to achive glory in less than an hour...that is if you know what you're doing. (Some skills required.)

Create Bootable Raspian Lite system. 

There are plenty tutorials online how to do it here.
  • Download Raspian Lite (at the time of writing it's STRETCH). Here
  • Image your microSD card (on linux I use dd)
  • Connect a keyboard (needs a microUSB to USB cable/adapter, OTG works) to USB port.
  • Connect monitor via you cable (you can use adapters but the connectors on RPIW are fragile and close together so cable work better).
  • Power it up by plugging the USB charger/power supply into PWR connector.
  • You should see RPI booting.
  • After booting, log in as pi, password raspberry

Configure the system, via raspi-config utility.

  • change password (if you want)
  • set hostname (if you want)
  • configure wifi (so you can log into it with ssh - applies only to RPIZ or if you plug inWiFi adaptor)
  • enable ssh
  • configure locale: US UTF-8, timezone
  • configure keyboard: Generic 104 PC, US than at the end there is option to set Ctrl-Alt-Backspace to kill X
  • configure boot to console (not graphical)
  • set to resize file system to full CD
  • reboot, check that wifi is connected (ping something), also check you can ssh to the system (handy later)

Add to the system, as Lite is a bit...lite.

  • first update  all
    • sudo apt-get update && sudo apt-get upgrade -y
  • reboot 
  • install graphical display (xorg) and other utils
    • sudo apt-get install --no-install-recommends xserver-xorg xinit x11-xserver-utils lightdm feh
    • note: the lightdm allows auto login to X; feh is the slide show application.

Configure scripts so things start automatically on every boot.

  • create ~/.xinitrc (this is what startx runs: disable x screen blanking and make it execute pf.sh at the end)
    xset s off
    xset -dpms
    xset s noblank
    /home/pi/bin/pf.sh
  • create ~/bin/pf.sh to start feh in desired mode - full screen, randomized slide show 60s, no mouse etc and make it look for pictures in ... Pictures directory)
    feh -xrzYZFD60 --no-pointer --auto-rotate ~/Pictures
  • in ~/.profile add the following at the end  (this runs startx on login from tty1 so autologin will use it)
    if [[ -z $DISPLAY ]] && [[ $(tty) = /dev/tty1 ]]; then
    startx
    fi

Enjoy the view

Put some pictures in Pictures directory (you can use the scp utility via network).
Connect da TV.
Reboot.
You should see RPI booting and then switching into graphical X and after a moment displaying the pictures.

DONE?


Ahh...No....not completely. I still want the YAPF.M3 to turn off when nobody is watching and back on when somebody is here (via a PIR sensor). More on this later  (hooking PIR sensor, using HDMI CEC TV control and wrapping it all in a script).

Here is how to add schedule to the YAPF.M3
http://minkbot.blogspot.com/2018/08/picture-frame-rpipf-on-schedule.html

Also, since it's already connected to wifi, perhaps it could suck in the pictures itself? The disadvantage is that I am still using Picassa and which do not modify my pictures (Picassa just remembers the operations) and the pictures would be the resolution unnecessary higher than 1080p (20MP anyone?).

Github Repository

All the files are in github. These can be copied to the home directory: ~/home/pi.
https://github.com/michkrom/pictureframe



Saturday, July 28, 2018

Google Assistant Marine Weatherman

Once upon a time, I decided that checking  the weather on the phone while driving is not conducive to safety.  But then I learnt about Google Assistant AI thingy that is changing the world...sorry Alexa!
And here is a story of trying to build a Google Assistant skill for accessing marine weather information.

WHY

Soo...what marine whether data am I interested in? Wind, tides, fog, swell....I am a kite-surfer and my day-to-day ability to kite depends on catching the windy days. I need to know! Since I am on north California coast I depend on NOAA and NWS.

WHAT

So here are some web weather resources I want to access via a speach-based application:

Coastal Forecasts
http://tgftp.nws.noaa.gov/data/forecasts/marine/coastal/
Tide Predictions
https://tidesandcurrents.noaa.gov/tide_predictions.html
Noaa Buoys:
https://www.ndbc.noaa.gov/mini_station_page.php?station=44017

HOW

Google Assistant AI. Well...really the AI?...That depends on your definition of 'I'.  IMHO, it is not AI - it is a speech-to-text (a very good one, mind you) then a text pattern matching filter with ability to invoke external web servers when matched a pre-canned pattern.  It can't think by itself (the 'I' in AI is rather weak).

LINGO


Here is the lingo, which took me quite a time to decipher:

Google Assistant

A user facing system that can accept user input and provide, by some magic means, relevant output. Google Assistant (GA) has a plethora of 'native skill' and ability to invoke 3rd party applications (via 'talk to XYX'). There is also notion of Actions on google..I think it's just a fancy name for an Google Asistant application?

Google Assistant Application

(aka skill...aka Action?) It is what I have created. GA apps are invoked by 'talk to XYZ', in my case it is 'talk to marine weather'. I tried to be funny but GA app name must be 2 or more words so Neptune or Poseidon did not work.

DialogFlow

Is a text pattern matching system - my application (my Google Action) is a set of text patterns, aka Intentions (read: 'user intentions', 'user statements').
(I suspect there is a close relation between defined patterns and a speech recognition as each time I update the pattern it says it's 'training...', likely speech recognition is much better if the correlator knows what to expect). DF also contain a system of describing parameters (ie the variables in intentions) via Entities and also linking intention to external web-based services (aka Fulfilment).

DialogFlow Intentions

I have mentioned the intentions. These are the patterns that constitute one user desire (intention) and DF app may have many intentions. In intention, the same thing may be expressed many different ways:
tides for bodega
give me tides for bodega 
what are the tides for bodega
bodega tides
give me bodega tides
etc..
Intentions can have parameters here a 'location' would be a parameter. One can select a part of the example sentence and make it a parameter. So:
tides for 'LOCATION'
where location is a parameter. Which brings us to entities.

DialogFlow Entity

Entity is a ... type (specification) of a parameter.  You can use predefined entity (such as 'city' 'time' 'color' etc) or define your own or ... use @sys.any which is ...well anything.

DialogFlow Fulfilment

Simple intention can have canned responses. You define them right in the dialog flow. But if you need to go to get some real time data, the fulfillment is where things get more interested. Fulfillment is essentially the GA/DF reaching to some other place in the WWW and getting a reply. So fulfillment is a www server accepting a https request returning a response. The request and response are GA-specific, JSON formatted collection of parameters. You can host this service yourself or you can use google services. The google services are called Firebase. Note, the Google Firebase is more generic than focused on GA/DF but there is a good support to make things easy.

It's all pretty convoluted and overwhelming on the first date...

Monday, July 9, 2018

It's been a while

Yup, I got busy/lazy/having too much fun. This post is just to unplug the pipes, move the joints and warm up my fingers...

Thing I have been toying with:
  • Picture Frame Mark2
  • Ghost-busters backpack (Halloween Outfit)
  • Fulling around with MSP430
  • Fulling around with rPIs (3,zero,zerow)
  • Fulling around with MSP8266
  • Lego robotics help training
  • Odyssey of the Mind training
  • Hacking Ikea ANSLUTA remote
  • Building and flying quads and fix wings RC
  • Flying man-lifting kites in the waves.
  • Sailing sailing boats
  • Remodeling
  • Life
My current toyject (short for toy-project): Google Assistant app for Marine Weather.

Stay put, some of these will find it's way here.