SunFounder 37 Modules Sensor Kit V2.0 for Raspberry PI 2: Using relay to activate IR – floodlight and take pictures at night

20151115_ir_photoAnother interesting item in the SunFounder 37 modules sensor kit is the relay module. This can be used to control electrical devices that cannot be connected directly to the Raspberry Pi because of higher voltage or current. In this example the relay will activate a floodlight built with 32 infrared LEDs to take a photo in the dark using the Raspberry Pi NoIr camera.


20151115_ir_cameraThe NoIr camera module for Raspberry Pi does not have an infrared filter like normal cameras. So it can be used for taking photos in the dark together with a floodlight built with infrared leds. This light is not visible and so it’s possible to take photos of animals without disturbing them. The IR floodlight used for this example is built out of 32 infrared LEDs. The voltage needed for this is 9V what is much too high for the Raspberry Pi and also the current needed for this light is more than then Raspberry PI can deliver. So we will use the relay module and a set of own batteries for this.

Again we will use the python bottle framework to build a simple web interface containing a button for taking a picture and the result as an image.

photo.py

#!/usr/bin/env python
from bottle import route, run, template,static_file
import RPi.GPIO as GPIO
import picamera
import time

# Pin for relay connected to IR floodlight
IR=11

# init GPIO and switch relay off
def setup():
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(IR,GPIO.OUT)
        GPIO.output(IR,GPIO.LOW)

# cleanup
def destroy():
        GPIO.output(IR,GPIO.LOW)
        GPIO.cleanup()

# the main interface
@route('/')
def index():
        return template('index')

# serving static files (the photo in this case)
@route('/static/<filename>')
def server_static(filename):
    return static_file(filename, root='./static/')

# switches on the floodlight and takes a picutre
@route('/photo')
def photo():
        GPIO.output(IR,GPIO.HIGH)
        camera = picamera.PiCamera()
        camera.capture('./static/photo.jpg')
        camera.close()
        GPIO.output(IR,GPIO.LOW)

if __name__ == '__main__':
        setup()
        try:
                run(host="0.0.0.0", port=8080)
        finally:
                destroy()

The photos will be stored in the folder named „static“, so it needs to be created:

mkdir static

We also need a template again, stored in „views“

./views/index.tpl

<html>
<head>
<title>Dual color LED control</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
function reloadPicture() {
  d = new Date();
  $("#photo").attr("src", "/static/photo.jpg?"+d.getTime());
}
</script>
<h1>Camera control</h1>
<img id="photo" src='static/photo.jpg'/><br/>
<button type="button" onclick="$.get('/photo', function(){reloadPicture()})" name = "photo" value = "photo">take photo</button>
</body>
</html>

When the button „take photo“ is pressed, the IR floodlight will be switched on, then a photo is taken and stored in static/photo.jpg and the floodlight will be switched off again. After that the picutre is reloaded so that the current picture is shown.
20151115_ir_gui

2 Gedanken zu „SunFounder 37 Modules Sensor Kit V2.0 for Raspberry PI 2: Using relay to activate IR – floodlight and take pictures at night

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.