|
|
爱科技、爱创意、爱折腾、爱极致,我们都是技术控
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 hackhack 于 2026-6-29 12:32 编辑
趁着有时间,把之前做的另一个项目也发了吧。
思路很简单:
DX-WF25通过接入人体检测器模块(接入GPIO3,LD2402有人返回高电压,无人返回低电压)统计人在座位上的时间,再通过GPIO8的led(PWM模式)亮灭和闪烁提示状态,构成一个提示定时休息的机器。
为了方便改代码,这个项目是用的python写的。
- # 创建时间:2025-12-25
- # 功能:DXWF-25+DL2402模块,实现定时久坐休息提醒
- # 版本:v1.0
- from machine import Pin, PWM, freq
- import time
- import network
- import webrepl
- import esp
- # ===================== 低功耗设置=====================
- # 关闭ADC
- # pin0 = Pin(0, Pin.IN, value=0) # ESP8266仅支持PULL_UP
- # ===================== 初始化配置 =====================
- person_pin = Pin(3, Pin.IN, Pin.PULL_UP) # DL2402人体检测引脚(GPIO3,输入模式,默认高电平)
- wifi_pin = Pin(4, Pin.IN, Pin.PULL_UP) # GPIO4用于判断是否开启WIFI与webrepl,默认上拉高电平
- led_pwm = PWM(Pin(8), freq=1000, duty=0) # LED PWM初始化(GPIO8,频率1000Hz)
- # 状态变量初始化
- person_time = 0 # 有人计时(秒)
- no_person_time = 0 # 无人计时(秒)
- led_state = "no_person" # led状态:初始无人
- # 时间阈值配置(秒)
- PERSON_ALARM_THRESHOLD = 45*60 # 持续有人45分钟触发报警
- NO_PERSON_RESET_THRESHOLD = 9*60 # 持续无人9分钟重置有人计时;持续有人9分钟重置无人计时;
- # wifi信息
- WIFI_SSID = "wifidd" # WiFi名称
- WIFI_PWD = "12345678900"# WiFi密码
- # flag标记
- webrepl_running = False
- # ===================== 功能函数部分 =====================
- def disable_wifi_webrepl():
- global webrepl_running
- """
- 功能1:关闭WEBREPL、关闭WIFI
- """
- # 关闭WEBREPL
- try:
- webrepl.stop()
- # print("WEBREPL已关闭")
- webrepl_running = False
- except Exception as e:
- # print(f"关闭WEBREPL失败: {e}")
- pass
-
- # 关闭WIFI(STA和AP模式都关闭)
- wlan_sta = network.WLAN(network.STA_IF)
- wlan_ap = network.WLAN(network.AP_IF)
-
- if wlan_sta.active():
- wlan_sta.active(False)
- # print("STA模式WiFi已关闭")
- if wlan_ap.active():
- wlan_ap.active(False)
- # print("AP模式WiFi已关闭")
- def enable_wifi_webrepl():
- global webrepl_running
- """
- 功能2:打开WIFI(STA模式)、打开WEBREPL
- """
- # 启动WIFI(STA模式连接路由器)
- wlan_sta = network.WLAN(network.STA_IF)
- if not wlan_sta.active():
- wlan_sta.active(True)
-
- # 连接WiFi(如果未连接)
- if not wlan_sta.isconnected():
- # print(f"正在连接WiFi: {WIFI_SSID}")
- wlan_sta.connect(WIFI_SSID, WIFI_PWD)
-
- # 等待连接(超时10秒)
- timeout = 10
- while not wlan_sta.isconnected() and timeout > 0:
- time.sleep(1)
- timeout -= 1
-
- if wlan_sta.isconnected():
- ip_info = wlan_sta.ifconfig()
- # print(f"WiFi连接成功!IP: {ip_info[0]}")
- # else:
- # print("WiFi连接超时!")
- else:
- ip_info = wlan_sta.ifconfig()
- # print(f"WiFi已连接,IP: {ip_info[0]}")
- # 启动WEBREPL(默认密码:1234,建议修改)
- if not webrepl_running:
- try:
- webrepl.start(password="1234") # 可修改密码
- # print("WEBREPL已启动,端口:8266")
- webrepl_running = True
- except Exception as e:
- # print(f"启动WEBREPL失败: {e}")
- pass
- def update_person_state():
- """更新人体检测状态及累计时间"""
- global person_time, no_person_time, led_state
- # 检测人体传感器状态(高电平=有人)
- if person_pin.value() == 1:
- led_state = "person"
- person_time += 1
- else:
- led_state = "no_person"
- no_person_time += 1
- def check_alarm_condition():
- """检查报警/重置条件"""
- global person_time, no_person_time, led_state
- # 条件1:有人超过xx分钟 → 触发报警
- if person_time > PERSON_ALARM_THRESHOLD:
- led_state = "alarm"
- # 条件2:无人时间超过(久坐时间每5分钟需要1分钟休息时间) → 重置所有计时
- if no_person_time > (NO_PERSON_RESET_THRESHOLD+round((person_time - PERSON_ALARM_THRESHOLD) / 300)*60):
- person_time = 0
- no_person_time = 0
- # 有人每过5分钟 → 无人时间清0
- if person_time % 300 == 0:
- no_person_time = 0
- def control_led_by_state():
- """根据状态控制LED,注意取反值"""
- if led_state == "alarm":
- # 报警:高亮闪烁(亮0.5秒,灭0.5秒)
- led_pwm.duty(0) # 最大亮度(10位PWM:0-1023)
- time.sleep(0.5)
- led_pwm.duty(1023)
- time.sleep(0.5)
- if led_state == "person":
- # 有人:低亮度常亮(30%亮度)
- led_pwm.duty(720)
- time.sleep(1)
- if led_state == "no_person":
- # 无人:熄灭
- led_pwm.duty(1023)
- time.sleep(1)
- # ===================== 主循环 =====================
- if __name__ == "__main__":
- # print("定时休息提示器启动...")
- try:
- while True:
- if wifi_pin.value() == 1:
- enable_wifi_webrepl()
- else:
- disable_wifi_webrepl()
- update_person_state() # 更新人体状态
- check_alarm_condition() # 检查报警/重置条件
- control_led_by_state() # 控制LED
- # 打印调试信息
- # print(f"{led_state},{person_time}s,{no_person_time}s;",end='')
- except KeyboardInterrupt:
- # 终止程序时关闭PWM
- led_pwm.deinit()
- led = Pin(8, Pin.OUT) # 重新初始化 GPIO8 为数字输出
- led.value(0) # LED常亮以方便看设备是否开启
- print("程序终止!")
复制代码
|
|