Linux+raspberry Pi+python
* * For students to do a smart home tutorial:
Using Raspberry Pi As a server, we have access to the server method, control the Raspberry Pi I/O port high and low level, I/O port on the relay (to achieve low pressure control), relays you can pick up the electrical appliances you want to control! You can control it on the Web page! **
Click the "Close", "open" button on the page directly to control the lights in your home via the network.
here are the following :
Tutorial
1. Installing Flask
Use Flask (http://flask.pocoo.org/), the Python web framework, to turn raspberry Pi into a Dynamic Web server. On the command line, enter the following command to install:
pi@raspberrypi~@sudo pip install flask
2. Writing a Python program
创建一个名为WebLight.py的文件,写入以下代码:
ImportRpi.gpio asGPIO fromFlaskImportFlask, render_template, Requestapp = Flask (__name__) Gpio.setmode (GPIO. BCM) pins = { -: {' name ':' TV ',' state ': GPIO. Low}, -: {' name ':' Light ',' state ': GPIO. Low}} forPininchPins:GPIO.setup (PIN, GPIO.) Out) Gpio.output (PIN, GPIO. Low)@app. Route ("/") def main(): forPininchpins:pins[pin][' state '] = Gpio.input (pin) TemplateData = {' pins ': pins}returnRender_template (' main.html ', **templatedata)@app. Route ("/<changepin>/<action>") def action(Changepin, action):Changepin = Int (changepin) devicename = pins[changepin][' name ']ifAction = ="on": Gpio.output (Changepin, GPIO. High) message ="turned"+ DeviceName +"on." ifAction = ="Off": Gpio.output (Changepin, GPIO. Low) message ="turned"+ DeviceName +"off." ifAction = ="Toggle": Gpio.output (Changepin, notGpio.input (changepin)) message ="toggled"+ DeviceName +"." forPininchpins:pins[pin][' state '] = Gpio.input (pin) TemplateData = {' message ': Message,' pins ': pins}returnRender_template (' main.html ', **templatedata)if__name__ = ="__main__": App.run (host=' 0.0.0.0 ', port= the, debug=True)
* * If we want to add another port control appliance, it's simple we can do this:
Put
pins = { 24 : {‘name‘‘TV‘‘state‘ : GPIO.LOW}, 25 : {‘name‘‘Light‘‘state‘ : GPIO.LOW} }
Switch
pins = { 24 : {‘name‘‘TV‘‘state‘ : GPIO.LOW}, 25 : {‘name‘‘Light‘‘state‘ : GPIO.LOW} 26 : {‘name‘‘Coffee Maker‘‘state‘ : GPIO.LOW} }
3. Writing HTML files
Under the directory where weblight.py is located, create a subdirectory named templates. In the Templates subdirectory, create a directory named
main.html the file and enter the following code:
All of the brackets in this HTML page template are parsed into variable names, which are replaced by actual data when the Python script calls the Render_template function.
<! DOCTYPE html><head><title>Home Intelligent Control</title></head><body><H1>Home automation Project</H1><img src="http://www.5qdd.com/files/all/121207/6-12120G43028.jpg" border ="0" widgth= "" " height=" > "</a><h2>Student Name:<font size ="6" color="Red" >Tom</font> </H2>{% for pin in pins%} <p> the{{Pins[pin].name}}{% if pins[pin].state = = true%}(<a href="/ {{pin}} /off "> close </a>){% else %}(<a href="/ {{pin}} /on "> open </a>){% endif %}</P>{% endfor %}{% if message%}<h3>{{message}}</h3>{% endif %} </body></html>
4. start the server
pion http://0.0.0.0:80/with reloader
Finally: The relay is connected to the control of the I/O, in the LAN landing the IP address of the Raspberry Pi, the realization of the network control home appliances!
Reference articles
Raspberry Pi-web Control Appliance