忙碌的死龙 发表于 2023-6-2 23:06:01

98行代码实现Web控制LED灯颜色以及获取SHT30温湿度数据

本帖最后由 忙碌的死龙 于 2023-6-27 15:14 编辑


98行代码,只计算了circuitPython的服务端代码,web控制使用Vue + tailwin css开发界面,压缩包已发附件,自行按前面教程配置好circuitPython的wifi设置按后面的教程做就可以了。

https://www.bilibili.com/video/BV11X4y1b77u/?vd_source=8273c27a0c36349999e7d90327ffbf7a

首先看一下成品的样子,本项目实现的内容如下:
1、使用circuitPython部署了mDNS服务,在手机端和电脑端可使用myesp32.local访问控制页面(也可以使用IP地址访问)
2、使用circuitPython实现了基于http服务,包括web控制程序的index.html等文件
3、使用circuitPython实现了用http修改LED灯状态,获取MCU温度和主频,获取sht30温湿度传感器数据







在自己的开发板上部署该服务的步骤:

1、按上一个教程的步骤,配置好wifi编辑页面,能正确访问。教程地址:https://mc.dfrobot.com.cn/thread-316202-1-1.html2、用wifi编辑页面,打开/settings.toml文件,并按以下内容调整,WIFI信息不用按图片改,改了就连不上!!!!!


3、下载附件的压缩包,并解压缩后上传至开发板上
4、使用IO0连接WS2812的IN引脚,IO4和IO5连接SHT30模块的SCL和SDA引脚
5、在手机上打开myesp32.local,就可以开始体验了

circuitPython的源码如下(code.py):

```
import mdns
import socketpool
import wifi
import os
import json
import microcontroller

from adafruit_httpserver.mime_type import MIMEType
from adafruit_httpserver.request import HTTPRequest
from adafruit_httpserver.response import HTTPResponse
from adafruit_httpserver.server import HTTPServer

ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")

print("Connecting to", ssid)
wifi.radio.connect(ssid, password)
print("Connected to", ssid)

mdns_server = mdns.Server(wifi.radio)
mdns_server.hostname = "myesp32"
mdns_server.advertise_service(service_type="_http", protocol="_tcp", port=80)

pool = socketpool.SocketPool(wifi.radio)
server = HTTPServer(pool, "/static")

import adafruit_sht31d
import busio
import board
import displayio
displayio.release_displays()


# Create sensor object, communicating over the board's default I2C bus
i2c = busio.I2C(board.IO4, board.IO5)# uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C()# For using the built-in STEMMA QT connector on a microcontroller
sensor = adafruit_sht31d.SHT31D(i2c)

import neopixel_write
import digitalio

pin = digitalio.DigitalInOut(board.IO0)
pin.direction = digitalio.Direction.OUTPUT
pixel_off = bytearray()
neopixel_write.neopixel_write(pin, pixel_off)

@server.route("/")
def base(request: HTTPRequest):
    """
    Serve the default index.html file.
    """
    with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response:
      response.send_file("index.html")

# URL parameters can be specified
@server.route("/assets/<my_parameter>")
def route_func(request: HTTPRequest , my_parameter):
    """
    Serve js & css file
    """
    with HTTPResponse(request, content_type=MIMEType.TYPE_TXT) as response:
      response.send_file("/assets/"+my_parameter)

@server.route("/getBoardInfo")
def board_handler(request: HTTPRequest):
    data = {
      "temperature": microcontroller.cpu.temperature,
      "frequency": microcontroller.cpu.frequency,
      "name": os.getenv("BOARD_NAME"),
      "led": "None",
      "pixel": "NEOPIXEL"
    }
    with HTTPResponse(request, content_type=MIMEType.TYPE_JSON) as response:
      response.send(json.dumps(data))

@server.route("/getsht31")
def board_handler(request: HTTPRequest):
    sensor.repeatability = adafruit_sht31d.REP_LOW
    data = {
      "temperature": sensor.temperature,
      "humidity": sensor.relative_humidity
    }
    with HTTPResponse(request, content_type=MIMEType.TYPE_JSON) as response:
      response.send(json.dumps(data))

@server.route("/Pixel/<r>/<g>/<b>")
def pixel_handler_url_params(request: HTTPRequest, r, g, b):
    neopixel_write.neopixel_write(pin, bytearray())
    data = {
      "pixel":
    }
    with HTTPResponse(request, content_type=MIMEType.TYPE_JSON) as response:
      response.send(json.dumps(data))

print(f"Listening on http://{wifi.radio.ipv4_address}")
server.serve_forever(str(wifi.radio.ipv4_address))
```

忙碌的死龙 发表于 2023-6-2 23:19:35



试了几次用嵌入代码导入视频,都不成功,不知道这论坛咋回事儿,直接贴链接吧。https://www.bilibili.com/video/BV11X4y1b77u/?vd_source=8273c27a0c36349999e7d90327ffbf7a

ASH腻 发表于 2023-6-6 16:36:11

https://www.bilibili.com/video/BV11X4y1b77u

你试试视频这里,粘贴url呢?


忙碌的死龙 发表于 2023-6-27 15:15:07

ASH腻 发表于 2023-6-6 16:36
你试试视频这里,粘贴url呢?

非常感谢,现在可以了
页: [1]
查看完整版本: 98行代码实现Web控制LED灯颜色以及获取SHT30温湿度数据