import dht
import network
import ujson
from machine import Timer, Pin, I2C
from ir import NEC_8
from utime import sleep
#from umqtt.simple import MQTTClient
d = dht.DHT22(Pin(28, Pin.IN))
fullLigth = Pin(19,Pin.OUT)
warmLigth = Pin(18,Pin.OUT)
coldLigth = Pin(17,Pin.OUT)
fan = Pin(16,Pin.OUT)
def flash(pin):
pin.high()
sleep(1)
pin.low()
sleep(1)
pin.high()
sleep(1)
pin.low()
def test():
d.measure()
print('Temperature: {}. Humidity: {}'.format(d.temperature(), d.humidity()))
flash(fullLigth)
flash(warmLigth)
flash(coldLigth)
flash(fan)
print('test')
def plus():
print('plus')
def reset():
print('reset')
def previus():
print('previus')
def play():
print('play')
def _next():
print('_next')
def key0():
print('key0')
def less():
print('less')
def keyClear():
print('clear')
def key1():
if fullLigth.value() == 0:
fullLigth.value(1)
else:
fullLigth.value(0)
print('key1')
def key2():
if warmLigth.value() == 0:
warmLigth.value(1)
else:
warmLigth.value(0)
print('key2')
def key3():
if coldLigth.value() == 0:
coldLigth.value(1)
else:
coldLigth.value(0)
print('key3')
def key4():
if fan.value() == 0:
fan.value(1)
else:
fan.value(0)
print('key4')
def key5():
print('key5')
def key6():
print('key6')
def key7():
print('key7')
def key8():
print('key8')
def key9():
print('key9')
keys = {
'22': test,
'02': plus,
'c2': reset,
'e0': previus,
'a8': play,
'90': _next,
'68': key0,
'98': less,
'b0': keyClear,
'30': key1,
'18': key2,
'7a': key3,
'10': key4,
'38': key5,
'5a': key6,
'42': key7,
'4a': key8,
'52': key9
}
def callback(data, addr, ctrl):
if data < 0: # NEC protocol sends repeat codes.
print('Repeat code.')
else:
print('Data {:02x} Addr {:04x}'.format(data, addr))
keys['{:02x}'.format(data)]()
ir = NEC_8(Pin(0, Pin.IN), callback)
# MQTT Server Parameters
MQTT_CLIENT_ID = "micropython-weather-demo"
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC = "wokwi-weather"
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="")
sleep(0.1)
print(" Connected!")
print("Connecting to MQTT server... ", end="")
# client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
# client.connect()
# print("Connected!")
# prev_weather = ""
# while True:
# print("Measuring weather conditions... ", end="")
# d.measure()
# message = ujson.dumps({
# "temp": d.temperature(),
# "humidity": d.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")
# sleep(1)