2017年3月9日 星期四
MicroPython ESP8266-dht11 sensor
# Import libraries and settings
# from settings import *
# APIKEY = 'T2I3BOAY1M0CCUEL'
# CHANNELID = '258108'
# WifiSSID='PTS-2F'
# WifiPSWD=''
import machine
import time
import dht
import network
import gc
from umqtt.simple import MQTTClient
# Run the ESP8266 at max speed
machine.freq(160000000)
# Functions
def measure():
d = dht.DHT11(machine.Pin(4))
try:
d.measure()
temp = d.temperature()
hum = d.humidity()
#print('Temp: ', temp) # eg. 23.6 (簞C)
#print('Humidity: ', hum) # eg. 41.3 (% RH)
return temp, hum
except Exception as e:
return (-1,-1)
def go_sleep():
# put the device to sleep for 60 seconds
time.sleep(60)
def do_connect():
# connect the ESP8266 to local wifi network
#
yourWifiSSID = "PTS-2F" # <--- replace with your WIFI network name
yourWifiPassword = "" # <--- replace with your WIFI network password
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(yourWifiSSID, yourWifiPassword)
while not sta_if.isconnected():
pass
def post_data(temp,hum):
# connect ESP8266 to Thingspeak using MQTT
#
myMqttClient = "my-mqtt-client" # can be anything unique
thingspeakIoUrl = "mqtt.thingspeak.com"
c = MQTTClient(myMqttClient, thingspeakIoUrl, 1883) # uses unsecure TCP connection
c.connect()
#
# publish temperature and free heap to Thingspeak using MQTT
#
thingspeakChannelId = "238108" # <--- replace with your Thingspeak Channel ID
thingspeakChannelWriteapi = "T2I4BOAY1M0CCUEL" # <--- replace with your Thingspeak Write API Key
# note: string concatenations below follow best practices as described in micropython reference doc
credentials = "channels/{:s}/publish/{:s}".format(thingspeakChannelId, thingspeakChannelWriteapi)
payload = "field1={:.1f}&field2={:.1f}&field3={:d}\n".format(temp,hum, gc.mem_free())
c.publish(credentials, payload)
# Start of actual program
while True:
do_connect()
(temp, hum) = measure()
post_data(temp,hum)
go_sleep()
c.disconnect()
訂閱:
張貼留言 (Atom)
Messaging API作為替代方案
LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
-
• 認 識 PreFix、InFix、PostFix PreFix(前序式):* + 1 2 + 3 4 InFix(中序式): (1+2)*(3+4) PostFix(後序式):1 2 + 3 4 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言