手机温度监测仪

知识:温湿度传感器 和 ...

知识:温湿度传感器 和 ADC 输入
模拟输入:
CircuitPython Analog In | CircuitPython Essentials | Adafruit Learning System

ADC(模拟数字转换器):是一种将模拟信号转换为数字信号的电子设备。

温湿度传感器:将温度和湿度  转换为  电信号
blank

任务一:测量温度值

import time
from board import IO5
import adafruit_dht as dht

# 初始化dht11设备,数据引脚连接到 IO5
dhtDevice = dht.DHT11(IO5)

while True:
    # 输出 温湿度数据
    temperature_c = dhtDevice.temperature
    humidity = dhtDevice.humidity
    print(f"温度:{temperature_c:.1f}°C    湿度: {humidity}%")
        
    time.sleep(2.0)

任务二:开发板联网WIFI 

import wifi

# 你的Wi-Fi网络名称和密码
ssid = 'bcm'
password = 'bcm****'

while not wifi.radio.connected:
    wifi.radio.connect(ssid, password)
    
ip = wifi.radio.ipv4_address
print(f"✅ Wifi!  IP地址:{ip}")

 

任务三:开发板连接到MQTT服务器发送测试数据

import wifi
import adafruit_connection_manager as conn
import adafruit_requests

# 你的Wi-Fi网络名称和密码
ssid = 'bcm'
password = 'bcm****'
uid="b97314f5d26b4b68a035d8383e429260"
topic = 'tempMonitor'
msg='测试消息'

# 连接到 Wi-Fi
print("正在连接 WiFi...")
while not wifi.radio.ipv4_address:
    try:
        wifi.radio.connect(ssid, password)
    except ConnectionError as e:
        print("❌ 连接失败:", e)
        print("请稍后重试")  
ip = wifi.radio.ipv4_address
print(f"✅ Wifi!  IP地址:{ip}")

# 初始化 Wifi, Socket Pool, Request Session
pool = conn.get_radio_socketpool(wifi.radio)
ssl_context = conn.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)

# 请求参数
url = 'https://apis.bemfa.com/va/postmsg'
data = {
    "uid":uid,
    "topic":topic,
    "type":1,
    "msg":msg
    }
# 发送请求
print(f"发送消息到主题: {topic}")
with requests.post(url,  data=data) as response:
    print(f" | ✅ 返回内容: {response.json()}" 

综合任务:手机温度监测仪

手机温度计人体排卵期 浴缸温度计多功能智能鱼缸温度计-阿里巴巴

import time
from board import IO5
import adafruit_dht as dht

import wifi
import adafruit_connection_manager as conn
import adafruit_requests

# 初始化dht11设备,数据引脚连接到 IO5
dhtDevice = dht.DHT11(IO5)

# 你的Wi-Fi网络名称和密码
ssid = 'bcm'
password = 'bcm****'
uid="b97314f5d26b4b68a035d8383e429260"
topic = 'tempMonitor'
msg=29

# 连接到 Wi-Fi
print("正在连接 WiFi...")
while not wifi.radio.ipv4_address:
    try:
        wifi.radio.connect(ssid, password)
    except ConnectionError as e:
        print("❌ 连接失败:", e)
        print("请稍后重试")  
ip = wifi.radio.ipv4_address
print(f"✅ Wifi!  IP地址:{ip}")

# 初始化 Wifi, Socket Pool, Request Session
pool = conn.get_radio_socketpool(wifi.radio)
ssl_context = conn.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)

def send_msg(topic,msg):
    # 请求参数
    url = 'https://apis.bemfa.com/va/postmsg'
    data = {
        "uid":uid,
        "topic":topic,
        "type":1,
        "msg":msg
        }

    print(f"发送消息到主题: {topic}")
    # 发送请求
    with requests.post(url,  data=data) as response:
        print(f" | ✅ 返回内容: {response.json()}")
    

while True:
    # 输出 温湿度数据
    temperature_c = dhtDevice.temperature
    humidity = dhtDevice.humidity
    print(f"温度:{temperature_c:.1f}°C    湿度: {humidity}%")
    
    # 发送数据到mqtt
    send_msg(topic,msg=temperature_c)
        
    time.sleep(6)

附件

lib(hid,dht,conn,requests).zip
尺寸: 18.62 KB