Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

The Door Server

by beretboy (Chaplain)
on Oct 06, 2002 at 16:17 UTC ( #203179=CUFP: print w/ replies, xml ) Need Help??

Well its been almost a year and a half since DigitalConvergance died. But its legacy was not forgotten for geeks all over the world got a neat gadget to hack (see cuecat). I have used one of my cuecats to connect my door to the web. First, I glued a barcode to the side of my door. Next, I propped the cuecat so that when the door swung by it would scan the barcode. Then I wrote this perl script which updates the webstite, incrementing the counter and adding a timestamp, when it gets the barcode information. To see live statistics point your browser to phrontist.org. Here is the code:
#!/usr/bin/perl use strict; my $i = 0; my ($time,$html); while (1) { my $meow = <STDIN>; open(TIME,"date|"); $time = <TIME>; # Get Time close(TIME); $i++; open(WWW,">/usr/home/web/www/index.html") or die "File did not open!\ +n"; $html = <<CATTAIL; <html> <title>The Door Server</title> <body bgcolor="#000000" alink="#ffffff" link="#ffffff" vlink="#ffffff" + text="#ffffff"> <center><table STYLE="border:solid; border-width:1px; border-color:#FF +FFFF"><tr><td><h1>The Door Server</h1></td></tr></table></center> <br> <center> <table STYLE="border:solid; border-width:1px; border-color:#FFFFFF"> <tr> <td> <center><pre> My door has been connected to the 'net through a somewhat unconvention +al use of a <A HREF="http://www.cuecatastrophe.com">CueCat</A> barcode scanner. I have adhered a barcode to the lower corner of my do +or and as it opens and closes it swings by the barcode scanner which sends the barcode in +formation to the FreeBSD (version 4.6) machine it my room. Upon getting this signal a < +A HREF="http://perlmonks.org">perl</A> script running on the machine springs into action and updates this page for your view +ing pleasure. Below you can see live statistics, such as how many times my door has been o +pened/closed and when it was last opened/closed. Enjoy </pre></center> </td> </tr> </table> </center> <br> <center> <table STYLE="border:solid; border-width:1px; border-color:#FFFFFF"> <tr> <td> <pre> <center><H1>Stats:</H1></center> Door Openings/Closings : $i Time Last Opened/Closed: $time </pre> </td> </tr> </table> </center> </body> </html> CATTAIL print WWW "$html" or die "did not print\n"; close(WWW); }


"Sanity is the playground of the unimaginative" -Unknown

Comment on The Door Server
Download Code
Re: The Door Server
by hossman (Prior) on Oct 07, 2002 at 05:55 UTC

    It should be possible to use two different barcodes, to detect the difference between the door opening and the door closing (based on the order of the scans within a short interval) so you could list the current status (open or closed).

    As to the code at hand ... why use open(TIME,"date|"); instead of localtime ?

    You might also want to change it so it updates a data file with the value of i ... that way if the script dies for any reason, you still know the total.

Re: The Door Server
by George_Sherston (Vicar) on Oct 07, 2002 at 10:58 UTC
    hossman's right, it would be interesting to know whether the door was open or closed - and presumably $meow contains this info - the scanner will read a different code (unless it's a palindrome) for open or close, no? Tell us more about your door!

    George Sherston
      IIRC, barcodes have some checkbit system, so they cannot be read differently in both directions.. maybe get two cuecats mounted in different directions with different barcodes to read each direction
Re: The Door Server
by beretboy (Chaplain) on Oct 07, 2002 at 11:58 UTC
    I like hossmans idea of adding two barcodes which would indeed allow me to find out if it was opened or closed. The problem is fitting the two barcodes on my door (I'm going to try to though).

    "Sanity is the playground of the unimaginative" -Unknown
Re: The Door Server
by Mr. Muskrat (Abbot) on Oct 08, 2002 at 14:44 UTC

    So you need two barcodes to sit side by side. '0' and '1'. You can create them with this code:

    #!/usr/bin/perl use strict; use warnings; use GD::Barcode::Code39; MakeBar('0'); MakeBar('1'); sub MakeBar { my $txt = shift; my $oGdBar = GD::Barcode::Code39->new("*$txt*"); die $GD::Barcode::Code39::errStr unless($oGdBar); open(IMG, ">", "$txt.png") or die $!; binmode(IMG); print IMG $oGdBar->plot(NoText=>1, Height => 40)->png; close(IMG); }

    Then print and affix them to your door. I haven't tested the following code because I have hacked my cuecat to decode the scans but you could use something like this:

    #!/usr/bin/perl use strict; use warnings; use Barcode::Cuecat; # seperate counters for open and closed my $open = 0; my $close = 0; my $door; my $bc = new Barcode::Cuecat(); while (<>) { $bc->scan($_); $door .= $bc->code(); if (length $door == 2) { $open++ if ($door eq "01"); $close++ if ($door eq "10"); $door = ""; # we will assume the door started out closed my $state = "closed"; $state = "open" if ($open > $closed); print "Opened: $open times\n"; print "Closed: $closed times\n"; print "Current state: $state\n"; # write to disk } }

Re: The Door Server
by Massyn (Hermit) on Oct 13, 2002 at 00:05 UTC
    I like the idea. Nicely done. I have one question: Why use a barcode scanner if you could use a normal toggle switch attached to the door, linked to either the serial or parallel port. Would that solution make it cheaper? Typically those magnetic switches you get used by alarm systems should do the trick perfectly.
      I would have really liked to have done that, unfortuneately I do not know how to connect a switch to a serial port, nor how to monitor the activity of such a switch. Any resources (online or otherwise) for connecting things to serial ports, would be appreciated =)

      "Sanity is the playground of the unimaginative" -Unknown

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://203179]
Approved by ybiC
Front-paged by hsmyers
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-09-23 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (234 votes), past polls