#! /bin/bash

#-----------------------------------------------------------------------#
#									#
# Script written by David McDowell and John Mitchell.			#
# Thanks also to fellow TriLUG (http://www.trilug.org) members.		#
#									#
# Use at your own risk. You may use this script freely providing you    #
# give due credit; please keep this information block for use and       #
# distribution. This script is provided WITHOUT ANY WARRANTY. Check     #
# http://www.turnpike420.net/linux/iTHX-M/ for the latest version.	#
#									#
# Version 1.2 - 2004-10-12 - comma delimited and output to database	#
#-----------------------------------------------------------------------#

# This script gets temperature/humidity data from a Newport Electronics, Inc.
# iServer MicroServer model iTHX-M: http://www.newportus.com
#
# Set this script up as a cron job to run every n minutes you will get
# a daily text log; mask: yyyy-mm-dd_iTHX-M.txt
#
# You will need "netcat" found here: http://netcat.sourceforge.net
# Currently tested with netcat-0.7.1-1.i386.rpm on Fedora Core 1
# Outputs as comma delimited text and is appended to file.

# Start by capturing date (DATENOW) and time (TIMENOW) from your computer
# if you don't want date and time from the iTHX-M.
# use %F for yyyy-mm-dd
# use %R for hr:mn OR use %T for hr:mn:se

DATENOW=`date +%F`
TIMENOW=`date +%T`

# This gets one temperature (TEMPERATURE) reading.
# We cut the date/time from the return string of the iTHX-M.
# The iTHX-M returns with this mask: 000.00 mm/dd/yyyy hr:mn:se
# We only want the temperature: 000.00

TEMPERATURE=`echo -e '*SRT\r' | netcat 192.168.1.253 1000 | cut -c1-6`

# Wait one second for the iTHX-M to recover

sleep 1

# This gets one humidity (HUMIDITY) reading.
# We cut the date/time from the return string of the iTHX-M.
# The iTHX-M returns with this mask: 000.00 mm/dd/yyyy hr:mn:se
# We only want the humidity: 000.00

HUMIDITY=`echo -e '*SRH\r' | netcat 192.168.1.253 1000 | cut -c1-6`

# Output as comma delimited text to file.
# Added "T_" in front of the temperature.
# Added "H_" in front of the humidity.

echo "$DATENOW,$TIMENOW,T_$TEMPERATURE,H_$HUMIDITY" >> ${DATENOW}_iTHX-M.txt

# Output to iTHX-M.txt looks like this:
# 2004-09-25,15:52:20,T_067.80,H_056.50

# Output data to a database using PHP.
# Note date/time is not passed to the PHP script.
# We allow the database to autofill date/time value.

php iTHX-M-1.2.php $TEMPERATURE $HUMIDITY

