HA7Net bash scripts

etd607

New Member
I have written a bash script that will allow a Linux server to get the temperature readings from 1 Wire temperature probes running off a HA7Net 1 Wire Bus Master. The script uses lynx to get the readings from the Bus Master, parses the html code for the reading, converts the reading to Fahrenheit and then stores the result in a file. (FYI: the linux I am running is RedHat 7.1. The script runs every 10 minutes as a cron job.)

Here is the shell script:

#!/bin/sh
#============================================================
#
# Script name: readHA7nettemps.sh
# Purpose: To read the temperature from the one wire
# temperature probes as html documents, parse
# the document to extract the temperature
# in centigrade, convert it to fahrenheit and
# store the result in a file on the server.
# Comparison is also made with the last high
# and low temperatures and updates their
# respective files.
#
# Files in use:
#
# outsidet.txt Holds the last reading taken from the
# outdoor sensor.
# outsidetmin.txt Holds the lowest reading since midnite
# for the outdoor sensor.
# outsidetmax.txt Holds the highest reading since midnite
# for the outdoor sensor.
# ght.txt Holds the last reading taken from the
# greenhouse sensor.
# ghtmin.txt Holds the lowest reading since midnite
# for the greenhouse sensor.
# ghtmax.txt Holds the highest reading since midnite
# for the greenhouse sensor.
#
#
# Change the string ipaddress to reflect the ip address of your HA7Net bus master.
#
ipaddress="192.168.0.5"
#============================================================
# Static string initialization area.
#
bccmd="/usr/bin/bc"
folder="/var/log/heyu"
htmlgetcmd="/usr/bin/lynx -source -preparsed"
tempsurl="http://$ipaddress/1Wire/ReadTemperature.html?Address_Array="
#
#============================================================
# The probecount integer contains the number of one wire temperature
# probes that are to be addressed.
#
probecount=2
#
# The tprobe array contains the one wire addresses of the temperature
# probes and is automatically sized to match the probecount integer.
#
# Probes:
# Address D8000000A8B91526 is located in the greenhouse,
# and address 9900080124F69D10 is the outdoor temperature
# sensor. You can get the address of your probes by running
# the web interface to the HA7Net and recording the probe(s) addresses.
#
declare tprobe[$probecount]
tprobe[0]="D8000000A8B91526"
tprobe[1]="9900080124F69D10"
#
# The lowfile array contains the names of the low temperature files.
#
declare lowfile[$probecount]
lowfile[0]="$folder/ghtmin.txt"
lowfile[1]="$folder/outsidetmin.txt"
#
# The highfile array contains the names of the high temperature files.
#
declare highfile[$probecount]
highfile[0]="$folder/ghtmax.txt"
highfile[1]="$folder/outsidetmax.txt"
#
#
# The outfile array is the target for the script to write the
# current temperature.
#
declare outfile[$probecount]
outfile[0]="$folder/ght.txt"
outfile[1]="$folder/outsidet.txt"
#
#============================================================
#
for (( j=0; j < $probecount ; j++ ))
do
temp1string=`$htmlgetcmd $tempsurl${tprobe[$j]}`
temp2string=`echo $temp1string | awk 'BEGIN { FS="VALUE" } { print $5 }'`
temp3string=`echo $temp2string | awk 'BEGIN { FS="=" } { print $2 }'`
temp4string=`echo $temp3string | awk 'BEGIN { FS="><" } { print $1 }'`
ctemp=`echo $temp4string | sed '/"/s// /g'`
#
# This next line does the Centigrade to Fahrenheit conversion.
#
ftemp=`echo "scale=0; ((($ctemp*9)/5)+32)" | $bccmd -l`
#
# Note: bc is a general purpose calculator. The scale is the number
# of decimals to be displayed in the answer. If you set scale to 1, the
# the value of ftemp will be to 1 decimal.
# The -l argument tells bc to use the standard math library.
#
# Now let's output the temperature to its storage file.
#
echo $ftemp > ${outfile[$j]}
#
# Now, let's compare the temperature to the last lowest and
# highest temperatures and overwrite the file(s) with the current
# temperature if it is higher or lower than the last recorded ones.
#
lastlowtemp=`cat ${lowfile[$j]}`
lasthightemp=`cat ${highfile[$j]}`
if [ $ftemp -gt $lasthightemp ]
then
echo $ftemp > ${highfile[$j]}
fi
if [ $ftemp -lt $lastlowtemp ]
then
echo $ftemp > ${lowfile[$j]}
fi
#
done
exit 0



After you have cut and pasted the above text, be sure to modify it for your site. You will
need to change the ipaddress, probecount and the tprobe array to suit your needs.

If you have any questions, post them to this topic and I will attempt to answer them.

Ed
 
Would the bash script work with the "HA4B - RS232 Isolated 1-Wire Host Adapter"?

The HA4B adapter provides electrical isolation between your 1-wire network and
computer. This could save the computer from lightning surges. I was looking at the
adapter earlier but did not see any Linux software available to drive it...

What would really be useful would be a library of perl routines written for the HA4B
that would allow you to access various 1-wire sensors. Perl is nice because it has a
decent interface to RRD (round robin database) which allows you to plot graphs and
display them on the web. Perl also has a nice interface to mysql in case you want to save your
readings and watch for the effects of global warming...

If you ever decide to put together such a library wrtten in perl that works with the
HA4B - let me know...
 
Would the bash script work with the "HA4B - RS232 Isolated 1-Wire Host Adapter"?

The HA4B adapter provides electrical isolation between your 1-wire network and
computer. This could save the computer from lightning surges. I was looking at the
adapter earlier but did not see any Linux software available to drive it...

What would really be useful would be a library of perl routines written for the HA4B
that would allow you to access various 1-wire sensors. Perl is nice because it has a
decent interface to RRD (round robin database) which allows you to plot graphs and
display them on the web. Perl also has a nice interface to mysql in case you want to save your
readings and watch for the effects of global warming...

If you ever decide to put together such a library wrtten in perl that works with the
HA4B - let me know...

Thanks for the question.

I did not know about the HA4B adapter until you asked the above question. Unfortunately, my bash script will not work with that adapter as it gets its information from the web server present on the HA7Net. I have my HA7Net on a network in my greenhouse that is connected wirelessly with my house network thus providing an inherent protection from lightning surges.

I originally looked into thermd for reading the HA7Net and could not get it to work. My installation of Perl is version 5 but it will not work with thermd. I updated all the Perl modules and it still would not run. I was then forced into writing bash scripts to get information from the HA7Net.

Would the digitemp software work with the HA4B? I have digitemp working off a Hobby Boards one wire serial adapter and I can currently read 3 temperature sensors just fine.

I have mysql running on my server but I need to get an SQL instruction book before I can set up and access any databases. Once I understand SQL, I will attempt to write bash scripts to access the SQL databases.

So much for my ramblings. Hope this helped.

Ed
 
Below this are the lines for a bash script to read humidity from a Hobby Boards temperature/humidity sensor on a one wire network off an HA7Net bus master. (It uses wget to retrieve the web pages.) I know that you cannot read humidity using the web interface on the HA7Net. Hopefully, this script will give others an idea of how get humidity readings from such a sensor.

Obviously, you will have to change some of the strings to match your environment: addr is the 64 bit address of your
sensor, ipaddress is the ip address of your HA7Net busmaster, and folder is a location where your humidity files are stored. Also, when you start this script, you can pass it the word debug or Debug and it will run in debug mode and echo out informational strings.


#!/bin/sh
#
debugflag=0
if [ $# -gt 0 ]
then
case $1 in
debug)
debugflag=1
;;
Debug)
debugflag=1
;;
esac
fi
#================================================
# Static string definition section
#
addr="D8000000A8B91526"
ipaddress="192.168.0.5"
addressdevice="http://$ipaddress/1Wire/AddressDevice.html"
AWKCMD="/bin/awk -f"
AWKROUNDER="/usr/share/awk/round.awk"
cutcmd="/bin/cut"
folder="/var/log/heyu"
getlock="http://$ipaddress/1Wire/GetLock.html"
low_rh_file="$folder/ghrhmin.txt"
high_rh_file="$folder/ghrhmax.txt"
outfile="$folder/ghrh.txt"
readpages="http://$ipaddress/1Wire/ReadPages.html"
releaselock="http://$ipaddress/1Wire/ReleaseLock.html"
rmcmd="/bin/rm -f"
tmpfile="/tmp/tmp.txt"
wgetcmd="/usr/local/bin/wget -r --tries=10 -q -O $tmpfile"
writeblock="http://$ipaddress/1Wire/WriteBlock.html"
#
#================================================
# Integer definition section
#
lockid=1
#
# Function to grep tmpfile for Resultdata
#
greptmpfile ()
{
tempstring=$( cat $tmpfile | grep ResultData_0 )
case $1 in
1)
# echo "$tempstring"
;;
esac
return
}
#================================================
# And, away we go!!!!
#
tmpstring=$( $wgetcmd $getlock )
lockstring=$( cat $tmpfile | grep LockID_0 )
lockstring=$( echo $lockstring | awk 'BEGIN{ FS="VALUE=" } { print $2 }' )
lockstring=$( echo $lockstring | sed '/</s///g' )
lockstring=$( echo $lockstring | sed '/>/s///g' )
lockstring=$( echo $lockstring | awk 'BEGIN{ FS="/" } { print $1 }' )
lockid=$( echo $lockstring | sed '/"/s///g' )
#
endstring="&Address=$addr&LockID=$lockid"
url="$wgetcmd $writeblock"
#
tempstring=$( $url?Data=4E0008$endstring )
greptmpfile $debugflag
tempstring=$( $url?Data=4800$endstring )
greptmpfile $debugflag
tempstring=$( $url?Data=44$endstring )
greptmpfile $debugflag
tempstring=$( $url?Data=B4$endstring )
greptmpfile $debugflag
tempstring=$( $url?Data=B800$endstring )
greptmpfile $debugflag
tempstring=$( $url?Data=BE00FFFFFFFFFFFFFFFFFF$endstring )
greptmpfile $debugflag
#
b=$( echo $tempstring | awk 'BEGIN{ FS="><" } { print $1 $2 $3 }' )
c=($:blink:
#
tempstring=$( echo ${c[5]} | sed '/=/s///g' )
tempstring=$( echo $tempstring | sed '/VALUE/s///g' )
tempstring=$( echo $tempstring | sed '/"/s///g' )
#
echo $tempstring > $tmpfile
#
templsb=$( $cutcmd -b7-8 $tmpfile )
tempmsb=$( $cutcmd -b9-10 $tmpfile )
#echo "$tempmsb"
#echo "$templsb"
#
tempmsbC=$( echo "obase=10;ibase=16; $tempmsb" | bc )
#
templsbC=$( echo "obase=10;ibase=16; $templsb" | bc )
#
tempnumber=$( echo "obase=2; ibase=16; $templsb/8" | bc )
#
tempnumber=$( echo "obase=10; ibase=2; $tempnumber" | bc )
#
tempnumber=$( echo "$tempnumber * 0.03125" | bc -l )
#
tempC=$( echo "$tempmsbC + $tempnumber" | bc -l )
#
hexvddmsb=$( $cutcmd -b13-14 $tmpfile )
#
hexvddlsb=$( $cutcmd -b11-12 $tmpfile )
#
tempnumber="$hexvddmsb$hexvddlsb"
vdd=$( echo "obase=10;ibase=16; $tempnumber" | bc )
vdd=$( echo "$vdd * 0.01" | bc )
#
tempstring=$( $url?Data=4E0000$endstring )
greptmpfile $debugflag
tempstring=$( $url?Data=4800$endstring )
greptmpfile $debugflag
tempstring=$( $url?Data=44$endstring )
greptmpfile $debugflag
tempstring=$( $url?Data=B4$endstring )
greptmpfile $debugflag
tempstring=$( $url?Data=B800$endstring )
greptmpfile $debugflag
tempstring=$( $url?Data=BE00FFFFFFFFFFFFFFFFFF$endstring )
greptmpfile $debugflag
#
b=$( echo $tempstring | awk 'BEGIN{ FS="><" } { print $1 $2 $3 }' )
c=($:)
#
tempstring=$( echo ${c[5]} | sed '/=/s///g' )
tempstring=$( echo $tempstring | sed '/VALUE/s///g' )
tempstring=$( echo $tempstring | sed '/"/s///g' )
#
echo "$tempstring" > $tmpfile
hexvadmsb=$( $cutcmd -b13-14 $tmpfile )
hexvadlsb=$( $cutcmd -b11-12 $tmpfile )
#
tempnumber="$hexvadmsb$hexvadlsb"
vad=$( echo "obase=10;ibase=16; $tempnumber" | bc )
vad=$( echo "$vad * 0.01" | bc )
#
sensorRH=$( echo "scale=3; (($vad/$vdd)-0.16)/0.0062" | bc )
trueRH=$( echo "scale=0; $sensorRH/(1.0546-0.00216*$tempC)" | bc )
if [ $debugflag -eq 1 ]
then
echo "Temperature in degrees C = $tempC"
echo "Vad voltage=$vad"
echo "Vdd voltage=$vdd"
echo "sensor RH=$sensorRH"
echo "true RH=$trueRH"
fi
lockstring=$( $wgetcmd $releaselock?LockID=$lockid )
$rmcmd $tmpfile
if [ $trueRH -le 100 ]
then
echo "$trueRH" > $outfile
fi
#
#====================================================
#
true_RH=$( cat $outfile )
high_RH=$( cat $high_rh_file )
low_RH=$( cat $low_rh_file )
#
if [ $debugflag -eq 1 ]
then
echo "high_RH=$high_RH"
echo "low_RH=$low_RH"
echo "true_RH=$true_RH"
fi
if [ "$true_RH" -gt "$high_RH" ]
then
echo "$trueRH" > $high_rh_file
fi
if [ $true_RH -lt $low_RH ]
then
echo "$trueRH" > $low_rh_file
fi
if [ $debugflag -eq 1 ]
then
echo "==============================="
echo "low_RH=$low_RH"
echo "high_RH=$high_RH"
echo "trueRH=$trueRH"
fi
exit 0
#
 
Back
Top