Hive.sh

From HiveTool
Revision as of 23:19, 7 November 2014 by Paul (talk | contribs)
Jump to: navigation, search

/home/hivetool/hive.sh is a bash script called by cron every 5 minutes that:

  1. gets the hostname and date/time,
  2. reads sensor calibration from config.conf (ver 0.5)
  3. reads the hive sensors,
  4. downloads the weather data in xml format from Weather Underground,
  5. appends the data to a text file,
  6. runs the graphing program /var/www/htdocs/graph_hive.pl,
  7. uploads an xml file to the hivetool database.

/home/hivetool/hivetool.log is over written with messages each time hive.sh is run.

#!/bin/bash

# redirect stdout and stderr to logfile
rm /home/hivetool/hivetool.log
exec >>/home/hivetool/hivetool.log 2>&1

# get the hostname
HOST=`hostname`

# and the date
DATE=`date +"%Y/%m/%d %H:%M:%S"`

#
# Adam Equipment CPWplus scale
# send the Net command
# and read it with a 3 second timeout

while [[ ! $SCALE ]]
do
    echo -e -n "N\r\n" > /dev/ttyUSB0
    read -t 3 SCALE < /dev/ttyUSB0
    SCALE=`echo $SCALE | gawk --posix '/^\+ [0-9]{1,3}\.[0-9] lb$/'`
done

echo "scale: $SCALE\n"

#
# Read the TEMPerHUM sensor
#                                                                                                                                                                                    

DATA_GOOD=0
COUNTER=1
while [[  $COUNTER -lt 20 && $DATA_GOOD -eq 0 ]]; do
         DATE2=`date +"%Y/%m/%d %H:%M:%S"`
         TEMPerHUM=`/usr/local/bin/tempered /dev/hidraw1`
         echo -ne "$DATE2 $COUNTER $? $TEMPerHUM \n" >> /home/hivetool/tempered.log
         if [[ -n $TEMPerHUM ]]
         then
           HUMIDITY=`echo $TEMPerHUM | grep  -o "[0-9]*\.[0-9]\%" | grep -o "[0-9]*\.[0-9]"`
           TEMP=`echo $TEMPerHUM | grep  -o "temperature \-*[0-9]*\.[0-9]" | grep -o "\-*[0-9]*\.[0-9]"`
           if [[ $HUMIDITY ]]
           then
            DATA_GOOD=1
           fi
         fi
         let "COUNTER += 1"
         sleep 1
   done
echo $COUNTER $TEMP $HUMIDITY

if [[ $COUNTER -gt 19 ]]
then
  echo "$DATE2 ERROR reading /dev/hidraw1" >> /home/hivetool/error.log
fi

if test $COUNTER -gt 2
then
  echo "$DATE WARNING reading /dev/hidraw1: retried $COUNTER" >> /home/hivetool/error.log
fi

TEMP=`echo "scale=1; ($TEMP-1)" | bc`
TEMPF=`echo "scale=1; (($TEMP*9)/5)+32" | bc`

# 
# get the local weather
#
curl --retry 5 -s http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KGADILLA1 > /tmp/wx.xml
temp_f=`grep temp_f /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
temp_c=`grep temp_c /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
wind_dir=`grep wind_dir /tmp/wx.xml | grep -o "[A-Z]*"`
wind_mph=`grep wind_mph /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
wind_gust_mph=`grep wind_gust_mph /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
pressure_mb=`grep pressure_mb /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
dewpoint_f=`grep dewpoint_f /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
#solar_radiation=`grep solar_radiation /tmp/wx.xml |  grep  -o "[0-9]*"`
precip_1hr_in=`grep precip_1hr_in /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
precip_today_in=`grep precip_today_in /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`


xml_temp_f=`grep temp_f /tmp/wx.xml`
xml_temp_c=`grep temp_c /tmp/wx.xml`
xml_relative_humidity=`grep relative_humidity /tmp/wx.xml`
xml_wind_dir=`grep wind_dir /tmp/wx.xml`
xml_wind_mph=`grep wind_mph /tmp/wx.xml`
xml_wind_gust_mph=`grep wind_gust_mph /tmp/wx.xml`
xml_pressure_mb=`grep pressure_mb /tmp/wx.xml`
xml_dewpoint_f=`grep dewpoint_f /tmp/wx.xml`
#solar_radiation=`grep solar_radiation /tmp/wx.xml`
xml_precip_1hr_in=`grep precip_1hr_in /tmp/wx.xml`
xml_precip_today_in=`grep precip_today_in /tmp/wx.xml`

AMBIENT=$temp_c

#
# create hive.xml to send to the database
#
echo "<hive_data>" > /tmp/hive.xml
source /home/hivetool/xml.sh >> /tmp/hive.xml
cat /tmp/wx.xml|grep -v "xml" >> /tmp/hive.xml
echo "</hive_data>" >> /tmp/hive.xml

# 
# Write everything to the log file
#
echo -ne "\n"$DATE $SCALE $TEMP $AMBIENT $temp_f $wind_dir $wind_mph $wind_gust_mph $dewpoint_f $relative_humidity $pressure_mb $solar_radiation $WX_EVAPOTRANSPIRATION $WX_VAPOR_PRESSURE $precip_today_in>> /home/hivetool/hive.log

#
#run the graphing program to create index.html and hive_graph.gif
#
/var/www/htdocs/graph_hive.pl

#
#send the data to hivetool
#
curl --retry 5 -k -u user:password -X POST --data-binary @/tmp/hive.xml https://hivetool.org/private/test_xml4.pl  -H 'Accept: application/xml' -H 'Content-Type: application/xml'