1

I'm newbie for raspberry pi and python coding. I'm working on a school project. I've already looked for some tutorials and examples but maybe I'm missing something. I want to build a web server based gpio controller. I'm using flask for this. For going into this, I've started with this example. Just turning on and off the led by refreshing the page.

So the problem is, I can't see the response value on the web server side. It's turning on and off the led. But I want to see the situation online. But I just couldn't. I'm getting and internal server error. I'm giving the python and html codes. Can you help me with solving the problem.

from flask import Flask
from flask import render_template
import RPi.GPIO as GPIO

app=Flask(__name__)

GPIO.setmode(GPIO.BCM)

GPIO.setup(4, GPIO.OUT)
GPIO.output(4,1)
status=GPIO.HIGH

@app.route('/')
def readPin():
    global status
    global response
    try:
        if status==GPIO.LOW:
            status=GPIO.HIGH
            print('ON')
            response="Pin is high"
        else:
            status=GPIO.LOW
            print('OFF')
            response="Pin is low"
    except:
        response="Error reading pin"

    GPIO.output(4, status)

    templateData= {
        'title' : 'Status of Pin' + status,
        'response' : response
        }

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

if __name__=="__main__":
    app.run('192.168.2.5')

And basically just this line is on my html page.

<h1>{{response}}</h1> 

I think "response" doesn't get a value. What's wrong on this?

8
  • Complete shot in the dark - change the 'response' value in the templateData dictionary to a value (any number or string) and then reload the web page and see if the {{response}} is updated ((it's a start to the debugging)) --- also on a side note, should it be <h1> {{response}} </h1> :D? Commented Apr 19, 2016 at 23:00
  • I've noticed your title is 'Status of Pin' + status, this might be breaking your dictionary.. try 'Status of Pin' + str(status) ?? Commented Apr 19, 2016 at 23:04
  • ye h1-h2 is right on my html page actually :D just wrote accidently wrong here :D editing right now Commented Apr 19, 2016 at 23:06
  • wow that helped me. thank you so much. i didn't actually get this. can you explain a little bit. thanks a lot again @Alan Commented Apr 19, 2016 at 23:08
  • Well it depends on which one it was that fixed your problem? If it was the str(status) that worked, then this will be because you tried to concatenate a string value with a non string type value, but by wrapping it in str(), it casts its type to a type string, so they can be combined. This would have thrown an exception when trying to create your dictionary, therefore the return render_template('pin.html', **templateData) was actually being passed an empty templateData variable (hence the non value on the client side) Commented Apr 19, 2016 at 23:16

2 Answers 2

2

Firstly it helps to run it in debug mode:

app.run(debug=True)

This will help you track down any errors which are being suppressed.

Next have a look at the line where you are building the title string:

'title' : 'Status of Pin' + status

If you enable the debug mode, then you should see something saying that an int/bool can't be converted to str implicitly. (Python doesn't know how to add a string and an int/bool).

In order to fix this, you should explicitly cast status to a string:

'title' : 'Status of Pin' + str(status)

Or better yet:

'title' : 'Status of Pin: {}'.format(status)

Sign up to request clarification or add additional context in comments.

Comments

0

Your server was probably throwing an exception when trying to create your dictionary, therefore the templateData value was being sent as an empty value.

Notice in this example, the TypeError which is thrown when trying to concatenate 2 variables of different type.

Hence, wrapping your variable in the str(status) will cast the status variable to it's string repersentation before attempting to combine the variables.

[root@cloud-ms-1 alan]# cat add.py
a = 'one'
b = 2
print a + b


[root@cloud-ms-1 alan]# python add.py
Traceback (most recent call last):
  File "add.py", line 6, in <module>
    print a + b
TypeError: cannot concatenate 'str' and 'int' objects


[root@cloud-ms-1 alan]# cat add.py
a = 'one'
b = str(2)
print a + b


[root@cloud-ms-1 alan]# python add.py
one2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.