1 Reply Latest reply on Sep 5, 2014 4:08 AM by mrpi64

    Control Galileo Gen2 GPIO By Python or webpage

    TOMMY_Z
      This is an example how to control the IO by click a button on a webpage, This will need to change the IO mapping if you want to make it work with Gen1.
      I only added IO13 which has the LED indicator just for example, you can add any more just follow the method i mentioned below if you like.
      I also tested this by using my cellphone and it's woking same as the laptop internet browser, This might be helpful if you want to control something remotely, This is ror the community discussion and I want to hear and learn more from you, Welcome any change if you want make it better, also please coach me if you have any suggestions.

       

      This will require boot up the board with SD card with full linux imagine which has the Python environment, and the Python web frame installed, you can go reference part at bottom see how to do that.
      After everything ready You can download the package at bottom before start the following.
      1, Unzip the package you will see the files inside. In this package there has submit.py is the core part to handle the port write , you can see the pinsetup module is just the module to write the port
      --------------------------------------------------------------------

      # -*- coding: utf-8 -*-

      import os
      import os.path

      import tornado.httpserver
      import tornado.ioloop
      import tornado.options
      import tornado.web

      from tornado.options import define, options
      import time
      from datetime import datetime

      define("port", default=8000, help="run on the given port", type=int)


      class IndexHandler(tornado.web.RequestHandler):
          def get(self):
              self.render('index.html' )
          def post(self):
              self.render('index.html')
      class confirmPageHandler(tornado.web.RequestHandler):
          def post(self):
              pinsetup('46','out','0')
              pinsetup('31','out','1')
              pinsetup('30','out','0')
              strDT = time.strftime('%Y-%m-%d %X',time.localtime())
              port = self.get_argument('port')
              state = self.get_argument('state')
              dt = strDT
              if state == "ON":
                  pinsetup('7','out','1')
              elif state == "OFF":
                  pinsetup('7','out','0')
              else:
                  pinsetup('7','out','0')   


              self.render('confirm.html', port=port, state=state,dt=dt )


      def pinsetup(pinexp,pindir,pinval):
          strexport=("echo \"%s\" > /sys/class/gpio/export" % pinexp) 
          strdir=("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir,pinexp))
          strval=("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval,pinexp))
          #os.system("echo \"46\" > D:\TEST.TXT") ;
          os.system(strexport)               #write a "30" to the export file, which reserves gpio30 for use
          os.system(strdir)  #sets gpio30 as an output
          os.system(strval)         #sets gpio30 low, enable dir_out
          print strexport
          print strdir
          print strval

      if __name__ == '__main__':
          tornado.options.parse_command_line()
          app = tornado.web.Application(
              handlers=[(r'/', IndexHandler),
                        (r'/confirm', confirmPageHandler)],
              template_path=os.path.join(os.path.dirname(__file__), "templates")
          )
          http_server = tornado.httpserver.HTTPServer(app)
          http_server.listen(options.port)
          tornado.ioloop.IOLoop.instance().start()

      ---------------------------------------------------------------------------------------------------------------
      also have some command to handle the Post and Get method of the webpage.
          The templates folder have templates for the index and confirm webpage.
      2, Remove your SD card, Copy WEB to the root folder, restart, go telnet and type
          Python /media/realroot/WEB/submit.py
      图像 1.png
          the webpage server should be running now.
      3, Now you will need to know your IP address by enter your router's config page.
          after you know your Galileo's IP address you can open your browser and type
          you will see the operation page:(for example like below)
               图像 001.png
      <>
      By select and submit you will see the LED change to corresponding state
      <>图像 002.png
      <>图像 003.png

      Reference

      :

      Preparation

      1, first of all you will need to boot up the board with SD card with full linux imagine which has the Python environment
      2, then you need the setuptool to install python add-on,
      you can download the package and unzip to SD card, go to the linux command VIA telnet and type
          python /thesetuptoolfolder/setup.py -install
      * if you want install the tornado web frame by download it and install it manually you dont need to do this step but i suggest you do this because you probably will need use the setuptool to install more addon
      3, after the setup tool installed you can simply type
          easy_install tornado
      one more method is you can go webpage and download it, use same method as install setuptool to install tornado.
      Tornado Web frame

      <Please be notice that i saw some error during install the tornado, it asked me to install GCC, i didn't do that, but i still can ran the webpage i wrote based on tornado with no problem, so you probably don't need to worry about this error. if you know how to solve that error, please do tell me i will appricate that>
      now all the environment are ready, we can start the test!

      How to Control the GPIO by command:

          basically to control the GPIO is just write those file, lets see how to control the GPIO by write those GPIO file
                  reference: Python with Galilleo
      • echo "3" > /sys/class/gpio/export                   //write a "3" to the export file, which reserves gpio3 for use
      • echo "out" > /sys/class/gpio/gpio3/direction   //sets gpio3 as an output
      • echo "1" > /sys/class/gpio/gpio3/value           //sets gpio3 high
      • echo "0" > /sys/class/gpio/gpio3/value           //sets gpio3 low
      • echo "3" > /sys/class/gpio/unexport               //unexport gpio3- can now be used by something/someone else
              in arduino sketch, it will be like this:
                system("echo "3" > /sys/class/gpio/unexport")
              this is just that simple that to use system method wirte the linux command

          Ok now lets move to the IO Mapping part, I choose the one with LED for example, that is the IO13 with LED.
                 Reference thanks RockPort find the Gen2 IO Mapping
                              https://communities.intel.com/thread/54725
             
          look from right to left,
              first, we will need trun on Mux1(set gpio46 to low),
              then we will need to setup the pull up/pulldown by config the gpio31 to high or low
              then we will need to config the dir_in/out, L will be out, H will be in
              at last we reach the gpio7 is actually the IO of IO13, to set gpio7 to H/L will set IO13 to H/L
          ok, now the command will be like below, you can see that for each IO there have three write operation, write export, direction, and value.
          Now you already know how to change that, lets change that into python
          In python we will need the os
           os.system("echo \"1\" > /sys/class/gpio/gpio7/value") ; //on
      thanks RockPort find the Gen2 IO Mapping