neat online appliance demo

bob fcsoft-3Emkkp+1Olsmp8TqCH86vg at public.gmane.org
Tue Aug 23 19:01:26 UTC 2005


I am preparing to update my iCanProgram course lesson on network appliances 
soon  (the old version is at 
http://www.icanprogram.com/34ux/lesson12/lesson12.html).      

I thought that I would share this neat piece of demo code I've been working 
on with you.     I'm too close into this stuff to spot rough edges and bugs 
easily.     Hopefully a couple more eyes can help nail those.

NOTE:  The iCanProgram courses have been offered without fees since early 
2002 and several thousand students have taken them since then.    All I ask 
of the students is that they make a contribution to their local Cancer 
research charity.  (http://www.icanprogram.com/nofeecourses.html)

The network appliance the students interface with is the box connected to the 
"Try Me" tab at the IO Anywhere website (http://www.io-anywhere.ca).    The 
folks at IO Anywhere have generously allowed my students to write code which 
bangs at this box over the Internet.

The newest variation of IOA appliance to be connected to the "Try Me" tab is 
what they are calling the micro IOA.       The engineers have connected this 
box up to an X.10 lamp module which in turn controls a small 7W night light.  
   A temperature sensor is also connected into this appliance and the working 
end of the sensor is placed in close enough proximity to the light that it 
warms up when the lamp is on.

The Tcl/Tk program below is a prototype of what the students will be working 
with and it does the following sequence:

i) reads the temperature sensor and sets the temperature trip point 3 C above 
the ambient reading

ii) configures the event message which will be issued as the temperature 
crosses that trip point

iii) presents a Tk window with a button which allows the user to activate the 
X.10 lamp module ie.  turn on the light

iv) accept the message from the appliance when the temperature rises above 
the trip point

At the moment the code doesn't turn off the lamp automatically but with a 
single line addition it could do this.

If you want to run this demo you'll need to be sure that the following 
packages are installed and configured on your Linux box:

a) Tcl/Tk
b) SIMPL (https://sourceforge.net/projects/simpl)  (someone was working on a 
Debian package for this code but I haven't heard if it was completed as yet)
c) IOA library (https://sourceforge.net/projects/ioanywhere)

You'll need to temporarily open ports 8001 and 8002 through the firewall 
while running this code to allow the SIMPL messages to be exchanged.

If you examine the script files:

	$IOANYWHERE_HOME/testing/testb011/scripts/runtest
	$IOANYWHERE_HOME/testing/testb011/scripts/auxtest

you'll see how to run the couple of other SIMPL processes you'll need to 
enable this network transparent messaging. 

All suggestions for improvement are welcome.

bob



=============== start of Tcl/Tk demo snip ===============

#!/usr/bin/wish
#=================================================
# demo script for temperature reading
# version 2
#=================================================

set IOA_SIMPL_name "65.48.172.249:IOA_temperature"
set myeventstr "this is from bob"

set this "kwlug"
set TRACE_MASK(MISC) 	0x10
set logMask 0xff

set myparam(temp.value) 0x0
set myparam(temp.label) .label

set myparam(X10.value) 0x0
set myparam(X10.button) .ioa
set myparam(X10.ioaLabel) heater

lappend auto_path $env(SIMPL_HOME)/lib
lappend auto_path $env(IOANYWHERE_HOME)/library/lib
package require Fctclx
package require ioalib


#========================================
# toggle the output
#========================================
proc toggleX10 { } {
global myparam

if { $myparam(X10.value) & 0x1000000 } {
	X10_IOA $myparam(X10.ioaLabel) 0 0 0
	set retval 0
} else {
	X10_IOA $myparam(X10.ioaLabel) 0 0 1
	set retval 0x1000000
}

set myparam(X10.value) $retval

if { $retval & 0x1000000 } { 
	$myparam(X10.button) config -text "turn lamp OFF" -bg green 
-activebackground green
} else {
	$myparam(X10.button) config -text "turn lamp ON" -bg red -activebackground 
red
}

} ;#end toggleX10

#========================================
# read the temperature
#========================================
proc readTemp { } {
global myparam

read_tc_IOA 1 2 retVal

binary scan $retVal s1 mytemp

$myparam(temp.label) config -text [format "%d C" $mytemp ]

return $mytemp

} ;#end readTemp


#=============================================
#	doReceive - entry point
#=============================================
proc doReceive {} {
global this
global loggerID
global TRACE_MASK
global logMask
set fn doReceive

set buf [Receive]

binary scan $buf i1i1 fromWhom nbytes 

logit $loggerID $this $fn $TRACE_MASK(MISC) $logMask [format "received %d 
bytes from %d" $nbytes $fromWhom]
	
binary scan $buf x8a$nbytes msg 
hndlMsg $fromWhom $msg

};# end doReceive

#=============================================
# 	hndlMsg - entry point
#=============================================
proc hndlMsg {fromWhom msg} {
set fn hndlMsg
global TEXT_MSG
global this
global loggerID
global TRACE_MASK
global logMask

binary scan $msg s1 token

logit $loggerID $this $fn $TRACE_MASK(MISC) $logMask \
	[format "token=0x%X fromWhom=%d" $token $fromWhom]

Reply $fromWhom NULL 0 

#========================================
#   TEXT_MSG
#========================================
if { $token == $TEXT_MSG } {
	binary scan $msg x16a* smsg

#
# display the text from that TEXT message
#
	.msgtext config -text $smsg

	logit $loggerID $this $fn $TRACE_MASK(MISC) $logMask \
		[format "msg=%s" $smsg]

	readTemp

} else {
	logit $loggerID $this $fn $TRACE_MASK(MISC) $logMask \
		[format "token=0x%X unsupported" $token]
};#end TEXT_MSG

};#end hndlMsg

#============================================
# main
#============================================
set fn main
wm geometry . 400x200+200+0
wm title . "Temperature Demo"
wm resizable . 0 0

label $myparam(temp.label) -text "0 C"
place $myparam(temp.label) -x 40 -y 20

button $myparam(X10.button) -text "turn lamp ON" -command [list toggleX10] 
place $myparam(X10.button) -x 40 -y 40

label .msgtext -justify left -text ""  
place .msgtext -x 40 -y 80

button .quit -text Quit -command {set x 1}
place .quit -x 350 -y 175

set myslot [name_attach KWLUG]
set myFifo [ format "%s/%s" $env(FIFO_PATH) $myslot]
set recvid [ open $myFifo {RDWR}]

set loggerID [name_locate LOGGER]

init_IOA $IOA_SIMPL_name

set mytemp [readTemp]

#
# configure the temperature limit on microIOA
# to be 3 C above the ambient just measured
#
set templimit [expr $mytemp + 3]
logit $loggerID $this $fn $TRACE_MASK(MISC) $logMask \
	[format "current temp=%d C limit=%d C" $mytemp $templimit]

#
# configure the event message and lower temp limit here
#
configure_IOA [format "msg_3=%s;event_limit_3L=%d" $myeventstr $templimit]

fileevent $recvid readable doReceive

vwait x

close $recvid
name_detach

exit
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list