CGI program on CHIP

From www.chip-community.org
Jump to: navigation, search

This is intended to be a simple "first project" for new CHIP owners. From your smart phone's browser (or any computer's browser) you can turn CHIP's status LED on and off with a simple tap (click).

NOTE: this program will conflict with the "blink" program, which also blinks the status LED (and handles graceful shutdowns). If you are running "blink", kill the "blink" process while running this.

This project requires that you create two files containing C code and compile them. If you're allergic to C, then it could be re-written in any other language, but there's a caveat. To manipulate the status LED requires "root" privilege. Unix lets you endow a C program with a form of "temporary root privilege". Other languages, like Python, shell script, java, or whatever, don't support this (to my knowledge). So to get this project to work with non C code would require running the web server as root. I would consider this an unacceptable security risk for any kind of long-term project, but for a simple "let's do something cool", it should be fine.

Anyway, since I'm a C programmer, I'll give you the step-by-step for C. The commands need to be entered exactly as shown. Feel free to cut-and-paste the commands into your terminal window.

  • Install the C compiler. From a CHIP shell prompt, enter:
    sudo apt-get install gcc make
    
  • Install Apache. There are other web servers that other people like much better. I'm familiar with apache, so that's what I'm using.
    sudo apt-get install apache2
    
  • Enable cgi-bin:
    cd /etc/apache2/mods-enabled
    sudo ln -s ../mods-available/cgi.load
    
  • Optional: if you want to access user accounts' "public_html" areas:
    cd /etc/apache2/mods-enabled
    sudo ln -s ../mods-available/userdir.load
    sudo ln -s ../mods-available/userdir.conf
    mkdir ~/public_html
    
  • Restart apache.
    sudo apachectl -k graceful
    
  • Create C source files. You would normally use a text editor (vi or pico), but I wanted something you could cut-and-paste into the terminal screen. The command blocks below can be cut and pasted to create the source files:
    cd
    cat >on.c <<__EOF__  # Create C programs to turn the status LED on
    #include <stdio.h>
    int main(int argc, char **argv)
    {
      system("/usr/sbin/i2cset -f -y 0 0x34 0x93 0x1");
      printf("Content-type: text/html\n\n");
      printf("<html><head><title>on</title></head>\n\n");
      printf("<body><a href=\"off\">Turn back off</a></body></html>\n\n");
      return 0;
    }
    __EOF__
    cat >off.c <<__EOF__  # Create C programs to turn the status LED off
    #include <stdio.h>
    int main(int argc, char **argv)
    {
      system("/usr/sbin/i2cset -f -y 0 0x34 0x93 0x0");
      printf("Content-type: text/html\n\n");
      printf("<html><head><title>off</title></head>\n\n");
      printf("<body><a href=\"on\">Turn back on</a></body></html>\n\n");
      return 0;
    }
    __EOF__
    

    If you cut-and-paste the above text exactly as shown, it should create "on.c" and "off.c".

  • Compile the C programs, putting the executables where apache expects them to be. Normally one would not use "sudo" to compile; I'm doing that so the executables end up owned by root. This is important for the following step.
    cd
    sudo gcc -o /usr/lib/cgi-bin/on on.c
    sudo gcc -o /usr/lib/cgi-bin/off off.c
    
  • Give the executables root privileges so that they can manipulate the status LED.
    sudo chmod +s /usr/lib/cgi-bin/on /usr/lib/cgi-bin/off
    

DONE!

You can now use a web browser to turn your status LED on and off. For example, let's say your CHIP's IP address is 10.0.0.20. Point your browser at:

    http://10.0.0.20/cgi-bin/off
    

and press the button. It will turn the LED off and display the "on" page. Clicking the "on" button turns it back on and displays the "off" page. SUCH AWESOME

And also, such insecure. Anybody who gets on your WIFI network can now control your CHIP's status LED. Issues of security are beyond the scope of this beginner project, but need to be considered for anything you plan to put into production. You don't want some script kiddie to be opening your garage door!

Having trouble?

Post an inquiry to @fordsfords on the message board.