CGI common gateway interface on A20-OLinuXino-MICRO

Started by Rukundo, July 10, 2014, 12:30:24 PM

Previous topic - Next topic

Rukundo


Hi, I'm a french teacher (new in linux !!!!). I want to make a robot controlled by a web page (this web page will be stored in the  A20-OLinuXino-MICRO board).

my question is how about installing or programming a CGI on  A20-OLinuXino-MICRO? Is it possible to make it in C++ ? What other tools (soft), we can use ?

Thanks for your response.


kantal

I think there is no easy way to do this, you must learn much.
What immediately comes to mind:

1. Write an arbitrary program for the A20 and control it over ssh.
2. CGI: you must install a lightweight webserver and write a CGI program in ARBITRARY programing language. You must learn the webserver configuration, etc., and search for "cgi programming tutorial" on the web.
3. Use socket programming.

Rukundo

Thanks  kantal;

i will try your advices.

I think, I 'll try just turn on or off one DEL.


martinayotte

You can either install Apache package and use some scripting language such python and place those scripts under your Apache /var/www folder.

Or even better, write your own lightweight webserver in python using HTTPServer class along  with a BaseHTTPRequestHandler class that handles the GET/POST done from the Browser client.
(It is what I'm doing to control A20 GPIOs, etc)

Take a look to some example such as https://wiki.python.org/moin/BaseHttpServer


Rukundo

Hi martinayotte,

the code was what I need. It seems to use httpd server (I'm a very novice in python).


have you secceded to manage IO of your A20 board?

If yes ,can you give me an exeample of what you did or explain me how to ?

thanks

martinayotte

Hi Rukundo,

HTTPServer is a base class from stand python.
So, you would only need to install the GPIO python library from https://pypi.python.org/pypi/pyA20
Then, you can run the following python script and open a browser to see the web page provided by this script, it will show "Led On" and "Led Off" buttons that will control GPIO Green Led of your Olimex A20 board.


#!/usr/bin/python

import os.path
from BaseHTTPServer import *
import urlparse
import A20_GPIO as GPIO


class GetHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        parsed_path = urlparse.urlparse(self.path)
        if (self.path.startswith('/ledon')):
            GPIO.output(GPIO.PIN3_7, GPIO.HIGH)
        if (self.path.startswith('/ledoff')):
            GPIO.output(GPIO.PIN3_7, GPIO.LOW)
        self.send_response(200)
        self.end_headers()
        html = "<html><head><meta charset=\"utf-8\">\n</head><body>\n"
html += "<input type=\"button\" value=\"Led On\" onclick=\"window.location.href='ledon';\"/>"
html += "<input type=\"button\" value=\"Led Off\" onclick=\"window.location.href='ledoff';\"/>"
html += "</body>"
self.wfile.write(html)
        return

class HTTP_LED_Server(HTTPServer):
    def __init__(self, addr, port):
        HTTPServer.__init__(self, (addr, port), GetHandler)
        self.ipaddr = addr
        GPIO.init()
        GPIO.setcfg(GPIO.PIN3_7, GPIO.OUTPUT)

if __name__ == '__main__':
    try:
        import socket, fcntl, struct
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        ipaddr = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', 'eth0'))[20:24])
        server = HTTP_LED_Server(ipaddr, 2222)
        print "Starting server on %s:2222, use <Ctrl-C> to stop" % ipaddr
        server.serve_forever()

    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()
        GPIO.cleanup()