You are on page 1of 2

#---MQTT Sending--from time import sleep_ms

from ubinascii import hexlify


from machine import unique_id
#import socket
from umqtt import MQTTClient
SERVER = "192.168.0.101"
CLIENT_ID = hexlify(unique_id())
TOPIC1 = b"/sensor1/tem"
TOPIC2 = b"/sensor1/hum"
TOPIC3 = b"/sensor1/led"
def envioMQTT(server=SERVER, topic="/foo", dato=None):
try:
c = MQTTClient(CLIENT_ID, server)
c.connect()
c.publish(topic, dato)
sleep_ms(200)
c.disconnect()
#led.value(1)
except Exception as e:
pass
#led.value(0)
state = 0
def sub_cb(topic, msg):
global state
print((topic, msg))
if msg == b"on":
led.value(0)
state = 1
elif msg == b"off":
led.value(1)
state = 0
def recepcionMQTT(server=SERVER, topic=TOPIC3):
c = MQTTClient(CLIENT_ID, server)
# Subscribed messages will be delivered to this callback
c.set_callback(sub_cb)
c.connect()
c.subscribe(topic)
#print("Connected to %s, subscribed to %s topic" % (server, topic))
try:
c.wait_msg()
finally:
c.disconnect()
#---End MQTT Sending--#---DHT22--from dht import DHT22
ds = DHT22(Pin(16)) #DHT22 connected to GPIO16
def medirTemHum():
try:
ds.measure()

tem = ds.temperature()
hum = ds.humidity()
#ed.value(1)
return (tem,hum)
except Exception as e:
#led.value(0)
return (-1,-1)
#---End DHT22---

You might also like