Community
cancel
Showing results for 
Search instead for 
Did you mean: 
nDE_S
New Contributor I
2,052 Views

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

Hi guys

I have this code;

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

0 Kudos
20 Replies
idata
Community Manager
102 Views

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
nDE_S
New Contributor I
102 Views

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)

idata
Community Manager
102 Views

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
idata
Community Manager
102 Views

Hello neuberfran,

 

 

I was wondering if you have updates on this.

 

 

Regards,

 

-Diego
nDE_S
New Contributor I
102 Views

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("//")

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)

idata
Community Manager
102 Views

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
nDE_S
New Contributor I
102 Views

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("//")

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)

idata
Community Manager
102 Views

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
nDE_S
New Contributor I
102 Views

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)

idata
Community Manager
102 Views

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
nDE_S
New Contributor I
102 Views

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("//")

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)

idata
Community Manager
102 Views

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
nDE_S
New Contributor I
102 Views

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

 

idata
Community Manager
102 Views

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
nDE_S
New Contributor I
102 Views

Hi

http://pastebin.com/zZ37Fyu3 import mraa as x import time from flask import Flask, render_template, request - Pastebin.com

Now I have new error: (the other I fix using nano file.py)

idata
Community Manager
102 Views

Hello neuberfran,

 

 

This error is because you are trying to access an attribute that is not valid for a bool object. When you say pins[pin]["state"], you are accessing the value there which is a boolean data type (False value). If you want to implement the dir() method of the MRAA library, you have to apply it on a GPIO object, like in the first for-loop.

 

 

Rather than an Edison or MRAA issue, it seems that you are not entirely familiar with Python, so I recommend you to learn the basis of this programming language so you can understand why you are having this kind of errors (syntax errors, data types errors, indentation errors, etc), and then proceed to debug your code.

 

 

Regards,

 

-Diego
nDE_S
New Contributor I
102 Views

Hi -Diego,

I do new code :

http://pastebin.com/JYUSP6Ak import mraa import time from flask import Flask, render_template, request a - Pastebin.com

But now I have new problem:

I can't active a relay of the 5v using arduino breakout board and my code.

Using this code: http://pastebin.com/3J38vYPr import mraa import time # This is a simple light switch code - Pastebin.com

everything worked

Using multimeter I measure gpios output (with my code) and the value change is between 0.40 and 0.48v (more or less)

idata
Community Manager
102 Views

Hello neuberfran,

 

 

If you are measuring a value of 0V in the GPIO's output, that means that you are not setting it to a HIGH value and the Relay won't be activated.

 

 

Could you please let me know in which part of the code the GPIO is supposed to be set as HIGH? I think this is done in the function a_helper(a, changePin, True, 1), correct? I recommend you to make sure you are actually calling that function in the if statement, otherwise you won't be able to set the GPIO to HIGH. This function is called inside the action() function, so make sure also that the action() function is being called. To test this, you can use print statements so you can know the execution flow of the code and have a better idea where the issue could be located.

 

 

Regards,

 

-Diego
nDE_S
New Contributor I
102 Views

Hi -Diego,

In this code I can't see the word HIGH:

http://pastebin.com/3J38vYPr import mraa import time # This is a simple light switch code - Pastebin.com

and this code worked

idata
Community Manager
42 Views

Hello neuberfran,

 

 

That's true, but what I was trying to say is that you have to set the GPIO to a HIGH level. This is done in the line 22 of the code: mraa.Gpio(8).write(1)

 

 

Make sure that you are setting the GPIO to a HIGH value in your code. If I'm not mistaken, you are already doing it, but probably the part of the code that sets the GPIO to HIGH is not been executed. That's why I recommended you to check that the function a_helper() is being called in the if statement. I highly recommend you to check the execution flow of your code so you can realize which parts of it are actually being executed and which parts don't.

 

 

Regards,

 

-Diego
Reply