Example CGI File

This is how I have the system setup:

setup

 

Let us begin by talking about how to setup the Pi to be a web server. I spent many hours looking into this, mostly because I knew nothing about it, and finally installed the lighttpd web server. Once installed and configured I then had to figure out how to get cgi-scripts to run. There is a lot of information out there about this, but one key piece that I found was how to modify the config file to point the server when a cgi request came in:

How to get CGI scripts to work under lighttpd:
http://www.acmesystems.it/foxg20_cgi

Now came the task of learning how CGI scripts can pass user information to the server:

CGI Information:
http://www.jmarshall.com/easy/cgi/

That website was a life saver. It is also where you will find “cgi_parse.h” which is needed to run the program.

 

So now that I had somewhat of an understanding on how CGI works, it was time to integrate it with the SigmaDSP. The way I went about it was the HTTP_GET request which adds the user variable onto the url sent back to the server. The server then runs the CGI script which calls “cgi_parse.h” which takes those variables and, well, parses them out. From there it is just a normal C code until it is time to return information back to the user. At this point you have to switch from C code mode to html code mode.

When it comes time to compile to the *.cgi file, you have to remember to use the -lm option (i.e. $gcc -lm example.c -o example.cgi ). The -lm option includes the math.h header file during compile. Without the option you will get a bunch of errors. Also, since I included the wiringPi library, I had to add “-l wiringPi” to get it to compile.

Now that you have the *.cgi file, you need to change the owner of it to “root” (granted you aren’t already root).
$sudo chown root example.cgi

Now we need to add the SUID sticky (more about it here: http://www.bashguru.com/2010/03/unixlinux-advanced-file-permissions.html )$sudo chmod u+s example.cgi

The SUID sticky is needed so that when the web server gets a request it will run the *.cgi file as root (and this is necessary because you can only access the i2c ports as root). Otherwise you’ll just get an error. There are other, and safer, ways of doing this, but again I just wanted to get it up and running.

 

The SigmaDSP part of the file is at the bottom of the code. I had a drop down menu that would let you select the settings for different things, and when you hit submit it would apply all that is needed.

I also need to point out that I had to modify the cgi parsing file (the 1st comment in the code reiterates this). Basically what I discovered is that if  you try to load the page with out any variables being sent (aka first load of the page before you make changes) it will fail to load UNLESS you force the variables to be empty… That probably doesn’t make much sense but with the limited knowledge that I have, I really don’t know how else to explain it..

    /** Then, from the list of pairs, extract the names and values. **/
 
/************ Modifications Done to put empty values in array *************/
   if(paircount == 0) { //added for mod
	 cgivars= (char **) malloc((2*3+1)*sizeof(char **)) ; //malloc needs to be 2*value pairs
 
	for (i=0; i<(2*3); i++) { //iteration number needs to be 2*value pairs
		cgivars[i] = strdup("");
		}
	}
/*******************End of modifications****************/

That is the mod I did for my case where I had 3 pairs (LED frequency, linear gain, and volume fader). I’m sure there is a way around this problem, but I don’t have enough programming knowledge or web server experience to figure out a better way…

Leave a Reply

© 2014 - Brain Overload. All Rights Reserved. Powered by Wordpress and Design by We Create Web Designs