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!

  1. #include <stdio.h>
  2. #include <unistd.h> /* needed for ioperm() */
  3. #include <asm/io.h> /* for outb() and inb() */
  4. #include <fcntl.h>
  5.  
  6. #define DATA 0x378
  7. #define STATUS DATA+1
  8. #define CONTROL DATA+2
  9.  
  10. #define LP_PE 0x04 /* shifted */
  11.  
  12. int runscript(char *cmd)
  13. {
  14. int ret;
  15.  
  16. if((ret=fork())==-1) { perror("Fork Failed"); return 1; }
  17.  
  18. if(ret==0) // child
  19. {
  20. char *args[] = { NULL };
  21. execve(cmd, args, NULL);
  22. perror("Exec Failed"); return 1;
  23. }
  24.  
  25. wait(&ret); // wait for child
  26. return 0;
  27. }
  28.  
  29. int main(void)
  30. {
  31. int v, v0;
  32.  
  33. if (ioperm(DATA,3,1))
  34. {
  35. printf("Sorry, you were not able to gain access to the ports\n");
  36. printf("You must be root to run this program\n");
  37. exit(1);
  38. }
  39.  
  40. for (;;) {
  41.  
  42. v = ((inb(STATUS)^0x80) >> 3);
  43.  
  44. if (v != v0)
  45. if ((v & LP_PE) != 0)
  46. {
  47. //printf("Status: opening\n");
  48. runscript("/root/bin/opendoor");
  49. }
  50. else
  51. {
  52. //printf("Status: closing\n");
  53. runscript("/root/bin/closedoor");
  54. }
  55. v0 = v;
  56.  
  57. sleep(1);
  58. //usleep(100000);
  59.  
  60. }
  61.  
  62. return 0;
  63. }

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:

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

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

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can use BBCode tags in the text.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options