【鼓捣树莓派】动态网页控制lcd显示

来源:互联网 发布:android app商城源码 编辑:程序博客网 时间:2024/06/02 07:15

动态网页控制lcd显示

本文是将server端放在sae上和放在本地有略微不同,后文会有说明

准备材料:

-树莓派
-SAE
-LCD(1602)
-i2c转LCD1602的转接板( PCF85741)

步骤:

1.搭建简单文本输入网页

用html编辑基本的文本输入提交网页,包含以下内容:一条label文本说明,一个文本输入框,一个提交按钮。使用了POST方法(关于flask详细教程)

效果图

<html>    <head>    <meta charset="UTF-8">        <title>输入窗口</title>    </head>    <body>            <div class="title">                <h1>动态网页控制树莓派LCD显示</h1>                <form method="POST" action="{{ url_for('show') }}">                  <label for="textinput"> 请输入你希望显示的字符串(数字或字母):</label>                  <input type="text" name="textinput" value="123456"/><br />                  <input type="submit" />                </form>            </div>    </body></html>

2.上面写好了submit页面,接着写action页面来接收前者post过来的值

{{name}}

3.创建应用myapp.py.

from flask import Flask, g, request,redirect, url_forfrom flask import render_templateapp = Flask(__name__)app.debug = Truename = 'Noting'@app.route('/')def main():    return redirect(url_for('textInput'))@app.route('/textInput')def form():    return render_template('form_submit.html')@app.route('/show',methods=['GET','POST'])def show():        global name        if request.method == 'POST':            name = request.form['textinput']            return render_template('form_action.html', name=name)        return render_template('form_action.html', name=name)

其中使用了redirect使得主页直接跳转到文本输入界面,由于server是放在sae端需要,树莓派获取数据时需要从sever GET数据,所以在show()中使用GET/POST方法判断,submit页向它POST值直接返回show页面,而当树莓派向它GET数据时返回name值,即输入的文本。

4.树莓派这边

import timeimport urllib2import LCD1602 as LCDif __name__ == '__main__':    LCD.init_lcd()    while True:        LCD.init_lcd()        ss = urllib2.urlopen('http://*****.applinzi.com/show').readline()        LCD.print_lcd(0,0,ss)        time.sleep(1)

通过urllib2.urlopen从网页get到文本内容并通过LCD输出。

0 0
原创粉丝点击