
文章目录esp32连接海萤物联网显示南京室外温度介绍硬件连接温度检测节点温度显示节点温度检测节点提供的服务开源本节点地址服务读取南京室外温度源码温度显示节点显示温度值开源源码在电脑上访问温度服务使用golang获取南京室外温度使用python获取南京室外温度参考资料esp32连接海萤物联网显示南京室外温度本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.在线文档地址介绍春季天气多变一会热一会冷手机上查看的温度不是实时室外温度。手头有一些esp32的开发板和温度传感器所以想做个东西检测实时室外温度。于是做了两个节点温度检测节点。连接海萤物联网提供温度服务温度显示节点。连接海萤物联网获取当前的室外温度并显示在oled屏幕上温度检测节点安装在窗口将温度传感器放在室外检测温度。温度显示节点安装在床头起床时可以看到温度。温度检测节点提供南京江北新区的室外温度面向海萤物联网全网提供服务。欢迎大家自己制作设备检测各自城市的温度可以做成服务发布在社区里共享给大家访问。硬件连接温度检测节点元件说明采购链接esp32开发板ESP DEVKITV1https://item.taobao.com/item.htm?spma1z09.2.0.0.e81e2e8dP9D3Zlid544831023710_u842t6s0fd91ds18b20传感器防水传感器https://detail.tmall.com/item.htm?id39449740131spma1z09.2.0.0.e81e2e8dP9D3Zl_u842t6s08c03ds18b20传感器引脚定义引脚定义红色电源黑色地黄色信号线注意信号线和电源线之间需接上拉电阻电阻值可选1K-100K欧姆本节点选择了5.1K电阻。将信号线接到esp32的GPIO4上温度显示节点元件说明采购链接esp32开发板ESP DEVKITV1https://item.taobao.com/item.htm?spma1z09.2.0.0.e81e2e8dP9D3Zlid544831023710_u842t6s0fd91oled屏0.96寸i2c接口https://detail.tmall.com/item.htm?id534583545403spma1z09.2.0.0.e81e2e8dP9D3Zl_u842t6s0a8fd接线方法温度检测节点提供的服务我在南京江北新区所以温度检测节点提供南京江北新区的室外温度服务。受益于海萤物联网的互联互通特性只要连接了海萤物联网联网任何节点可以是软件或者是设备都可以获取此温度节点提供的温度信息。开源温度检测服务在github上的项目地址温度检测服务在gitee上的项目地址本节点地址0x2141000000010001服务服务号服务1读取南京室外温度读取南京室外温度CON请求空ACK应答字段字节数带符号温度值.分度0.1摄氏度2应答的是字节流大端格式。温度分度是0.1摄氏度所以假设两个字节是0x00 0xe8则对应的温度是0x00e823223.2摄氏度。温度值是带符号的数所以如果温度大于0x8000则温度为负数。C语言直接使用int16_t强制转换即可其他语言可以使用0x10000-温度值算出温度值。比如温度值0xff18使用0x10000-0xff18232所以温度是-23.2摄氏度。源码 Copyright 2021-2021 The jdh99 Authors. All rights reserved. 室外温度检测服务 Authors: jdh99 jdh821163.com importtziotimporttimeimportmachine,onewire,ds18x20# 本节点地址和密码IA0x2141000000010001PWD# WIFI账号密码WIFI_SSIDJDHOME_MASTERWIFI_PWD# 服务号# 读取温度RID_GET_TEMP1# 温度传感器ds_sensor0romNone# 当前温度.分度0.1摄氏度temp_now0defmain():# 初始化温度传感器init_temp_sensor()# 连接wificonnect_wifi()tziot.bind_pipe_net(IA,PWD,0.0.0.0,12025)tziot.register(RID_GET_TEMP,get_temp_service)tziot.run(app)definit_temp_sensor():globalds_sensor,rom ds_pinmachine.Pin(4)ds_sensords18x20.DS18X20(onewire.OneWire(ds_pin))romsds_sensor.scan()print(Found DS devices: ,roms)iflen(roms)0:# 检测不到温度传感器则复位time.sleep(10)machine.reset()romroms[0]defconnect_wifi():print(connect wifi)oktziot.connect_wifi(WIFI_SSID,WIFI_PWD)ifokisFalse:print(connect wifi failed)machine.reset()print(connect wlan success)defget_temp_service(pipe:int,src_ia:int,req:bytearray)-(bytearray,int):读取温度服务globaltemp_nowprint(src ia0x%x%src_ia)databytearray()data.append((temp_now8)0xff)data.append(temp_now0xff)returndata,0defapp():业务程序.每10s检测一次温度globalds_sensor,rom,temp_now run_time0whileTrue:ds_sensor.convert_temp()time.sleep(1)temp_nowint(ds_sensor.read_temp(rom)*10)print(temp:,temp_now)# 转换成int8iftemp_now0:temp_now0x10000temp_now time.sleep(10)# 每半小时定时复位run_time10ifrun_time1800: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 jdh821163.com importtziotimporttimeimportssd1306pyaslcdimportmachine# 本节点地址和密码IA0x2141000000010002PWD# WIFI账号密码WIFI_SSIDJDHOME_MASTERWIFI_PWD# 服务号# 读取NTP网络时间RID_GET_TIME1# 读取温度RID_GET_TEMP1# 通信管道pipe0defmain():globalpipe# 初始化OLED屏init_oled()# 连接wificonnect_wifi()pipetziot.bind_pipe_net(IA,PWD,0.0.0.0,12025)tziot.run(app)definit_oled():lcd.init_i2c(22,21,128,64)lcd.text(start system,0,0,16)lcd.text(please wait...,0,20,16)lcd.show()defconnect_wifi():print(connect wifi)oktziot.connect_wifi(WIFI_SSID,WIFI_PWD)ifokisFalse:print(connect wifi failed)machine.reset()print(connect wlan success)defapp():业务程序.每分钟获取ntp时间和温度值globalpipe# 连接物联网fail_num0whileTrue:iffail_num10:print(connect too many.reset!)machine.reset()ifnottziot.is_conn():fail_num1time.sleep(10)continuebreakprint(connect ok)lcd.clear()lcd.text(connect ok,0,20,16)lcd.show()# 周期业务fail_num0run_time0whileTrue:iffail_num10:print(fail too many.reset!)machine.reset()lcd.clear()# 获取ntp网络时间resp,errtziot.call(pipe,0x2141000000000004,RID_GET_TIME,5000,bytearray())iferr!0:fail_num1continuelcd.text(tziot.bytearray_to_str(resp),0,0,8)# 获取南京室外温度resp,errtziot.call(pipe,0x2141000000010001,RID_GET_TEMP,5000,bytearray())iferr!0orlen(resp)!2:fail_num1continuefail_num0temp(resp[0]8)resp[1]iftemp0x8000:temp0x10000-temp temptemp/10lcd.text(%.1f%temp,30,20,32)lcd.show()time.sleep(60)# 每半小时定时复位run_time60ifrun_time1800:print(reset system!)machine.reset()if__name____main__:main()在电脑上访问温度服务海萤物联网的节点可以是软件也可以是硬件温度检测服务面向全网提供服务所以也可以用电脑获取温度。大家感兴趣可以申请IA地址然后在电脑上获取温度服务试试。海萤物联网教程IA地址格式及地址申请方法使用golang获取南京室外温度packagemainimport(fmtgithub.com/jdhxyy/tziot)funcmain(){pipe,_:tziot.BindPipeNet(0x2141000000000401,abc123,192.168.1.119,12021)fortziot.IsConn()false{}resp,err:tziot.Call(pipe,0x2141000000010001,1,3000,nil)iferr!0{return}temp:(resp[0]8)resp[1]fmt.Println(float32(temp)/10)}输出16.3使用python获取南京室外温度importtziotdefmain():pipetziot.bind_pipe_net(0x2141000000000401,pwd,192.168.1.119,12021)whilenottziot.is_conn():passresp,errtziot.call(pipe,0x2141000000010001,1,3000,bytearray())iferr!0:returntemp(resp[0]8)resp[1]print(temp/10)if__name____main__:main()输出16.3参考资料MicroPython: DS18B20 Temperature Sensor with ESP32 and ESP8266