from machine import Pin, I2C,Timer
import machine, time, math
import ssd1306
import network
import dht
import ujson
from umqtt.simple import MQTTClient
from machine import Timer

# ESP32 Pin assignment 
i2c = I2C(0, scl=Pin(22), sda=Pin(21))

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

oled.text('xujiahao', 10, 10)      
oled.show()

d=dht.DHT22(Pin(15))

def f(t):
    d.measure()
    a = d.temperature()
    b= d.humidity()
    print('temperature: {:.1f} dC'.format(a))
    print('humidity: {:.1f} %'.format(b))
    oled.fill(0)
    oled.text("xujiahao",10,10)
    oled.text('temp: {:.1f} dc'.format(a),10,25)
    oled.text('humidity: {:.1f} %'.format(b), 10, 40)
    oled.show()

tim = Timer(-1)
tim.init(period=2000, mode=Timer.PERIODIC, callback=f)


# MQTT Server Parameters
MQTT_CLIENT_ID = "xujiahao2306023151ASDFGH"
MQTT_BROKER    = "10.160.29.85"
MQTT_USER      = "MQTT"
MQTT_PASSWORD  = "123456"
MQTT_TOPIC     = "xujiahao/2306023151"
TOPIC1 = b"xujiahao2306023151ledctl"  #灯控主题,为了避免主题和其他同学一样,末尾需要添加自己的学号
TOPIC2 = b"xujiahao2306023151pwmled"
TOPIC3 = b"xujiahao2306023151ledstatus"
prev_weather = ""
sensor = dht.DHT22(Pin(15))
# 选择gpio2引脚
led_blue = machine.PWM(machine.Pin(2), freq=1000)  # 设置 GPIO2 为输出
led_blue.duty(1023)

#连接wifi配置
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
  print(".", end="")
  time.sleep(0.1)
print(" Connected!")

#配置定义回调函数
def sub_cb(topic, msg):   #定义收到mqtt主题信息之后的回调函数
    global pwm_old, pwmval
    if topic == TOPIC1:
        print((topic, msg))
        if msg == b"on":
            pwmval = 0
            led_blue.duty(pwmval)
        elif msg == b"off":
            pwmval = 1023;
            led_blue.duty(pwmval)

    if topic == TOPIC2:
        pwmval = int(((100 - int(msg)) / 100) * 1023)
        if pwmval>1023 : pwmval=1023
        if pwmval<0:    pwmval=0
        print((topic, pwmval))
        led_blue.duty(pwmval)

print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.set_callback(sub_cb)    #定义好client参数之后,就需要在connect之前
client.connect()
client.subscribe(TOPIC1)     #connect之后就需要马上订阅主题,才能收到mqtt服务器送过来的信息
client.subscribe(TOPIC2)     
print("Connected!")
#定义采集温湿度的函数,方便后面定时器定时调用
def sample_weather(V):
    global prev_weather
    print("Measuring weather conditions... ", end="")
    sensor.measure() 
    message = ujson.dumps({
        "temp": sensor.temperature(),
        "humidity": sensor.humidity(),
    })
    if message != prev_weather:
        print("Updated!")
        print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message))
        client.publish(MQTT_TOPIC, message)
        prev_weather = message
    else:
        print("No change")

tim = Timer(-1)  #新建一个虚拟定时器
#此处执行时程序会等待5秒读取一次温湿度,period=5000 以毫秒为单位,mode=Timer.ONE_SHOT 表示只执行一次,Timer.PERIODIC表示循环执行,callback绑定回调函数
tim.init(period=5000, mode=Timer.PERIODIC, callback=sample_weather)

while True:
    client.wait_msg()
    time.sleep(1)