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)
Telegram Bot 即時告警推播 與 每 30 分鐘定時遙測回報
Telegram Bot 即時告警推播 與 每 30 分鐘定時遙測回報 將 Telegram Bot 即時告警推播 與 每 30 分鐘定時遙測回報 完整整合進程式中。 新增功能重點 Telegram 推播設定面板(區塊 2-B) :可直接輸入 Bot Token 與 ...
-
Line 發報機 Python TKinter (CONFIG_LINE Message API_2.py) import serial import serial.tools.list_ports import tkinter as tk from tkinter import...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
-
ESP32 DHT22 + Telegram Telegram Bot 指令列表 /start - 開始對話 /help - 指令說明 /status - 查詢目前溫濕度 /mute - 暫停變化通知 /resume - 恢復變化通知 /stopreport - 停止定時回報 /...
沒有留言:
張貼留言