8564| 7
|
【micropython】四行代码做微信报警系统 |
本帖最后由 小和尚 于 2017-6-25 15:38 编辑 一, 申请ServerChan密钥 打开该网址http://sc.ftqq.com/3.version,根据说明生成自己的密钥。 二, 写一个温度报警系统,温度达到临界点推送给微信 下载以下库文件并上传到flash https://github.com/micropython/micropython/blob/master/esp8266/modules/dht.py https://github.com/micropython/m ... quests/urequests.py 最终目录结构如下: 创建一个空文件__init__.py 编写boot脚本: [mw_shl_code=python,true]import network import utime pdcn = network.WLAN(network.STA_IF) pdcn.active(True) pdcn.connect('wifi账号', 'wifi密码') utime.sleep(5) if pdcn.isconnected(): print("WiFi is connected %s."%pdcn.ifconfig()[0]) else: pdcn.active(False) utime.sleep(5) print("WiFi cannot connect.")[/mw_shl_code] 编写main.py脚本[mw_shl_code=python,true]# coding=utf-8 from lib import urequests from lib import dht import machine class AlarmSystem: def __init__(self): self.d = dht.DHT11(machine.Pin(5)) def dht11(self): self.d.measure() return "Temp:%s" % self.d.temperature(), "Humi:%s" % self.d.humidity() def push(self, result): title = "FireBeetle_ESP32" content = "text=%s" % title + "&" + "desp=%s"%result r = urequests.get("https://sc.ftqq.com/你的密钥.send?%s" % content) r.close() if __name__ == '__main__': a = AlarmSystem() while True: result = a.dht11()[0] if result >= 28: a.push(result) [/mw_shl_code] 最关键的就是main.py脚本中的push方法,四行代码(其实一行就行了,不过urequests的接受参数不能太长)就可以做到报警了。是不是炒鸡简单。 测试结果如下: |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed