#Oled Display ansteuern
from machine import Pin, I2C
import ssd1306
import network
import time
import ujson
i2c = I2C(0, scl = Pin(22), sda = Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
oled.text('Vergrab dich', 15, 0)
oled.text('Mortis -JoJonas', 5, 10)
oled.show()
oled.fill(0)
oled.show()
#WiFi
print("Connecting to Wifi...")
sta_if=network.WLAN(network.STA_IF) #WLAN Object
sta_if.active(True) #activate WLAN
sta_if.connect('Wokwi-GUEST','')#ssid und PW
#wait until WLAN is connected
while not sta_if.isconnected():
print(".",end="")
time.sleep(0.1)
print("Wifi connected")
#MQTT Setup
SERVER="industrial.api.ubidots.com"
port=1883
client= 'Watherstationmelih'
topic=b"/v1.6/devices/Weather"
ubidotsToken = 'BBUS-gMk9vNoWePf5M4YM6pnnkFCxteN7al'
#Client Object
client = MQTTClient("client", "industrial.api.ubidots.com", 1883, user = ubidotsToken, password = ubidotsToken)
temp=-999 #Mustertemperatur
def sub_cb(TOPIC, msg):
a=ujson.loads(msg)
global temp
temp=a["value"]
client.set_callback(sub_cb)
while True:
client.connect()
client.publish(topic,msg)
client.disconnect()
time.sleep(3)