import network
import time
from machine import Pin,ADC,SoftI2C
from i2c_lcd import I2cLcd 
import dht
import ujson
from umqtt.simple import MQTTClient

# MQTT broker Parameters
MQTT_CLIENT_ID = "wokwi"
MQTT_BROKER    = "5fbcd303cf5d4f4aa437ee13fb50fd99.s1.eu.hivemq.cloud"
MQTT_USER      = "domotique"
MQTT_PASSWORD  = "Domotique123"
MQTT_TOPIC     = "climat"
heart = bytearray([0x00,0x00,0x1B,0x1F,0x1F,0x0E,0x04,0x00])
happy = bytearray([0x00,0x0A,0x00,0x04,0x00,0x11,0x0E,0x00])
sad = bytearray([0x00,0x0A,0x00,0x04,0x00,0x0E,0x11,0x00])
grin = bytearray([0x00,0x00,0x0A,0x00,0x1F,0x11,0x0E,0x00])
shock = bytearray([0x0A,0x00,0x04,0x00,0x0E,0x11,0x11,0x0E])
meh = bytearray([0x00,0x0A,0x00,0x04,0x00,0x1F,0x00,0x00])
angry = bytearray([0x11,0x0A,0x11,0x04,0x00,0x0E,0x11,0x00])
bell = bytearray([0x04,0x0e,0x0e,0x0e,0x1f,0x00,0x04,0x00])
note = bytearray([0x02,0x03,0x02,0x0e,0x1e,0x0c,0x00,0x00])
clock = bytearray([0x00,0x0e,0x15,0x17,0x11,0x0e,0x00,0x00])
heart = bytearray([0x00,0x0a,0x1f,0x1f,0x0e,0x04,0x00,0x00])
duck = bytearray([0x00,0x0c,0x1d,0x0f,0x0f,0x06,0x00,0x00])
check = bytearray([0x00,0x01,0x03,0x16,0x1c,0x08,0x00,0x00])
cross = bytearray([0x00,0x1b,0x0e,0x04,0x0e,0x1b,0x00,0x00])
retarrow = bytearray([0x01,0x01,0x05,0x09,0x1f,0x08,0x04,0x00])

sensor = dht.DHT22(Pin(15))
adc = ADC(Pin(32))
pir_sensor = Pin(23, Pin.IN)
I2C_ADDR = 0x27
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
lcd.custom_char(0, heart)
lcd.custom_char(1,bell)
lcd.putstr(chr(1)+" Systeme domotique sans fil "+chr(1)+chr(1))

#connection to 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!")
#connection to mqtt server
print("Connecting to MQTT server... ", end="")
client = MQTTClient(client_id=b"domotique",server=b"5fbcd303cf5d4f4aa437ee13fb50fd99.s1.eu.hivemq.cloud",port=8883,user=b"hivemq.webclient.1708862134429",password=b"ldCh6534$sJGK.Yc#zD!",keepalive=7200,ssl=True,ssl_params={'server_hostname':'5fbcd303cf5d4f4aa437ee13fb50fd99.s1.eu.hivemq.cloud'})
client.connect()
print("Connected!")


#track the changes!
prev_weather = ""
while True:
#   analog_value = adc.read(32)
    # Print the analog value
  print("Analog Value:", adc.read()) #works fine
  pir_value = pir_sensor.value()
  print("PIR Sensor Value:", pir_value)
  print("Measuring weather conditions... ", end="")
  sensor.measure() 
  # print(round(adc.read_u16()/65535*100,2))
  lcd.clear()
  lcd.putstr(chr(1)+ " temp : "+ str(sensor.temperature()) + "\n" + chr(1)+" humidity :"+str(sensor.humidity()))
#    + chr(0)+" humidity: "+sensor.humidity()+" movement "+ pir_value  +" light "+ adc.read())  
  message = ujson.dumps({
    b"temp": sensor.temperature(),
    b"humidity": sensor.humidity(),
    b"movement":pir_value,
    b"light":adc.read()
  })
  if message != prev_weather:
    print("Updated!")
    print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message))
    client.publish(MQTT_TOPIC, message)
    prev_weather = message
  else:
    client.publish(MQTT_TOPIC, message)
    print("No change")

  time.sleep(2)

#keep connection 
client.loop_forever()