from machine import Pin, SoftI2C,PWM
import dht
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
import wifi
from umqtt.simple import MQTTClient
import uasyncio as asyncio
servo1 = PWM(Pin(15))
servo1.freq(5000)
sensor = dht.DHT22(Pin(14))
I2C_ADDR = 0x27
totalRows = 4
totalColumns = 16
i2c1 = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
lcd1 = I2cLcd(i2c1, I2C_ADDR, totalRows, totalColumns)
lcd1.putstr("Hello")
h = -1
t=-1
# wifi.connect_ap()
# client_id = "Jw86ADQsLjQqFSUaJDUiFgo"
# user_name = "Jw86ADQsLjQqFSUaJDUiFgo"
# password = "hp4BGV7S3PGPmy7kmiFq+B9v"
# server = "mqtt3.thingspeak.com"
# client = MQTTClient(client_id = client_id, server =server, user=user_name, password=password)
# client.set_callback(handle_callback)
# client.connect()
# client.subscribe(b"channels/2362836/subscribe/fields/field3")
# publish_topic = b"channels/2362836/publish"
while True:
t = sensor.temperature()
h = sensor.humidity()
sensor.measure() # read the parameters from the sensor
if h != sensor.humidity() or t != sensor.temperature():
h = sensor.humidity()
t = sensor.temperature()
lcd1.clear()
lcd1.putstr("The H: ")
lcd1.putstr(str(h))
lcd1.putstr("\nThe T: ")
lcd1.putstr(str(t))
time.sleep(1)
# if(t > 40) and (h <60 ):
# servo1.angle(50, 1000)
# time.sleep(1)
# else:
# servo1.angle(100, 1000)
# time.sleep(1)
# # Task 1: Publish data to MQTT broker each 5s
# async def task1():
# while True:
# sensor.measure()
# t = sensor.temperature()
# h = sensor.humidity()
# if h != sensor.humidity() or t != sensor.temperature():
# h = sensor.humidity()
# t = sensor.temperature()
# lcd1.clear()
# lcd1.putstr("The H: ")
# lcd1.putstr(str(h))
# lcd1.putstr("\nThe T: ")
# lcd1.putstr(str(t))
# time.sleep(1)
# payload = f"field1={t}&field2={h}&status=MQTTPUBLISH"
# client.publish(publish_topic,payload.encode("utf-8"))
# await asyncio.sleep_ms(5000)
# sensor.measure() # read the parameters from the sensor
# if __name__ == "__main__":
# loop = asyncio.get_event_loop()
# loop.create_task(task1())
# loop.run_forever()