from machine import Pin, I2C
import network, dht, time
from umqtt.robust import MQTTClient

d = dht.DHT22(Pin(13))


mqtt_server = 'br5.maqiatto.com'
mqtt_user = 'elsch0pi'
mqtt_pass = 'werdnuss667'
topic_pub = 'DHT22'

def sub_cb(topic, msg):
  print((topic, msg))


def do_connect(ssid, passphrase):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect(ssid, passphrase)
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())

def wifi_scanner():
  wlan = network.WLAN(network.STA_IF)
  wlan.active(True)

  wifi = wlan.scan()

  for ap in wifi:
    print(ap)


wifi_scanner()

do_connect('Wokwi-GUEST','')

client = MQTTClient('ESP32_WOKWI', 'br5.maqiatto.com', 1883, 'elsch0pi', 'werdnuss667')
client.connect()

while True:
  d.measure()
  print('Temp: {}°C'.format(d.temperature()))
  print('Hum: {}%rH'.format(d.humidity()))
  msg = '/*{};{}*/'.format(d.temperature(), d.humidity())
  client.publish(topic_pub, msg)
  time.sleep(1)