因为经常在办公室里面不知道实际室内温度是多少,所以用ESP32做了一个工具来进行温度&湿度的监测。在之前的文章当中,已经完成了ESP32的数据上云工作,如果要进行温度/湿度的检测。从原理上就是给ESP32连接对应的传感器,并把传感器的数据上报到阿里云物联网平台。

我们先来看看效果

这样的话,每天上班前在家里可以先看看办公室空调是否已经把公司的温度提升上去,如果没有提升上去。那说明可能空调有问题,今日的取暖只能靠抖了。

下面我们说说,这个实现怎么搞。首先在阿里云IOT平台上,对我们之前的产品添加2个功能分别为当前湿度和当前温度。

实现步骤如下:

  1. 根据所使用的硬件,进行board.json的配置。 因为我们的温度传感器使用的是sht3x, 使用I2C,在board.json的配置如下:
{
"name": "haasedu",
"version": "1.0.0",
"io": {
"sht3x": {
"type": "I2C",
"port": 0,
"addrWidth": 7,
"freq": 400000,
"mode": "master",
"devAddr": 68
}
},
"debugLevel": "ERROR",
"repl": "disable"
}
  1. 实现代码
from driver import I2C
import sht3x
def report_iot_data(temperature, humidity ):
upload_data = {'params': ujson.dumps({
'CurrentHumidity': humidity, 'CurrentTemperature':temperature
})
}
device.postProps(upload_data)
print('UPDATE IOT SUCCESS!!!') def get_light_temp_humi(): temperature = humitureDev.getTemperature()
humidity = humitureDev.getHumidity()
report_iot_data(temperature, humidity)
return temperature, humidity
i2cObj = I2C()
i2cObj.open("sht3x")
humitureDev = sht3x.SHT3X(i2cObj) while True:
data = get_light_temp_humi()
utime.sleep(3)

进行烧录就可以了。

获取温度湿润还需要使用Haas团队写的一个驱动,代码如下

sht3x.py

"""
Copyright (C) 2015-2021 Alibaba Group Holding Limited
MicroPython's driver for CHT8305
Author: HaaS
Date: 2021/09/14
"""
from micropython import const
import utime
from driver import I2C
'''
# sht3x commands definations
# read serial number: CMD_READ_SERIALNBR 0x3780
# read status register: CMD_READ_STATUS 0xF32D
# clear status register: CMD_CLEAR_STATUS 0x3041
# enabled heater: CMD_HEATER_ENABLE 0x306D
# disable heater: CMD_HEATER_DISABLE 0x3066
# soft reset: CMD_SOFT_RESET 0x30A2
# accelerated response time: CMD_ART 0x2B32
# break, stop periodic data acquisition mode: CMD_BREAK 0x3093
# measurement: polling, high repeatability: CMD_MEAS_POLLING_H 0x2400
# measurement: polling, medium repeatability: CMD_MEAS_POLLING_M 0x240B
# measurement: polling, low repeatability: CMD_MEAS_POLLING_L 0x2416
'''
class SHT3X(object):
# i2cDev should be an I2C object and it should be opened before __init__ is called
def __init__(self, i2cDev):
self._i2cDev = None
if not isinstance(i2cDev, I2C):
raise ValueError("parameter is not an I2C object")
# make AHB21B's internal object points to _i2cDev
self._i2cDev = i2cDev
self.start()
def start(self):
# make sure AHB21B's internal object is valid before I2C operation
if self._i2cDev is None:
raise ValueError("invalid I2C object")
# send clear status register command - 0x3041 - CMD_CLEAR_STATUS
cmd = bytearray(2)
cmd[0] = 0x30
cmd[1] = 0x41
self._i2cDev.write(cmd)
# wait for 20ms
utime.sleep_ms(20)
return 0
def getTempHumidity(self):
if self._i2cDev is None:
raise ValueError("invalid I2C object")
tempHumidity = [-1, 2] # start measurement: polling, medium repeatability - 0x240B - CMD_MEAS_POLLING_M
# if you want to adjust measure repeatability, you can send the following commands:
# high repeatability: 0x2400 - CMD_MEAS_POLLING_H
# low repeatability: 0x2416 - CMD_MEAS_POLLING_L
cmd = bytearray(2)
cmd[0] = 0x24
cmd[1] = 0x0b
self._i2cDev.write(cmd)
# must wait for a little before the measurement finished
utime.sleep_ms(20)
dataBuffer = bytearray(6)
# read the measurement result
self._i2cDev.read(dataBuffer)
# print(dataBuffer)
# calculate real temperature and humidity according to SHT3X-DIS' data sheet
temp = (dataBuffer[0]<<8) | dataBuffer[1]
humi = (dataBuffer[3]<<8) | dataBuffer[4]
tempHumidity[1] = humi * 0.0015259022
tempHumidity[0] = -45.0 + (temp) * 175.0 / (0xFFFF - 1)
return tempHumidity
def getTemperature(self):
data = self.getTempHumidity()
return data[0]
def getHumidity(self):
data = self.getTempHumidity()
return data[1]
def stop(self):
if self._i2cDev is None:
raise ValueError("invalid I2C object")
# stop periodic data acquisition mode
cmd = bytearray(3)
cmd[0] = 0x30
cmd[1] = 0x93
self._i2cDev.write(cmd)
# wait for a little while
utime.sleep_ms(20)
self._i2cDev = None
return 0
def __del__(self):
print('sht3x __del__')
if __name__ == "__main__":
'''
The below i2c configuration is needed in your board.json.
"sht3x": {
"type": "I2C",
"port": 1,
"addrWidth": 7,
"freq": 400000,
"mode": "master",
"devAddr": 68
},
'''
print("Testing sht3x ...")
i2cDev = I2C()
i2cDev.open("sht3x")
sht3xDev = SHT3X(i2cDev)
'''
# future usage:
i2cDev = I2C("sht3x")
sht3xDev = sht3x.SHT3X(i2cDev)
'''
temperature = sht3xDev.getTemperature()
print("The temperature is: %f" % temperature)
humidity = sht3xDev.getHumidity()
print("The humidity is: %f" % humidity)
print("Test sht3x done!")

将程序代码烧录到开发板后,设备会联网,并且每3秒上报一次数据。发布的数据, 我们可以在阿里云物联网平台上的设备的物模型数据中看到。



关于board.json的部分,需要根据自己采用的温度/湿度传感器,调整对应的GPIO的编号就行。

(3)ESP32 Python 制作一个办公室温度计的更多相关文章

  1. 用 Python 制作一个艺术签名小工具,给自己设计一个优雅的签名

    生活中有很多场景都需要我们签字(签名),如果是一些不重要的场景,我们的签名好坏基本无所谓了,但如果是一些比较重要的场景,如果我们的签名比较差的话,就有可能给别人留下不太好的印象了,俗话说字如其人嘛,本 ...

  2. 利用Python制作一个只属于和她的聊天器,再也不用担心隐私泄露啦!

    ------------恢复内容开始------------ 是否担心微信的数据流会被监视?是否担心你和ta聊天的小秘密会被保存到某个数据库里?没关系,现在我们可以用Python做一个只属于你和ta的 ...

  3. python制作一个简单的中奖系统

    注释: 展示图下的代码,我是用pycharm写的,是python解释器中的一种,本课没不同解释器的要求,可根据自己喜欢的解释器编写. 步骤: 本期给大家带来的是,一个简单的中奖系统,首先打开自己电脑上 ...

  4. python制作一个简单词云

    首先需要安装三个包:# 安装:pip install matplotlib# 安装:pip install jieba# 安装pip install wordcloud 1.制作英文字母的词云 效果图 ...

  5. 3分钟教你用python制作一个简单词云

    首先需要安装三个包: # 安装:pip install matplotlib # 安装:pip install jieba # 安装pip install wordcloud 1.制作英文字母的词云 ...

  6. 使用Python制作一个简单的刷博器

    呵呵,不得不佩服Python的强大,寥寥几句代码就能做一个简单的刷博器. import webbrowser as web import time import os count=0 while co ...

  7. Python学习之旅:用Python制作一个打字训练小工具

    一.写在前面 说道程序员,你会想到什么呢?有人认为程序员象征着高薪,有人认为程序员都是死肥宅,还有人想到的则是996和 ICU. 别人眼中的程序员:飞快的敲击键盘.酷炫的切换屏幕.各种看不懂的字符代码 ...

  8. python制作一个小型翻译软件

    from urllib import parse,request import requests,re,execjs,json,time 英语查词翻译 class Tencent(): def ini ...

  9. python制作exe可执行文件的方法---使用pyinstaller

    python制作exe可执行文件的方法---使用pyinstaller   python生成windows下exe格式的可执行程序有三种可选方案: py2exe是大家所熟知的,今天要介绍pyinsta ...

随机推荐

  1. MLNX网卡驱动安装

    安装/升级MLNX驱动 1. 安装准备 驱动下载地址:https://www.mellanox.com/products/ethernet-drivers/linux/mlnx_en 选择和系统版本匹 ...

  2. fcntl 加锁模块

    #!/usr/bin/python # coding:utf8 import os import sys import time import fcntl # 导入模块 class FLOCK(obj ...

  3. 请注意JS方法,方法同名,参数个数不一样是不能区分方法的,

    请注意JS方法,方法同名,参数个数不一样是不能区分方法的, 所以要区分方法,只能利用方法名不同来区分,而不能利用参数个数与参数类型来分.

  4. uniapp+nvue实现仿微信/得物相册插件:选择界面 +自定义相册+图片视频过滤

    本篇文章基于uniapp 框架+ nvue,实现了uniapp仿微信/得物相册选择功能实例项目,该插件实例实现了以下功能: 1: 相册过滤 2: 图视频过滤 3: 界面UI定制化 4: 栅格列数定制化 ...

  5. 如何下载哔哩哔哩、爱奇艺、腾讯视频、优酷、斗鱼、TED、YouTube网页视频

    这里使用you-get工具进行下载 github地址:https://github.com/soimort/you-get/ github项目文档:https://github.com/soimort ...

  6. vc++ 调用winapi调节屏幕亮度(增加win7代码demo)

    1.关于 代码是通过测试的,测试环境: win7 + MFC 为什么要发在这里? 区别于上一篇随笔. MD排版更顺眼 demo 会放到 这里 更正了上一篇随笔中的代码错误 2.头文件 #include ...

  7. 【LeetCode】1085. Sum of Digits in the Minimum Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  8. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  9. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  10. 安装并配置 Android Studio 开发工具和 Genymotion 模拟器

    需求说明: 安装并配置 Android Studio 开发工具和 Genymotion 模拟器. 熟练使用 Genymotion 模拟器,掌握 Genymotion 模拟器的基本设置和程序安装. 实现 ...