import network, time, urequests
from machine import Pin
from utime import sleep, sleep_ms, ticks_us
from hcsr04 import HCSR04
import ujson
from umqtt.simple import MQTTClient

# MQTT Server Parameters
MQTT_CLIENT_ID = "pepitoxx23467900"#cambiar siempre
MQTT_BROKER    = "broker.hivemq.com"
MQTT_USER      = ""
MQTT_PASSWORD  = ""
MQTT_TOPIC     = "datos/uni/gps/"

contenedor1 = HCSR04(trigger_pin=2, echo_pin=4)
contenedor2 = HCSR04(trigger_pin=16, echo_pin=5)
contenedor3 = HCSR04(trigger_pin=18, echo_pin=19)


def conectaWifi (red, password):
      global miRed
      miRed = network.WLAN(network.STA_IF)     
      if not miRed.isconnected():              #Si no está conectado…
          miRed.active(True)                   #activa la interface
          miRed.connect(red, password)         #Intenta conectar con la red
          print('Conectando a la red', red +"…")
          timeout = time.time ()
          while not miRed.isconnected():           #Mientras no se conecte..
              if (time.ticks_diff (time.time (), timeout) > 10):
                  return False
      return True


if conectaWifi ("Wokwi-GUEST", ""):

    print ("Conexión exitosa!")
    print('Datos de la red (IP/netmask/gw/DNS):', miRed.ifconfig())
    
    print ("Conexión exitosa!")
    print('Datos de la red (IP/netmask/gw/DNS):', miRed.ifconfig())
     
    print("Conectando a  MQTT server... ",MQTT_BROKER,"...", end="")
    client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
    client.connect()
    
    print("Conectado al Broker!")
    
     
    while True:

        distance_80= contenedor1.distance_cm()
        distance_90= contenedor2.distance_cm()
        distance_per= contenedor3.distance_cm()

        print("Revisando Condiciones ...... ")
        print(distance_80, distance_90, distance_per)

        if distance_80 < 200 and distance_90 > 200 and distance_per > 200:
            
            message = ujson.dumps({
            "Latitud": 4.69870,
            "Longitud":-74.08957,
            "Nombre": "Sector 1"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)
            
        
        elif distance_80 > 200 and distance_90  < 200 and distance_per > 200:

            message = ujson.dumps({
            "Latitud": 4.71137,
            "Longitud":-74.09612,
            "Nombre": "Sector 2"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)


            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)
            

        elif distance_80 > 200 and distance_90 > 200 and distance_per < 200:

            message = ujson.dumps({
            "Latitud": 4.59380,
            "Longitud":-74.16748,
            "Nombre": "Sector 3"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })
            
            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)
            
            

        elif distance_80 < 200 and distance_90 < 200 and distance_per > 200:

            message = ujson.dumps({
            "Latitud": 4.69870,
            "Longitud":-74.08957,
            "Nombre": "Sector 1"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.71137,
            "Longitud":-74.09612,
            "Nombre": "Sector 2"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)
           
        
        elif distance_80 < 200 and distance_90 > 200 and distance_per < 200:

            message = ujson.dumps({
            "Latitud": 4.69870,
            "Longitud":-74.08957,
            "Nombre": "Sector 1"
            })
            
            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.59380,
            "Longitud":-74.16748,
            "Nombre": "Sector 3"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)
           
        
        elif distance_80 > 200 and distance_90 < 200 and distance_per < 200:

            message = ujson.dumps({
            "Latitud": 4.71137,
            "Longitud":-74.09612,
            "Nombre": "Sector 2"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.59380,
            "Longitud":-74.16748,
            "Nombre": "Sector 3"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)
           
            
        elif distance_80 < 200 and distance_90 < 200 and distance_per < 200:

            message = ujson.dumps({
            "Latitud": 4.69870,
            "Longitud":-74.08957,
            "Nombre": "Sector 1"
            })
            
            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.71137,
            "Longitud":-74.09612,
            "Nombre": "Sector 2"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.59380,
            "Longitud":-74.16748,
            "Nombre": "Sector 3"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

   
        else:
            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)

            message = ujson.dumps({
            "Latitud": 4.50582,
            "Longitud":-74.14475,
            "Nombre": "Relleno Sanitario"
            })

            print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
            client.publish(MQTT_TOPIC, message)



        time.sleep(5)

        
 
else:
       print ("Imposible conectar")
       miRed.active (False)