Jan 212008
 

Some time ago at http://www.akizukidenshi.com/ I bought a IP Power 9200 unit. It allows you to control 4 relays via Ethernet. It was cheap enough to be part of the “buy now, think about how to use it later” stuff. Using the internal web server is nice for testing, but bad for automatization.

http://www.thebmwz3.co.uk/content/view/23/29/ showed how to find out which commands to send to the unit using simple UDP connections. After a little bit of playing with the VB demo supplied with the board and sniffing the network traffic it created, I figured out how to control it with simple UDP packets.

Here the (small) Perl program which expects 1 bitmask (in decimal, using bit 0..3) to set the outputs, and no argument to just read and display the current state:

#!/usr/bin/perl -w

use strict;

## Create a UDP socket to the device
use IO::Socket::INET;
my $MySocket=new IO::Socket::INET->new(PeerPort=>7070,
        Proto=>'udp',
        PeerAddr=>'192.168.11.35',
        TimeOut=>10);

# Read the status of the 4 outputs
# Map the 4 bits to the bit 0-3 instead of bit 0,2,5,7

sub read_ippower {
    my ($current, $text, $return_val, $junk);
    my $socket=shift;
    $socket->send("AVOISYS00");
    $socket->recv($text,128);
    chomp($text);
    if ($text) {
        $current=0;
        ($return_val,$junk)=split(/avoisys/,$text);
        if ($return_val eq "90\0") {
            $current=15;
        } else {
            if (($return_val & 128)==0) {$current|=8;};
            if (($return_val &  32)==0) {$current|=4;};
            if (($return_val &   4)==0) {$current|=2;};
            if (($return_val &   1)==0) {$current|=1;};
        }
    }
    return $current;
}

# Write a new configuration
# Map the 4 bits to the bit 0-3 instead of bit 0,2,5,7

sub write_ippower {
    my $socket=shift;
    my $new=shift;
    my $mask=0;

    if ( $new & 8 ) {$mask|=128;};
    if ( $new & 4 ) {$mask|= 32;};
    if ( $new & 2 ) {$mask|=  4;};
    if ( $new & 1 ) {$mask|=  1;};

    $socket->send("AVOISYS1$mask");
}

if ($#ARGV < 0) {
    my $a=read_ippower($MySocket);
    print "$a\n";
} else {
    my $a=shift;
    if ("$a" eq "-h" || "$a" eq "--help") {
        print "Usage: $0 [bitmask]\n";
        print "If no bitmask is given, then only the old state will be printed.\n";
        print "If bitmask is supplied (in decimal), then the 4 lowest bits need to\n";
        print "correspondent to the 4 outputs of the IP Power module.\n";
        print "bitmask is given in decimal.\n";
        print "Examples:\n";
        print "$0\n";
        print "Print the current state.\n";
        print "$0 7\n";
        print "Set output 0, 1 and 2 to on, output 3 to off\n";
        exit 1;
    } else {
        write_ippower($MySocket, $a);
    }
}