简体   繁体   中英

Getting an internal server error on flask web server

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?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM