Beruflich Dokumente
Kultur Dokumente
Dissertation submitted in partial fulfilment for the degree of BSc (Hons) Computing
Science
Department of Computing Science and Mathematics
University of Stirling
Abstract
The Raspberry Pi a newly released credit card sized microcomputer has sparked a lot of
interest since its release resulting in a large amount of projects being carried out using it. This
project looks at using the Raspberry Pi to create a cost efficient data logger. Data loggers are
generally expensive and limit what the end user can achieve, this project allows the end user
to create what they wish what they want to record, where they want to record it it allows
the device to be expanded which no expensive data logger offers. This work researches
various software packages and hardware available allowing a cost efficient data logger. The
finished data logger combines the Raspberry Pi and the Gertboard with a temperature sensor,
optical sensor, LEDs and a webcam and allows the user to control it both on the device and
remotely. This is a simple and effective solution with scope for expansion.
Attestation
I understand the nature of plagiarism, and am aware of the Universitys policy on this. I
certify that this dissertation reports original work by me during my University project except
for the following:
The ffserver configuration code in Appendix B was largely adapted from
http://sirlagz.net/2012/08/04/how-to-stream-a-webcam-from-the-raspberry-pi.
The code for uploading data to COSM in the main program (Appendix E) was largely
adapted from http://learn.adafruit.com/send-raspberry-pi-data-to-cosm/python-script.
The code for receiving an analogue to digital conversation value in the main program
(Appendix
E)
was
largely
adapted
from
https://sites.google.com/a/joekamphaus.net/raspberry-pi-spi-interface-to-mcp3002/
The code for editing the terminal to allow key detection in the main program
(Appendix E) was largely adapted from https://bbs.archlinux.org/viewtopic.php?
pid=105189#p105189
The code for reloading the SPI drivers to prevent SPI failures (Appendix E) was
largely
adapted
from
http://www.raspberrypi.org/phpBB3/viewtopic.php?
p=271375&sid=bc3f254f5c652710b9a645acc48ab771#p271375
Signature
Date
Acknowledgements
I would like to thank my supervisor, Dr Andrew Abel. From finding an appropriate topic for
the project to the writing of the thesis he has offered constant support, motivation and
feedback. I would also like to thank Professor Leslie Smith for ideas during the initial
proposal of the project.
Table of Contents
Abstract...............................................................................................................................1
Attestation...........................................................................................................................2
Acknowledgements.............................................................................................................3
Table of Contents.................................................................................................................4
List of Figures.....................................................................................................................5
1 Introduction..................................................................................................................6
1.1 Background and Context.......................................................................................6
1.2 Scope and Objectives............................................................................................7
1.3 Achievements........................................................................................................7
1.4 Overview of Dissertation......................................................................................7
2 State-of-The-Art...........................................................................................................9
2.1 Hardware...............................................................................................................9
2.2 Commercial data loggers.....................................................................................10
2.3 Solar Logger........................................................................................................11
2.4 Garage monitor...................................................................................................11
2.5 PiEye...................................................................................................................11
2.6 Temperature Sensor.............................................................................................12
2.7 Summary.............................................................................................................12
3 Development...............................................................................................................13
3.1 Design.................................................................................................................13
3.1.1
Installer.......................................................................................................13
3.1.2
Data logger..................................................................................................13
3.2 Implementation...................................................................................................16
3.2.1
Software......................................................................................................16
3.2.2
Hardware.....................................................................................................19
4 Testing and Evaluation................................................................................................20
4.1 Testing.................................................................................................................20
4.1.1
Data logging over the network....................................................................20
4.1.2
Data logging on the SD card.......................................................................20
4.1.3
Taking a single image..................................................................................21
4.1.4
Data logging with no webcam plugged in...................................................21
4.1.5
Single image capture with no webcam plugged in......................................21
4.1.6
Using unauthorised keys.............................................................................22
4.1.7
Testing the optical sensor............................................................................22
4.1.8
Testing the temperature sensor....................................................................23
4.1.9
Using a standard user account.....................................................................24
4.2 Issues and limitations..........................................................................................24
5 Conclusion..................................................................................................................26
5.1 Conclusion..........................................................................................................26
5.2 Future Work........................................................................................................26
5.2.1
Portability....................................................................................................26
5.2.2
Sensors........................................................................................................27
5.2.3
Audio feedback...........................................................................................27
5.2.4
Recording software.....................................................................................27
References.........................................................................................................................28
Appendix A Source code for FFServer.conf file................................................................30
Appendix B Source code for installation file.....................................................................31
Appendix C Software Installation guide............................................................................32
Appendix D User guide.....................................................................................................36
Appendix E Source code for logger.py Program................................................................41
List of Figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Figure 10.
Figure 11.
Figure 12.
Figure 13.
Figure 14.
Figure 15.
Figure 16.
Figure 17.
Figure 18.
Figure 19.
Figure 20.
Figure 21.
Figure 22.
Figure 23.
Figure 24.
Figure 25.
1 Introduction
The aim of this project is to create a functional data logger using the Raspberry Pi that is
capable of recording information from the webcam, microphone and various GPIO sensors
such as temperature and optical sensors. This will allow the user to control the data logger
using buttons connected via GPIO. The Raspberry Pi required various software packages and
Python modules to be installed for the Raspbian Wheezy operating system to allow
interaction with both USB devices and GPIO devices.
The project aims to allow anyone who has a spare webcam (compatible with the Pi) or
microphone and a Raspberry Pi to use the software in order to create a data logging device,
which could be used for things such as a cost efficient CCTV camera, recording audio
information (such as notes the user may wish to hear back later). If the users have a battery
pack this would enable it to be potentially used anywhere (limited only by battery life) even
in extreme cases such as being used to record climbing mountains or bungee jumping. The
final device will allow the user to upload the video and audio at a later date (or stream if
wirelessly connected to a network). This can be extended to make use of the GPIO by
including other sensors (a typical temperature sensor such as the one used in this project the
TMP36 costs about one pound).
The project that has been undertaken is to develop a portable data logger using the newly
released Raspberry Pi microcomputer. As the Raspberry Pi was newly released onset of this
project (the official release was 29 February 2012) this involved research into the device, as
well as any required hardware to ensure there were no compatibility issues, the operating
systems available to such a device, the software available which would allow the device to
interact with sensors (such as webcams and microphones) and interaction with the GPIO
(General Purpose Input / Output). The GPIO is a collection of small pins on the Raspberry Pi
which allows devices to be attached and controlled. The operating system can detect and
configure for either input or output, and this project makes use of this to allow LEDs to be
enabled and disabled for purposes of user feedback, and also allow buttons to be pressed
(enabling interaction with the program and the carrying out of commands such as initialising
data logging) as well as receiving sensor data (temperature and optical for this project,
however many more exist) using the GPIO pins. GPIO pins can be used using various
different methods; this project makes use of the newly released Gertboard 15, which slots onto
the GPIO pins to extend the capability of the GPIO. The Gertboard includes motor controller,
dual channel digital to analogue converter, dual channel analogue to digital converter, onboard LEDs, buttons15.
Raspberry Pi a $25 Linux box22 and a small number of accessories which includes; an
optical sensor23, a temperature sensor24 and a webcam25, all of which can be purchased for
under 25 to turn the Raspberry Pi into a data logger capable of recording and steaming
webcam video, storing webcam video and audio and recording via text document, internet
upload graphing temperature and optical sensor data in real time.
1.3 Achievements
Developments that have occurred through the duration of the project include:
Learning to use Raspbian Wheezy operating system
The overall achievement from this project is a Raspberry Pi with the suitable software and
hardware to allow it to record data from sensors; this includes two items of software one
which runs the Python script to allow data logging and one to assist in the installation of the
software with the installation guide.
2 State-of-The-Art
Although the Raspberry Pi is a relatively new device (officially released 29 February 2012) it
has generated a massive amount of interest due to the low cost of the device, this has resulted
in a massive amount of projects being undertaken on the Raspberry Pi in the short time it has
been available.
2.1 Hardware
To the best knowledge of the author, no project of this exact specification has been
developed. Data loggers using the Raspberry Pi appear to be common however little appear to
be based on visual and audio information, using GPIO inputs and outputs as well as allowing
portability. The GPIO (General Purpose Input / Output) is a collection of pins which can be
accessed on the Raspberry Pi to allow use with other electrical components such as LEDs,
buttons, and sensors. The project also makes use of the Gertboard Rev 2 which has only
recently been released (officially released 17 Jan 2013) 11 to interact with and extend the GPIO
capabilities, the Gertboard connects directly to the Raspberry Pi GPIO pins by slotting on to
the pins, it contains several LEDs, switches, converters, and a motor controller, one of these
converters is the MCP3002 analogue to digital converter which is built into the board. The
fully assembled Gertboard is shown in Figure 1 (image taken from ModMyPi 32)
development from RS Components UK is the camera board, they aim to produce a camera
board which is capable of 5 megapixels and can record 1080p H.264 videos at 30 frames per
second. The Adafruit Pi Cobbler is a breakout kit for the Raspberry Pi; it allows the user to
make use of a ribbon-type cable to connect the GPIO pins to the custom PCB which can then
be used on a solderless breadboard allowing easy connection to specific gpio pins. The
Adafruit Pi Cobbler is shown in Figure 2 (image taken from eBay33)
10
11
2.5 PiEye
A similar project is the PiEye by Calin Crisan 4 in which he uses a webcam to do a live
stream of his aquarium over the internet 5. Similar to the PiEye project is the product from one
of the retailers of the Raspberry Pi (RS Components UK) which have been developing a new
camera board (which would be used instead of a webcam) 7, in this they aim to produce a
camera board which is capable of 5 megapixel and recording in High Definition (1080p) this
uses CSI pins (camera serial interface, visually looks similar to GPIO pins) 8. Another similar
project is the guest blog posted on the official Raspberry Pi website from Francis Aguis 10 in
which he uses the Raspberry Pi and a webcam to live stream his bird table in the garden 9.
These projects mainly focus on recording the visual data but again appear to lack the audio as
well as any GPIO capabilities. The PiEye project makes use of the package MJPG streamer
which streams the video over the network but does not appear to allow storing it on the device
whereas Francis project makes use of Motion which is used to detect if part of the image
from the webcam has changed19 and FFMPEG to stream the webcam over the network. This
project similarly makes use of FFMPEG and FFServer to stream the webcam over the
network and store it on the SD card; neither of these projects appears to include the ability to
save it to the SD card which is one of the features of this project.
2.7 Summary
There are a small number of data loggers which exist but to the best knowledge of the
author all of them either appear to focus solely on GPIO use for temperature, current, and
other sensor readings or focus solely on visual, none appear to make use of GPIO and USB
sensors and GPIO inputs and outputs. A small number of the projects allow for portability
such as the PiEye and Francis project which can be used outdoors and moved. None of the
projects make use of the Gertboard to extend the GPIO and appear to stick to standalone
analogue to digital converters such as the MCP3008 and a small number use the same
temperature sensor the TMP36 as this appears to be a reliable and cost efficient sensor. No
projects appear to allow recording video and audio through USB while connecting to the
GPIO using a Gertboard and making use of temperature and optical sensors, using buttons to
12
control the program and LEDs to show the current status which is required for efficient data
logging and so a project that does this and investigates portability is of interest for further
development.
13
3 Development
3.1 Design
Two programs were created during this project, one to aid in the installation of software
and another to run the main program to allow data logging. Both of these run in the terminal
and have no Graphical User Interface designed, the installation program only requires the user
to input Y/N for saving required modules. The data logging program only allows externally
connected computers to input 1, 2, 3, and Q. No other interaction is required between
the user and program other than these or through use of the GPIO buttons.
3.1.1
Installer
The installation program (installation.py) makes use of two modules, os and time, the
os module is used to allow Python to interact with the operating system to create system
calls which allow Python to make use of the terminal, the time module is used for the sleep
command. The program then uses system calls to run various commands allowing it to
download the necessary software packages and Python modules, as well as creating a
directory for the logger on the desktop. The installer was produced to allow an efficient
method of downloading the software packages and Python modules that are required by the
data logger. It creates a specific folder for the logger in which all the data will be saved in.
This saves the user from having to sit at their computer downloading each required package
and module which could take hours depending on their network connection.
3.1.2
Data logger
The data logging program makes use of several modules and functions. These modules
allow the ability to extend the Python library which allows extension of the functionality. The
Python program makes use of several different functions, this allows the program to create
different processes for each of the function calls this would ensure that if an error occurred
with one of these processes it would not terminate the overall program and continue to run the
other processes (for example if the process for recording the optical sensor failed, both
webcam recording and temperature sensor recording are done through two other processes
both of these would continue to work successfully, if one function was used in the same case
data logging would end as one section of the function had failed). The program makes use of
command line interface rather than using a graphical user interface, as the Raspberry Pi has
very limited resources available this ensures no extra resources are used simply to run the
GUI. With the use of GPIO buttons to allow control of the program there was simply no need
for an extra GUI to be implemented, which would also require more space on the SD card
allowing less recordings to be saved. Accessing the data logger remotely allows overwriting
of the GPIO buttons, allowing 1, 2 and 3 to act as buttons (B1, B2 and B3) if the user is
unable to physically press the buttons then its still possible to run the data logger.
3.1.2.1 COSM
COSM (previously Pachube) is a service which allows users to connect to and upload
sensor readings. This project makes use of EEML (Extended Environments Markup
Language) which allows a connection to COSM to upload sensor reading in real-time, COSM
then records the data and automatically graph the data. This allows sharing of sensor data
14
with anyone in the world with an internet connection as well as the ability to monitor the
device from anywhere in the world. COSM allows users to read and graph data stored up to
three months old. COSM allows users to see webpages for individual feeds (one sensor) or
view a persons profile which includes any sensors they may upload. EEML connects to
COSM using an API key, an API feed number and an API URL, using this it then sends a
single reading from the sensor then COSM does the rest (updating the graphs and updating
the website reading). An alternative to COSM is open.sen.se; however this appears to be an
invite only service for now. Open.sen.se appears to offer better graphing (use of colours,
multiple figures plotted on one graph instead of a different feed for each). This project made
use of COSM to create two feeds, one for the temperature sensor data and one for the optical
sensor data. Using EEML this connects to COSM using the API url, key and feed number to
update with real time sensor readings and graph the data.
3.1.2.2 Modules
The data logging program (logger.py) makes use of eleven modules which includes os,
time, datetime, RPi.GPIO, spidev, multiprocessing, eeml, evdev, signal,
termios and thread. The os module is used by Python to allow interaction with the
operating system, throughout this file it is used for various commands such as; allowing
system calls similarly to the installation program (these are used to run external software
packages such as ffmpeg as well as resetting the terminal on close), get an environment
variable from the operating system to determine if the user is connected remotely or not, the
ability to send a kill signal for a process, and reading a key press from the terminal. The
time module allows Python access to various time related functions, in this program the
module has various uses which include the ability to get the current time which is then
formatted and used for a timestamp on newly created files and sensor recording. It is also
used to allow sleep calls which are used to allow sufficient time for reading messages, prevent
buttons from being detected several times due to holding it down, and spacing out the time
between processes starting. The datetime module allows manipulation of dates which
includes the format, in this program it is used to allow a string to be produced from the time
integer received from the time module.
The RPi.GPIO module allows the Python program to communicate with the GPIO pins
on the Raspberry Pi, this module is used to allow gpio pins to be set up as an input or output
and then attempt to detect changes such as a button press (this would change the input from 1
to 0), it is also be used to make changes such as enabling and disabling an LED, when closing
the program it is also used to perform a clean-up which resets the gpio ports to defaults before
the program was run. The spidev module is used to allow the Python program to
communicate with the SPI port. This is used to send and receive bits from the sensors; this
allows it to read the raw values from the sensors. The multiprocessing module is used to
allow various different functions to be run concurrently; this also allows Python to terminate
the processes which threading does not offer in Python. It is used to run each of the sensors
individual functions when data logging begins. The eeml module is the extended
environments mark-up language which allows connection to COSM to be able to send real
time sensor information.
15
The evdev module is used to import UInput and ecodes. This is used to allow Python to
create an input device and inject key presses, allowing a key press to end the webcam stream
when connected locally. The module signal is used with the os module to allow Python to
send a kill signal for the ffmpeg process if it is running when the user wishes to begin data
logging. The termios module is used to modify the terminal that the program is running in,
this gets the current terminal, modifies it to allow detection of key presses. When exiting the
program the reset allows the terminal to return to its unmodified state. The thread module is
used for threading within Python. This is used for the key listener function as it does not need
to be terminated during the running of the program and will run in the back waiting for a key
press, threading is used rather than multiprocessing as it allows use of shared memory, this
allows it to alter global variables.
3.1.2.3 Functions
The data logging program makes use of several different functions, each of these functions
provide a purpose to the main section of the program. The functions in this program are
get_adc, recordTemp, recordLight, startRecord, stopRecord, singleImg and
keyPress. The get_adc function receives a channel parameter, this function opens a spidev
wrapper to communicate with the SPI port and send bits to and receive bits from the SPI
channel, and this gets the output from the sensors, it then uses this raw data to return them to
the function which called it.
The recordTemp function sets up a variable using time and datetime modules to get the
current timestamp, it then calls the get_adc function to receive the reading for channel 1
(temperature sensor), converts this reading to a Celsius reading and Fahrenheit then opens a
file to write it to as well as sending it to COSM. The function then gets put to sleep for 30
seconds then repeats, it will repeat until the logging is cancelled or the program is closed. The
recordLight function is similar to recordTemp but calls get_adc function using the parameter
0 indicating the light sensor is on channel 0. This function just simply writes the reading value
to the file and COSM without any calculations. This function again waits 30 seconds then
repeats, and will repeat until logging is cancelled or the program is closed. The startRecord
function receives a method parameter indicating the method of logging for the webcam
(store or stream); the function sets up a list of parameters which allow changing of frame
rates, frame size, and other important parameters. The function then gets a list of the open
processes attempting to check if ffmpeg is running, if so end the process. The method variable
is used to determine which code is run to store to SD card or to stream over the network, and
then the appropriate variables are used within the system call.
The stopRecord function checks if data logging is currently in process, if it is it then
terminates the three processes if they are alive (a process may have terminated prior to this if
it failed, for example no webcam plugged in). If locally connected it then uses evdev to create
an input device and inject q to end the webcam stream. The singleImg function is similar to
the startRecord; it sets up variables to hold information such as the frame size, input name,
and other important parameters. It then checks if the ffmpeg process is running, if it is
attempts to kill it then run the system call to take the image.
16
The keyPress function is used to modify the terminal which allows reading of inputs. It
then checks these and passes them back out of the program to be used for overwriting the
buttons. This function originally posed as a problem as to start with the function was called
using multiprocessing, doing this caused issues with detecting buttons and the ability to pass
the key back out of the function as multiprocess functions do not share the same memory, so
is unable to access global variables or return any variable from the function. This problem
was overcome by using threading, threading allowed access to global variables allowing the
detected key to be passed back out of the thread.
3.1.2.4 Exception handling
The functions in this program all include exception handling, each function will include a
try and except KeyboardInterrupt which is the exception if the user presses Ctrl + C the
program will end, at the same time the program will perform a system call to reset the
terminal (removing the previous edits it made to allow key detection) as well as a clean-up of
the gpio ports ensuring no random LEDs are left on. If the user does not cause a
KeyboardInterrupt then it will be skipped.
3.1.2.5 Main section
The main section of the data logging program is an infinite loop which will wait for
detection of a key or button, once either of these are detected the appropriate section of code
will be executed resulting in a change to the LEDs, variables and a function call. Various
sleep calls are used here to prevent several detections from one button press, again this
section of code includes a try and except KeyboardInterrupt clause which performs the
gpio clean-up and terminal reset ensuring the terminal is back to how it was before the
program was run. There are various prints used throughout the program to assist the user and
give the user feedback to their interaction, such as recording has started and recording method
has changed.
3.1.2.6 Output
The end user will see a terminal in which any appropriate messages will appear. When the
program begins the user may get a message if they are connected remotely allowing the
ability to overwrite the GPIO buttons, a message will also appear stating the program has
fully loaded. Enabling data logging will show another suitable warning message for users
then begin data logging, during data logging every 30 seconds the temperature and optical
sensors reading will be displayed. The user will receive a message when changing the
streaming method between store to the SD card or stream over the network. The user will also
receive a message detailing the process of an image capture. Figure 4 shows typical output the
user will see when beginning the program and beginning data logging, when remotely
connected.
17
Figure 4. Typical output the user will see when Data Logging
3.2 Implementation
To implement this project and create a data logger using the Raspberry Pi, an investigation of
the available hardware and software was required.
3.2.1
Software
The project includes a variety of software packages and Python modules installed onto the
Raspberry Pi to allow data logging, this includes:
Raspbian Wheezy
o This is the free operating system that is based on Debian and has been
customised and optimized for use on the Raspberry Pi.
o This project makes use of Raspbian Wheezy as the operating system.
o It was decided that Raspbian was suitable for use as it was the most
recommended version from the Raspberry Pi Foundation and had been
designed specifically for the Raspberry Pis hardware. Raspbian is widely
supported, offers both command line and GUI, and is capable of
everything required for this project
Python
o The programming language used for the main software in the project, this
comes pre-installed on Raspbian Wheezy. This allows interaction with the
USB (for peripherals such as webcam and microphone), GPIO (taking
inputs, setting outputs) and interacts with the operating system (making
use of terminal).
o This project makes use of Python as the sole programming language.
Geany
o This is a software package; Geany is a lightweight IDE (integrated
development environment) for various languages including Python.
o This project makes use of Geany when editing the Python code and
running it from within the Raspberry Pi Graphical desktop.
FFMPEG
o This is a software package that allows the Raspberry Pi to interact with the
USB webcam or microphone, it is used to capture the webcam, stream it
over the network or save it on the SD card. This needs to be installed with
18
FFServer
o This is a software package that allows the Raspberry Pi using FFMPEG to
stream over a network or internet allowing other devices to connect to the
stream and view it. This is installed with the FFMPEG package and needs
a configuration file to set some parameters (such as address port, bitrate,
frame size, and other important parameters).
o This project makes use of FFServer when the logging method is set to
stream at which point ffserver will run and FFMPEG output will be set
to the ip and port that FFServer is currently running on.
Fswebcam
o This is a software package that allows the Raspberry Pi to capture a single
image from the webcam, this needs to be installed with some knowledge
on the arguments used (again such as frame rates, frame sizes, output
location, and other important parameters).
o This project allows a single image to be taken using the right button /
number 3; this calls the Fswebcam code using various variables again
allowing the user to set the frame size, input name, save location.
uvcdynctrl
o This is a software package that allows the Raspberry Pi to configure the
webcam; this includes options such as changing this brightness, contract,
hue, saturation, sharpness, and zoom. It can also be used to list all the
current webcam devices detected by Raspbian, and all the formats
compatible with these devices
o This project made use of the uvcdynctrl software package to receive the
available frame sizes and pixel formats that can be used with this webcam,
it will also allow other users to make use of the command to see the
available formats for their webcams and change the variables in the code
to suit.
Git
19
Python-RPi.GPIO
o This is a Python module that allows the Raspberry Pi and Python program
to communicate with the gpio pins and make use of them, this module is
required to make use of LEDs and buttons that are connected via the
Gertboard or directly on the GPIO pins.
o This is used within the project to set up the GPIO buttons and LEDs then
further to enable/disable LEDs and listen for button presses, when exiting
the program this is again used to clean up the gpio (setting it back to the
default values before the program ran).
Py-Spidev
o This is a Python module that allows the Raspberry Pi and Python program
to communicate with SPI devices as the Python-RPi.GPIO module
currently does not allow interfacing with SPI devices.
o This is used within the project to communicate with sensors, this allows
Python to communicate with an SPI port, open a channel, send a signal
and receive a signal and make use of the received signal to get the current
reading on the sensors.
Python-dev
o This is a Python module that needs to be installed to allow PythonRPi.GPIO to be installed correctly.
o This is used during installation to allow the other Python module to be
installed and ran.
SetupTools
o This is a Python module again that needs to be installed to assist another
module; this is required for the EEML installation.
o Again this is used during installation to allow EEML to be installed and
run correctly.
Python-EEML
o This is a Python module thats used to communicate over the internet with
COSM (previously Pachube) which allows real time uploading of data and
graphing.
o This module is used within the project to allow the Python code to send
the sensor data to COSM using the API URL and API Key provided
20
during COSM registration, this also allows a data format to be set or send
raw data, in the project temperature is sent using Celsius and optical
sensor sent using raw data.
3.2.2
Python-evdev
o This is a Python module thats used to allow Python to create an input
device and allow input events to be placed directly into the system.
o This module is used within the project to create an input device then inject
the key event q into it, used when ending the data logging to allow the
webcam stream to end.
Hardware
SD card An SD card is used to run the Raspbian Wheezy operating system with
all the software packages and Python modules installed as well as the two Python
programs to install and run the data logger.
Microsoft LifeCam VX-2000 This Microsoft webcam is used for visual and
audio data (using the inbuilt microphone), this is not a verified peripheral however
testing of this peripheral revealed no issues with functionality, and so it was
deemed suitable for use26.
Gertboard This project makes use of the Fully Assembled Gertboard Rev 2
which slots directly on to the Raspberry Pis GPIO pins to extend its functionality,
this is set up to allow use of the 3 on-board buttons and using the on-board
MCP3002 to connect to the optical and temperature sensor located on the
breadboard, it also supplies 3V3 and GND to the breadboard as well as the
appropriate GPIO pins.
Breadboard This is used to connect the LEDs and sensors to the Gertboard and
supply power to them.
TMP36 This is the temperature sensor used in the project; it is a low voltage
sensor that records between -40C to +125C 27. This is connected to the
Gertboards on-board analogue to digital converter to allow a digital reading within
Python.
TSL250 This is the optical sensor used in the project, it is a low power light-tovoltage optical sensor. This is connected to the Gertboards on-board analogue to
digital converter to allow a digital reading within Python.
21
22
4.1.1
Test case Enabling data logging, streaming webcam feed over the network
o In this test case, the goal was to enable data logging and stream the
webcam feed over the network. To do this it required changing the logging
method and starting the logging.
Test steps Run the program, press the middle button (or 2), press the left button
(or 1)
Expected results The program should show information then start streaming over
the network with the ability to connect to it via VLC
Result Pass, as expected (Figure 5)
4.1.2
Test case Enabling data logging, storing the webcam feed on the SD card
o In this test case, the goal was to enable data logging and store the webcam
feed on to the SD card. To do this it required starting the logging.
Test steps Run the program, press the left button (or 1)
Expected results The program should show information then start recording to
the SD card
Result Pass, as expected (Figure 6)
23
4.1.3
Expected results The program should show that it is taking an image then store it
4.1.4
24
4.1.5
4.1.6
Test case Pressing keys which are not assigned to do something checking the
result
o In this test case, the goal was to press keys which are not assigned by the
Python program to do anything, showing the feedback the user gets and
continuing to function as before.
Test steps Run the program, press random keys
4.1.7
Test case Flashing a light on the optical sensor to attempt to alter the readings
25
In this test case, the goal was to test the accuracy and reliability of the
optical sensor by altering the light available to the sensor (through use of a
torch) ensuring that it reports lower readings if the light increases.
Test steps Record five optical sensor readings, put a flashlight near the optical
sensor and record another five while altering the brightness of the flashlight (first
2 full brightness, 3rd medium brightness, 4th lowest brightness, 5th off)
Expected results Second five numbers should be lower than first five
o
4.1.8
Test case Place freezing item next to temperature sensor to attempt to alter the
readings
o In this test case, the goal was to test the accuracy and reliability of the
temperature sensor by altering the temperature around the sensor (using a
frozen item), ensuring that it reports a lower temperature when the item is
next to it than prior to it being there.
Test steps Record five temperature sensor readings, put a frozen item next to the
temperature sensor and record another five
Expected results Second five numbers should be considerably lower than the
first five
Result Pass, as expected (Figure 12)
o First five: 17.0, 17.0, 17.0, 17.0, 17.7
o Second five: 10.9, 10.9, 9.3, 9.0, 8.7
26
4.1.9
Test case Running the data logger with a standard user (not root) who has no
access to specific parts of the system that is required for data logging
o In this test case, the goal was to use a standard user account to attempt to
run the data logger which requires root access.
Test steps Log into the Pi using pi user, run the program
27
28
errors, although it will still record with these during play back of the video file it will appear
to skip a frame when each of the errors occurs.
Several other packages were tested such as MEncoder and guvcview, MEncoder attempts
to do the same as ffmpeg does but similarly frame drops still occurred, a lower video quality
was created, and had issues with the webcam used for this project. Guvcview appears more
preferred for graphical interface rather than over a command line interface resulting in little
use for this project. Removing audio from the data logging appeared to allow ffmpeg to run
fine when storing it to the SD card. It would definitely appear that it is a limitation of the
CPU, and appears to be attempting to record too much data which is causing random skipping
of frames. Although the error exists the output file is not corrupt and still usable, at a cost of a
few frames based on the length of the recording.
Similarly with the previous problem, when FFMPEG ends the streaming and storing it to
the SD card it can take some time to finish encoding the audio, which resulted in the need to
implement code to kill the previous process when starting a new process if it was still
running. The time for encoding the audio is typically instant or a few seconds and can vary by
the length of the video or the amount of audio information stored, however in some tests this
was taking over ten minutes to fully encode and end the process. During which would not
allow the user to start a new stream as the process is already running, implementation of this
code will kill the previous ffmpeg process (meaning the audio in the file may be corrupt or
not complete) and allow the new process to start. This could again be a limitation caused by
the lack of processing power to allow the encoding of the file.
29
5 Conclusion
5.1 Conclusion
The goal of this project was to create a functional data logger using the Raspberry Pi,
which would involve developing of software to make this possible. The data logger would
allow the data to be stored locally as well as the possibility of it being sent across the
network/internet. The project would make use of both USB and GPIO sensors to allow
logging and allow control of the device through GPIO.
The author believes the goal has been met with this project, the final product is a
functioning data logger allowing recording of temperature, optical data as well as streaming,
storing and single image capture from the webcam. Users have the ability to control the
device through the GPIO buttons and receive feedback on their actions via the GPIO LEDs.
The project includes the development of two items of software, an installer and the logger.
This software allows a user to set up their own data logger, the logger allows remote access to
control it or be controlled via buttons on the GPIO, this will run commands allowing the
stream to start, method to change, and single image capture. The data logging makes use of
the online service COSM to create real-time graphs using the sensor data. The user can make
use of the user guides and installation guides created for this software to assist them in using
it.
The project mainly went according to plan creating the data logger; however a few issues
were encountered. Although the storing to SD card with audio does not work as intended due
to the limitation, removal of audio would allow a fully functioning data logger with no errors
caused by audio or CPU limitations. This project has successfully allowed for a low cost data
logger to be produced with the ability to be used in the real world with various different uses
and the ability to extend on the data loggers functionality such as portability and additional
sensors.
5.2.1
Portability
The first major improvement would be portability; this was initially one of the planned
goals for this project however due to time this was unable to be researched sufficiently and
implemented, to extend the data logger to include portability this would involve the
purchasing of a 5V regulated output external battery pack which is compatible with the
Raspberry Pi, eLinux contains a list of verified peripherals which includes working external
battery packs of which its users have tested and commented on 28. TeckNet iEP392 Dual-Port
12,000 mAh External Power Bank appears to be the most favoured choice for external battery
packs, with reported use of up to 16.5 hours before requiring a recharge, although this would
be reduced due to the amount of power being used to stream/save webcam footage it would
still allow quite a few hours of portable data logging which could be useful for outdoor trips.
30
To allow portability it would also include purchasing a USB WiFi adapter to allow EEML to
connect to COSM for graphing as well as network streaming if required, these typically
require more power than a standard Raspberry Pi USB port provides so may be required to
use an externally powered USB hub, alternatively the EW-7811Un from Edimax works
without the need for an externally powered USB hub and should work with the webcam in
one USB port and this adapter in the other, the EW-7811Un also appears to be the only WiFi
adapter that is instantly recognized by the Raspbian Wheezy operating system (as of 2012-0918 version) so should be a simple installation just editing the SSID and key29.
5.2.2
Sensors
Another area for extension is sensors, although currently the Gertboard only has a two
channel analogue to digital converter, this can be resolved by making use of MCP3004 or
MCP3008 analogue to digital converters, allowing 4 channel and 8 channel respectively. This
would allow for addition sensors (two to six) to be used with the data logger. Each sensor
would simply need a new function to retrieve the data then make use of it (print it, store it
then send it to COSM), there are various sensors available on the market such as sound,
proximity, location/GPS, movement, touch, humidity, radiation, pressure, biometric, infrared,
weather, and countless others. There are hundreds if not thousands of sensors available to
implement onto the data logger which would increase the usefulness of the data logger as it
has the ability to record even more relevant data than before, it would also increase the gap
between this and any other data logger as they typically stick to very few different sensors.
5.2.3
Audio feedback
The product can be extended to include some form of output possibly via audio feedback
such as a buzzer, this could be used for various situations such as warnings to the user, for
example if the temperature is very low, if the proximity to something is very close and the
users speed is enough to impact, if theres any errors with the program such as faulty sensor
data. It would also be used to give audio feedback to button presses and an action taken, such
as beginning to record rather than relying solely on the green and red LEDs, i.e. if the data
logger is in a place that is hard to see the user may not know if they have successfully enabled
recording or not without the ability to see the data logger.
5.2.4
Recording software
Another improvement is the webcam recording software, currently the FFMPEG software
makes use of the full CPU which in turn limits its ability to successfully record both visual
and audio data in the case it is storing it to the SD card, this can cause an issue when
recording from the webcam and in turn an error appears. The only similar software to this
thats Raspbian Wheezy / Raspberry Pi friendly appears to be MEncoder which has issues
with the webcam used within this project, further research could be done into how to reduce
the CPU load that ffmpeg requires to possibly fixing the issue, if it is possible.
31
References
[1] Raspberry Pi data logger, http://neilbaldwin.net/blog/weather/raspberry-pi-data-logger/,
November 2012
[2] Raspberry Pi Solar Data logger, http://www.briandorey.com/post/Raspberry-Pi-SolarData-Logger.aspx, July 2012
[3] Home Solar PV and Water Current Report, http://home.briandorey.com/, November 2012
[4] PiEye
streaming
webcam
in
JPG
format
with
Raspberry
Pi,
http://www.bobtech.ro/tutoriale/raspberry-pi/78-streaming-webcam-in-format-m-jpg-curaspberry-pi, September 2012
[5] PiEye, http://pieye.dnsdynamic.com:8080/?action=stream, November 2012
[6] University of Cambridge Computer Laboratory: Raspberry Pi Temperature Sensor,
http://www.cl.cam.ac.uk/freshers/raspberrypi/tutorials/temperature/, November 2012
[7] Raspberry Pi Camera board, http://www.raspberrypi.org/archives/2555, November
2012
[8] YouTube - Raspberry Pi Camera, http://www.youtube.com/watch?v=8N7kgtL0-Ts,
November 2012
[9] Raspberry Pi Guest blog #7 Bird table webcam by Francis Agius,
http://www.raspberrypi.org/archives/2504, November 2012
[10] Raspberry
Pi
Bird
table
webcam,
http://www.raspberrypi.org/wpcontent/uploads/2012/11/Robin.avi, November 2012.
[11]Assembled
Gertboard
for
Raspberry
Pi
Element14,
http://www.element14.com/community/docs/DOC-51726/l/assembled-gertboard-forraspberry-pi, January 2013
[12] Raspberry Pi + Temperature Sensors, http://monirulpathan.com/afterhours/raspberry-pitemperature-sensors/, September 2012
[13] Simple ADC with the Raspberry Pi, http://scruss.com/blog/2013/02/02/simple-adc-withthe-raspberry-pi/, February 2013
[14] My Raspberry Powered Garage Monitor, http://brianhanifin.com/2012/11/raspberry-pigarage-monitor/, November 2012
[15] Assembled
Gertboard
for
Raspberry
Pi,
http://www.element14.com/community/docs/DOC-51726/l/assembled-gertboard-forraspberry-pi, January 2013
[16] Microchip
MCP3002
user
manual,
http://ww1.microchip.com/downloads/en/DeviceDoc/21294C.pdf, April 2013
[17] Microchip
MCP
3004/8
user
manual,
http://ww1.microchip.com/downloads/en/DeviceDoc/21295d.pdf, April 2013
[18] Home Solar PV and Water Current Report Gas and Electric meter readings,
http://home.briandorey.com/meter/default.aspx, April 2013
[19] Motion Web Home, http://www.lavrsen.dk/foswiki/bin/view/Motion, February 2011
[20] Data
Loggers
|
Datalogging
|
Data
Acquisition
|
Datalogger
http://www.loggershop.co.uk/, April 2013
[21] CnM
Secure
Wireless
Camera
Kit
at
Argos.co.uk
http://www.argos.co.uk/webapp/wcs/stores/servlet/Browse?
storeId=10151&langId=110&catalogId=23051&mRR=true&q=CCTV&c_1=1%7Ccate
gory_root%7CHome+and+garden%7C33005908&r_001=3%7CCamera+type
%7CCMOS+wireless%7C1, April 2013
[22] Raspberry Pi - http://www.raspberrypi.org/, April 2013
[23] TSL250R-LF
TAOS
PHOTODIODE,
SENSOR,
L/VOLTS,
http://uk.farnell.com/jsp/displayProduct.jsp?sku=1182346&action=view&CMP=GRHBFINDCHIPS1-1004321, April 2013
[24] TMP36
Temperature
Sensor
SparkFun
Electronics,
https://www.sparkfun.com/products/10988, April 2013
32
[25] Microsoft
Webcam:
LifeCam
VX-800
|
Microsoft
Hardware,
http://www.microsoft.com/hardware/en-gb/p/lifecam-vx-800, April 2013
[26] RPi VerifiedPeripherals eLinux, http://elinux.org/RPi_VerifiedPeripherals, April 2013
[27] TMP36 Datasheet and product info | Voltage Output Temperature Sensors,
http://www.analog.com/en/mems-sensors/digital-temperaturesensors/tmp36/products/product.html, April 2013
[28] RPi
VerifiedPeripherals
eLinux,
http://elinux.org/RPi_VerifiedPeripherals#Working_external_Battery_packs_.28with_5.C
2.A0V_regulated_output.29, April 2013
[29] RPi
VerifiedPeripherals
eLinux,
http://elinux.org/RPi_VerifiedPeripherals#Working_USB_Wi-Fi_Adapters, April 2013
[30] Raspberry Pi Wikipedia, http://en.wikipedia.org/wiki/Raspberry_Pi, April 2013
[31] MadgeTech
Temp101A
Miniature
Temperature
Data
Logger,
http://www.loggershop.co.uk/madgetech/temp101a-temperature-datalogger/prod_532.html, April 2013
[32] Gertboard Kit Fully Assembled, https://www.modmypi.com/raspberry-pi-fullyassembled-gertboard, April 2013
[33] Raspberry Pi Cobbler GPIO Super Starter Kit, http://www.ebay.co.uk/itm/Raspberry-PiCobbler-GPIO-Super-Starter-Kit-Breadboard-Cable-LEDs-Switches-/181071750824,
April 2013
[34] Pi-Face Interface for Raspberry Pi, http://pi.cs.man.ac.uk/interface.htm, April 2013
33
34
print 'WARNING: This could take a while, installing latest packages may take a while'
print 'FFMPEG will take a while to compile'
time.sleep(2)
# give time to read warning
os.system("sudo apt-get update")
os.system("sudo apt-get upgrade")
if not os.path.exists("logger"):
# check if the logger path doesn't exist
os.makedirs("logger")
# if so, create it
os.system("sudo apt-get install python-dev")
# install Python dev module
os.system("sudo apt-get install python-rpi.gpio")
# install gpio module
os.system("sudo apt-get install git")
# install git
os.system("git clone git://github.com/doceme/py-spidev")
# clone the github files
os.system("wget
http://pypi.python.org/packages/source/s/setuptools/setuptools0.6c11.tar.gz")
# get the compressed file
os.system("tar zxvf setuptools-0.6c11.tar.gz")
# get setuptools module
(req'd for eeml install)
os.system("wget -O geekman-python-eeml.tar.gz https://github.com/geekman/pythoneeml/tarball/master")
#get EEML module
os.system("tar zxvf geekman-python-eeml.tar.gz")
# extract it
os.system("git clone git://github.com/gvalkov/python-evdev.git")
# clone github files
os.system("sudo apt-get install fswebcam")
# get fswebcam
os.system("sudo apt-get install uvcdynctrl")
# get uvcdynctrl
print "Complete! Consult the user installation guide to proceed"
35
36
This navigates the user into the python-evdev directory, uses the versiontag which
updates files in the tree to match the version specified and runs setup.py to install.
9. Install FFMPEG
a. FTP or log into root then edit /etc/apt/sources.list
b. Add deb-src http://www.deb-multimedia.org sid main
c. Add deb http://www.deb-multimedia.org wheezy main non-free
d. Run sudo apt-get update
e. Run sudo apt-get install deb-multimedia-keyring
f. Remove deb http://www.deb-multimedia.org wheezy main non-free
from /etc/apt/sources.list
g. Run sudo apt-get source ffmpeg-dmo
h. Navigate to the folder using cd ffmpeg-dmo*
i. Run ./configure
j. Run make && make install
k. Remove deb-src http://www.deb-multimedia.org sid main from
/etc/apt/sources.list
This will download the ffmpeg files and install them, FFMPEG could take up to a
few hours to install (step I and J).
10. Create the ffserver.conf file in /etc/ The source code for this file is contained
with the documentation.
37
To view the list of available actions enter uvcdynctrl c, this will show a list
of controls for the device such as zoom, brightness, and contract.
To get the current value of any control enter uvcdynctrl g control where
control is the value the user wishes to obtain such as the contrast.
To set a value for any of the controls enter uvcdynctrl s control value for
example uvcdynctrl -s hue 0 to set the Hue value to 0.
To list the available frame formats enter uvcdynctrl f, this will give a list
of the available frame sizes, frame rates and pixel formats.
39
40
8. Restart the Raspberry Pi and the script should automatically start after logging in (if
not using the auto login).
Running the script:
NOTE: When running the python script try to make sure it is only loaded once (either
remotely or locally), if the user load it via SSH and again from the Pi directly it could
cause issues and unexpected commands.
Using graphical session:
To use the program on the graphical desktop a small number of extra steps will be required to
ensure the file can be executed
1.
41
Locate the middle button (indicated by S2) on the Gertboard and press it (or press 2
on a keyboard if remotely connected).
2. A message in the terminal should state that the Recording method has changed to
stream.
42
The default method when the script is loaded sets it to store data to the SD card (if it
has been changed press 2 or middle button/S2).
Locate the right button (indicated by S1) on the Gertboard and press it (or press 3 on
a keyboard if remotely connected).
2. The yellow LED will flash on for a second and off again.
3. A message in terminal should state Writing JPEG image to .
43
44
import os
#
used
for
terminal
commands
import time
# get timestamp to put on
files and gpio
import datetime
# used for timestamp
import RPi.GPIO as gpio
# access to the GPIO
import spidev
# for A/D converters
import multiprocessing
# for threading/multiprocessing
import eeml
# Markup language for
COSM
from evdev import UInput, ecodes as e # Imitating a key event
import signal
# for killing task
import termios
# for key presses
import thread
# for key presses thread
# reload spi drivers prevent spi failures
import subprocess
unload_spi
=
subprocess.Popen('sudo
rmmod
stdout=subprocess.PIPE)
start_spi
=
subprocess.Popen('sudo
modprobe
stdout=subprocess.PIPE)
45
spi_bcm2708',
shell=True,
spi_bcm2708',
shell=True,
conMethod = "remote"
# set the method to 'remote'
print yellow + 'INFO: As you are remotely connected you can overwrite buttons' +
endc
# print info
print yellow + 'Key 1 = GPIO #23 (Record), Key 2 = GPIO #24 (Stop record), Key 3
= GPIO #25 (Capture)' + endc # print info
else:
# if it's not remotely
connected
conMethod = "local"
# set method to 'local'
# COSM info - allows real time graphing over the internet
temp_api_key = 'R-TZoaYnOUWVnf-s_mLMHwL3LQeSAKxZY0tNdThiS0I2VT0g'
# API key for COSM - Temp
temp_api_feed = 117505
# Feed ID for COSM - Temp
light_api_key = '_Ea8XluPFqxAv3IQF-Zhk_3VqBySAKwrZGxXT0hPQTREOD0g'
# API key for COSM - Light
light_api_feed = 117510
# Feed ID for COSM - Light
temp_api_url = '/v2/feeds/{feednum}.xml'.format(feednum = temp_api_feed)
# API URL
light_api_url = '/v2/feeds/{feednum}.xml'.format(feednum = light_api_feed)
# API URL
# https://cosm.com/users/scottrpi
- shows both feeds
# https://cosm.com/feeds/117510
- shows feed for light sensor
# https://cosm.com/feeds/117505
- shows feed for temp sensor
# Function to obtain the Analog to Digital conversion and return the value
def get_adc(channel):
spi = spidev.SpiDev()
python wrapper to communicate with SPI port
spi.open(0,0)
open channel 0 (ADC is on SPI channel 0 (CE0 / GPIO8)
#r = spi.xfer2([1,(3)<<4,0])
#adc_val = ((r[1]&3) << 8) + r[2]
if (channel == 0):
Channel 0 (actual AC1) is the Light sensor
# Send start bit, sgl/diff, odd/sign, MSBF
# channel = 0 sends 0000 0001 1000 0000 0000 0000
# channel = 1 sends 0000 0001 1100 0000 0000 0000
# sgl/diff = 1; odd/sign = channel; MSBF = 0
r = spi.xfer2([1,(2+channel)<<6,0])
# spi.xfer2 returns same number of 8 bit bytes
# as sent. In this case, 3 - 8 bit bytes are returned
# We must then parse out the correct 10 bit byte from
# the 24 bits returned. The following line discards
# all bits but the 10 data bits from the center of
47
# use spidev
#
# Function to request the digital value from temperature sensor and record it and graph it
def recordTemp():
try:
while 1:
# Loop
timeInt = time.time()
# Get current timestamp as an integer
timeStr = datetime.datetime.fromtimestamp(timeInt).strftime('%Y%m-%d %H:%M:%S') # Convert it to a string
reading = get_adc(1)
# get temp sensor digital reading
c_temp = (((reading * (3300.0 / 1024.0)) - 100.0) / 10.0) - 40.0 #
Calculate temp in C
f_temp = ( c_temp * 9.0 / 5.0) + 32
#
Calculate temp in F
c_temp = "%.1f" % c_temp
# set C temp to 1 decimal place
f_temp = "%.1f" % f_temp
# set F temp to 1 decimal place
print "Temperature: ", c_temp, "C", f_temp, "F" #
print
the
temperatures into terminal
f = open('/home/pi/Desktop/logger/temperatures.txt', 'a')
# Opens
file 'temperatures' with method A for append
f.write("Time: " + timeStr + " Temperature: " + c_temp + "C " +
f_temp + "F\n") # write temp to file
f.close()
# close file
once finished the write
#pac = eeml.Pachube(temp_api_url, temp_api_key)
# using markup language to connect to COSM (previously Pachube)
48
# Function to set variables required for recording and start the recording
def startRecord(method):
try:
# set up the variables required for the recording
serverConf = "/etc/ffserver.conf"
# path to the ffserver config
logLevel = "error"
# logging level, quiet, panic,
fatal, error, warning, info, verbose, debug
rframeRate = "1"
# Frame rate in Hz
(for saving/recording)
sframeRate = "30"
# Frame rate in Hz
(for streaming)
frameSize = "352x288"
# Set frame size in width x
height (640x480, 352x288, 320x240, 176x144, 160x120 supported)
fileFormat = "video4linux2"
# output format
inputName = "/dev/video0"
# Input type, video0 for
camera
outLocation = "http://localhost:8001/webcam.ffm"
# Output location,
over network
timeInt = time.time()
# Get current timestamp as
an integer
timeStr = datetime.datetime.fromtimestamp(timeInt).strftime('%Y-%m-%d~
%H:%M:%S') # Convert it to a string
outLocationSave = "/home/pi/Desktop/logger/webcam-%s.avi" %timeStr
# Output location, in this case save under /home/pi/ with name 'webcamtimestamp.mpg'
vSync = "1"
#
video
sync (make sure no duplicate frames etc
aSync = "1"
#
audio
sync to fix wait at start
aFormat = "alsa"
# format for audio:
oss or alsa
aChannel = "1"
# audio channels
aInput = "hw:1,0"
# input device for
audio (sound card #1 sub #0)
50
if (method == "store"):
# if the selected method is to store it
to the SD card (video + audio)
#
print "Recording has started - Storing to SD card"
#
print appropriate message
#
os.system("ffmpeg -v " + logLevel + " -r " + frameRate + " -s " +
frameSize + " -f " + fileFormat +
#
" -i " + inputName + " -f alsa -ac 1 -i hw:1,0 " + " " +
outLocationSave)
os.system("ffmpeg -v " + logLevel + " -r " + rframeRate + " -s " +
frameSize + " -f " + fileFormat +
" -vsync " + vSync + " -i " + inputName + " -f " +
aFormat + " -ar 8000 -ac " + aChannel + " -async " +
aSync + " -i " + aInput + " " + outLocationSave)
elif (method == "stream"):
# if the selected method is to strream it over
network (video only)
print "Recording has started - Streaming across network" #
print
appropriate message
os.system("ffserver -f " + serverConf + " & ffmpeg -v " + logLevel +
" -r " + sframeRate + " -s " + frameSize + " -f " +
fileFormat +
" -i " + inputName + " " + outLocation)
return;
except KeyboardInterrupt:
os.system("reset")
after program ends
gpio.cleanup()
# Ctrl C on keyboard
# Reset terminal to prevent hidden characters
# reset GPIO ports if KeyboardInterrupt
def stopRecord(recording):
try:
if (recording == True):
started
if (p1.is_alive()):
1 (recordTemp) is alive
p1.terminate()
process
if (p2.is_alive()):
2 (recordLight) is alive
p2.terminate()
process
if (p3.is_alive()):
3 (startRecord) is alive
p3.terminate()
process
# if recording has
# If process
# terminate
# If process
# terminate
# If process
# terminate
# Ctrl C on keyboard
# Reset terminal to prevent hidden characters
# reset GPIO ports if KeyboardInterrupt
frameSize = "352x288"
inputName = "/dev/video0"
timeInt = time.time()
integer
timeStr = datetime.datetime.fromtimestamp(timeInt).strftime('%Y-%m-%d~%H:%M:
%S') # Convert it to a string
picOut = "/home/pi/Desktop/logger/image-%s.jpg" % timeStr
# Output location
for single image timestamp attached
p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
open processes
out, err = p.communicate()
# record them in 'out'
check
try:
# try
key = os.read(fd, 1)
finally:
leaving try statement
allow
overwriting
of
allow
overwriting
of
if
key
pressed is '1'
keyDetect = '1'
# then set it
to 1
elif key == '2':
keyDetect = '2'
elif key == '3':
keyDetect = '3'
elif key == 'Q':
print 'Please try again'
else:
# if key is '2'
# set to 2
# if key is '3'
# set to 3
# if key is 'Q'
# print message to try again
#
otherwise
print 'Unusable key detected'
key
keyRun = False
false as it's not longer running
except KeyboardInterrupt:
os.system("reset")
after program ends
gpio.cleanup()
# set it to
# Ctrl C on keyboard
# Reset terminal to prevent hidden characters
# reset GPIO ports if KeyboardInterrupt
try:
time.sleep(1)
#
prevent
some misreading buttons during start up
print "Python Program loaded, press a button to continue"
while 1:
# Infinite loop
if (((gpio.input(23) == False) or (keyDetect == '1')) and (recording ==
False)): # if record button is pressed and not recording, record
54
if (conMethod == "remote"):
connected
print yellow + "INFO: As remotely connected you must press
Q to end the webcam stream" + endc
print yellow + "WARNING: When ending the stream, it may take a
few minutes depending on the length of recording" + endc
time.sleep(2)
# give time
to read the info
gpio.output(redLED, False)
# disable the red
LED
gpio.output(greenLED, True)
# enable the LED to
show it's recording (green)
recording = True
#
Set
recording variable to true
p1 = multiprocessing.Process(target=recordTemp)
#
create a process with target as recordTemp
p2 = multiprocessing.Process(target=recordLight)
# create a
process with target as recordLight
p3 = multiprocessing.Process(target=startRecord, args=(method,))
# create a process with target as startRecord taking 'method' variable as param
p3.start()
#
Start process to startRecord
time.sleep(2)
# wait a few
seconds
p1.start()
#
Start process to recordTemp
time.sleep(3)
# wait a few
seconds
p2.start()
#
Start process to recordLight
keyDetect = ''
#
Reset
keyDetect
time.sleep(1)
#
prevent
button from being detected several times with 1 press
if (((gpio.input(23) == False) or (keyDetect == '1')) and (recording == True)):
# if record button is pressed and recording, stop
gpio.output(greenLED, False)
# disable the LED to show
it's recording (green)
gpio.output(redLED, True)
# enable the LED to
show it's not recording (red)
stopRecord(recording);
#
Call
the
stopRecord function (pass recording boolean)
recording = False
#
Set
recording variable to false
keyDetect = ''
#
reset
keyDetect
55
time.sleep(1)
#
prevent
button from being detected several times with 1 press
if (((gpio.input(24) == False) or (keyDetect == '2')) and (method == "store")):
# if switch button is pressed and current method is store
method = "stream"
#
change
method to stream
print "Recording method has changed to stream - The recording will
be streamed across the network" # print appropriate message
keyDetect = ''
#
reset
keyDetect
time.sleep(1)
# sleep for 1
second
if (((gpio.input(24) == False) or (keyDetect == '2')) and (method ==
"stream")):
# if switch button is pressed and current method is stream
method = "store"
#
change
method to store
print "Recording method has changed to store - The recording will be
saved to the SD card" # print appropriate message
keyDetect = ''
#
reset
keyDetect
time.sleep(1)
# sleep for 1
second
if ((gpio.input(25) == False) or (keyDetect == '3')):
gpio.output(yellowLED, True)
# enable LED for short
duration to show it's taken image (yellow)
time.sleep(1)
# sleep for
one second
gpio.output(yellowLED, False)
# disable LED again
singleImg();
# Call the
singleImg function
keyDetect = ''
#
reset
keyDetect
time.sleep(1)
#
prevent
button from being detected several times with 1 press
if ((conMethod == "remote") and (keyRun == False)):
# if remotely connected start function to allow key detection
time.sleep(1)
# allow time
for q to be pressed
thread.start_new_thread( keyPress, ())
keyRun = True
# set it to
true so it won't call this statement each loop
except KeyboardInterrupt:
os.system("reset")
program ends
gpio.cleanup()
# Ctrl C on keyboard
# Reset terminal to prevent hidden characters after
# reset GPIO ports if KeyboardInterrupt
56
gpio.cleanup()
os.system("reset")
ends
57