Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

micropython 网页控制ESP32引脚


网页控制ESP32引脚


Snipaste_2022-07-26_23-27-22.jpg


main.py

import socket
import _thread,gc
from machine import Pin
import time
import network


ip_address =""
led=Pin(22,Pin.OUT)             # 板载指示灯初始化

def hard_reset():
    from machine import reset
    reset()
    
def wifi_connect():
  wlan = network.WLAN(network.STA_IF)  # 以工作站 (wlan) 模式运行,需要创建一个工作站Wi-Fi接口的实例
  wlan.active(True)                    # 在工作站对象上调用激活方法并以True作为输入值传递来激活网络接口
  start_time=time.time()               # 记录开始时间
  
  if not wlan.isconnected():              # 如果尚未联网成功
    print("当前无线未联网,正在连接中....")  
    wlan.connect("NBWIFI", "xxxxxx")   # 无线网SSID、密码,开始联网
    while not wlan.isconnected():         # 如果还未连接成功,则LED灯闪烁提示
      time.sleep_ms(1000)
      print("正在尝试连接到wifi....")
      print(time.time())
      
      if time.time()-start_time>15:       # 如果超过15秒还不行,就退出
        print("连接失败!!!无线网连接超过15秒,请检查无线网名称和密码是否正确..")
        time.sleep_ms(3000)
        hard_reset()
        break
        
  if wlan.isconnected():                  # 如果联接成功
    IP_info=wlan.ifconfig()    
    print("无线网已经连接,信息如下:")
    print("IP地址:"+IP_info[0])
    global ip_address
    ip_address =IP_info[0]
    print("子网掩码:"+IP_info[1])
    print("网关:"+IP_info[2])
    print("DNS:"+IP_info[3])

#开始执行联网模块
wifi_connect()

#建立需要显示的网页信息

def web_page():
    #先判断当前引脚状态并保存到变量gpio_state中
  if led.value() == 1:
    gpio_state="OFF"
  else:
    gpio_state="ON"
  #gpio_state变量使用“+”符号连接字符串,并将其合并到 HTML 文本中。
  html = """<html>
	<head>
		<title>ESP Web Server</title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="icon" href="data:,">
		<style>
			html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
			  h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none; 
			  border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
			  .button2{background-color: #4286f4;}
		</style>
	</head>
	<body>
		<h1>ESP Web Server</h1>
		<p>GPIO state: <strong>""" + gpio_state + """</strong>
		</p>
		<p>
			<a href="/led=on">
				<button class="button">ON</button>
			</a>
		</p>
		<p>
			<a href="/led=off">
				<button class="button button2">OFF</button>
			</a>
		</p>
	</body>
</html>"""
  #最终返回标准的HTML网页代码
  return html
#创建套接字
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#我们将一个空字符串 ' ' 作为 IP 地址和端口 80 传递。在本例中,
#空字符串是指本地主机 IP 地址(这意味着 ESP32 或 ESP8266 IP 地址)
s.bind(('', 80))
#开始监听最大数量为5
s.listen(5)

while True:
    #当客户端连接时,服务器调用accept()方法来接受连接。
    #当客户端连接时,它会保存一个新的套接字对象,以接受
    #和发送数据CONN变量,并保存客户端地址以连接到 服务器上的addr变量。
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))#打印连接的地址
  request = conn.recv(1024)
  request = str(request)
  #print('Content = %s' % request)
  led_on = request.find('/led=on')
  led_off = request.find('/led=off')
  print(led_on,led_off)
  if led_on == 6:
    print('LED OFF')
    led.value(0)
  if led_off == 6:
    print('LED ON')
    led.value(1)
  response = web_page()
  #发送页面
  conn.send('HTTP/1.1 200 OK\n')
  conn.send('Content-Type: text/html\n')
  conn.send('Connection: close\n\n')
  conn.sendall(response)
  #关闭创建的套接字。
  conn.close()



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.0092589855194092 seconds