Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。
Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台
Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。
Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。
import utime import ahtx0 import network import urequests import ujson import machine from machine import RTC, I2C, Pin from machine import reset from machine import WDT import time import sys import os wdt = WDT(timeout=20000) # enable it with a timeout of 2s #import gc #gc.enable() # I2C for the Wemos D1 Mini with ESP8266 i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000) # 注意例程居然是反的,可能是针对其他开发板吧 # Create the sensor object using I2C sensor = ahtx0.AHT10(i2c) # user data ssid = "NBWIFI" # wifi router name pw = "z7758521" # wifi router password url = 'http://www.esp56.com/api/wenshidu/api_write.php' print("Connecting to wifi...") def getx(): import ubinascii import machine client_id = ubinascii.hexlify(machine.unique_id()) return ubinascii.hexlify(machine.unique_id()).decode() print("ID: " + getx() + "\n") print("浏览器查看地址: http://www.esp56.com/api/wenshidu/?id=" + getx() + "\n") # wifi connection wifi = network.WLAN(network.STA_IF) # station mode wifi.active(True) wifi.connect(ssid, pw) # wait for connection while not wifi.isconnected(): pass # wifi connected print("IP: " + str(wifi.ifconfig()[0]) + "\n") # main loop while True: # if lose wifi connection reboot ESP8266 if not wifi.isconnected(): machine.reset() # query and get web JSON every web_query_delay ms # test default wdt #wdt = machine.WDT() while True: # HTTP GET data try: wdt.feed() temp_ = str(sensor.temperature)#读取measure()函数中的温度数据 hum_ = str(sensor.relative_humidity) # 读取measure()函数中的湿度数据 print("\nTemperature: %0.2f C" % sensor.temperature) print("Humidity: %0.2f %%" % sensor.relative_humidity) wd="%0.2f" % sensor.temperature sd="%0.2f" % sensor.relative_humidity #wdt.feed() response = urequests.get(url+"?id="+getx()+"&wd="+wd+"&sd="+sd) time.sleep(15) except: machine.reset() print('holle')
引用参数:开发板硬件ID
引用链接:http://www.esp56.com/api/wenshidu/?id=6055f9778b34
效果图:
ahtx0.py
# The MIT License (MIT) # # Copyright (c) 2020 Kattni Rembor for Adafruit Industries # Copyright (c) 2020 Andreas Bühl # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """ MicroPython driver for the AHT10 and AHT20 Humidity and Temperature Sensor Author(s): Andreas Bühl, Kattni Rembor """ import utime from micropython import const class AHT10: """Interface library for AHT10/AHT20 temperature+humidity sensors""" AHTX0_I2CADDR_DEFAULT = const(0x38) # Default I2C address AHTX0_CMD_INITIALIZE = 0xE1 # Initialization command AHTX0_CMD_TRIGGER = const(0xAC) # Trigger reading command AHTX0_CMD_SOFTRESET = const(0xBA) # Soft reset command AHTX0_STATUS_BUSY = const(0x80) # Status bit for busy AHTX0_STATUS_CALIBRATED = const(0x08) # Status bit for calibrated def __init__(self, i2c, address=AHTX0_I2CADDR_DEFAULT): utime.sleep_ms(20) # 20ms delay to wake up self._i2c = i2c self._address = address self._buf = bytearray(6) self.reset() if not self.initialize(): raise RuntimeError("Could not initialize") self._temp = None self._humidity = None def reset(self): """Perform a soft-reset of the AHT""" self._buf[0] = self.AHTX0_CMD_SOFTRESET self._i2c.writeto(self._address, self._buf[0:1]) utime.sleep_ms(20) # 20ms delay to wake up def initialize(self): """Ask the sensor to self-initialize. Returns True on success, False otherwise""" self._buf[0] = self.AHTX0_CMD_INITIALIZE self._buf[1] = 0x08 self._buf[2] = 0x00 self._i2c.writeto(self._address, self._buf[0:3]) self._wait_for_idle() if not self.status & self.AHTX0_STATUS_CALIBRATED: return False return True @property def status(self): """The status byte initially returned from the sensor, see datasheet for details""" self._read_to_buffer() return self._buf[0] @property def relative_humidity(self): """The measured relative humidity in percent.""" self._perform_measurement() self._humidity = ( (self._buf[1] << 12) | (self._buf[2] << 4) | (self._buf[3] >> 4) ) self._humidity = (self._humidity * 100) / 0x100000 return self._humidity @property def temperature(self): """The measured temperature in degrees Celcius.""" self._perform_measurement() self._temp = ((self._buf[3] & 0xF) << 16) | (self._buf[4] << 8) | self._buf[5] self._temp = ((self._temp * 200.0) / 0x100000) - 50 return self._temp def _read_to_buffer(self): """Read sensor data to buffer""" self._i2c.readfrom_into(self._address, self._buf) def _trigger_measurement(self): """Internal function for triggering the AHT to read temp/humidity""" self._buf[0] = self.AHTX0_CMD_TRIGGER self._buf[1] = 0x33 self._buf[2] = 0x00 self._i2c.writeto(self._address, self._buf[0:3]) def _wait_for_idle(self): """Wait until sensor can receive a new command""" while self.status & self.AHTX0_STATUS_BUSY: utime.sleep_ms(5) def _perform_measurement(self): """Trigger measurement and write result to buffer""" self._trigger_measurement() self._wait_for_idle() self._read_to_buffer() class AHT20(AHT10): AHTX0_CMD_INITIALIZE = 0xBE # Calibration command
Copyright © 2014 ESP56.com All Rights Reserved
执行时间: 0.0082240104675293 seconds