1 2 Previous Next 20 Replies Latest reply on Sep 14, 2016 7:09 AM by Intel Corporation

    Help to transpose python code used in Raspberry pi to USE in intel edison with mraa

    neuberfran

      Hi guys

       

      I have this code;

       

      codepythonb.jpg

       

       

      In this moment I I'm having trouble transpose this part:

       

      pins = {

      24 : {'name' : 'coffee maker', 'state' : GPIO.LOW},

      25 : {'name' : 'lamp', 'state' : GPIO.LOW}

      }

      for pin in pins:

      GPIO.setup(pin, GPIO.OUT)

      GPIO.output(pin, GPIO.LOW)

       

      Can we help me

        • 1. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi neuberfran,

          I recommend you to check the MRAA examples for Python. In the following code for example, you will find how to setup a GPIO and define it as an output: https://github.com/intel-iot-devkit/mraa/blob/master/examples/python/blink-io8.py. You can use this example to know what syntax you should use and then try to modify your code with this information.

          Regards,
          -Diego

          • 2. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
            neuberfran

            OK -Diego

            But, You can help more?

             

            Because I have the template html READY and I can not change almost anything in the code above

            inclusive this part:

             

            pins = {

            24 : {'name' : 'coffee maker', 'state' : GPIO.LOW},

            25 : {'name' : 'lamp', 'state' : GPIO.LOW}

            }

            for pin in pins:

            GPIO.setup(pin, GPIO.OUT)

            GPIO.output(pin, GPIO.LOW)

            • 3. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              Hi neuberfran,

               

              The syntax used by MRAA is different so you'd have to make little modifications to the code above. From the part of the code you posted, I understand that you are setting the GPIOs as outputs and setting them to a LOW state. This can be done with MRAA with the following methods:

               

              import mraa
              
              
              x = mraa.Gpio([pin_number]) # In this line you set which GPIO you will use: [pin_number]
              x.dir(mraa.DIR_OUT)         # In this line you set the GPIO to be an output
              x.write(0)                  # In this line you set the GPIO to a LOW state
              

               

              That's the way you should follow to set the GPIOs as outputs and to set them to a LOW state.

               

              Regards,
              -Diego

              • 4. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                Intel Corporation
                This message was posted on behalf of Intel Corporation

                Hello neuberfran,

                I was wondering if you have updates on this.

                Regards,
                -Diego

                • 5. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                  neuberfran

                  Hi

                  -Diego

                   

                   

                  I stay try with this code. But now I have a logical error.

                   

                  import mraa as x

                  import time

                  from flask import Flask, render_template, request

                  app = Flask(__name__)

                   

                  # GPIO.setmode(GPIO.BCM)

                  pins = {

                    7 : {'name' : 'coffee maker', 'state' : False },

                    8 : {'name' : 'lamp', 'state' : False }

                    }

                   

                  for pin in pins:

                    a = x.Gpio(pin)

                    a.dir(x.DIR_OUT)

                    b = x.Gpio(pin)

                    b.dir(x.DIR_OUT)

                   

                  @app.route("/")

                  def main():

                     for pin in pins:

                    pins[pin]['state'] = pin.read()

                    templateData = {

                     'pins' : pins

                    }

                     return render_template('main.html', **templateData)

                   

                  @app.route("/<changePin>/<action>")

                  def action(changePin, action):

                    changePin = int(changePin)

                    deviceName = pins[changePin]['name']

                     if action == "on":

                    b.write(1)

                     # GPIO.output(changePin, GPIO.HIGH)
                    message = "Turned " + deviceName + " on."

                     if action == "off":

                    b.write(1)

                     # GPIO.output(changePin, GPIO.LOW)
                    message = "Turned " + deviceName + " off."

                     if action == "toggle":

                    b.write(1)

                     # GPIO.output(changePin, not GPIO.input(changePin))
                    message = "Toggled " + deviceName + "."

                   

                     for pin in pins:

                    pins[pin]['state'] = pin.read()

                   

                    templateData = {

                     'message' : message,

                     'pins' : pins

                    }

                     return render_template('main.html', **templateData)

                  if __name__ == "__main__":

                    app.run(host='0.0.0.0', port=80, debug=True)

                   

                   

                   

                  procara11.jpgprocara12.jpg

                  • 6. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                    Intel Corporation
                    This message was posted on behalf of Intel Corporation

                    Hello neuberfran,

                    That error is because you are trying to perform a read() method for an int type variable. Check the following line pins[pin]['state'] = pin.read()

                    The variable pin is an int and it doesn't have a .read() method. That's why the error happens. In order to read a value from a GPIO you would have to do the following:

                    x = mraa.Gpio(7)
                    x.dir(mraa.DIR_IN)
                    x.read()


                    Applying this into your code, you would have to include the above lines in the for loop. The variable "pin" has the pin number value so it would have to be like x = mraa.Gpio(pin)

                    I hope this information has clarified your doubt about the error message.

                    Regards,
                    -Diego

                    • 7. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                      neuberfran

                      Hi

                      -Diego

                       

                       

                      I change my code and I tried this but I have errors:

                       

                      import mraa as x

                      import time

                      from flask import Flask, render_template, request

                      app = Flask(__name__)

                       

                       

                      # GPIO.setmode(GPIO.BCM)

                       

                       

                      a = x.Gpio(7)

                      a.dir(x.DIR_OUT)

                      b = x.Gpio(8)

                      b.dir(x.DIR_OUT)

                       

                       

                       

                       

                      pins = {

                         7 : {'name' : 'coffee maker', 'state' : a.write(0) },

                         8 : {'name' : 'lamp', 'state' : b.write(0) }

                         }

                       

                       

                      #for pin in pins:

                      #   a = x.Gpio(pin)

                      #   a.dir(x.DIR_OUT)

                      #   b = x.Gpio(pin)

                      #   b.dir(x.DIR_OUT)

                      #   w = x.Gpio(pin)

                      #   w.dir(x.DIR_OUT)

                       

                       

                      @app.route("/")

                      def main():

                         for pin in pins:

                            w = x.Gpio(pin)

                            w.dir(x.DIR_IN)

                            pins[pin]['state'] = w.read()

                         templateData = {

                            'pins' : pins

                            }

                         return render_template('main.html', **templateData)

                       

                       

                      @app.route("/<changePin>/<action>")

                      def action(changePin, action):

                         changePin = int(changePin)

                         deviceName = pins[changePin]['name']

                         if action == "on":

                            w.dir(x.DIR_OUT)

                            w.write(1)

                          # GPIO.output(changePin, GPIO.HIGH)

                            message = "Turned " + deviceName + " on."

                         if action == "off":

                            w.dir(x.DIR_OUT)

                            w.write(0)

                          # GPIO.output(changePin, GPIO.LOW)

                            message = "Turned " + deviceName + " off."

                         if action == "toggle":

                          # GPIO.output(changePin, not GPIO.input(changePin))

                            message = "Toggled " + deviceName + "."

                         

                         for pin in pins:

                            w = x.Gpio(pin)

                            w.dir(x.DIR_IN)

                            pins[pin]['state'] = w.read()

                         

                         templateData = {

                             'message' : message,

                             'pins' : pins

                         }

                         return render_template('main.html', **templateData)

                        

                      if __name__ == "__main__":

                         app.run(host='0.0.0.0', port=8182, debug=True)

                       

                      procara15.jpg

                      • 8. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                        Intel Corporation
                        This message was posted on behalf of Intel Corporation

                        Hello neuberfran,

                         

                        This time the error you are getting is because you are trying to use the variable "w" without defining first what the variable "w" is exactly. I recommend you to split your code in small pieces so you can debug it in an easier way.

                         

                        According to your original post, the code you want to transpose can be divided in 3 pieces. The first one is the part where you define the "pins" dictionary and the for-loop the setup up the pin settings. The second part is the main() function and the last part corresponds to the action() function, so let's try doing one part at the time. Once you are sure that a specific part is working correctly, then proceed with the next one, otherwise it'll be more difficult to debug the code.

                         

                        For the setup part, you can try the following code. It'll will setup two GPIOs as outputs and set them to a LOW state.

                         

                        import mraa
                        
                        # Dictionary for GPIOs
                        pins = {
                            7:{"gpioMraa":mraa.Gpio(7), "name":"coffe maker", "state":False},
                            8:{"gpioMraa":mraa.Gpio(8), "name":"lamp", "state":False}
                            }
                        
                        # Sets GPIOs as outputs with LOW State
                        for pin in pins:
                            pins[pin]["gpioMraa"].dir(mraa.DIR_OUT)
                            pins[pin]["gpioMraa"].write(0)
                        

                         

                        I haven't tested the code above, however I ran it and there weren't error messages.

                         

                        For the main() function, you have to read the state of another pin in order to execute some action according to this value. It might be easier if you write your own version of this code according to what you want to achieve, instead of trying to modify the Raspberry Pi code version.

                         

                        Regards,
                        -Diego

                        • 9. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                          neuberfran

                          Hi

                          -Diego

                           

                          The main() It has been the hardest part to transpose because this  communicates directly with the dictionary.

                           

                          How I will use "gpioMraa" in template (html)

                           

                          • 10. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                            Intel Corporation
                            This message was posted on behalf of Intel Corporation

                            Hello neuberfran,

                            The part that I added related to "gpioMraa" is just to create the GPIO object to interact with the GPIOs, as it's shown in the for-loop. I added it in order to have an equivalent version according to the code you posted in your first post. It might not be the best approach for your application because I don't know the details of it, but it might be useful as a starting to point to continue with the rest of the code.

                            As I explained to you in my last post, it might be easier if you write your code from scratch instead of trying to modify it to get an equivalent version of the Raspberry Pi code.

                            I strongly recommend you to understand the original code, how many GPIOs are used and which ones are inputs and/or outputs. Then, check what every part of the code actually does, so you can get a general understanding of it. Once you have a clear idea, proceed to write your code. It doesn't have to be exactly the same or structured in the same way. Remember that the original code is used in Raspberry Pi, so in Edison it will probably be different.

                            Regards,
                            -Diego

                            • 11. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                              neuberfran

                              Hi -Diego

                              excuse my insistence But I stay now with their help almost managed to transpose the code literally, as for my future project it is important to be so

                               

                              This is my issue today:

                              root@neuberfran:/weblamp# python weblc.py

                                File "weblc.py", line 23

                              pins[pin]["state"].read()

                              ^

                              IndentationError: unindent does not match any outer indentation level

                               

                              import mraa as x

                              import time

                              from flask import Flask, render_template, request

                              app = Flask(__name__)

                               

                               

                              # GPIO.setmode(GPIO.BCM)

                               

                               

                              pins = {

                                  7:{"gpioMraa":x.Gpio(7), "name":"coffe maker", "state":False},

                                  8:{"gpioMraa":x.Gpio(8), "name":"lamp", "state":False}

                                  }

                               

                              # Sets GPIOs as outputs with LOW State

                              for pin in pins:

                                  pins[pin]["gpioMraa"].dir(x.DIR_OUT)

                                  pins[pin]["gpioMraa"].write(0)

                               

                               

                              @app.route("/")

                              def main():

                                 for pin in pins:

                                 pins[pin]["state"].dir(x.DIR_IN)

                                    pins[pin]["state"].read()

                                 templateData = {

                                    'pins' : pins

                                    }

                                 return render_template('main.html', **templateData)

                               

                               

                              @app.route("/<changePin>/<action>")

                              def action(changePin, action):

                                 changePin = int(changePin)

                                 deviceName = pins[changePin]['name']

                                 if action == "on":

                                    pin.dir(x.DIR_OUT)

                                    pin.write(1)

                                  # GPIO.output(changePin, GPIO.HIGH)

                                    message = "Turned " + deviceName + " on."

                                 if action == "off":

                                    pin.dir(x.DIR_OUT)

                                    pin.write(0)

                                  # GPIO.output(changePin, GPIO.LOW)

                                    message = "Turned " + deviceName + " off."

                                  

                                 for pin in pins:

                                    pins[pin]["state"].dir(x.DIR_IN)

                                    pins[pin]["state"].read()

                                 templateData = {

                                    'message': message,

                                    'pins' : pins

                                    }

                                 return render_template('main.html', **templateData)

                               

                              if __name__ == "__main__":

                                 app.run(host='0.0.0.0', port=8182, debug=True)

                              • 12. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                                Intel Corporation
                                This message was posted on behalf of Intel Corporation

                                Hello neuberfran,

                                Don't worry, I just wanted to let you know that transposing a code literally part by part and get an equivalent code is not always possible because of many factors like hardware, libraries used, etc. But as I said, don't worry, I'll try to help you with this.

                                The error you are getting this time is because there is a line with wrong indentation. According to the error message, it's the line 23. I recommend you to check the code where that line is located and make sure the indentation is correct.

                                Regards,
                                -Diego

                                • 13. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                                  neuberfran

                                  Hi

                                   

                                  The line 23 is:

                                  pins[pin]["state"].read()

                                  and/or

                                  pins[pin]['state'] = pins[pin]["state"].read()

                                   

                                  I stay study about IndentationError: unindent does not match any outer indentation level

                                   

                                   

                                   

                                  • 14. Re: Help to transpose python code used in Raspberry pi to USE in intel edison with mraa
                                    Intel Corporation
                                    This message was posted on behalf of Intel Corporation

                                    Those lines are supposed to be inside the for-loop, but since the indentation is not correct, you are getting the error message. Just make sure both lines are correctly indented inside the for-loop.

                                    Additionally, I recommend you to check the following page where you will find a very good description about indentation in Python: https://docs.python.org/2.3/ref/indentation.html. It'll help you to understand better why the indentation error is happening.

                                    Regards,
                                    -Diego

                                    1 2 Previous Next