Low-tech door monitoring with ICQ

Do you want to monitor some door? Get notified when it opens/closes? This is low-tech solution for the trusty old parallel port.

Just solder a Reed-switch and protective resistor to a DB-25 connector. Then run this simple program as daemon to monitor the switch. (This project is about 10 years old. You might consider fancy things like DBUS today...)

I my case the program runs scripts which controls the status of a running ICQ client. The door in question is the front door of our club room. Yeah!

#include <stdio.h>
#include <unistd.h> /* needed for ioperm() */
#include <asm/io.h> /* for outb() and inb() */
#include <fcntl.h>
 
#define DATA 0x378
#define STATUS DATA+1
#define CONTROL DATA+2
 
#define LP_PE 0x04 /* shifted */
 
int runscript(char *cmd)
{
    int ret;
 
    if((ret=fork())==-1) { perror("Fork Failed"); return 1; }
 
    if(ret==0) // child 
    {
        char *args[] = { NULL };
        execve(cmd, args, NULL);
        perror("Exec Failed"); return 1;
    }
 
    wait(&ret);  // wait for child
    return 0;
}
 
int main(void)
{       
    int v, v0;
 
    if (ioperm(DATA,3,1))
    {
        printf("Sorry, you were not able to gain access to the ports\n");
        printf("You must be root to run this program\n");
        exit(1);
    }
 
    for (;;) {
 
        v = ((inb(STATUS)^0x80) >> 3);
 
        if (v != v0)
            if ((v & LP_PE) != 0)
            {
                //printf("Status: opening\n");
                runscript("/root/bin/opendoor");
            }
            else
            {
                //printf("Status: closing\n");
                runscript("/root/bin/closedoor");
            }
        v0 = v;
 
        sleep(1);
        //usleep(100000);
 
    }
 
    return 0;
}

The script should run some commands (also consider dropping the privileges) but must not stay in the foreground (i.e. needs to terminate). Simple example:

#!/bin/sh
exec su someuser -l -c /usr/local/bin/opendoor-user

Where opendoor-user would set the status in licq, micq, pidgin, ...

Tags: 

Leave a comment or suggestion...

triq.net blog © 1999-2011 Christian Zuckschwerdt — powered by seven of drupal — run on lamp