admin管理员组文章数量:1621117
文章目录
- esp32连接海萤物联网显示南京室外温度
- 介绍
- 硬件连接
- 温度检测节点
- 温度显示节点
- 温度检测节点提供的服务
- 开源
- 本节点地址
- 服务
- 读取南京室外温度
- 源码
- 温度显示节点显示温度值
- 开源
- 源码
- 在电脑上访问温度服务
- 使用golang获取南京室外温度
- 使用python获取南京室外温度
- 参考资料
esp32连接海萤物联网显示南京室外温度
本文博客链接:http://blog.csdn/jdh99,作者:jdh,转载请注明.
欢迎前往社区交流:海萤物联网社区
在线文档地址
介绍
春季天气多变,一会热一会冷,手机上查看的温度不是实时室外温度。手头有一些esp32的开发板和温度传感器,所以想做个东西检测实时室外温度。于是做了两个节点:
- 温度检测节点。连接海萤物联网,提供温度服务
- 温度显示节点。连接海萤物联网,获取当前的室外温度,并显示在oled屏幕上
温度检测节点安装在窗口,将温度传感器放在室外检测温度。温度显示节点安装在床头,起床时可以看到温度。
温度检测节点提供南京江北新区的室外温度,面向海萤物联网全网提供服务。欢迎大家自己制作设备,检测各自城市的温度,可以做成服务发布在社区里共享给大家访问。
硬件连接
温度检测节点
元件 | 说明 | 采购链接 |
---|---|---|
esp32开发板 | ESP DEVKITV1 | https://item.taobao/item.htm?spm=a1z09.2.0.0.e81e2e8dP9D3Zl&id=544831023710&_u=842t6s0fd91 |
ds18b20传感器 | 防水传感器 | https://detail.tmall/item.htm?id=39449740131&spm=a1z09.2.0.0.e81e2e8dP9D3Zl&_u=842t6s08c03 |
ds18b20传感器引脚定义:
引脚 | 定义 |
---|---|
红色 | 电源 |
黑色 | 地 |
黄色 | 信号线 |
注意:信号线和电源线之间需接上拉电阻,电阻值可选1K-100K欧姆,本节点选择了5.1K电阻。
将信号线接到esp32的GPIO4上:
温度显示节点
元件 | 说明 | 采购链接 |
---|---|---|
esp32开发板 | ESP DEVKITV1 | https://item.taobao/item.htm?spm=a1z09.2.0.0.e81e2e8dP9D3Zl&id=544831023710&_u=842t6s0fd91 |
oled屏 | 0.96寸,i2c接口 | https://detail.tmall/item.htm?id=534583545403&spm=a1z09.2.0.0.e81e2e8dP9D3Zl&_u=842t6s0a8fd |
接线方法:
温度检测节点提供的服务
我在南京江北新区,所以温度检测节点提供南京江北新区的室外温度服务。受益于海萤物联网的互联互通特性,只要连接了海萤物联网联网,任何节点可以是软件或者是设备,都可以获取此温度节点提供的温度信息。
开源
- 温度检测服务在github上的项目地址
- 温度检测服务在gitee上的项目地址
本节点地址
0x2141000000010001
服务
服务号 | 服务 |
---|---|
1 | 读取南京室外温度 |
读取南京室外温度
-
CON请求:空
-
ACK应答:
字段 | 字节数 |
---|---|
带符号温度值.分度0.1摄氏度 | 2 |
应答的是字节流,大端格式。温度分度是0.1摄氏度,所以假设两个字节是0x00 0xe8,则对应的温度是0x00e8=232=23.2摄氏度。
温度值是带符号的数,所以如果温度大于0x8000,则温度为负数。C语言直接使用int16_t强制转换即可,其他语言可以使用0x10000-温度值算出温度值。
比如温度值0xff18,使用0x10000-0xff18=232,所以温度是-23.2摄氏度。
源码
"""
Copyright 2021-2021 The jdh99 Authors. All rights reserved.
室外温度检测服务
Authors: jdh99 <jdh821@163>
"""
import tziot
import time
import machine, onewire, ds18x20
# 本节点地址和密码
IA = 0x2141000000010001
PWD =
# WIFI账号密码
WIFI_SSID = 'JDHOME_MASTER'
WIFI_PWD =
# 服务号
# 读取温度
RID_GET_TEMP = 1
# 温度传感器
ds_sensor = 0
rom = None
# 当前温度.分度0.1摄氏度
temp_now = 0
def main():
# 初始化温度传感器
init_temp_sensor()
# 连接wifi
connect_wifi()
tziot.bind_pipe_net(IA, PWD, '0.0.0.0', 12025)
tziot.register(RID_GET_TEMP, get_temp_service)
tziot.run(app)
def init_temp_sensor():
global ds_sensor, rom
ds_pin = machine.Pin(4)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_sensor.scan()
print('Found DS devices: ', roms)
if len(roms) == 0:
# 检测不到温度传感器则复位
time.sleep(10)
machine.reset()
rom = roms[0]
def connect_wifi():
print('connect wifi')
ok = tziot.connect_wifi(WIFI_SSID, WIFI_PWD)
if ok is False:
print('connect wifi failed')
machine.reset()
print('connect wlan success')
def get_temp_service(pipe: int, src_ia: int, req: bytearray) -> (bytearray, int):
"""读取温度服务"""
global temp_now
print('src ia=0x%x' % src_ia)
data = bytearray()
data.append((temp_now >> 8) & 0xff)
data.append(temp_now & 0xff)
return data, 0
def app():
"""业务程序.每10s检测一次温度"""
global ds_sensor, rom, temp_now
run_time = 0
while True:
ds_sensor.convert_temp()
time.sleep(1)
temp_now = int(ds_sensor.read_temp(rom) * 10)
print('temp:', temp_now)
# 转换成int8
if temp_now < 0:
temp_now = 0x10000 + temp_now
time.sleep(10)
# 每半小时定时复位
run_time += 10
if run_time >= 1800:
print('reset system!')
machine.reset()
if __name__ == '__main__':
main()
温度显示节点显示温度值
此节点从两个海萤物联网的服务获取信息:
- 从ntp服务获取当前时间
- 从温度检测服务获取室外温度
效果如下图:
MicroPython驱动oled屏幕显示不同字号的英文和汉字,可以参考这篇文章:esp32使用MicroPython驱动oled屏显示中文和英文
开源
- 温度检测服务在github上的项目地址
- 温度检测服务在gitee上的项目地址
源码
"""
Copyright 2021-2021 The jdh99 Authors. All rights reserved.
室外温度显示
Authors: jdh99 <jdh821@163>
"""
import tziot
import time
import ssd1306py as lcd
import machine
# 本节点地址和密码
IA = 0x2141000000010002
PWD =
# WIFI账号密码
WIFI_SSID = 'JDHOME_MASTER'
WIFI_PWD =
# 服务号
# 读取NTP网络时间
RID_GET_TIME = 1
# 读取温度
RID_GET_TEMP = 1
# 通信管道
pipe = 0
def main():
global pipe
# 初始化OLED屏
init_oled()
# 连接wifi
connect_wifi()
pipe = tziot.bind_pipe_net(IA, PWD, '0.0.0.0', 12025)
tziot.run(app)
def init_oled():
lcd.init_i2c(22, 21, 128, 64)
lcd.text('start system', 0, 0, 16)
lcd.text('please wait...', 0, 20, 16)
lcd.show()
def connect_wifi():
print('connect wifi')
ok = tziot.connect_wifi(WIFI_SSID, WIFI_PWD)
if ok is False:
print('connect wifi failed')
machine.reset()
print('connect wlan success')
def app():
"""业务程序.每分钟获取ntp时间和温度值"""
global pipe
# 连接物联网
fail_num = 0
while True:
if fail_num >= 10:
print('connect too many.reset!')
machine.reset()
if not tziot.is_conn():
fail_num += 1
time.sleep(10)
continue
break
print('connect ok')
lcd.clear()
lcd.text('connect ok', 0, 20, 16)
lcd.show()
# 周期业务
fail_num = 0
run_time = 0
while True:
if fail_num >= 10:
print('fail too many.reset!')
machine.reset()
lcd.clear()
# 获取ntp网络时间
resp, err = tziot.call(pipe, 0x2141000000000004, RID_GET_TIME, 5000, bytearray())
if err != 0:
fail_num += 1
continue
lcd.text(tziot.bytearray_to_str(resp), 0, 0, 8)
# 获取南京室外温度
resp, err = tziot.call(pipe, 0x2141000000010001, RID_GET_TEMP, 5000, bytearray())
if err != 0 or len(resp) != 2:
fail_num += 1
continue
fail_num = 0
temp = (resp[0] << 8) + resp[1]
if temp >= 0x8000:
temp = 0x10000 - temp
temp = temp / 10
lcd.text('%.1f' % temp, 30, 20, 32)
lcd.show()
time.sleep(60)
# 每半小时定时复位
run_time += 60
if run_time >= 1800:
print('reset system!')
machine.reset()
if __name__ == '__main__':
main()
在电脑上访问温度服务
海萤物联网的节点可以是软件也可以是硬件,温度检测服务面向全网提供服务,所以也可以用电脑获取温度。大家感兴趣可以申请IA地址,然后在电脑上获取温度服务试试。海萤物联网教程:IA地址格式及地址申请方法
使用golang获取南京室外温度
package main
import (
"fmt"
"github/jdhxyy/tziot"
)
func main() {
pipe, _ := tziot.BindPipeNet(0x2141000000000401, "abc123", "192.168.1.119", 12021)
for tziot.IsConn() == false{}
resp, err := tziot.Call(pipe, 0x2141000000010001, 1, 3000, nil)
if err != 0 {
return
}
temp := (resp[0] << 8) + resp[1]
fmt.Println(float32(temp) / 10)
}
输出:
16.3
使用python获取南京室外温度
import tziot
def main():
pipe = tziot.bind_pipe_net(0x2141000000000401, pwd, '192.168.1.119', 12021)
while not tziot.is_conn():
pass
resp, err = tziot.call(pipe, 0x2141000000010001, 1, 3000, bytearray())
if err != 0:
return
temp = (resp[0] << 8) + resp[1]
print(temp / 10)
if __name__ == '__main__':
main()
输出:
16.3
参考资料
- MicroPython: DS18B20 Temperature Sensor with ESP32 and ESP8266
版权声明:本文标题:esp32连接海萤物联网显示南京室外温度 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1728819964a1175300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论