ascii image


0010101000011111101001101010000010011000
1110101110110101011011111010010010001011
0001011100011101111001010011010010111110
0000010111101001100000110011101100001000
0011000000111010001111111000100110000001
1010110110000000000001011001000110001010
0101100010101100000100000010100100010101
0001011001011101100011000101110111101110
0110010100110100111101110100110011111101
0010111100110011010010110010101111011011
0100000000001001001011000010110100101001
1101000111100000110111011100110111000010
1111110001111111101101001010000111101100
0010110000100000111011000000101100010110
0101111000011100111010000000011111101111
0010010011110010011101001000110101000101
0000000001000100001111111100111010001111

Friday 31 December 2010

DS2406 1-wire 240VAC powerswitch

To control my greenhouse heater, which are notoriously bad for keeping a decent temperature i have found, i have added a 240VAC 1-wire controlled socket.  This has been kicking around for a while, but since moving to Linux it has been solid. ( see 1-wire-weirdness )

A nice 3d-rendering of the circuit.

And some bash shell code to control it.


#!/bin/sh

#
# Turn on/off the greenhouse heater/fan
#

# v1.0 - 31-Dec-2010 - NIC - Initial release

# nick .at. kiwi-hacker .dot. net

# test the outside temperature before doing anything
# if above MAX then on, expecting the fan not the heater to be connected
# if below MIN then on, expecting the heater to be attached
# else off
#  check the power state before doing anything

# path to DS2406 switch and DS1820 temp sensor
SWITCH=/var/1wire/Greenhouse_Hub/aux/Power_Switch/
OUTTEMP=`cat /var/1wire/Greenhouse_Hub/aux/Outdoor_Greenhouse/temperature`

# min max temps
MIN=2
MAX=29

# DS2406 output pin, usually A
SWA=$SWITCH/PIO.A

DEBUG=1

switchit () {

    sw=$1
    echo $sw >> $SWA
    ERR=$?
    echo $ERR
}

state () {
    st=`cat $SWA`
    # CHECKED = on in GUI, off in switch
    if [ "$st" = "0" ] ; then
        echo "ON"
    else
        echo "OFF"    
    fi
}

log () {
    logger -t "$0" $1
    if [ "$DEBUG" -gt "0" ] ; then
        echo $1
    fi
}

on () {
# off in webgui turns on actual power switch - its back to front
ERR=`switchit off`
if [ "$ERR" = "0" ] ; then
    log "turned on"
else
    log "failed turning on"
fi
}

off () {
ERR=`switchit on`
if [ "$ERR" = "0" ] ; then
    log "turned off"
else
    log "failed turning off $ERR"
fi
}

# bash doesnt do decimals, so round off
ROUND_TEMP=`printf "%.0f\n" $OUTTEMP`

if [ "$ROUND_TEMP" -le "$MIN" ] ; then
    if [ `state` = "OFF" ] ; then
        log "Outside = $OUTTEMP < $MIN turning power on"
        on
    fi
elif [ "$ROUND_TEMP" -gt "$MAX" ] ; then                                                                          
        if [ `state` = "OFF" ] ; then                                                                           
                log "Outside = $OUTTEMP > $MAX turning power on"                                                       
                on                                                                                              
        fi  
else
    if [ `state` = "ON" ] ; then
        log "Outside = $OUTTEMP turning power off"
        off
    else
            if [ "$DEBUG" -gt "0" ] ; then
                    log "Doing nothing as temp:$ROUND_TEMP min:$MIN max:$MAX"
        fi 
        fi
fi

exit 0

No comments:

Post a Comment