Skip to content

Instantly share code, notes, and snippets.

@ypetya
Last active August 29, 2015 14:11
Show Gist options
  • Save ypetya/36d331c4d6504c19480a to your computer and use it in GitHub Desktop.
Save ypetya/36d331c4d6504c19480a to your computer and use it in GitHub Desktop.
Raspberry bash scripts for temperature webservice and backend. + Take a photo example
#!/bin/bash
#
# @author: Peter Kiss <[email protected]>
#
# this script reads and saves the humidity and temperature sensor data to an
# sqlite database database
#
readonly SENSORS=( /dev/ttyACM0 )
readonly TEMPERATURE_DB_FILE="temperature.sqlite.db"
function get_date_stamp() {
date +%Y%m%d
}
function get_date_time_stamp() {
date +%Y%m%d%H%M
}
function is_installed() {
local installed=$(dpkg -s "$1" 2>&1 | grep 'install ok installed')
if [ "" == "$installed" ] ; then
return 1
fi
return 0
}
function require() {
UNAME="$(uname)"
case $UNAME in
MINGW*)
echo "MinGW not supported"
return 0
;;
Linux|*)
echo "require $1"
if ! is_installed $1 ; then
echo "$1 not installed yet!"
sudo apt-get install $1
return $?
fi
;;
esac
}
require sqlite
function run_sql() {
sqlite $TEMPERATURE_DB_FILE "$1"
}
function initialize_db() {
if [ ! -f "$TEMPERATURE_DB_FILE" ] ; then
run_sql "create table temperature (
date TIMESTAMP DEFAULT CURRENT_DATE NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
sensor_id TEXT NOT NULL,
status TEXT NOT NULL,
humidity NUMERIC NOT NULL,
temperature NUMERIC NOT NULL
);"
run_sql "create index by_date ON temperature ( date );"
fi
}
function insert_into_db() {
run_sql "insert into temperature (date, timestamp, sensor_id, status, humidity, temperature)
values ( '$1', '$2', '$3', '$4', '$5', '$6' );"
}
function setup_sensor() {
stty -F $1 raw ispeed 115200 ospeed 115200 -ignpar cs8 -cstopb -echo
}
function read_and_persist_sensor_data() {
local SENSOR=$1
setup_sensor $SENSOR
local DATE=$(get_date_stamp)
local TIMESTAMP=$(get_date_time_stamp)
local INFO="$(grep -m1 -A2 OK $1)"
local STATUS=$(echo "$INFO" | grep sensor |cut -d ' ' -f 3|tr -d '\r\n')
local HUMIDITY=$(echo "$INFO" | grep Humidity |cut -d ' ' -f 3|tr -d '\r\n')
local TEMPERATURE=$(echo "$INFO" | grep Temperature |cut -d ' ' -f 3|tr -d '\r\n')
insert_into_db "$DATE" "$TIMESTAMP" "$SENSOR" "$STATUS" "$HUMIDITY" "$TEMPERATURE"
}
# main
initialize_db
read_and_persist_sensor_data ${SENSORS[0]}
#!/bin/bash
# @author: Peter Kiss <[email protected]>
#
# This script takes a photo from the following device:
readonly VIDEO=/dev/video0
# Output directory and filename parameters
readonly OUTPUT_DIR=/var/www/
readonly OUTPUT_FILE="snap_"
# Maximum files stored
# 1 week = 24 * 2 * 7 = 480
readonly FILE_LIMIT=480
# if given it can append info from an arduino for example
# to each image
readonly SERIAL_INFO_ENABLED=1
readonly SERIAL_DATA=/dev/ttyACM0
function get_date_time_stamp() {
date +%Y%m%d%H%M
}
function get_photo_snapshot_filename() {
echo "${OUTPUT_DIR}${OUTPUT_FILE}$(get_date_time_stamp).jpg"
}
function keep_only_last_N_files() {
local LIMIT=$1
set +f
local FILES=($( ls ${OUTPUT_DIR}$OUTPUT_FILE* | sort))
while [ ${#FILES[@]} -gt $LIMIT ] ; do
rm -v ${FILES[0]}
FILES=($( ls ${OUTPUT_DIR}$OUTPUT_FILE* | sort))
done
}
function is_installed() {
local installed=$(dpkg -s "$1" 2>&1 | grep 'install ok installed')
if [ "" == "$installed" ] ; then
return 1
fi
return 0
}
function require() {
UNAME="$(uname)"
case $UNAME in
MINGW*)
echo "MinGW not supported"
return 0
;;
Linux|*)
echo "require $1"
if ! is_installed $1 ; then
echo "$1 not installed yet!"
sudo apt-get install $1
return $?
fi
;;
esac
}
require vgrabbj
FILENAME=$(get_photo_snapshot_filename)
vgrabbj -d $VIDEO -i vga -o jpg -F 4 -a -f $FILENAME
if [ "$SERIAL_INFO_ENABLED" == "1" ] ; then
require imagemagick
# setup port and read
stty -F $SERIAL_DATA raw ispeed 115200 ospeed 115200 -ignpar cs8 -cstopb -echo
# save temperature data
INFO="$(date)"
INFO="$INFO\n$(grep -m1 -A2 OK $SERIAL_DATA | tr -d %)"
# create label with imagemagic
convert $FILENAME -background Khaki label:"$INFO" -gravity Center -append $FILENAME
fi
keep_only_last_N_files $FILE_LIMIT
ln -sf $FILENAME /var/www/snap.jpg
#!/bin/bash
function test_request() {
echo "Content-type: text/html"
echo
echo "<html><body><pre>"
env
echo "</pre></body></html>"
echo
# => log input parameters to response
}
function response_jsonp() {
echo "Content-type: application/javascript"
echo
echo "$1( { array:["
sqlite /home/pi/temperature.sqlite.db "$2" | while read line ; do
echo -n "{"
local date=$(echo $line | cut -d '|' -f 1)
local timestamp=$(echo $line | cut -d '|' -f 2)
local sensor_id=$(echo $line | cut -d '|' -f 3)
local status=$(echo $line | cut -d '|' -f 4)
local temperature=$(echo $line | cut -d '|' -f 5)
local humidity=$(echo $line | cut -d '|' -f 6)
echo -n "date:'$date',"
echo -n "timestamp:'$timestamp',"
echo -n "sensor_id:'$sensor_id',"
echo -n "status:'$status',"
echo -n "temperature:'$temperature',"
echo -n "humidity:'$humidity'"
echo "},"
done
echo "]});"
echo
}
readonly PAGE_SIZE=10
readonly JS_FN=$(echo $PATH_INFO | cut -d '/' -f 2)
if [ "$JS_FN" == "" ] ; then
echo "Content-type: text/html"
echo
cat << @
<html>
<body>
<h1>Temperature service</h1>
<p>This is a REST <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> webservice.
You can use it to query and filter data. See example usage below.</p>
<dl>
<dt>Print out this help screen</dt>
<dd><b>HTTP GET /temp</b> without parameters</dd>
</dl>
<dl>
<dt>Query the most recent data</dt>
<dd><b>HTTP GET /temp/jsFunctionToCall</b> return the most recent data in the form of jsonp.
This function will return a limited set of data. The default page size is $PAGE_SIZE.
</dd>
</dl>
<dl>
<dt>Filter data for a specific day</dt>
<dd><b>HTTP GET /temp/jsFunctionToCall/date/YYYYMMDD</b> return filtered data</dd>
</dl>
<dl>
<dt>Query a set of data by defining offset</dt>
<dd><b>HTTP GET /temp/jsFunctionToCall/page/offset</b> return a limited set of data from the given offset.
The default page size is $PAGE_SIZE.</dd>
</dl>
<p>
For more info please drop a mail to <a href="mailto:[email protected]">Peter</a>
</p>
</body>
</html>
@
echo
else
readonly FILTER=$(echo $PATH_INFO | cut -d '/' -f 3)
readonly VALUE=$(echo $PATH_INFO | cut -d '/' -f 4)
QUERY="select date,timestamp,sensor_id,status,temperature,humidity from temperature order by timestamp desc limit $PAGE_SIZE"
if [[ "date" == "$FILTER" ]] && [[ $VALUE =~ [0-9]+ ]] ; then
QUERY="select date,timestamp,sensor_id,status,temperature,humidity from temperature where date='$VALUE'"
elif [[ "page" == "$FILTER" ]] && [[ $VALUE =~ [0-9]+ ]] ; then
QUERY="select date,timestamp,sensor_id,status,temperature,humidity from temperature order by timestamp desc limit $PAGE_SIZE OFFSET $VALUE"
fi
response_jsonp $JS_FN "$QUERY"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment