A program to control an LED through a web browser

Let us write a program to turn an LED connected to BeagleBone ON and OFF via a web browser. You can replace the LED with a buzzer to control sound remotely or with a relay to control AC devices. Here the web browser needs to send web server information if it wants the LED to be ON or OFF. So, the HTTP command GET is not useful. We will use the HTTP command POST here to send information to the Flask web server. Create a new file in the templates directory. Write the following code inside and save it as led.html. This file has two buttons which use the POST method to send information state = on or state = off to the Flask application. The code for led.html is as follows:

<html>
<body>
<h1>LED</h1>
<form method="post">
<input type="submit" name="state" value="on" />
<input type="submit" name="state" value="off" />
</form>
</body>
</html>

Create an LED circuit setup like we did in Chapter 3, Blinking External LEDs. Create a new file with the name flask_LED.py inside the cloud9 directory and write the following code in it. Save and run the program. Then open the web browser from your smartphone/computer. Put this address in the address bar: http://<Beaglebone's IP address>:3002/led.html. You should be able to see on and off buttons. If you press the on button, the LED should turn on. If you press the off button, the LED should turn off. The code for flask_LED.py is as follows:

import Adafruit_BBIO.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)

led = "P8_10"
GPIO.setup(led,GPIO.OUT)

@app.route('/led.html', methods=['GET','POST'])
def change_LED_state():
    if request.method == 'POST':
        current_state = request.form['state']
        print current_state
        if (current_state == 'on'):
            GPIO.output(led,GPIO.HIGH)
        elif (current_state == "off"):
            GPIO.output(led,GPIO.LOW)
    return render_template('led.html')

app.run('0.0.0.0', 3002)

Explanation

In this program, the route() decorator specified that if the requested URL is led.html then we are interested in handling the method GET and method POST. This decorator will trigger the change_LED_state() function. Inside the change_LED_state() function, we are checking if the requested method is POST. If yes, then we collected the value of state from the template HTML file. If the value is on, we turn on the LED by the usual Adafruit BBIO method. If the value is off, we turn off the LED. In the end, we loaded the led.html page again irrespective of whether the method was GET or POST. Now, we can control the LED connected to BeagleBone from the remote web browser.

Troubleshooting steps for this program are the same as the last program. You can use a web browser Firefox extension Poster to create custom POST requests in GUI. You can also use the popular Linux command curl to create custom POST requests for debugging. For more details, check the manual page of curl.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.169