3 Replies Latest reply on Mar 5, 2016 1:30 PM by BMEJ

    Controlling LED from Web

    joe-iot

      After blinking an LED the next thing an IoT newb wants to do is control an LED from the web. I wanted to share what I did in the event someone else wants to do this. The app runs on SSL and requires auth to control the LED. To install I stopped the lighttpd server first.

       

      systemctl stop lighttpd
      systemctl disable lighttpd
      
      
      

       

      Then install flask and pyopenssl

      pip install flask
      pip install pyopenssl
      
      
      

       

      Download or clone the files here: GitHub - joemcmanus/GalileoPyServer: A sample web server using Flask that can read digital & analog sensors as well as c…

       

      Change the line

      pin = mraa.Gpio(12)
      
      
      


      Top match your pin for your LED.  If you have a TMP36 temp sensor wire it up to A1.

       

      Start the server.

       

      ./server.py
      
      
      

      pyserver.png

       

      There is a default SSL cert included so go to https://yourGalileoIP

       

      Username is admin and password is galileo .

       

      Sample wiring if you don't want to change any code.

      GalileoGen2-PyServer_bb.png

      Thanks for looking.

       

      Cheers,

      -Joe

        • 1. Re: Controlling LED from Web
          BMEJ

          Thanks Joe this is great.

           

          Could this also be used to execute python script on the server? 

           

          Cheers

          • 2. Re: Controlling LED from Web
            joe-iot

            Absolutely. Since Flask is python you would just move your python script in to the server.py script.

             

            i.e.

            
            from flask import Flask                                                                          
                                                                                                             
            @app.route('/')                                                                                  
            def index():                                                                                     
                    return("<html> <body> <a href=/runScript> Run my Script </a> </body </html>")            
                                                                                                             
            @app.route('/runScript')                                                                         
            def runScript():                                                                                 
                    #Put all of your regular python code here.                                               
                    #i.e. cut and paste your python script in to this section.                               
                    return("Ran Script")                                                                     
                                                                                                             
            if __name__ == '__main__':                                                                       
                    app.run(host='0.0.0.0', port=80) 
            


            When you click on the link runScript it would execute your code. When it is done the text "Ran Script" would be returned to the screen. Flask is kind of amazing!

             

             

            -Joe


            • 3. Re: Controlling LED from Web
              BMEJ

              Hey joe,

               

              Where do I place the files at?

               

              Nevermind I got it.  I put everything in as is using git clone ...

               

              Thanks