-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadafruitio-dht.py
48 lines (37 loc) · 1.1 KB
/
adafruitio-dht.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import time
import machine
import dht
import network
from umqtt.simple import MQTTClient
yourWifiSSID = " "
yourWifiPassword = " "
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(yourWifiSSID, yourWifiPassword)
while not sta_if.isconnected():
pass
myMqttClient = "chalei-mqtt" # can be anything unique
adafruitIoUrl = "io.adafruit.com"
adafruitUsername = "user" # can be found at "My Account" at adafruit.com
adafruitAioKey = "aiokey" # can be found by clicking on "VIEW AIO KEYS" when viewing an Adafruit IO Feed
c = MQTTClient(myMqttClient, adafruitIoUrl, 0, adafruitUsername, adafruitAioKey)
c.connect()
sensor = dht.DHT22(machine.Pin(2))
sensor.measure()
kelembaban = sensor.humidity()
suhu = sensor.temperature()
time.sleep(2)
def monitor():
sensor.measure()
time.sleep(1)
print('kelembaban: {0:0.2f}'.format(kelembaban))
c.publish("user/feeds/esptemp", str(suhu))
c.publish("user/feeds/esphum", str(kelembaban))
time.sleep(60*10)
#time.sleep(0.5)
#for i in range(1,20):
while True:
try:
monitor()
except OSError:
pass