Skip to content

Instantly share code, notes, and snippets.

@kosso
Last active August 19, 2017 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosso/4b25a7d4aaa34c7b484cc6ec89078f66 to your computer and use it in GitHub Desktop.
Save kosso/4b25a7d4aaa34c7b484cc6ec89078f66 to your computer and use it in GitHub Desktop.
Uses PiCamera to take a photo with a timestamped filename, then add some text with transparent 'underlays' and a logo. Will get the Temp and Humidity data via GPIO from the DHT22 sensor. Light via Enviro Phat and Moisture via analog input from TE-215 sensor to Enviro Phat analog pin 0. Also draws a circle filled with the colours detected by the …
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 'SENTREE'
# ----------------
# Uses PiCamera to take a photo with a timestamped filename,
# then add some text with transparent 'underlays' and a logo.
# Will get the Temp and Humidity data via GPIO from the DHT22 sensor.
# Light via Enviro Phat and Moisture via analog input from TE-215 sensor to Enviro Phat analog pin 0.
# Also draws a circle filled with the colours detected by the Enviro Phat
# Kosso : 19 Aug 2017
import picamera
import datetime as dt
from subprocess import call
import sys
from time import sleep
from picamera import PiCamera
import pytz
import Adafruit_DHT
#import RPi.GPIO as GPIO # This is the GPIO library we need to use the GPIO pins on the Raspberry Pi
from envirophat import light
from envirophat import leds
from envirophat import analog
print(analog.read(0))
# Reads analog signal from TE215 connected to Enviro PHAT Analog pin 0.
# out of water reads : ~3.3
dry = 3.311
# submerged in water reads : ~1.1
wet = 1.1 #submerged
moisture = analog.read(0)
moisture_perc = ((moisture - dry) / (wet - dry)) * 100
print ('moisture percentage between 1.1 and 3.3: {0}'.format(moisture_perc))
#leds.on()
#leds.off()
r, g, b = light.rgb()
light_value = light.light()
print 'LIGHT: ' + str(light_value)
#print light.rgb()
print('RED:{0} GREEN:{1} BLUE:{2}'.format(r, g, b))
sensor = Adafruit_DHT.DHT22
# DHT sensor data is connected to GPIO PIN 4
pin = 4
output_folder = '/home/pi/_SENTREE/_cap/'
humidity = 0
temperature = 0
def get_temp_humidity():
global humidity, temperature
print('get_temp_humidity... ')
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
if humidity is not None and temperature is not None:
print('TEMP: {0:0.1f}°C HUMIDITY: {1:0.1f}%'.format(temperature, humidity))
else:
print('Failed to get reading. Try again!')
def take_photo():
global humidity, temperature, light_value, r, g, b, moisture
print('take_photo ... ')
camera = picamera.PiCamera(resolution=(720, 480), framerate=12)
# camera = PiCamera()
camera.vflip = True
camera.hflip = True
#camera.brightness = 60
#camera.contrast = 70
i = dt.datetime.now(pytz.timezone('Europe/London'))
now = i.strftime('%Y%m%d-%H%M%S')
the_time = i.strftime('%d/%m/%Y %H:%M:%S')
the_text = 'SENTREE'
photo_name = now + '.jpg'
# image is 720 x 480
camera.capture(output_folder + photo_name)
print "photo taken"
photo_path = output_folder + photo_name
status = the_text + ' : ' + the_time
header_text = 'PIKOGRO MONITOR 0.1'
# build the imagemagick command to draw some underlay boxes and draw some text over them.
# set up the imput file first
overlay_text_cmd = "/usr/bin/convert "+ output_folder + photo_name
# draw a transparent white underlay at top
overlay_text_cmd += " -strokewidth 0 -fill 'rgba( 255, 255, 255, 0.5 )' -draw 'rectangle 0,0 720,36' "
# add some text at the top, aligned left (north-eastWEST). Position from top right
overlay_text_cmd += " -gravity north-WEST -fill black -pointsize 18 -annotate +80+12 '" + header_text + "' "
# add some text at the top, aligned right (north-east). Position from top right
#overlay_text_cmd += " -gravity north-east -fill black -pointsize 16 -annotate +12+12 '" + header_text + "' "
# draw a transparent black underlay at bottom
overlay_text_cmd += " -strokewidth 0 -fill 'rgba( 0, 0, 0, 0.5 )' -draw 'rectangle 0,480 720, 420' "
if humidity is not None and temperature is not None and moisture is not None:
status += '\nTEMP : {0:0.1f}°C HUM : {1:0.1f}% MOISTNESS : {2:0.1f}'.format(temperature, humidity, moisture_perc)
# add some text at the bottom, aligned left (south-west). Position from bottom left
overlay_text_cmd += " -gravity south-west -fill white -pointsize 16 -annotate +12+8 '" + status + "' "
light_text = 'LIGHTNESS : ' + str(light_value)
light_text += '\nR : {0} G : {1} B : {2}'.format(r, g, b)
overlay_text_cmd += " -gravity south-east -fill white -pointsize 16 -annotate +70+10 '" + light_text + "' "
overlay_text_cmd += " -strokewidth 0 -fill 'rgb( "+str(r)+", "+str(g)+", "+str(b)+" )' -draw 'circle 680,450 680,470' "
# the output file path (same as input)
overlay_text_cmd += output_folder + photo_name
print "setting text: " + status
call ([overlay_text_cmd], shell=True)
# to add an image ..
print "adding logo" # you'll need a file called overlay.png in /home/pi
overlay_logo_cmd = '/usr/bin/convert '+ photo_path + ' /home/pi/_SENTREE/pikogro2.png -geometry +10+0 -composite ' + photo_path
call ([overlay_logo_cmd], shell=True)
print "added logo"
get_temp_humidity()
take_photo()
#camera.start_recording("test.h264")
#sleep(5)
#camera.stop_recording()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment