|
本帖最后由 mao_jin_dao 于 2020-4-18 18:31 编辑
功能
联网,时间获取,时钟显示,环境温湿度读取,环境信息上传, 整点蜂鸣
代码
tm1637.py
文件见micropython-tm1637
main.py
- from machine import Pin, I2C
- from ssd1306 import SSD1306_I2C
- import urequests as requests
- import network
- import time
- import tm1637
- import dht
- def connectionWifi(ssid, password):
- wlan = network.WLAN(network.STA_IF)
- wlan.active(True)
- wlan.connect(ssid, password)
- while True:
- if not wlan.isconnected():
- time.sleep_ms(2000)
- else:
- print('connected to network')
- break
- def getJsonInfoFromURL(url):
- response = requests.get(url)
- return response.json()
- def tm1637_LED(hours, minutes):
- if hours > 22 or hours < 6:
- tm.brightness(0)
- else:
- tm.brightness(7)
- tm.numbers(hours, minutes)
- def buzzer(hours, minutes):
- if hours == 7 and minutes == 0:
- buzzer_seconds(10)
- if hours >= 22 :
- if minutes%2 == 0:
- buzzer_seconds(hours/2)
- if minutes == 30:
- buzzer_seconds(1)
- def buzzer_seconds(seconds):
- bing.value(1)
- print ("should buzzer")
- time.sleep_ms(seconds * 1000)
- bing.value(0)
-
-
- def getTHFromDHT11(dht11):
- dht11.measure()
- return dht11.temperature(),dht11.humidity()
-
- def uploadTempAndHumidity(dht11,url,position):
- temperature, humidity = getTHFromDHT11(dht11)
- url = url+"/"+position+"/"+str(temperature)+"/"+str(humidity)
- print(url)
- requests.get(url)
-
- def myDHT11(hours,minutes, dth11, url, position):
- global dht11_last_hours
- if hours != dht11_last_hours:
- uploadTempAndHumidity(dht11, dht11_api, "home")
- dht11_last_hours = hours
-
- if __name__== '__main__':
- ssid='****'
- password='****'
- time_api="http://ip/bills/ESP8266TimeAPI"
- dht11_api="http://ip/temperature"
- position="home"
- tm = tm1637.TM1637(clk=Pin(5), dio=Pin(4))
- bing = Pin(12,Pin.OUT)
- dht11 = dht.DHT11(machine.Pin(14))
- dht11_last_hours = 0
- connectionWifi(ssid, password)
- while True:
- try:
- result = getJsonInfoFromURL(time_api)
- tm1637_LED(result['hours'], result['minutes'])
- buzzer(result['hours'], result['minutes'])
- myDHT11(result['hours'], result['minutes'], dht11, dht11_api, position)
- time.sleep_ms(10000)
- except:
- connectionWifi(ssid, password)
- tm = tm1637.TM1637(clk=Pin(5), dio=Pin(4))
- bing = Pin(12,Pin.OUT)
- dht11 = dht.DHT11(machine.Pin(14))
- bing.value(0)
复制代码
图片
补充内容 (2020-5-26 21:30):
def getTHFromDHT11(dht11)函数修改如下
# 以多次读值求平均数的方式降低传感器误差
def getTHFromDHT11(dht11):
print ("enter into getTHFromFHT11")
sumT=0
sumH=0
repeats=... |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
打赏
-
查看全部打赏
|