#MQTT and ThingSpeak
import network
import time
from linelib import tiny_line
from machine import Pin, I2C, ADC
from dht import DHT22 # DHT11//DHT22
from ssd1306 import SSD1306_I2C
"""# Connecting wifi
#wlan_ssid = "HANA-HARPER" # ID wifi
#wlan_password = "qwerty1234" # password wifi
wlan_ssid = "Hana_Harpel22.4G" # ID wifi
wlan_password = "hanaharper2513" # password wifi
wlan = network.WLAN(network.STA_IF)
def wlan_connect():
wlan.active(True)
time.sleep(0.5)
wlan.connect(wlan_ssid,wlan_password)
time.sleep(10)
print("Connecting Wifi ...")
wlan_connect()
if wlan.isconnected():
print("WLAN : ",wlan.ifconfig())
conf = wlan.ifconfig()
print("SSID : ",wlan.config('essid'))
print("IP : ",conf[0])
else :
print("Cannot connect")"""
# DHT22
dht22 = DHT22(Pin(19))
# OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
width = 128 # 128 pixel
heigh = 64
oled = SSD1306_I2C(width, heigh, i2c)
relay = Pin(23, Pin.OUT)
#LDR
photoPIN = 34
while True:
try:
#DHT
time.sleep(1)
dht22.measure()
temp = dht22.temperature() # 25
temp = round(temp,2) # 25.55
humi = dht22.humidity() # 75
humi = round(humi,2) # 75.32
#temp = float(f"{temperature:.2f}")
#humi = float(f"{humidity:.2f}")
print("Temperature: ", temp , "Humidity: ", humi)
def readLight(photoGP):
photoRes = ADC(Pin(34))
light = photoRes.read_u16()
light = round(light/65535*100,2)
return light
print("light: " + str(readLight(photoPIN)) +"%")
time.sleep(1) # set a delay between readings
if temp >= 28:
#OLED
relay.on()
oled.fill(0)
oled.rect(0, 0, 127, 63, 2) # Big
oled.rect(0, 0, 127, 20, 2) # Small
oled.text('WEATHER TODAY', 12, 6) # (X,Y) : (0-127, 0-63)
oled.text("Temp: ", 10, 25)
oled.text(str(temp), 60, 25)
oled.text("C", 100, 25)
oled.text("Humi: ", 10, 35)
oled.text(str(humi), 60, 35)
oled.text("%", 100, 35)
oled.text("Light: ", 10, 45)
oled.text(str(readLight(photoPIN)), 60, 45)
oled.text("%", 100, 45)
oled.show()
"""#Line Connection
access_token = 'Nf8rIQj0J76CdnkRtX9LpoCrsjMbJzVr7dFetedom1L'
message = tiny_line(access_token)
message.notify("Fan On")
time.sleep(5)"""
else:
#OLED
relay.off()
oled.fill(0)
oled.rect(0, 0, 127, 63, 2) # Big
oled.rect(0, 0, 127, 20, 2) # Small
oled.text('WEATHER TODAY', 12, 6) # (X,Y) : (0-127, 0-63)
oled.text("Temp: ", 10, 25)
oled.text(str(temp), 60, 25)
oled.text("C", 100, 25)
oled.text("Humi: ", 10, 35)
oled.text(str(humi), 60, 35)
oled.text("%", 100, 35)
oled.text("Light: ", 10, 45)
oled.text(str(light), 60, 45)
oled.text("%", 100, 45)
oled.show()
"""#Line Connection
access_token = 'Nf8rIQj0J76CdnkRtX9LpoCrsjMbJzVr7dFetedom1L'
message = tiny_line(access_token)
message.notify("Fan Off")
time.sleep(5)"""
except OSError as e:
print("Failed to Read Sensor")