Un peu de domotique

Après avoir acheté pour une trentaine d’euros un ensemble carte 8 relais + controleur USB (cf.photo), me voici parti dans la prog. du port série.

1. Module pour faire de l’USB un port série :|

usbserial + ftdi_sio (/dev/ttyUSB0)

2. Le Perl script :

#!/usr/bin/perl -w

use Device::SerialPort;
use strict ;

my $str = «  » ;
my @cmd = () ;

# Set up the serial port
# 9600, 81N on the USB ftdi driver
my $port = Device::SerialPort->new(« /dev/ttyUSB0″);
$port->databits(8);
$port->baudrate(9600);
$port->parity(« none »);
$port->stopbits(1);
$port->handshake(« none »);

$port->write_settings || die(« Could not set up port »);

# 8 relays test
foreach my $nb_relay (1 .. 8 ) {
push (@cmd, [ 'on', 255, ${nb_relay}, 1 ,
'off', 255, ${nb_relay}, 0 ] ) ;
}

# Test
foreach my $c (@cmd) {
# Turn relay on
$str = join(  », map {pack(‘C’,$_)} (@{$c}[1..3]) ) ;
print « Relay $c->[2] => $c->[0]\n »;
$port->write( $str. »\r\n » );
$port->write_done();
sleep(1);
# Turn relay off
$str = join(  », map {pack(‘C’,$_)} (@{$c}[5..7]) ) ;
print « Relay $c->[2] => $c->[4]\n »;
$port->write( $str. »\r\n » );
$port->write_done();
sleep(1);
}
$port->close();

3. Photo

4. Video

Autres articles susceptibles de vous intéresser :

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*

Vous pouvez utiliser ces balises et attributs HTML : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recent Tweets

Catégories