Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。
Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台
Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。
Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。
micropython ESP32通过网页传输数据
来源:micropython学习交流QQ群786510434 群主分享
main.py
import socket import _thread,gc from machine import Pin import time import network ip_address ="" def hard_reset(): from machine import reset reset() def wifi_connect(): wifi_led=Pin(22,Pin.OUT) # 板载指示灯初始化 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灯闪烁提示 wifi_led.value(1) time.sleep_ms(1000) wifi_led.value(1) 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(): # 如果联接成功 wifi_led.value(1) # LED灯常亮 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() # 数据采集中 web_page = open('index.html').read() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((ip_address, 80)) s.listen(2) i=0 def add_01(): global i i+=1 time.sleep(1) def app(): global web_page,s,i while True: try: print('waiting\n\r') if gc.mem_free() < 102000: gc.collect() conn, addr = s.accept() conn.settimeout(3.0) print('Got a connection from %s' % str(addr)) request = conn.recv(1024) conn.settimeout(None) request = str(request) print('Content = %s' % request) index = web_page%(i,'采集中',35,29) #这里 35为湿度 29为温度 conn.send('HTTP/1.1 200 OK\n') conn.send('Content-Type: text/html\n') conn.send('Connection: close\n\n') conn.sendall(index) conn.close() i+=1 except OSError as e: conn.close() print(e,'Connection closed') _thread.start_new_thread(add_01, ()) _thread.start_new_thread(app, ())
index.html
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" href="data:,"> <meta charset="UTF-8" > <meta http-equiv="refresh" content="10"> <style> body { text-align: center; font-family: "Trebuchet MS", Arial;} table { border-collapse: collapse; width: 60px; margin-left:auto; margin-right:auto; } th { padding: 12px; background-color: #0043af; color: white; } tr { border: 1px solid #ddd; padding: 12px; } tr:hover { background-color: #bcbcbc; } td { border: none; padding: 12px; } .sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px;} </style> </head> <body> <h1>数据展示</h1> <table> <tr> <th>数据类型</th> <th>当前值</th> </tr> <tr> <td>累加值</td> <td> <span class="sensor">%s</span> </td> </tr> <tr> <td>设备状态</td> <td> <span class="sensor">%s</span> </td> </tr> <tr> <td>湿度</td> <td> <span class="sensor">%s%%</span> </td> </tr> <tr> <td>温度</td> <td> <span class="sensor">%s℃</span> </td> </tr> </table>> </body> </html>
Copyright © 2014 ESP56.com All Rights Reserved
执行时间: 0.0071289539337158 seconds