Difference between revisions of "Hive.sh"

From HiveTool
Jump to: navigation, search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
/home/hivetool/hive.sh is a bash script called by cron every 5 minutes that:
 
/home/hivetool/hive.sh is a bash script called by cron every 5 minutes that:
 
#gets the hostname and date/time,
 
#gets the hostname and date/time,
 +
#reads sensor calibration from config.conf (ver 0.5)
 
#reads the hive sensors,  
 
#reads the hive sensors,  
 
#downloads the weather data in xml format from Weather Underground,  
 
#downloads the weather data in xml format from Weather Underground,  
 
#appends the data to a text file,  
 
#appends the data to a text file,  
#calls the graphing program and
+
#inserts a row in the local database,
 
#uploads an xml file to the hivetool database.
 
#uploads an xml file to the hivetool database.
 +
 +
/home/hivetool/hivetool.log is over written with messages each time hive.sh is run.
  
 
<pre>
 
<pre>
 +
 
#!/bin/bash
 
#!/bin/bash
 +
# ##############################################################################
 +
#                        hive.sh ver 0.8.4
 +
#
 +
# Reads sensors, logs them and sends the data to hivetool.net
 +
#
 +
# May  use:
 +
# hive.conf            Configuration file
 +
# bme680.py            Read temperature, humidity, pressure, gas resistance from Bosh BME680
 +
# cpw200plus.sh        Read weight from Adam Equipment CPW200plus scale
 +
# dht22.sh              Read temperature and humidity from DHT22 (from Grove Seeed_DHT22) DOEN"T WORK IN 0.8)
 +
# dht22_adafruit.sh    Read temperature and humidity from DHT22 (Replaces Grove Seeed_DHT22)
 +
# dhtxx.sh              Read temperature and humidity from DHTxx
 +
# ds18b20.sh            Read temperature from DS18B20
 +
# hx711.sh              Read weight from HX711 board (c/wiringPi)
 +
# hx711py.sh            Read weight from HX711 board (python/pigpio)
 +
# hx711b.sh            Read voltage from HX711 board (c/wiringPi)
 +
# hx711pyB.sh          Read voltage from HX711 board (python/pigpio)
 +
# phidget.sh            Read weight from Phidgets Bridge board
 +
# temperhum.sh          Read temperature and humidity from TEMPerHUM model 2
 +
# mysql.sh              Log data to local MySQL database
 +
# sqlite.sh            Log data to local SQLite database
 +
# tsl2561.sh            Read lux from TSL2561
 +
# tsl2591.sh            Read lux from TSL2561
 +
# raingauge.conf        Read rain gauge
 +
# xml.sh                Writes the data in XML format to upload to hivetool.org
 +
#
 +
# ##############################################################################
 +
 +
REDIRECT=1                            # comment this line to turn off redirecting stdout and stderr to hivetool.log
 +
VERBOSE=1                              # set to 1 for more info
 +
export LC_NUMERIC="en_US.UTF-8"
 +
 +
#
 +
# Get the date and time
 +
#
 +
export DATE=`date +"%Y-%m-%d %H:%M:%S"`
 +
#
 +
# Redirect stdout and stderr to logfile.  Useful to check for errors when run with no console (cron).
 +
#
 +
if [ "$REDIRECT" -gt "0" ]
 +
then
 +
  rm /home/hivetool/hivetool.log            # delete the last log
 +
 +
  exec >>/home/hivetool/hivetool.log 2>&1    # redirect stdout and stderr to logfile
 +
fi
 +
#
 +
# delete the last server response if it exits
 +
#
 +
if [ -f "/tmp/hive_command.xml" ]
 +
then
 +
rm /tmp/hive_command.xml
 +
fi
 +
#
 +
# Read the configuration file and set the shell variables
 +
#
 +
echo
 +
echo "Starting /home/hivetool/hive.sh at $DATE"
 +
echo "Reading /home/hivetool/hive.conf configuration file."
  
# redirect stdout and stderr to logfile
+
IFS="="                              # set the Internal Field Separator to equal sign
rm /home/hivetool/hivetool.log
+
while read name value; do            # split each line on the IFS (=) into 2 variables: name and value
exec >>/home/hivetool/hivetool.log 2>&1
+
  value=${value#\"}                  # remove leading "
 +
  value=${value%\"}                  # remove trailing "
 +
  export $name="$value"              # export the variables to make them available to child processes
 +
done <"/home/hivetool/hive.conf"      # read each line from the config file
 +
IFS=" "                              # set the IFS back to space or reading TEMPerHUM will fail
  
# get the hostname
+
[ ! -z HIVE_NAME ] && HIVE1_NAME="TEST1"
HOST=`hostname`
 
  
# and the date
+
echo
DATE=`date +"%Y/%m/%d %H:%M:%S"`
+
echo "Hive Name: $HIVE_NAME"
  
 +
if [ "$SYSTEM_STATUS" == "DISABLED"  ]
 +
then
 +
  echo "Hivetool is DISABLED. Doing nothing."
 +
  echo ""
 +
  exit 1
 +
fi
 +
 +
#
 +
# test for PI GPIO daemon running
 
#
 
#
# Adam Equipment CPWplus scale
+
echo "Checking if pigpiod is running"
# send the Net command
+
 
# and read it with a 3 second timeout
+
PIGPIOD_PID=`/bin/pidof pigpiod`
 +
 
 +
if [ $? -eq 0 ]
 +
then
 +
  echo "Running.  Process ID (pid): $PIGPIOD_PID"
 +
else
 +
  echo "pigpiod is NOT running.  Trying to start it."
 +
  /usr/bin/pigpiod
 +
  echo "pigpiod exit code: $?"
 +
fi
 +
 
 +
#
 +
# Turn on pull up resistor on GPIO 7 for 1-Wire
 +
/usr/local/bin/gpio mode 7 up
 +
#
 +
# test for Maintenance Hold (hive manipulation)
 +
#
 +
QUALITY=`cat /dev/shm/quality`
 +
if [[ $QUALITY -eq 4 ]]
 +
then
 +
  echo "*** MAINTENANCE MODE **"
 +
fi
 +
echo
 +
echo
 +
echo "Reading Sensors"
 +
echo "  Variable            Scaled    Driver    GPIO/device/params/ID      Slope    Intercept    Raw"
 +
#
 +
# Read scale
 +
#
 +
case "$HIVE_WEIGHT_SENSOR" in
 +
        CPW200plus)
 +
            RAW_HIVE_WEIGHT=$(/home/hivetool/cpw200plus.sh $HIVE_WEIGHT_DEV)
 +
            set -- junk $HIVE_WEIGHT
 +
            shift
 +
            RAW_HIVE_WEIGHT=$2
 +
            ;;
 +
        HX711)
 +
            RAW_HIVE_WEIGHT=$(/home/hivetool/hx711.sh)
 +
            ;;
 +
        HX711py)
 +
            RAW_HIVE_WEIGHT=$(/home/hivetool/hx711py.sh)
 +
            ;;
 +
        Phidget)
 +
            RAW_HIVE_WEIGHT=$(/home/hivetool/phidget.sh)
 +
            ;;
 +
        none)
 +
            RAW_HIVE_WEIGHT="NULL"              # Set hive weight to NULL if scale type is set to "none"
 +
#            echo "No scale selected HIVE_WEIGHT_SENSOR: $HIVE_WEIGHT_SENSOR"
 +
            ;;
 +
        *)
 +
            RAW_HIVE_WEIGHT="NULL"              # Set hive weight to NULL if scale type is not set or set wrong
 +
            echo "No scale selected or unknown HIVE_WEIGHT_SENSOR: $HIVE_WEIGHT_SENSOR"    # and display warning
 +
            ;;
 +
esac
 +
 
 +
 
 +
if [ $RAW_HIVE_WEIGHT != "NULL" ]
 +
then
 +
  HIVE_WEIGHT=`echo "scale=3; (($RAW_HIVE_WEIGHT*$HIVE_WEIGHT_SLOPE)+$HIVE_WEIGHT_INTERCEPT)/1" | bc`
 +
  printf "%-16s %12.3f %12s %24s %12.9f %8.3f %12.2f\n" "Hive Weight" "$HIVE_WEIGHT" "$HIVE_WEIGHT_SENSOR" "$HIVE_WEIGHT_DEV" "$HIVE_WEIGHT_SLOPE" "$HIVE_WEIGHT_INTERCEPT" "$RAW_HIVE_WEIGHT"
 +
else
 +
  HIVE_WEIGHT="NULL"
 +
  HIVE_WEIGHT_F="NULL"
 +
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Hive Weight" "$HIVE_WEIGHT" "$HIVE_WEIGHT_SENSOR" "$HIVE_WEIGHT_DEV" "$HIVE_WEIGHT_SLOPE" "$HIVE_WEIGHT_INTERCEPT" "$RAW_HIVE_WEIGHT"
 +
fi
 +
 
 +
#
 +
# Read temp and humidity inside hive
 +
#
 +
case "$HIVE_TEMP_SENSOR" in
 +
        TEMPerHUM)
 +
              TEMPerHUM=$(/home/hivetool/temperhum.sh -d $HIVE_TEMP_DEV)
 +
              set -- junk $TEMPerHUM
 +
              shift
 +
              RAW_HIVE_TEMP=$1
 +
              RAW_HIVE_HUMIDITY=$2
 +
              if [ -z "$RAW_HIVE_TEMP" ]
 +
              then
 +
                RAW_HIVE_TEMP="NULL"
 +
                RAW_HIVE_HUMIDITY="NULL"
 +
              fi
 +
              ;;
 +
        BME680)
 +
             
 +
              ;;
 +
        DHT11)
 +
              TEMPerHUM=$(/home/hivetool/dht11_adafruit.sh -d $HIVE_TEMP_DEV)
 +
              set -- junk $TEMPerHUM
 +
              shift
 +
              RAW_HIVE_TEMP=$1
 +
              RAW_HIVE_HUMIDITY=$2
 +
              sleep 4                # fixes a weirdness with bad readings when reading 2 DHT22's in a row.  (not necessary it the second temp/rh sensor is not a DHT22
 +
              ;;
 +
          DHT22)
 +
#            TEMPerHUM=$(/home/hivetool/dht22.sh -d $HIVE_TEMP_DEV)
 +
              TEMPerHUM=$(/home/hivetool/dht22_adafruit.sh -d $HIVE_TEMP_DEV)
 +
#            TEMPerHUM=$(nice --20 /usr/local/bin/Seeed_DHT22  $HIVE_TEMP_DEV S)
 +
              set -- junk $TEMPerHUM
 +
              shift
 +
              RAW_HIVE_TEMP=$1
 +
              RAW_HIVE_HUMIDITY=$2
 +
              sleep 4                # fixes a weirdness with bad readings when reading 2 DHT22's in a row.  (not necessary it the second temp/rh sensor is not a DHT22
 +
              ;;
 +
        DHTxx)
 +
              TEMPerHUM=$(/home/hivetool/dhtxx.sh -d $HIVE_TEMP_DEV)
 +
              set -- junk $TEMPerHUM
 +
              shift
 +
              RAW_HIVE_TEMP=$1
 +
              RAW_HIVE_HUMIDITY=$2
 +
              ;;
 +
        DS18B20)
 +
              TEMPerHUM=$(/home/hivetool/ds18b20.sh -d $HIVE_TEMP_DEV)
 +
 
 +
              set -- junk $TEMPerHUM
 +
 
 +
              shift
 +
              RAW_HIVE_TEMP=$1
 +
              ;;
 +
          *)
 +
              RAW_HIVE_TEMP="NULL"
 +
              RAW_HIVE_HUMIDITY="NULL"
 +
              ;;
 +
esac
 +
 
 +
 
 +
 
 +
if [ "$RAW_HIVE_TEMP" != "NULL" ]
 +
then
 +
  HIVE_TEMP=`echo "scale=2; (($RAW_HIVE_TEMP*$HIVE_TEMP_SLOPE)+$HIVE_TEMP_INTERCEPT)/1" | bc`
 +
  HIVE_TEMP_F=`echo "scale=2; ((($HIVE_TEMP*9)/5)+32)/1" | bc`
 +
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Hive Temp" "$HIVE_TEMP" "$HIVE_TEMP_SENSOR" "$HIVE_TEMP_DEV" "$HIVE_TEMP_SLOPE" "$HIVE_TEMP_INTERCEPT" "$RAW_HIVE_TEMP"
 +
else
 +
  HIVE_TEMP="NULL"
 +
  HIVE_TEMP_F="NULL"
 +
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Hive Temp" "$HIVE_TEMP" "$HIVE_TEMP_SENSOR" "$HIVE_TEMP_DEV" "$HIVE_TEMP_SLOPE" "$HIVE_TEMP_INTERCEPT" "$RAW_HIVE_TEMP"
 +
fi
 +
 
 +
if [ $RAW_HIVE_HUMIDITY != "NULL" ]
 +
then
 +
  HIVE_HUMIDITY=`echo "scale=2; (($RAW_HIVE_HUMIDITY*$HIVE_HUMIDITY_SLOPE)+$HIVE_HUMIDITY_INTERCEPT)/1" | bc`
 +
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Hive Humidity" "$HIVE_HUMIDITY" "$HIVE_HUMIDITY_SENSOR" "$HIVE_HUMIDITY_DEV" "$HIVE_HUMIDITY_SLOPE" "$HIVE_HUMIDITY_INTERCEPT" "$RAW_HIVE_HUMIDITY"
 +
else
 +
  HIVE_HUMIDITY="NULL"
 +
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Hive Humidity" "$HIVE_HUMIDITY" "$HIVE_HUMIDITY_SENSOR" "$HIVE_HUMIDITY_DEV" "$HIVE_HUMIDITY_SLOPE" "$HIVE_HUMIDITY_INTERCEPT" "$RAW_HIVE_HUMIDITY"
 +
fi
  
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
+
# Read outside temp and humidity
#                                                                                                                                                                                  
+
#
 +
case "$AMBIENT_TEMP_SENSOR" in
 +
        TEMPerHUM)
 +
              TEMPerHUM=$(/home/hivetool/temperhum.sh -d $AMBIENT_TEMP_DEV)
 +
              set -- junk $TEMPerHUM
 +
              shift
 +
              RAW_AMBIENT_TEMP=$1
 +
              RAW_AMBIENT_HUMIDITY=$2
 +
              if [ -z "$RAW_AMBIENT_TEMP" ]
 +
              then
 +
                RAW_AMBIENT_TEMP="NULL"
 +
                RAW_AMBIENT_HUMIDITY="NULL"
 +
              fi
 +
              ;;
 +
        BME680)
 +
              ;;
 +
        DHT11)
 +
              TEMPerHUM=$(/home/hivetool/dht11_adafruit.sh -d $HIVE_TEMP_DEV)
 +
              set -- junk $TEMPerHUM
 +
              shift
 +
              RAW_HIVE_TEMP=$1
 +
              RAW_HIVE_HUMIDITY=$2
 +
              sleep 4                # fixes a weirdness with bad readings when reading 2 DHT22's in a row.  (not necessary it the second temp/rh sensor is not a DHT22
 +
              ;;
 +
        DHT22)
 +
#             TEMPerHUM=$(/home/hivetool/dht22.sh -d $AMBIENT_TEMP_DEV)
 +
              TEMPerHUM=$(/home/hivetool/dht22_adafruit.sh -d $AMBIENT_TEMP_DEV)
  
DATA_GOOD=0
+
#            TEMPerHUM=$(nice --20 /usr/local/bin/Seeed_DHT22  $AMBIENT_TEMP_DEV S)
COUNTER=1
+
              set -- junk $TEMPerHUM
while [[  $COUNTER -lt 20 && $DATA_GOOD -eq 0 ]]; do
+
              shift
        DATE2=`date +"%Y/%m/%d %H:%M:%S"`
+
              RAW_AMBIENT_TEMP=$1
        TEMPerHUM=`/usr/local/bin/tempered /dev/hidraw1`
+
              RAW_AMBIENT_HUMIDITY=$2
        echo -ne "$DATE2 $COUNTER $? $TEMPerHUM \n" >> /home/hivetool/tempered.log
+
              ;;
        if [[ -n $TEMPerHUM ]]
+
        DHTxx)
        then
+
              TEMPerHUM=$(/home/hivetool/dhtxx.sh -d $AMBIENT_TEMP_DEV)
          HUMIDITY=`echo $TEMPerHUM | grep  -o "[0-9]*\.[0-9]\%" | grep -o "[0-9]*\.[0-9]"`
+
              set -- junk $TEMPerHUM
          TEMP=`echo $TEMPerHUM | grep  -o "temperature \-*[0-9]*\.[0-9]" | grep -o "\-*[0-9]*\.[0-9]"`
+
              shift
          if [[ $HUMIDITY ]]
+
              RAW_AMBIENT_TEMP=$1
          then
+
              RAW_AMBIENT_HUMIDITY=$2
            DATA_GOOD=1
+
              ;;
           fi
+
      DS18B20)
        fi
+
              TEMPerHUM=$(/home/hivetool/ds18b20.sh -d $AMBIENT_TEMP_DEV)
        let "COUNTER += 1"
+
              set -- junk $TEMPerHUM
        sleep 1
+
              shift
  done
+
              RAW_AMBIENT_TEMP=$1
echo $COUNTER $TEMP $HUMIDITY
+
              RAW_AMBIENT_HUMIDITY="NULL"
 +
              ;;
 +
           *)
 +
              RAW_AMBIENT_TEMP="NULL"
 +
              RAW_AMBIENT_HUMIDITY="NULL"
 +
              ;;
 +
esac
  
if [[ $COUNTER -gt 19 ]]
+
if [ "$RAW_AMBIENT_TEMP" != "NULL" ]
 
then
 
then
   echo "$DATE2 ERROR reading /dev/hidraw1" >> /home/hivetool/error.log
+
   AMBIENT_TEMP=`echo "scale=2; (($RAW_AMBIENT_TEMP*$AMBIENT_TEMP_SLOPE)+$AMBIENT_TEMP_INTERCEPT)/1" | bc`
 +
  AMBIENT_TEMP_F=`echo "scale=2; ((($AMBIENT_TEMP*9)/5)+32)/1" | bc`
 +
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Ambient Temp" "$AMBIENT_TEMP" "$AMBIENT_TEMP_SENSOR" "$AMBIENT_TEMP_DEV" "$AMBIENT_TEMP_SLOPE" "$AMBIENT_TEMP_INTERCEPT" "$RAW_AMBIENT_TEMP"
 +
else
 +
  AMBIENT_TEMP="NULL"
 +
  AMBIENT_TEMP_F="NULL"
 +
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Ambient Temp" "$AMBIENT_TEMP" "$AMBIENT_TEMP_SENSOR" "$AMBIENT_TEMP_DEV" "$AMBIENT_TEMP_SLOPE" "$AMBIENT_TEMP_INTERCEPT" "$RAW_AMBIENT_TEMP"
 
fi
 
fi
  
if test $COUNTER -gt 2
+
if [ "$RAW_AMBIENT_HUMIDITY" != "NULL" ]
 
then
 
then
   echo "$DATE WARNING reading /dev/hidraw1: retried $COUNTER" >> /home/hivetool/error.log
+
   AMBIENT_HUMIDITY=`echo "scale=2; (($RAW_AMBIENT_HUMIDITY*$AMBIENT_HUMIDITY_SLOPE)+$AMBIENT_HUMIDITY_INTERCEPT)/1" | bc`
 +
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Ambient Humidity" "$AMBIENT_HUMIDITY" "$AMBIENT_HUMIDITY_SENSOR" "$AMBIENT_HUMIDITY_DEV" "$AMBIENT_HUMIDITY_SLOPE" "$AMBIENT_HUMIDITY_INTERCEPT" "$RAW_AMBIENT_HUMIDITY"
 +
else
 +
  AMBIENT_HUMIDITY="NULL"
 +
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Ambient Humidity" "$AMBIENT_HUMIDITY" "$AMBIENT_HUMIDITY_SENSOR" "$AMBIENT_HUMIDITY_DEV" "$AMBIENT_HUMIDITY_SLOPE" "$AMBIENT_HUMIDITY_INTERCEPT" "$RAW_AMBIENT_HUMIDITY"
 
fi
 
fi
  
TEMP=`echo "scale=1; ($TEMP-1)" | bc`
+
#
TEMPF=`echo "scale=1; (($TEMP*9)/5)+32" | bc`
+
# Read outside light
 +
#
 +
case "$AMBIENT_LIGHT_SENSOR" in
 +
        TSL2561)
 +
              LIGHT=$(/home/hivetool/2561.sh)
 +
              set -- junk $LIGHT
 +
              shift
 +
              RAW_AMBIENT_LIGHT=$1
 +
              if [ -z "$RAW_AMBIENT_LIGHT" ]
 +
              then
 +
                RAW_AMBIENT_LIGHT="NULL"
 +
              fi
 +
              ;;
 +
        TSL2591)
 +
              LIGHT=$(/home/hivetool/2591.sh)
 +
              set -- junk $LIGHT
 +
              shift
 +
              RAW_AMBIENT_LIGHT=$1
 +
              if [ -z "$RAW_AMBIENT_LIGHT" ]
 +
              then
 +
                RAW_AMBIENT_LIGHT="NULL"
 +
              fi
 +
              ;;
 +
          *)
 +
              RAW_AMBIENT_LIGHT="NULL"
 +
              ;;
 +
esac
  
#  
+
if [ "$RAW_AMBIENT_LIGHT" != "NULL" ]
# get the local weather
+
then
 +
  AMBIENT_LIGHT=`echo "scale=2; (($RAW_AMBIENT_LIGHT*$AMBIENT_LIGHT_SLOPE)+$AMBIENT_LIGHT_INTERCEPT)/1" | bc`
 +
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Ambient Light" "$AMBIENT_LIGHT" "$AMBIENT_LIGHT_SENSOR" "$AMBIENT_LIGHT_DEV" "$AMBIENT_LIGHT_SLOPE" "$AMBIENT_LIGHT_INTERCEPT" "$RAW_AMBIENT_LIGHT"
 +
else
 +
  AMBIENT_LIGHT="NULL"
 +
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Ambient Light" "$AMBIENT_LIGHT" "$AMBIENT_LIGHT_SENSOR" "$AMBIENT_LIGHT_DEV" "$AMBIENT_LIGHT_SLOPE" "$AMBIENT_LIGHT_INTERCEPT" "$RAW_AMBIENT_LIGHT"
 +
fi
 +
 
 +
#
 +
# Read rain gauge
 
#
 
#
curl --retry 5 -s http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KGADILLA1 > /tmp/wx.xml
+
case "$AMBIENT_RAIN_SENSOR" in
temp_f=`grep temp_f /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
+
        yes)
temp_c=`grep temp_c /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
+
              RAW_AMBIENT_RAIN=`cat /home/hivetool/rain_total.conf`
wind_dir=`grep wind_dir /tmp/wx.xml | grep -o "[A-Z]*"`
+
              if [ -z "$RAW_AMBIENT_RAIN" ]
wind_mph=`grep wind_mph /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
+
              then
wind_gust_mph=`grep wind_gust_mph /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
+
                RAW_AMBIENT_RAIN="NULL"
pressure_mb=`grep pressure_mb /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
+
              fi
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]*"`
+
              RAW_AMBIENT_RAIN="NULL"
precip_today_in=`grep precip_today_in /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
+
              ;;
 +
esac
 +
 
 +
 
 +
if [ "$RAW_AMBIENT_RAIN" != "NULL" ]
 +
then
 +
  AMBIENT_RAIN=`echo "scale=2; (($RAW_AMBIENT_RAIN*$AMBIENT_RAIN_SLOPE)+$AMBIENT_RAIN_INTERCEPT)/1" | bc`
 +
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Ambient Rain" "$AMBIENT_RAIN" "$AMBIENT_RAIN_SENSOR" "$AMBIENT_RAIN_DEV" "$AMBIENT_RAIN_SLOPE" "$AMBIENT_RAIN_INTERCEPT" "$RAW_AMBIENT_RAIN"
 +
else
 +
  AMBIENT_RAIN="NULL"
 +
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Rain" "$RAW_AMBIENT_RAIN" "$AMBIENT_RAIN_SENSOR" "$AMBIENT_RAIN_DEV" "$AMBIENT_RAIN_SLOPE" "$AMBIENT_RAIN_INTERCEPT" "$AMBIENT_RAIN"
 +
fi
  
 +
#
 +
# Read the Battery Voltage
 +
#
 +
case "$BATTERY_VOLTAGE_SENSOR" in
 +
        HX711b)
 +
              VOLTAGE=$(/home/hivetool/hx711b.sh)
 +
              set -- junk $VOLTAGE
 +
              shift
 +
              RAW_BATTERY_VOLTAGE=$1
 +
              if [ -z "$RAW_BATTERY_VOLTAGE" ]
 +
              then
 +
                RAW_BATTERY_VOLTAGE="NULL"
 +
              fi
 +
              ;;
 +
        HX711pyB)
 +
              VOLTAGE=$(/home/hivetool/hx711pyB.sh)
 +
              set -- junk $VOLTAGE
 +
              shift
 +
              RAW_BATTERY_VOLTAGE=$1
 +
              if [ -z "$RAW_BATTERY_VOLTAGE" ]
 +
              then
 +
                RAW_BATTERY_VOLTAGE="NULL"
 +
              fi
 +
              ;;
 +
          *)
 +
              RAW_BATTERY_VOLTAGE="NULL"
 +
              ;;
 +
esac
  
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
+
if [ "$RAW_BATTERY_VOLTAGE" != "NULL" ]
 +
then
 +
  BATTERY_VOLTAGE=`echo "scale=2; (($RAW_BATTERY_VOLTAGE*$BATTERY_VOLTAGE_SLOPE)+$BATTERY_VOLTAGE_INTERCEPT)/1" | bc`
 +
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Battery Voltage" "$BATTERY_VOLTAGE" "$BATTERY_VOLTAGE_SENSOR" "$BATTERY_VOLTAGE_DEV" "$BATTERY_VOLTAGE_SLOPE" "$BATTERY_VOLTAGE_INTERCEPT" "$RAW_BATTERY_VOLTAGE"
 +
else
 +
  BATTERY_VOLTAGE="NULL"
 +
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Battery Voltage" "$BATTERY_VOLTAGE" "$BATTERY_VOLTAGE_SENSOR" "$BATTERY_VOLTAGE_DEV" "$BATTERY_VOLTAGE_SLOPE" "$BATTERY_VOLTAGE_INTERCEPT" "$RAW_BATTERY_VOLTAGE"
 +
fi
 +
  
 +
if [ -n "$WX_STATION_ID" ]
 +
then
 
#
 
#
# create hive.xml to send to the database
+
# Get the weather from a local wx station via weatherunderground
 
#
 
#
 +
echo
 +
echo "Downloading data from WeatherUnderground weather station $WX_STATION_ID to /tmp/wx.xml. cURL reports:"
 +
echo
 +
curl --retry 5 http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=$WX_STATION_ID > /tmp/wx.xml
 +
WX_DOWNLOAD_EXIT_CODE=$?
 +
 +
if [ $WX_DOWNLOAD_EXIT_CODE -eq 0 ]
 +
then
 +
echo
 +
echo "Parsing weather data from /tmp/wx.xml"
 +
WX_TEMP_F=`grep temp_f /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
 +
WX_TEMP_C=`grep temp_c /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
 +
WX_RELATIVE_HUMIDITY=`grep relative_humidity /tmp/wx.xml | grep  -o "[0-9]*"`
 +
WX_WIND_DIR=`grep wind_dir /tmp/wx.xml | grep -o "[A-Z]*"`
 +
WX_WIND_DEGREES=`grep wind_degrees /tmp/wx.xml | grep -o "[0-9]*"`
 +
WX_WIND_MPH=`grep wind_mph /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
 +
WX_WIND_GUST_MPH=`grep wind_gust_mph /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
 +
WX_PRESSURE_MB=`grep pressure_mb /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
 +
WX_DEWPOINT_F=`grep dewpoint_f /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
 +
WX_DEWPOINT_C=`grep dewpoint_c /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
 +
WX_PRECIP_1HR_IN=`grep precip_1hr_in /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
 +
WX_PRECIP_TODAY_IN=`grep precip_today_in /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
 +
fi
 +
 +
fi              # end if [ -n "$WX_STATION_ID" ]
 +
 +
if [ -z "$WX_TEMP_F" ]; then WX_TEMP_F="NULL"; fi
 +
if [ -z "$WX_TEMP_C" ]; then WX_TEMP_C="NULL"; fi
 +
if [ -z "$WX_RELATIVE_HUMIDITY" ]; then WX_RELATIVE_HUMIDITY="NULL"; fi
 +
if [ -z "$WX_WIND_DIR" ]; then WX_WIND_DIR="NULL"; fi
 +
if [ -z "$WX_WIND_DEGREES" ]; then WX_WIND_DEGREES="NULL"; fi
 +
if [ -z "$WX_WIND_MPH" ]; then WX_WIND_MPH="NULL"; fi
 +
if [ -z "$WX_WIND_GUST_MPH" ]; then WX_WIND_GUST_MPH="NULL"; fi
 +
if [ -z "$WX_PRESSURE_MB" ]; then WX_PRESSURE_MB="NULL"; fi
 +
if [ -z "$WX_DEWPOINT_F" ]; then WX_DEWPOINT_F="NULL"; fi
 +
if [ -z "$WX_DEWPOINT_C" ]; then WX_DEWPOINT_C="NULL"; fi
 +
if [ -z "$WX_PRECIP_1HR_IN" ]; then WX_PRECIP_1HR_IN="NULL"; fi
 +
if [ -z "$WX_PRECIP_TODAY_IN" ]; then WX_PRECIP_TODAY_IN="NULL"; fi
 +
 +
 +
if [[ $VERBOSE -gt 0 ]]
 +
then
 +
# This is what gets logged.
 +
 +
echo "WS Station ID    " $WX_STATION_ID
 +
echo "WS Temperature    " $WX_TEMP_F
 +
echo "WS Wind Direction " $WX_WIND_DIR
 +
echo "WS Wind Degrees  " $WX_WIND_DEGREES
 +
echo "WS Wind Speed    " $WX_WIND_MPH
 +
echo "WS Wind Gust      " $WX_WIND_GUST_MPH
 +
echo "WS Dewpoint F    " $WX_DEWPOINT_F
 +
echo "WS Dewpoint C    " $WX_DEWPOINT_C
 +
echo "WS Humidity      " $WX_RELATIVE_HUMIDITY
 +
echo "WS Pressure      " $WX_PRESSURE_MB
 +
echo "WS Precip Today  " $WX_PRECIP_TODAY_IN
 +
fi
 +
 +
#
 +
# Append everything to a local flat text log file
 +
#
 +
echo
 +
echo "Logging to /home/hivetool/$HIVE_NAME.log"
 +
echo "$DATE,$HIVE_WEIGHT,$HIVE_TEMP,$HIVE_HUMIDITY,$AMBIENT_TEMP,$AMBIENT_HUMIDITY,$AMBIENT_LIGHT,$AMBIENT_RAIN,$WX_TEMP_F,$WX_WIND_DIR,$WX_WIND_DEGREES,$WX_WIND_MPH,$WX_WIND_GUST_MPH,$WX_DEWPOINT_F,$WX_DEWPOINT_C,$WX_RELATIVE_HUMIDITY,$WX_PRESSURE_MB,$WX_PRECIP_TODAY_IN"
 +
 +
echo -ne "\n"$DATE,$HIVE_WEIGHT,$HIVE_TEMP,$HIVE_HUMIDITY,$AMBIENT_TEMP,$AMBIENT_HUMIDITY,$AMBIENT_LIGHT,$AMBIENT_RAIN,$WX_TEMP_F,$WX_WIND_DIR,$WX_WIND_DEGREES,$WX_WIND_MPH,$WX_WIND_GUST_MPH,$WX_DEWPOINT_F,$WX_DEWPOINT_C,$WX_RELATIVE_HUMIDITY,$WX_PRESSURE_MB,$WX_PRECIP_TODAY_IN>> /home/hivetool/$HIVE_NAME.log
 +
#
 +
# Insert it into a local SQL database
 +
#
 +
if [ "$DATABASE_LOCAL" = "YES" ]
 +
then
 +
  echo
 +
  echo "Inserting row in table HIVE_DATA in sqlite database /home/hivetool/hivetool_raw.db"
 +
  #source /home/hivetool/sql.sh
 +
  source /home/hivetool/sqlite.sh
 +
else
 +
  echo
 +
  echo "WARNING: Logging to local database disabled."
 +
fi
 +
 +
 +
#
 +
# Create hive1 xml data file
 +
#
 +
echo
 +
echo "Writing /tmp/hive.xml"
 
echo "<hive_data>" > /tmp/hive.xml
 
echo "<hive_data>" > /tmp/hive.xml
 
source /home/hivetool/xml.sh >> /tmp/hive.xml
 
source /home/hivetool/xml.sh >> /tmp/hive.xml
cat /tmp/wx.xml|grep -v "xml" >> /tmp/hive.xml
+
 
 +
if [[ -n "$WX_STATION_ID" && "$WX_DOWNLOAD_EXIT_CODE" -eq 0 ]] ; then cat /tmp/wx.xml|grep -v "xml" >> /tmp/hive.xml; fi
 +
 
 
echo "</hive_data>" >> /tmp/hive.xml
 
echo "</hive_data>" >> /tmp/hive.xml
 +
#
 +
# Send hive1 data to hivetool
 +
#
 +
 +
# ### REMOVE ###
 +
# HIVETOOL_UPLOAD="YES"
 +
 +
if [ "$HIVETOOL_UPLOAD" = "YES" ]
 +
then
  
#
+
echo "Uploading /tmp/hive.xml to HiveTool.org. cURL reports:"
# Write everything to the log file
+
echo
#
+
#/usr/bin/curl --retry 3 -k -u $HIVETOOL_USER:$HIVETOOL_PASSWORD -X POST --data-binary @/tmp/hive.xml https://hivetool.org/private/log_hive9.pl  -H 'Accept: application/xml' -H 'Content-Type: application/xml' 1>/tmp/hive_command.xml
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
 
  
 +
HTTP_STATUS=$(/usr/bin/curl  --write-out "%{http_code}\n" --output /tmp/hive_command.xml --retry 3 -f -k -u $HIVETOOL_USER:$HIVETOOL_PASSWORD -X POST --data-binary @/tmp/hive.xml https://hivetool.org/private/log_hive9.pl  -H 'Accept: application/xml' -H 'Content-Type: application/xml')
 +
EXIT_CODE=$?
 
#
 
#
#run the graphing program to create index.html and hive_graph.gif
+
# Test for login failure and suspend uploads to avoid blacklist
 
#
 
#
/var/www/htdocs/graph_hive.pl
 
  
 
#
 
#
#send the data to hivetool
+
# ### REMOVE EXIT_CODE=0 when working ###
 +
# setting EXIT_CODE to zero skips the next section of code that isn't working right.
 
#
 
#
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'
+
EXIT_CODE=0
 +
 
 +
if [ $EXIT_CODE -ne 0 ]
 +
then
 +
  echo
 +
  echo "ERROR: curl upload failed. Exit code:  $EXIT_CODE  Server return code= $HTTP_STATUS"
 +
  if [ $HTTP_STATUS -eq 401 ]
 +
  then
 +
    echo "Bad user/password. Uploads suspended."
 +
    sed -i -e 's/HIVETOOL_UPLOAD=YES/HIVETOOL_UPLOAD=NO/' /home/hivetool/hive.conf
 +
  fi
 +
  if [ $HTTP_STATUS -eq 404 ]
 +
  then
 +
    echo "Bad URL. Uploads suspended."
 +
    sed -i -e 's/HIVETOOL_UPLOAD=YES/HIVETOOL_UPLOAD=NO/' /home/hivetool/hive.conf
 +
  fi
 +
fi
 +
 
 +
echo
 +
echo "The server responed:"
 +
cat /tmp/hive_command.xml
 +
#
 +
# run a remote command
 +
#
 +
echo
 +
echo "Checking for hive command"
 +
/home/hivetool/hive_command.pl
 +
else
 +
  echo
 +
  echo "WARNING: Data uploads are disabled."
 +
fi
 +
 
 +
#
 +
# End of hive.sh
 +
DATE=`date +"%Y-%m-%d %H:%M:%S"`
 +
echo "Finished /home/hivetool/hive.sh at $DATE"
 
</pre>
 
</pre>

Latest revision as of 17:08, 12 May 2019

/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. inserts a row in the local database,
  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
# ##############################################################################
#                         hive.sh ver 0.8.4
#
# Reads sensors, logs them and sends the data to hivetool.net
# 
# May  use:
# hive.conf             Configuration file
# bme680.py             Read temperature, humidity, pressure, gas resistance from Bosh BME680
# cpw200plus.sh         Read weight from Adam Equipment CPW200plus scale
# dht22.sh              Read temperature and humidity from DHT22 (from Grove Seeed_DHT22) DOEN"T WORK IN 0.8)
# dht22_adafruit.sh     Read temperature and humidity from DHT22 (Replaces Grove Seeed_DHT22)
# dhtxx.sh              Read temperature and humidity from DHTxx
# ds18b20.sh            Read temperature from DS18B20
# hx711.sh              Read weight from HX711 board (c/wiringPi)
# hx711py.sh            Read weight from HX711 board (python/pigpio)
# hx711b.sh             Read voltage from HX711 board (c/wiringPi)
# hx711pyB.sh           Read voltage from HX711 board (python/pigpio)
# phidget.sh            Read weight from Phidgets Bridge board
# temperhum.sh          Read temperature and humidity from TEMPerHUM model 2
# mysql.sh              Log data to local MySQL database
# sqlite.sh             Log data to local SQLite database
# tsl2561.sh            Read lux from TSL2561
# tsl2591.sh            Read lux from TSL2561
# raingauge.conf        Read rain gauge
# xml.sh                Writes the data in XML format to upload to hivetool.org
#
# ##############################################################################

REDIRECT=1                             # comment this line to turn off redirecting stdout and stderr to hivetool.log
VERBOSE=1                              # set to 1 for more info
export LC_NUMERIC="en_US.UTF-8"

#
# Get the date and time
#
export DATE=`date +"%Y-%m-%d %H:%M:%S"`
#
# Redirect stdout and stderr to logfile.  Useful to check for errors when run with no console (cron).
#
if [ "$REDIRECT" -gt "0" ]
then
   rm /home/hivetool/hivetool.log             # delete the last log

   exec >>/home/hivetool/hivetool.log 2>&1    # redirect stdout and stderr to logfile
fi
#
# delete the last server response if it exits
#
if [ -f "/tmp/hive_command.xml" ]
then
 rm /tmp/hive_command.xml
fi
#
# Read the configuration file and set the shell variables
#
echo
echo "Starting /home/hivetool/hive.sh at $DATE"
echo "Reading /home/hivetool/hive.conf configuration file."

IFS="="                               # set the Internal Field Separator to equal sign
while read name value; do             # split each line on the IFS (=) into 2 variables: name and value
  value=${value#\"}                   # remove leading "
  value=${value%\"}                   # remove trailing "
  export $name="$value"               # export the variables to make them available to child processes
done <"/home/hivetool/hive.conf"      # read each line from the config file
IFS=" "                               # set the IFS back to space or reading TEMPerHUM will fail

[ ! -z HIVE_NAME ] && HIVE1_NAME="TEST1"

echo
echo "Hive Name: $HIVE_NAME"

if [ "$SYSTEM_STATUS" == "DISABLED"  ]
then
  echo "Hivetool is DISABLED. Doing nothing."
  echo ""
  exit 1
fi

#
# test for PI GPIO daemon running
#
echo "Checking if pigpiod is running"

PIGPIOD_PID=`/bin/pidof pigpiod`

if [ $? -eq 0 ]
then
   echo "Running.  Process ID (pid): $PIGPIOD_PID"
else
   echo "pigpiod is NOT running.  Trying to start it."
   /usr/bin/pigpiod
   echo "pigpiod exit code: $?"
fi

#
# Turn on pull up resistor on GPIO 7 for 1-Wire
/usr/local/bin/gpio mode 7 up
#
# test for Maintenance Hold (hive manipulation)
#
QUALITY=`cat /dev/shm/quality`
if [[ $QUALITY -eq 4 ]]
then 
  echo "*** MAINTENANCE MODE **"
fi
echo 
echo
echo "Reading Sensors"
echo "   Variable            Scaled     Driver     GPIO/device/params/ID      Slope     Intercept    Raw"
#
# Read scale
#
case "$HIVE_WEIGHT_SENSOR" in
        CPW200plus)
            RAW_HIVE_WEIGHT=$(/home/hivetool/cpw200plus.sh $HIVE_WEIGHT_DEV)
            set -- junk $HIVE_WEIGHT
            shift
            RAW_HIVE_WEIGHT=$2
            ;;
        HX711)
            RAW_HIVE_WEIGHT=$(/home/hivetool/hx711.sh) 
            ;;
        HX711py)
            RAW_HIVE_WEIGHT=$(/home/hivetool/hx711py.sh)
            ;;
        Phidget)
            RAW_HIVE_WEIGHT=$(/home/hivetool/phidget.sh)
            ;;
        none)
            RAW_HIVE_WEIGHT="NULL"               # Set hive weight to NULL if scale type is set to "none"
#            echo "No scale selected HIVE_WEIGHT_SENSOR: $HIVE_WEIGHT_SENSOR"
            ;;
        *)
            RAW_HIVE_WEIGHT="NULL"               # Set hive weight to NULL if scale type is not set or set wrong
            echo "No scale selected or unknown HIVE_WEIGHT_SENSOR: $HIVE_WEIGHT_SENSOR"    # and display warning
            ;;
esac


if [ $RAW_HIVE_WEIGHT != "NULL" ]
then
  HIVE_WEIGHT=`echo "scale=3; (($RAW_HIVE_WEIGHT*$HIVE_WEIGHT_SLOPE)+$HIVE_WEIGHT_INTERCEPT)/1" | bc`
  printf "%-16s %12.3f %12s %24s %12.9f %8.3f %12.2f\n" "Hive Weight" "$HIVE_WEIGHT" "$HIVE_WEIGHT_SENSOR" "$HIVE_WEIGHT_DEV" "$HIVE_WEIGHT_SLOPE" "$HIVE_WEIGHT_INTERCEPT" "$RAW_HIVE_WEIGHT"
else
  HIVE_WEIGHT="NULL"
  HIVE_WEIGHT_F="NULL"
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Hive Weight" "$HIVE_WEIGHT" "$HIVE_WEIGHT_SENSOR" "$HIVE_WEIGHT_DEV" "$HIVE_WEIGHT_SLOPE" "$HIVE_WEIGHT_INTERCEPT" "$RAW_HIVE_WEIGHT"
fi

#
# Read temp and humidity inside hive
#
case "$HIVE_TEMP_SENSOR" in
         TEMPerHUM)
              TEMPerHUM=$(/home/hivetool/temperhum.sh -d $HIVE_TEMP_DEV)
              set -- junk $TEMPerHUM
              shift
              RAW_HIVE_TEMP=$1
              RAW_HIVE_HUMIDITY=$2
              if [ -z "$RAW_HIVE_TEMP" ]
              then
                 RAW_HIVE_TEMP="NULL"
                 RAW_HIVE_HUMIDITY="NULL"
              fi
              ;;
         BME680)
              
              ;;
         DHT11)
              TEMPerHUM=$(/home/hivetool/dht11_adafruit.sh -d $HIVE_TEMP_DEV)
              set -- junk $TEMPerHUM
              shift
              RAW_HIVE_TEMP=$1
              RAW_HIVE_HUMIDITY=$2
              sleep 4                # fixes a weirdness with bad readings when reading 2 DHT22's in a row.  (not necessary it the second temp/rh sensor is not a DHT22
              ;; 
          DHT22)
#             TEMPerHUM=$(/home/hivetool/dht22.sh -d $HIVE_TEMP_DEV)
              TEMPerHUM=$(/home/hivetool/dht22_adafruit.sh -d $HIVE_TEMP_DEV)
#             TEMPerHUM=$(nice --20 /usr/local/bin/Seeed_DHT22  $HIVE_TEMP_DEV S)
              set -- junk $TEMPerHUM
              shift
              RAW_HIVE_TEMP=$1
              RAW_HIVE_HUMIDITY=$2
              sleep 4                # fixes a weirdness with bad readings when reading 2 DHT22's in a row.  (not necessary it the second temp/rh sensor is not a DHT22
              ;; 
         DHTxx)
              TEMPerHUM=$(/home/hivetool/dhtxx.sh -d $HIVE_TEMP_DEV)
              set -- junk $TEMPerHUM
              shift
              RAW_HIVE_TEMP=$1
              RAW_HIVE_HUMIDITY=$2
              ;; 
         DS18B20)
              TEMPerHUM=$(/home/hivetool/ds18b20.sh -d $HIVE_TEMP_DEV)

              set -- junk $TEMPerHUM

              shift
              RAW_HIVE_TEMP=$1
              ;;
           *)
              RAW_HIVE_TEMP="NULL"
              RAW_HIVE_HUMIDITY="NULL"
              ;;
esac



if [ "$RAW_HIVE_TEMP" != "NULL" ]
then
  HIVE_TEMP=`echo "scale=2; (($RAW_HIVE_TEMP*$HIVE_TEMP_SLOPE)+$HIVE_TEMP_INTERCEPT)/1" | bc`
  HIVE_TEMP_F=`echo "scale=2; ((($HIVE_TEMP*9)/5)+32)/1" | bc`
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Hive Temp" "$HIVE_TEMP" "$HIVE_TEMP_SENSOR" "$HIVE_TEMP_DEV" "$HIVE_TEMP_SLOPE" "$HIVE_TEMP_INTERCEPT" "$RAW_HIVE_TEMP"
else
  HIVE_TEMP="NULL"
  HIVE_TEMP_F="NULL"
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Hive Temp" "$HIVE_TEMP" "$HIVE_TEMP_SENSOR" "$HIVE_TEMP_DEV" "$HIVE_TEMP_SLOPE" "$HIVE_TEMP_INTERCEPT" "$RAW_HIVE_TEMP"
fi

if [ $RAW_HIVE_HUMIDITY != "NULL" ]
then
  HIVE_HUMIDITY=`echo "scale=2; (($RAW_HIVE_HUMIDITY*$HIVE_HUMIDITY_SLOPE)+$HIVE_HUMIDITY_INTERCEPT)/1" | bc`
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Hive Humidity" "$HIVE_HUMIDITY" "$HIVE_HUMIDITY_SENSOR" "$HIVE_HUMIDITY_DEV" "$HIVE_HUMIDITY_SLOPE" "$HIVE_HUMIDITY_INTERCEPT" "$RAW_HIVE_HUMIDITY"
else
  HIVE_HUMIDITY="NULL"
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Hive Humidity" "$HIVE_HUMIDITY" "$HIVE_HUMIDITY_SENSOR" "$HIVE_HUMIDITY_DEV" "$HIVE_HUMIDITY_SLOPE" "$HIVE_HUMIDITY_INTERCEPT" "$RAW_HIVE_HUMIDITY"
fi



#
# Read outside temp and humidity
#
case "$AMBIENT_TEMP_SENSOR" in
         TEMPerHUM)
              TEMPerHUM=$(/home/hivetool/temperhum.sh -d $AMBIENT_TEMP_DEV)
              set -- junk $TEMPerHUM
              shift
              RAW_AMBIENT_TEMP=$1
              RAW_AMBIENT_HUMIDITY=$2
              if [ -z "$RAW_AMBIENT_TEMP" ]
              then
                 RAW_AMBIENT_TEMP="NULL"
                 RAW_AMBIENT_HUMIDITY="NULL"
              fi
              ;;
         BME680)
              ;;
         DHT11)
              TEMPerHUM=$(/home/hivetool/dht11_adafruit.sh -d $HIVE_TEMP_DEV)
              set -- junk $TEMPerHUM
              shift
              RAW_HIVE_TEMP=$1
              RAW_HIVE_HUMIDITY=$2
              sleep 4                # fixes a weirdness with bad readings when reading 2 DHT22's in a row.  (not necessary it the second temp/rh sensor is not a DHT22
              ;; 
         DHT22)
#             TEMPerHUM=$(/home/hivetool/dht22.sh -d $AMBIENT_TEMP_DEV)
              TEMPerHUM=$(/home/hivetool/dht22_adafruit.sh -d $AMBIENT_TEMP_DEV)

#             TEMPerHUM=$(nice --20 /usr/local/bin/Seeed_DHT22  $AMBIENT_TEMP_DEV S)
              set -- junk $TEMPerHUM
              shift
              RAW_AMBIENT_TEMP=$1
              RAW_AMBIENT_HUMIDITY=$2
              ;;
        DHTxx)
              TEMPerHUM=$(/home/hivetool/dhtxx.sh -d $AMBIENT_TEMP_DEV)
              set -- junk $TEMPerHUM
              shift
              RAW_AMBIENT_TEMP=$1
              RAW_AMBIENT_HUMIDITY=$2
              ;; 
      DS18B20)
              TEMPerHUM=$(/home/hivetool/ds18b20.sh -d $AMBIENT_TEMP_DEV)
              set -- junk $TEMPerHUM
              shift
              RAW_AMBIENT_TEMP=$1
              RAW_AMBIENT_HUMIDITY="NULL"
              ;;
           *)
              RAW_AMBIENT_TEMP="NULL"
              RAW_AMBIENT_HUMIDITY="NULL"
              ;;
esac

if [ "$RAW_AMBIENT_TEMP" != "NULL" ]
then
  AMBIENT_TEMP=`echo "scale=2; (($RAW_AMBIENT_TEMP*$AMBIENT_TEMP_SLOPE)+$AMBIENT_TEMP_INTERCEPT)/1" | bc`
  AMBIENT_TEMP_F=`echo "scale=2; ((($AMBIENT_TEMP*9)/5)+32)/1" | bc`
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Ambient Temp" "$AMBIENT_TEMP" "$AMBIENT_TEMP_SENSOR" "$AMBIENT_TEMP_DEV" "$AMBIENT_TEMP_SLOPE" "$AMBIENT_TEMP_INTERCEPT" "$RAW_AMBIENT_TEMP"
else
  AMBIENT_TEMP="NULL"
  AMBIENT_TEMP_F="NULL"
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Ambient Temp" "$AMBIENT_TEMP" "$AMBIENT_TEMP_SENSOR" "$AMBIENT_TEMP_DEV" "$AMBIENT_TEMP_SLOPE" "$AMBIENT_TEMP_INTERCEPT" "$RAW_AMBIENT_TEMP"
fi

if [ "$RAW_AMBIENT_HUMIDITY" != "NULL" ]
then
  AMBIENT_HUMIDITY=`echo "scale=2; (($RAW_AMBIENT_HUMIDITY*$AMBIENT_HUMIDITY_SLOPE)+$AMBIENT_HUMIDITY_INTERCEPT)/1" | bc`
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Ambient Humidity" "$AMBIENT_HUMIDITY" "$AMBIENT_HUMIDITY_SENSOR" "$AMBIENT_HUMIDITY_DEV" "$AMBIENT_HUMIDITY_SLOPE" "$AMBIENT_HUMIDITY_INTERCEPT" "$RAW_AMBIENT_HUMIDITY"
else
  AMBIENT_HUMIDITY="NULL"
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Ambient Humidity" "$AMBIENT_HUMIDITY" "$AMBIENT_HUMIDITY_SENSOR" "$AMBIENT_HUMIDITY_DEV" "$AMBIENT_HUMIDITY_SLOPE" "$AMBIENT_HUMIDITY_INTERCEPT" "$RAW_AMBIENT_HUMIDITY"
fi

#
# Read outside light
#
case "$AMBIENT_LIGHT_SENSOR" in
         TSL2561)
              LIGHT=$(/home/hivetool/2561.sh)
              set -- junk $LIGHT
              shift
              RAW_AMBIENT_LIGHT=$1
              if [ -z "$RAW_AMBIENT_LIGHT" ]
              then
                 RAW_AMBIENT_LIGHT="NULL"
              fi
              ;;
         TSL2591)
              LIGHT=$(/home/hivetool/2591.sh)
              set -- junk $LIGHT
              shift
              RAW_AMBIENT_LIGHT=$1
              if [ -z "$RAW_AMBIENT_LIGHT" ]
              then
                 RAW_AMBIENT_LIGHT="NULL"
              fi
              ;;
          *)
              RAW_AMBIENT_LIGHT="NULL"
              ;;
esac

if [ "$RAW_AMBIENT_LIGHT" != "NULL" ]
then
  AMBIENT_LIGHT=`echo "scale=2; (($RAW_AMBIENT_LIGHT*$AMBIENT_LIGHT_SLOPE)+$AMBIENT_LIGHT_INTERCEPT)/1" | bc`
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Ambient Light" "$AMBIENT_LIGHT" "$AMBIENT_LIGHT_SENSOR" "$AMBIENT_LIGHT_DEV" "$AMBIENT_LIGHT_SLOPE" "$AMBIENT_LIGHT_INTERCEPT" "$RAW_AMBIENT_LIGHT"
else
  AMBIENT_LIGHT="NULL"
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Ambient Light" "$AMBIENT_LIGHT" "$AMBIENT_LIGHT_SENSOR" "$AMBIENT_LIGHT_DEV" "$AMBIENT_LIGHT_SLOPE" "$AMBIENT_LIGHT_INTERCEPT" "$RAW_AMBIENT_LIGHT"
fi

#
# Read rain gauge
#
case "$AMBIENT_RAIN_SENSOR" in
         yes)
              RAW_AMBIENT_RAIN=`cat /home/hivetool/rain_total.conf`
              if [ -z "$RAW_AMBIENT_RAIN" ]
              then
                 RAW_AMBIENT_RAIN="NULL"
              fi
              ;;
          *)
              RAW_AMBIENT_RAIN="NULL"
              ;;
esac


if [ "$RAW_AMBIENT_RAIN" != "NULL" ]
then
  AMBIENT_RAIN=`echo "scale=2; (($RAW_AMBIENT_RAIN*$AMBIENT_RAIN_SLOPE)+$AMBIENT_RAIN_INTERCEPT)/1" | bc`
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Ambient Rain" "$AMBIENT_RAIN" "$AMBIENT_RAIN_SENSOR" "$AMBIENT_RAIN_DEV" "$AMBIENT_RAIN_SLOPE" "$AMBIENT_RAIN_INTERCEPT" "$RAW_AMBIENT_RAIN"
else
  AMBIENT_RAIN="NULL"
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Rain" "$RAW_AMBIENT_RAIN" "$AMBIENT_RAIN_SENSOR" "$AMBIENT_RAIN_DEV" "$AMBIENT_RAIN_SLOPE" "$AMBIENT_RAIN_INTERCEPT" "$AMBIENT_RAIN"
fi

#
# Read the Battery Voltage
#
case "$BATTERY_VOLTAGE_SENSOR" in
        HX711b)
              VOLTAGE=$(/home/hivetool/hx711b.sh)
              set -- junk $VOLTAGE
              shift
              RAW_BATTERY_VOLTAGE=$1
              if [ -z "$RAW_BATTERY_VOLTAGE" ]
              then
                 RAW_BATTERY_VOLTAGE="NULL"
              fi
              ;;
        HX711pyB)
              VOLTAGE=$(/home/hivetool/hx711pyB.sh)
              set -- junk $VOLTAGE
              shift
              RAW_BATTERY_VOLTAGE=$1
              if [ -z "$RAW_BATTERY_VOLTAGE" ]
              then
                 RAW_BATTERY_VOLTAGE="NULL"
              fi
              ;;
          *)
              RAW_BATTERY_VOLTAGE="NULL"
              ;;
esac


if [ "$RAW_BATTERY_VOLTAGE" != "NULL" ]
 then
  BATTERY_VOLTAGE=`echo "scale=2; (($RAW_BATTERY_VOLTAGE*$BATTERY_VOLTAGE_SLOPE)+$BATTERY_VOLTAGE_INTERCEPT)/1" | bc`
  printf "%-16s %12.2f %12s %24s %12.9f %8.3f %12.2f\n" "Battery Voltage" "$BATTERY_VOLTAGE" "$BATTERY_VOLTAGE_SENSOR" "$BATTERY_VOLTAGE_DEV" "$BATTERY_VOLTAGE_SLOPE" "$BATTERY_VOLTAGE_INTERCEPT" "$RAW_BATTERY_VOLTAGE"
 else
  BATTERY_VOLTAGE="NULL"
  printf "%-16s %12s %12s %24s %12.9f %8.3f %12s\n" "Battery Voltage" "$BATTERY_VOLTAGE" "$BATTERY_VOLTAGE_SENSOR" "$BATTERY_VOLTAGE_DEV" "$BATTERY_VOLTAGE_SLOPE" "$BATTERY_VOLTAGE_INTERCEPT" "$RAW_BATTERY_VOLTAGE"
 fi
 

if [ -n "$WX_STATION_ID" ]
then
#
# Get the weather from a local wx station via weatherunderground
#
echo
echo "Downloading data from WeatherUnderground weather station $WX_STATION_ID to /tmp/wx.xml. cURL reports:"
echo
curl --retry 5 http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=$WX_STATION_ID > /tmp/wx.xml
WX_DOWNLOAD_EXIT_CODE=$?

if [ $WX_DOWNLOAD_EXIT_CODE -eq 0 ]
then
echo
echo "Parsing weather data from /tmp/wx.xml"
WX_TEMP_F=`grep temp_f /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
WX_TEMP_C=`grep temp_c /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
WX_RELATIVE_HUMIDITY=`grep relative_humidity /tmp/wx.xml | grep  -o "[0-9]*"`
WX_WIND_DIR=`grep wind_dir /tmp/wx.xml | grep -o "[A-Z]*"`
WX_WIND_DEGREES=`grep wind_degrees /tmp/wx.xml | grep -o "[0-9]*"`
WX_WIND_MPH=`grep wind_mph /tmp/wx.xml | grep  -o "[0-9]*\.[0-9]*"`
WX_WIND_GUST_MPH=`grep wind_gust_mph /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
WX_PRESSURE_MB=`grep pressure_mb /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
WX_DEWPOINT_F=`grep dewpoint_f /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
WX_DEWPOINT_C=`grep dewpoint_c /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
WX_PRECIP_1HR_IN=`grep precip_1hr_in /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
WX_PRECIP_TODAY_IN=`grep precip_today_in /tmp/wx.xml |  grep  -o "[0-9]*\.[0-9]*"`
fi

fi               # end if [ -n "$WX_STATION_ID" ]

if [ -z "$WX_TEMP_F" ]; then WX_TEMP_F="NULL"; fi
if [ -z "$WX_TEMP_C" ]; then WX_TEMP_C="NULL"; fi
if [ -z "$WX_RELATIVE_HUMIDITY" ]; then WX_RELATIVE_HUMIDITY="NULL"; fi
if [ -z "$WX_WIND_DIR" ]; then WX_WIND_DIR="NULL"; fi
if [ -z "$WX_WIND_DEGREES" ]; then WX_WIND_DEGREES="NULL"; fi
if [ -z "$WX_WIND_MPH" ]; then WX_WIND_MPH="NULL"; fi
if [ -z "$WX_WIND_GUST_MPH" ]; then WX_WIND_GUST_MPH="NULL"; fi
if [ -z "$WX_PRESSURE_MB" ]; then WX_PRESSURE_MB="NULL"; fi
if [ -z "$WX_DEWPOINT_F" ]; then WX_DEWPOINT_F="NULL"; fi
if [ -z "$WX_DEWPOINT_C" ]; then WX_DEWPOINT_C="NULL"; fi
if [ -z "$WX_PRECIP_1HR_IN" ]; then WX_PRECIP_1HR_IN="NULL"; fi
if [ -z "$WX_PRECIP_TODAY_IN" ]; then WX_PRECIP_TODAY_IN="NULL"; fi


if [[ $VERBOSE -gt 0 ]]
then
# This is what gets logged.
 
 echo "WS Station ID     " $WX_STATION_ID
 echo "WS Temperature    " $WX_TEMP_F
 echo "WS Wind Direction " $WX_WIND_DIR
 echo "WS Wind Degrees   " $WX_WIND_DEGREES
 echo "WS Wind Speed     " $WX_WIND_MPH
 echo "WS Wind Gust      " $WX_WIND_GUST_MPH
 echo "WS Dewpoint F     " $WX_DEWPOINT_F
 echo "WS Dewpoint C     " $WX_DEWPOINT_C
 echo "WS Humidity       " $WX_RELATIVE_HUMIDITY
 echo "WS Pressure       " $WX_PRESSURE_MB
 echo "WS Precip Today   " $WX_PRECIP_TODAY_IN
fi

#
# Append everything to a local flat text log file
#
echo
echo "Logging to /home/hivetool/$HIVE_NAME.log"
echo "$DATE,$HIVE_WEIGHT,$HIVE_TEMP,$HIVE_HUMIDITY,$AMBIENT_TEMP,$AMBIENT_HUMIDITY,$AMBIENT_LIGHT,$AMBIENT_RAIN,$WX_TEMP_F,$WX_WIND_DIR,$WX_WIND_DEGREES,$WX_WIND_MPH,$WX_WIND_GUST_MPH,$WX_DEWPOINT_F,$WX_DEWPOINT_C,$WX_RELATIVE_HUMIDITY,$WX_PRESSURE_MB,$WX_PRECIP_TODAY_IN"

echo -ne "\n"$DATE,$HIVE_WEIGHT,$HIVE_TEMP,$HIVE_HUMIDITY,$AMBIENT_TEMP,$AMBIENT_HUMIDITY,$AMBIENT_LIGHT,$AMBIENT_RAIN,$WX_TEMP_F,$WX_WIND_DIR,$WX_WIND_DEGREES,$WX_WIND_MPH,$WX_WIND_GUST_MPH,$WX_DEWPOINT_F,$WX_DEWPOINT_C,$WX_RELATIVE_HUMIDITY,$WX_PRESSURE_MB,$WX_PRECIP_TODAY_IN>> /home/hivetool/$HIVE_NAME.log
#
# Insert it into a local SQL database
#
if [ "$DATABASE_LOCAL" = "YES" ]
then
  echo
  echo "Inserting row in table HIVE_DATA in sqlite database /home/hivetool/hivetool_raw.db"
  #source /home/hivetool/sql.sh
  source /home/hivetool/sqlite.sh
else
  echo
  echo "WARNING: Logging to local database disabled."
fi


#
# Create hive1 xml data file
#
echo
echo "Writing /tmp/hive.xml"
echo "<hive_data>" > /tmp/hive.xml
source /home/hivetool/xml.sh >> /tmp/hive.xml

if [[ -n "$WX_STATION_ID" && "$WX_DOWNLOAD_EXIT_CODE" -eq 0 ]] ; then cat /tmp/wx.xml|grep -v "xml" >> /tmp/hive.xml; fi

echo "</hive_data>" >> /tmp/hive.xml
#
# Send hive1 data to hivetool
#

# ### REMOVE ###
# HIVETOOL_UPLOAD="YES"

if [ "$HIVETOOL_UPLOAD" = "YES" ]
then

 echo "Uploading /tmp/hive.xml to HiveTool.org. cURL reports:"
 echo
 #/usr/bin/curl --retry 3 -k -u $HIVETOOL_USER:$HIVETOOL_PASSWORD -X POST --data-binary @/tmp/hive.xml https://hivetool.org/private/log_hive9.pl  -H 'Accept: application/xml' -H 'Content-Type: application/xml' 1>/tmp/hive_command.xml

 HTTP_STATUS=$(/usr/bin/curl  --write-out "%{http_code}\n" --output /tmp/hive_command.xml --retry 3 -f -k -u $HIVETOOL_USER:$HIVETOOL_PASSWORD -X POST --data-binary @/tmp/hive.xml https://hivetool.org/private/log_hive9.pl  -H 'Accept: application/xml' -H 'Content-Type: application/xml')
 EXIT_CODE=$?
#
# Test for login failure and suspend uploads to avoid blacklist
#

#
# ### REMOVE EXIT_CODE=0 when working ###
# setting EXIT_CODE to zero skips the next section of code that isn't working right.
#
EXIT_CODE=0

 if [ $EXIT_CODE -ne 0 ]
 then
   echo
   echo "ERROR: curl upload failed. Exit code:  $EXIT_CODE  Server return code= $HTTP_STATUS"
   if [ $HTTP_STATUS -eq 401 ]
   then
     echo "Bad user/password. Uploads suspended."
     sed -i -e 's/HIVETOOL_UPLOAD=YES/HIVETOOL_UPLOAD=NO/' /home/hivetool/hive.conf
   fi
   if [ $HTTP_STATUS -eq 404 ]
   then
     echo "Bad URL. Uploads suspended."
     sed -i -e 's/HIVETOOL_UPLOAD=YES/HIVETOOL_UPLOAD=NO/' /home/hivetool/hive.conf
   fi
 fi

 echo
 echo "The server responed:"
 cat /tmp/hive_command.xml
 #
 # run a remote command
 #
 echo
 echo "Checking for hive command"
 /home/hivetool/hive_command.pl
else
  echo
  echo "WARNING: Data uploads are disabled."
fi

# 
# End of hive.sh
DATE=`date +"%Y-%m-%d %H:%M:%S"`
echo "Finished /home/hivetool/hive.sh at $DATE"