I use three scripts to capture and record temperature data using a 1-wire system consisting of an HA-7 Controller and several DS18B20 temperature sensors. Included in these scripts is programming to store the data for plotting by a program called pawsPlot.
Two are scripts that run in the home automation program Xtension, and one that runs as an applescript applet.
All of these scripts can be adapted to run within Xtension, or in plain Applescript
The scripts are as follows:
XTENSION SCRIPT “SUNROOM THERMOSTAT”
--Set initial values and clean up Scheduled events
set tnow to time of (current date)
set temp to (Get Unit Property "actualTemp" from unit "Sunroom Temperature")
set ton to (Get Unit Property "on time" from unit "Sunroom Temperature")
set toff to (Get Unit Property "off time" from unit "Sunroom Temperature")
remove unit events "Sun Room Heat Lamp"
remove unit events "Sun Room Outlet"
--Reset on time and off time counters daily at noon
if (tnow is greater than 12 * hours) and (tnow is less than 12.5 * hours) and (toff is greater than 30) then
-- There is probably a better way to ensure that the reset occurs once and only once per day.
Set Unit Property "yesterdays on time" to ton in unit "Sunroom Temperature"
set ton to 0
Set Unit Property "yesterdays off time" to toff in unit "Sunroom Temperature"
set toff to 0
Set Unit Property "on time" to ton in unit "Sunroom Temperature"
Set Unit Property "off time" to toff in unit "Sunroom Temperature"
end if
--Calculate time since last command, and update on and off times.
set Tdelta to ((10 * (time delta of "Sun Room Outlet") / 60) as integer) / 10
-- Calculate Tdelta in tenths of a minute
if ((status of "Sun Room outlet") is true) then
set ton to ((ton) + Tdelta)
Set Unit Property "on time" to ton in unit "Sunroom Temperature"
else
set toff to ((toff) + Tdelta)
Set Unit Property "off time" to toff in unit "Sunroom Temperature"
end if
write log " " color blue
--Send heater on command if temp is less than 58, or else send heater off command.
if temp is less than 58 then
turnon "Sun Room outlet" for 11 * minutes
write log " Turning on Heater" color blue
else
turnoff "Sun Room outlet"
write log " Turning off Heater" color blue
end if
--Set Heat Lamp for next period.
if (temp is less than 60) then
dim "Sun Room Heat Lamp" to 100
dim "Sun Room Heat Lamp" to 0 in 11 * minutes
write log " Setting Heat Lamp to 100" color blue
else if temp is less than 65 then
dim "Sun Room Heat Lamp" to 60
dim "Sun Room Heat Lamp" to 0 in 11 * minutes
write log " Setting Heat Lamp to 60" color blue
else if temp is greater than 64.9 then
dim "Sun Room Heat Lamp" to 0
write log " Turning Off Heat Lamp" color blue
end if
--Record status of heater and heat lamp for pawsplot
try
set unitValue to ""
set unitNm to "Sun Room Outlet" --unit name in XTension
set pathFileLoc to "MACOS:Xtension:PplotData:SunRoom Heater:" & "HeaterOnOff "
load_Data_pawsPlot(unitValue, unitNm, pathFileLoc)
on error
write log "There was a problem writing to the file HeaterOnOff" color red
end try
try
set unitNm to "Sun Room Heat Lamp" --unit name in XTension
set pathFileLoc to "MACOS:Xtension:PplotData:SunRoom Heat Lamp:" & "SRHeatLamp "
load_Data_pawsPlot(unitValue, unitNm, pathFileLoc)
on error
write log "There was a problem writing to the file SRHeatLamp" color red
end try
--Schedule next round.
remove unit events "Sun Room Temp Reader"
remove unit events "Sun Room Thermostat"
execute script "Get Weather" in 4 * minutes
execute script "Sun Room Temp Reader" in 4.5 * minutes
execute script (thisScript) in 5.5 * minutes
--Record heater on and off times to log
write log "Cumulative on time since noon is " & ton & " minutes."
write log "Cumulative off time since noon is " & toff & " minutes."
write log " " color blue
THE XTENSION SCRIPT “SUN ROOM TEMP READER”
ignoring application responses
tell application "SunRoomTempReader"
run
end tell
end ignoring
THE THIRD IS APPLESCRIPT SCRIPT “SUNROOMTEMPREADER.APP”
--Program Setup
--Initialize variables
set temperature to "initial value Temp"
set TempString to "initial value TS"
set SensorAddress to "initial value SA"
set iData to "initial value iData"
--Setup information for PortTerm
--set PortSelect to "cu.KeySerial1"
--set baud rate to 9600 with 8 data bits, no parity, one stop bit, and
--flowcontrol to Xon / Xoff
--Set up Sensor ID and Locations
--By using these lists instead of the 1-wire search routines, sensors can be processed
--in any order.
set NumberOfSensors to 5
set CurrentSensorList to {"70000000BC31AB28", "02000000BC0E5E28", "CC000000BC2FC328", "27000000BC288928", "C6000000BC7B6728"}
set SensorLocationList to {"Outdoor Temperature", "Indoor Temperature", "Sunroom Temperature", "Basement Temperature", "Garage Temperature"}
set DirectReadFile to {"OutdoorTemp ", "IndoorTemp ", "SunroomTemp ", "BasementTemp ", "GarageTemp "}
set DirectReadFileFolder to {"Outdoor Temp", "Indoor Temp", "Sunroom Temp", "Basement Temp", "Garage Temp"}
-----------------------------------------------------------------------------------
--Main Program
--This script obtains the temperature from each sensor in the list and stores the values in a
--corresponding psudo unit in the master list, then records the temps in a data file for pawsPlot
repeat with I from 1 to NumberOfSensors
set CurrentSensor to item I of CurrentSensorList
repeat with J from 1 to 5 --10
set TempString to DataFromSensor(CurrentSensor)
--set TempString to "BEFFFF4B467FFF0410F4" -- This line is used for diagnostic use only
set temperature to getTemperatureFromSensorData(TempString)
if temperature is greater than 150 then
tell application "XTension"
write log "Temp reading in error, rerunning Temp Reader" color red
end tell
else
tell application "XTension"
--write log TempString -- this line was used as a diagnostic tool only.
dim item I of SensorLocationList to temperature with no transmit -- this line
-- updates the psuedo units in Xtension
-- the following line is used to store the sunroom temp, in tenths of a degree,
-- in a unit property since the psudo unit only keeps the temp in whole degrees.
if item I of SensorLocationList is equal to "Sunroom Temperature" then Set Unit Property "actualTemp" to temperature in unit "Sunroom Temperature"
write log "The " & (item I of SensorLocationList) & " is " & temperature & " degrees." color green
end tell
try
-- this "try" writes the temps for pawsplot. Not using this try command caused the whole
-- temp control system to crash if this script is unable to write to any of the files.
set pathFileLoc to "MACOS:Xtension:PplotData:" & item I of DirectReadFileFolder & ":" & item I of DirectReadFile
load_Data_pawsPlot(temperature, pathFileLoc)
on error
tell application "XTension"
write log "There was a problem writing to the file: " & item I of DirectReadFile
end tell
end try
exit repeat
end if
end repeat
end repeat
--The following subroutines are used by the above main program
------------------------------------------------------------------------------------
on DataFromSensor(CurrentSensor)
--This routine transfers control to the program PortTerm, available from kulesh-software.com,
--which in turn links to an HA-7E 1-wire controller with one or more DS18B20 temperature
-- sensors connected.
tell application "PortTerm"
-----------------------------------
--Setup for PortTerm
--once this section is set up, it should
--never need to be changed again.
connect to port "cu.KeySerial1"
configure baud rate "9600" parity "none"
-----------------------------------
write data "A" & CurrentSensor format "atcm"
delay 0.1
write data "W044E4B467F" format "atcm"
delay 0.05
write data "M" format "atcm"
delay 0.05
write data "W0144" format "atcm"
delay 0.75
write data "M" format "atcm"
delay 0.05
read
write data "W0ABEFFFFFFFFFFFFFFFFFF" format "atcm"
set iData to ""
delay 0.05
read
disconnect
return iData
end tell
end DataFromSensor
----------------------------------------------------------------------------------
on getTemperatureFromSensorData(theData) -- this script returns Fahrenheit temp rathar than celsius
--This script extracts the temperature from the returned data string (e.g. "BE52014B467FFF0E10FF")
--and converts the temperature (Celsius) from a hexadecimal number to a decimal number, and
--then converts the temperature to Fahrenheit. Temperature data is contained in characters
-- 5, 6, 3, and 4 of the data string, in that order.(the temp in the example is 015.2 degrees Hex or 21.125 dec)
-- degrees are characters 5, 6 and 3 (the integer part, in that order) and the fraction is character 4
-- the temperature is a two byte signed integer (so -0.0625 degree C is represented as FFFF)
-- to convert hex to decimal is easiest with the scripting addition "24U Hex OSAX" from 24U Software,
-- but it can be done without:
try
set hundredsHex to (ASCII number (character 5 of theData)) - 48 -- ASCII (48) is "0", (49) is "1" etc
if hundredsHex > 9 then set hundredsHex to hundredsHex - 7 -- ASCII "distance" between 9 and A
set tensHex to (ASCII number (character 6 of theData)) - 48
if tensHex > 9 then set tensHex to tensHex - 7
set unitsHex to (ASCII number (character 3 of theData)) - 48
if unitsHex > 9 then set unitsHex to unitsHex - 7
set fractionHex to (ASCII number (character 4 of theData)) - 48
if fractionHex > 9 then set fractionHex to fractionHex - 7
set temperature to (hundredsHex * 256 + tensHex * 16 + unitsHex + fractionHex / 16)
if temperature is greater than 3000 then set temperature to temperature - 4096
--calculates temperature in Celsius
set temperature to (round 10 * (32 + temperature * 9 / 5)) / 10 -- converts Celsius to Fahrenheit
return temperature
on error the error_message number the error_number
set temperature to 180
return temperature
end try
end getTemperatureFromSensorData
----------------------------------------------------------------------------------
--This subroutine sets up the text string for pawsPlot and calls the write_to_files subroutine below.
on load_Data_pawsPlot(unitValue, pathFileLoc)
-- set unitValue to value of unitNm
set theDate to (current date) as string
set logTime to ""
set logTime to round ((time of (current date)) / 60)
set hourWord to word 5 of theDate
set minWord to word 6 of theDate
set secWord to word 7 of theDate
set apWord to word 8 of theDate
set theTime to hourWord & ":" & minWord & ":" & secWord & ":" & apWord
set this_data to (theTime & space & space & space & unitValue & space & space & space & logTime & return)
set target_file to (pathFileLoc) & ShortDate()
my write_to_file(this_data, target_file, true)
end load_Data_pawsPlot
----------------------------------------------------------------------------------
--This subroutine sets up the date string used in the pawsPlot files.
on ShortDate() --example 01/01/04
set myShortDate to ""
set MyDate to current date
set theMonth to the (month of MyDate) as integer
set theDay to the (day of MyDate) as integer
set theYear to (text 3 thru 4 of ((year of MyDate) as text))
if theMonth is less than 10 then
set myShortDate to myShortDate & "0"
end if
set myShortDate to myShortDate & theMonth & "/"
if theDay is less than 10 then
set myShortDate to myShortDate & "0"
end if
set myShortDate to myShortDate & theDay & "/"
set myShortDate to myShortDate & theYear
end ShortDate
----------------------------------------------------------------------------------
--This subroutine actually writes the data to the pawsPlot files.
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
if append_data is false then
tell application "Finder"
if (exists of file target_file) then
delete file target_file
end if
end tell
end if
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
close access file target_file
end try
end write_to_file
Model: 1.8GHZ G-5 Tower
AppleScript: 1.10.7
Browser: Netscape/7.2
Operating System: Mac OS X (10.4)