Webcam Snapshot Skript

Version vom 1. Januar 2021, 21:18 Uhr von Andreas (Diskussion | Beiträge) (Textersetzung - „<source“ durch „<syntaxhighlight“)

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.

Beispiel Bild des webcamsnapshot.sh Skriptes
Beispiel Bild des webcamsnapshot.sh Skriptes

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: <syntaxhighlight lang=bash> aptitude install fswebcam </source> Zusätzlich wird um das Yahoo Wetter zu benutzen das folgende Python Skript weather.py aus dem Git Repository benötigt: <syntaxhighlight lang=bash> cd /srv git clone git://github.com/tupton/python-yahoo-weather.git chmod +x python-yahoo-weather/weather.py </source> 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: <syntaxhighlight lang=bash> cd /srv/python-yahoo-weather/ ./weather.py -mlvf2 GMXX1081 --output='weather.txt' && cat weather.txt </source>

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 <syntaxhighlight lang=bash>

  1. !/bin/bash
  2. Name: webcamsnapshot.sh
  3. Subscription: This script fetches the weather from yahoo, makes a snapshot of a webcam picture
  4. and combines the weather forecast with the snapshot picture.
  5. It may also integrate a temparture sensor output to the webcampicture
  6. by A. Laub
  7. andreas[-at-]laub-home.de
  8. License:
  9. This program is free software: you can redistribute it and/or modify it
  10. under the terms of the GNU General Public License as published by the
  11. Free Software Foundation, either version 3 of the License, or (at your option)
  12. any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. or FITNESS FOR A PARTICULAR PURPOSE.
  16. Set the language

export LANG="en_US.UTF-8"

  1. Load the Pathes

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

  1. 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"

  1. 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/"

  1. 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 }')

  1. Weatherforecast text

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

  1. Text for the webcam picture

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

  1. do the things
  2. 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

  1. copy the picture to Webserver

if [ "$ENABLEWEBSERVER" == "yes" ]; then

       scp -q $CAMPIC $USER@$WEBSERVER:$WEBHOME

fi </source> nun noch wie immer das Ausführrecht vergeben <syntaxhighlight lang=bash> chmod +x /usr/local/sbin/webcamsnapshot.sh </source> und am besten das ganze erstmal lokal testen <syntaxhighlight lang=bash> webcamsnapshot.sh gpicview /tmp/webcam.jpg </source>

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: