Webcam Snapshot Skript

Aus Laub-Home Wiki
Beispiel Bild des webcamsnapshot.sh Skriptes

Dieses Skript ist entstanden, als ich den Raspberry Pi als Webcam Server einrichten wollte. Da mir Motion zu viel Performance gefressen hat, habe ich eine Alternative gesucht, die ich auch im Tool fswebcam gefunden habe. Zusätzlich ließt es einen am Raspberry Pi angeschlossenen Temperatur Sensor aus und nutzt das Yahoo Wetter für eine 2 Tages Wettervorschau.

Voraussetzungen

  1. Linux PC mit fswebcam (bei mir Raspberry Pi mit Raspbian Installation)
  2. USB Webcam
  3. Temperatur Sensor
  4. Internet Zugang
  5. Webserver

Installation

Als erstes wird das Tool fswebcam benötigt. Unter Debian oder Ubuntu kann dies ganz einfach aus dem Repository installiert werden:

aptitude install fswebcam

Zusätzlich wird um das Yahoo Wetter zu benutzen das folgende Python Skript weather.py aus dem Git Repository benötigt:

cd /srv
git clone git://github.com/tupton/python-yahoo-weather.git
chmod +x python-yahoo-weather/weather.py

Den Code für euren Ort findet ihr über http://de.weather.yahoo.com. Hier einfach euren Ort oder PLZ eingeben und in den Suchergebnissen die richtige Location auswählen. Nun unten auf "10 Tages Vorschau" klicken und oben in die Adressleiste eures Browsers schauen (z.B. http://de.weather.com/weather/local/GMXX2088..._pla=10day)
Nun kann ein erster Test Erfolgen:

cd /srv/python-yahoo-weather/
./weather.py -mlvf2 GMXX1081 --output='weather.txt' && cat weather.txt
Location:
Heddesheim 

Current conditions:
13C and Mostly Cloudy

Forecast:
  27 Sep 2012
    High: 18C
    Low: 9C
    Conditions: Showers Late
  28 Sep 2012
    High: 19C
    Low: 6C
    Conditions: Partly Cloudy

Nachdem nun das Internet Wetter funktioniert sollte noch der Temperatur Messer getestet werden. Falls keiner existiert, kann man dies auch einfach im Skript später deaktivieren. Mehr Informationen zum Raspberry Pi und Temperatur Sensoren findet ihr hier:

Nun sind alle Abhängigkeiten erfüllt und man kann das Skript anlegen und anpassen: /usr/local/sbin/webcamsnapshot.sh

#!/bin/bash
#########################################################################
#Name: webcamsnapshot.sh
#Subscription: This script fetches the weather from yahoo, makes a snapshot of a webcam picture
#and combines the weather forecast with the snapshot picture.
#It may also integrate a temparture sensor output to the webcampicture
#by A. Laub
#andreas[-at-]laub-home.de
#
#License:
#This program is free software: you can redistribute it and/or modify it
#under the terms of the GNU General Public License as published by the
#Free Software Foundation, either version 3 of the License, or (at your option)
#any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#or FITNESS FOR A PARTICULAR PURPOSE.
#########################################################################
#Set the language
export LANG="en_US.UTF-8"
#Load the Pathes
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
 
#set the variables:
LOCATION=GMXX1081
WEATHER=/tmp/weatherforecast.txt
WEATHERBIN=/srv/python-yahoo-weather/weather.py
SENSOR=$(echo "scale=2; $(grep 't=' /sys/bus/w1/devices/w1_bus_master1/28-000004118a7f/w1_slave | awk -F't=' '{print $2}')/1000" | bc -l)
CAMPIC="/tmp/webcam.jpg"
LOGFILE="/var/log/webcamsnapshot.log"

#Remote Webserver (copy the picture via scp to the webserver)
ENABLEWEBSERVER="yes"   # yes or no
WEBSERVER="webserver.domain.de"
USER="root"
WEBHOME="/srv/httpd/vhosts/www.domain.de/htdocs/"

# Fetch the weather
$WEATHERBIN -mlvf2 GMXX1081 | sed 's/^[ \t]*//;s/[ \t]*$//;s/\([0-9]\{1,3\}\)C/\1°C/g' > $WEATHER

CURRENTCOND=$(sed -n '5p' $WEATHER)
DATE1=$(sed -n '8p' $WEATHER | sed 's/[ \t]/./'|awk '{ print $1}')
HIGH1=$(sed -n '9p' $WEATHER | awk -F': ' '{ print $2 }')
LOW1=$(sed -n '10p' $WEATHER | awk -F': ' '{ print $2 }')
COND1=$(sed -n '11p' $WEATHER | awk -F': ' '{ print $2 }')
DATE2=$(sed -n '12p' $WEATHER | sed 's/[ \t]/./'|awk '{ print $1}')
HIGH2=$(sed -n '13p' $WEATHER | awk -F': ' '{ print $2 }')
LOW2=$(sed -n '14p' $WEATHER | awk -F': ' '{ print $2 }')
COND2=$(sed -n '15p' $WEATHER | awk -F': ' '{ print $2 }')

# Weatherforecast text
WEATHERTEXT="$DATE1: H:$HIGH1 L:$LOW1 $COND1  |  $DATE2: H:$HIGH2 L:$LOW2 $COND2"

# Text for the webcam picture
TITLE="Heddesheim: $CURRENTCOND"
SUBTITLE="$WEATHERTEXT"
TIMESTAMP="%Y-%m-%d %H:%M (%Z)"
INFO="current temperature: $SENSOR°C"

# do the things
# make the picture
function takepicture {
        fswebcam -s contrast=55% -D 3 -S 5 -F 20 -r 640x480 -d /dev/video0 -q --title "$TITLE" --subtitle "$SUBTITLE" --timestamp "$TIMESTAMP" --info "$INFO" --jpeg "-1" $CAMPIC > $LOGFILE 2>&1
}
takepicture
while [ -n "$(cat $LOGFILE | grep 'Corrupt JPEG data')" ]; do
        takepicture
done

# copy the picture to Webserver
if [ "$ENABLEWEBSERVER" == "yes" ]; then
        scp -q $CAMPIC $USER@$WEBSERVER:$WEBHOME
fi

nun noch wie immer das Ausführrecht vergeben

chmod +x /usr/local/sbin/webcamsnapshot.sh

und am besten das ganze erstmal lokal testen

webcamsnapshot.sh
gpicview /tmp/webcam.jpg

How it works

Was macht das Skript? Naja, als erstes mal holt es mittels der Variable LOCATION das aktuelle Wetter deines Ortes bei Yahoo. Dann liest es, wenn vorhanden bei der Variable SENSOR den Temperatur Sensor aus, nun schießt ein Bild mittels fswebcam und beschriftet das Bild mit dem aktuellen Wetter. Das Bild wird dann als CAMPIC abgespeichert. Falls man auf dem Rechner einen installierten Webserver hat, kann man das Bild direkt in dessen Webroot abspeichern, wenn nicht gibt es die Option via SCP das Bild auf den Webserver zu übertragen (ENABLEWEBSERVER, WEBSERVER, USER, WEBHOME).

Mehr Infos

Mehr Infos findet ihr hier: