演示代码
Github仓库:https://github.com/ELE-Clouds/ModuleSam ... IFI/httpcc
代码: 全选
# -*- coding: UTF-8 -*-
u'''
******************************************************************************
* 文 件:__init__.py
* 概 述:模块初始化
* 版 本:V0.10
* 作 者:Robin Chen
* 日 期:2018年5月8日
* 历 史: 日期 编辑 版本 记录
2018年5月8日 Robin Chen V0.10 创建文件
******************************************************************************'''
from ESP8266.WIFI.httpcc import netConnect
SSID="WIFI名称"
PASSWORD="WIFI密码"
ip = netConnect.connectWifi(SSID,PASSWORD) #连接网络
if ip != "0,0,0,0":
print("连接成功,当前IP为:",ip)
else:
print("连接失败,3秒后重新连接!")
l = ("Web控制功能演示","WEB数据传输功能演示")
while True:
print("\n\n\n 操作选择 \n -------")
for i in range(len(l)):
print(i, ":", l[i])
print("====================")
n = input("请输入序号进行选择,输入'Q'或'q'回车后退出:")
if n == "0":
from ESP8266.WIFI.httpcc import WebControl
break
elif n == "1":
from ESP8266.WIFI.httpcc import DataUp
break
elif n == "Q" or n == "q":
break
else:
continue
代码: 全选
# -*- coding: UTF-8 -*-
u'''
******************************************************************************
* 文 件:netConnect.py
* 概 述:网络连接函数
* 版 本:V0.10
* 作 者:Robin Chen
* 日 期:2018年5月8日
* 历 史: 日期 编辑 版本 记录
2018年5月8日 Robin Chen V0.10 创建文件
******************************************************************************'''
import network
import time
# 设当前设备为“客户端”模式,并连接WIFI
def connectWifi(_ssid, _passwd):
global wlan
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.disconnect()
wlan.connect(_ssid, _passwd)
while (wlan.ifconfig()[0] == '0.0.0.0'):
time.sleep(3) # 3秒后重新连接
return wlan.ifconfig()[0]
# 设当前设备为AP模式
代码: 全选
<!DOCTYPE html>
<html>
<head>
<title>ESP8266 WEB控制功能演示</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8">
<meta http-equiv="content-style-type" content="text/css">
</head>
<body>
<div style="text-align: center;"><h1>ESP8266 Web 控制三色灯亮灭</h1></div>
<form action="" method="">
<div style="text-align: center;">
<img src="ESP8266/WIFI/httpcc/chinabird.jpg" alt="">
<hr align="center" width="100%">
</div>
<div style="text-align: center;">
<button name="CMD" value="redlight" type="submit"> 红 灯 </button>
<button name="CMD" value="greenlight" type="submit" > 绿 灯 </button>
<button name="CMD" value="bluelight" type="submit" > 蓝 灯 </button>
</div>
<br>
<div style="text-align: center;">
<button name="CMD" value="alloff" type="submit"> 全部关闭 </button>
<button name="CMD" value="allon" type="submit" > 全部打开 </button>
</div>
</form>
</body>
</html>
代码: 全选
# -*- coding: UTF-8 -*-
u'''
******************************************************************************
* 文 件:WebControl.py
* 概 述:Web页面控制设备
* 版 本:V0.10
* 作 者:Robin Chen
* 日 期:2018年5月8日
* 历 史: 日期 编辑 版本 记录
2018年5月8日 Robin Chen V0.10 创建文件
******************************************************************************'''
from ESP8266.WIFI.httpcc import netConnect # 此处引用为获取netConnect文件中的全局变量“wlan”。
import socket
from machine import Pin
# 读取WEB页面代码文件
htmls = open("ESP8266/WIFI/httpcc/WebKZ.html",“r”)
html= htmls.read() #此处直接由文件读取,如果直接在此处写入页面代码,须使用 ''' ''' 将代码包含,否则无法显示
htmls.close()
# 功能和引脚对照表
#D1->GPIO5 ---- 红色
#D2->GPIO4 ---- 绿色
#D5->GPIO14 ---- 蓝色
ledBlue = Pin(14, Pin.OUT) # 蓝色
ledGrean = Pin(4, Pin.OUT) # 绿色
ledRed = Pin(5, Pin.OUT) # 红色
# 只亮绿色
def greanOnly():
ledBlue.off()
ledGrean.on()
ledRed.off()
# 全开
def allOn():
ledBlue.on()
ledGrean.on()
ledRed.on()
# 只亮红色
def redOnly():
ledBlue.off()
ledGrean.off()
ledRed.on()
# 只亮蓝色
def blueOnly():
ledBlue.on()
ledGrean.off()
ledRed.off()
# 全关
def allOff():
ledBlue.off()
ledGrean.off()
ledRed.off()
port=80
listenSocket=None
ip=netConnect.wlan.ifconfig()[0]
listenSocket = socket.socket() #建立一个实例
listenSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listenSocket.bind((ip,port)) #绑定建立网路连接的ip地址和端口
listenSocket.listen(5) #开始侦听
print ('等待TCP连接...')
while True:
print("连接中.....")
conn, addr = listenSocket.accept()
print("已连接 %s" % str(addr))
request = conn.recv(1024)
print("内容: %s" % str(request))
request = str(request)
CMD_grean = request.find('/?CMD=greenlight') #如果在请求的包中,发现有?CMD=greenlight,下同
CMD_allon = request.find('/?CMD=allon')
CMD_red = request.find('/?CMD=redlight')
CMD_blue = request.find('/?CMD=bluelight')
CMD_alloff = request.find('/?CMD=alloff')
print("Data: " + str(CMD_grean))
print("Data: " + str(CMD_allon))
print("Data: " + str(CMD_red))
print("Data: " + str(CMD_blue))
print("Data: " + str(CMD_alloff))
if CMD_grean == 6: #如果此命令有效,下同
print('+grean')
greanOnly() #调用仅点亮绿灯函数,下同
if CMD_allon == 6:
print('+allon')
allOn()
if CMD_red == 6:
print('+red')
redOnly()
if CMD_blue == 6:
print('+blue')
blueOnly()
if CMD_alloff == 6:
print('+alloff')
allOff()
response = html #将html的网页定义装载在回应字段
conn.send(response) #send到浏览器上,就形成了控制界面
conn.close()
web控制的前提首先是要在ESP8266模块中加入web控制页面代码,当有外部设备访问时,ESP8266就将代码发送到对方的浏览器中进行显示。然后在浏览器中通过按钮将信息以WEB连接地址的形式发送给ESP8266模块。ESP8266模块再对连接地址信息进行解析,找到有用的信息,然后进行判断,并作出相应的动作。
在做WEB页面时要注意使用表单系统,即按钮部件要在<form></form>标签内,否则无效。ESP8266主要根据按钮的name属性和value属性值进行判断是哪个按钮进行了动作。从而作出相应的动作。在点击按钮后,按钮的name属性与value属性值将会被提交到连接地址中,然后被ESP8266模块截获。