from machine import Pin, ADC
import time
import paho.mqtt.client as paho
from paho import mqtt
import ssl
#MQTT Objects
user = "alyta"
pwd = "Alej9U70"
host = "c7109855e2d0462bb6b5635980df156d.s1.eu.hivemq.cloud"
port = 8884
Publish_topic = "Watreon/Test"
Subscribe_topic = "Watreon/#"
msg = str(input("ingrese un mensaje")) #"holaaa"
#Sensor Objects
led_r = Pin (14, Pin.OUT)
led_v = Pin (26, Pin.OUT)
caudalimetro = ADC (Pin (34))
caudalimetro.width(ADC.WIDTH_10BIT)
caudalimetro.atten(ADC.ATTN_11DB)
#Programming the sensor
while True:
lectura = int(caudalimetro.read())
time.sleep_ms(500)
Agua = 75 / 1023
Flujo = Agua * lectura
if Flujo <= 0:
print ("No está pasando agua por el caudalímetro")
led_r.value(1)
led_v.value(0)
if Flujo > 0 and Flujo < 75:
print (f"se están usando {Flujo} Litros por segundo de agua en la casa")
led_r.value(0)
led_v.value(1)
if Flujo >= 75:
print ("conecte el caudalimetro a una tuberia de 25mm o menor")
led_r.value(1)
led_v.value(0)
time.sleep_ms(500)
# setting callbacks for different events to see if it works, print the message etc.
def on_connect(client, userdata, flags, rc, properties=None):
print("connected with resuilt code %s" % rc)
# with this callback you can see if your publish was successful
def on_publish(client, userdata, mid, properties=None):
print("Mensaje Publicado, mid:" + str(mid))
# print which topic was subscribed to
def on_subscribe(client, userdata, mid, granted_qos, properties=None):
print(f"Suscrito a el tema con mid : {str(mid)}, granted qos : {str(granted_qos)}")
# print message, useful for checking if it was successful
def on_message(client, userdata, msg):
print(f"mensaje recibido del tema : {msg.topic} , con QOS : {msg.qos}, entrada : {msg.payload.decode('utf-8')}")
# using MQTT version 5 here, for 3.1.1: MQTTv311, 3.1: MQTTv31
# userdata is user defined data of any type, updated by user_data_set()
# client_id is the given name of the client
client = paho.Client(client_id="Watreon", userdata=None, protocol=paho.MQTTv5, transport = "websockets")
client.on_connect = on_connect
# setting callbacks, use separate functions like above for better visibility
client.on_message = on_message
client.on_publish = on_publish
client.on_subscribe = on_subscribe
# enable TLS for secure connection
client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)
# set username and password
client.username_pw_set(user, pwd)
# connect to HiveMQ Cloud on port 8883 (default for MQTT)
client.connect(host, port)
# subscribe to topic
#client.loop_start()
def subscribe ():
client.subscribe(Subscribe_topic)
# a single publish, this can also be done in loops, etc.
def publish (msg):
client.publish(Publish_topic, msg)
time.sleep(2)
publish (msg)
subscribe ()
# Stop the MQTT connection
try :
while True :
time.sleep(2)
except KeyboardInterrupt :
print ("cancelando la coneccion MQTT...")
client.loop_stop()
client.disconnect()
# loop_forever for simplicity, here you need to stop the loop manually
# you can also use loop_start and loop_stop
#client.loop_start()