some Tcl/Tk code to interact with online network appliance
- From: bob findlay <fcsoft@xxxxxxxxxxxxx>
- Date: Tue, 30 Aug 2005 15:18:18 -0400
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 piece of Tcl/Tk 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
v) turns the lamp off when that trip message is received.
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
c) IOA library (https://sourceforge.net/projects/ioanywhere)
You'll need to do the normal stuff to allow the SIMPL surrogates access
through any firewall etc.
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
#========================================
# lampOn
#========================================
proc lampOn { } {
global myparam
X10_IOA $myparam(X10.ioaLabel) 0 0 1
set myparam(X10.value) 0x1000000
$myparam(X10.button) config -text "turn lamp OFF" -bg green
-activebackground green
} ;#end lampOn
#========================================
# lampOff
#========================================
proc lampOff { } {
global myparam
X10_IOA $myparam(X10.ioaLabel) 0 0 0
set myparam(X10.value) 0
$myparam(X10.button) config -text "turn lamp ON" -bg red -activebackground
red
} ;#end lampOff
#========================================
# toggle the output
#========================================
proc toggleX10 { } {
global myparam
if { $myparam(X10.value) & 0x1000000 } {
lampOff
} else {
lampOn
}
} ;#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 ]
after 20000 readTemp
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 i1s1 mystamp token
logit $loggerID $this $fn $TRACE_MASK(MISC) $logMask \
[format "%s: token=0x%X fromWhom=%d" \
[clock format $mystamp -format "%y%b%d %H:%M:%S" ] $token $fromWhom ]
Reply $fromWhom NULL 0
#========================================
# TEXT_MSG
#========================================
if { $token == $TEXT_MSG } {
binary scan $msg x20a* smsg
#
# display the text from that TEXT message
#
.msgtext config -text $smsg
logit $loggerID $this $fn $TRACE_MASK(MISC) $logMask \
[format "msg=%s" $smsg]
lampOff
} 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
.
- Prev by Date: EZSMTP
- Next by Date: Re: Tcl core wasn't compiled for multithreading
- Previous by thread: EZSMTP
- Next by thread: PSPad: Syntax highlighting for Tcl
- Index(es):