Another 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.
The 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.
#!/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:
We also need a template again, stored in „views“
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.