Saturday, June 18, 2016

Live webcam and weather with Raspberry Pi

How did I do?

Connect DHT11 sensor:

Download Adafruit Python scripts:
git clone
cd Adafruit_Python_DHT
Install software:
sudo apt-get update
sudo apt-get install build-essential imagemagick
cd Adafruit_Python_DHT
sudo python install 
Test sensor:
cd examples
sudo ./ 11 4  
28.0 38.0
Connect Raspberry Camera:

As root capture photo & add temperature/humidity/date on jpeg with imagemagick:
DATE=`TZ=Europe/Rome date`
var=`/home/pi/Adafruit_Python_DHT/examples/ 11 4`
temperature=$(echo $var | cut -d' ' -f1)°C
humidity=$(echo $var | cut -d' ' -f2)%

raspistill -n -w 640 -h 480 -q 90 -o capture.jpg
convert -pointsize 15 -fill '#CDCDCD88' -draw "rectangle 0,0 640,20" -fill black -draw "text 90,16 'Casapulla OnLine Live Webcam - Temperatura:'" -fill black -draw "text 402,16 '$temperature'"  -fill black -draw "text 450,16 ' - Umidità:'" -fill black -draw "text 517,16 '$humidity'" -fill '#CDCDCD88' -draw "rectangle 0,460 640,480" -fill black -draw "text 215,475 '$DATE'" capture.jpg webcam.jpg
Upload on the site:
scp webcam.jpg
ssh -x cp /var/www/html/webcam.jpg /var/www/html/last_frame.jpg
Html page:
<head> <title>Live Webcam</title>
<img src="last_frame.jpg?1466267480856" id="myImage">
setInterval(function() {
var myImageElement = document.getElementById('myImage');
myImageElement.src = 'last_frame.jpg?' + new Date().getTime(); },60000); </script>