1 Star 0 Fork 4

feiman/micropython-dht11-iot

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.py 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
Rddapi 提交于 2020-02-08 10:43 +08:00 . 我又加一行注释
from machine import Timer
from machine import Pin
from chipid import chipid
import dht
import wifi
import register
import usocket
import ujson
from machine import reset
KEY = sasdasdfadfa #
#abc
#
'''
http 协议封装
'''
class http:
# 构造器
def __init__(self, url='', apikey=''):
'''
方法介绍:构造器
babalallala
'''
self.url=url+"/datapoints?type=3" # 硬编码通信接口, 移动iot,已经失效
self.apikey=apikey
self.dht11=dht.DHT11(Pin(2))
self.pid = 0
def updata(self,t):
self.dht11.measure()
jdata = {"temp":self.dht11.temperature(),"humi":self.dht11.humidity()} # 封装传感器温度湿度
data = ujson.dumps(jdata) # 转json格式
# print(jdata)
# print(data)
_, _, host, path = self.url.split('/', 3)
# print(self.url)
# print(host)
# print(path)
addr = usocket.getaddrinfo(host, 80)[0][-1]
s = usocket.socket()
s.connect(addr)
s.send(bytes('POST /%s HTTP/1.0\r\napi-key:%s\r\nHost:%s\r\nContent-Length:%d\r\n\r\n%s' % (path, self.apikey,host,len(data),data), 'utf8'))
# print('POST /%s HTTP/1.0\r\napi-key:%s\r\nHost:%s\r\nContent-Length:%d\r\n\r\n%s' % (path, self.apikey,host,len(data),data))
while True:
data = s.recv(100)
if data:
print(str(data, 'utf8'), end='')
else:
break
s.close()
def timerdata(self):
tim = Timer(-1)
tim.init(period=30000, mode=Timer.PERIODIC, callback=self.updata)
def checkWifi(t):
wifi.do_connect(False)
def main():
wifi.do_connect(True)
netTim = Timer(1)
netTim.init(period=60000, mode=Timer.PERIODIC, callback=checkWifi)
apikey="=your apikey="
#替换成你自己的key和id
apiUrl = 'http://api.heclouds.com/devices/=youerdeviceid='
hr=http(url=apiUrl,apikey=apikey)
hr.timerdata()
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/feiman8888/micropython-dht11-iot.git
git@gitee.com:feiman8888/micropython-dht11-iot.git
feiman8888
micropython-dht11-iot
micropython-dht11-iot
master

搜索帮助