import time
from machine import Pin
import neopixel
import onewire
import ds18x20
import network
from umqttsimple import MQTTClient

ds = ds18x20.DS18X20(onewire.OneWire(Pin(21)))
sensoren = ds.scan()
sensor = sensoren[0]
np = neopixel.NeoPixel(Pin(27),1)

ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)
sta_if = network.WLAN(network.STA_IF)
sta_if.active(False)
sta_if.active(True)
sta_if.connect("wvss-iotlab", "geheim1234")


taster = Pin(39, Pin.IN)



while sta_if.isconnected() == False:
    pass
print(sta_if.ifconfig())


mqtt = MQTTClient(b"Lauer", "10.16.1.246")
mqtt.connect()

while True:
    temp = round(ds.read_temp(sensor),1)
    ds.convert_temp()
    time.sleep(1)
    print(temp)
    mqtt.publish(b"E1FS3/lauer", str(temp))
    
    if temp >= 28 and  temp <= 30:
        np[0] = (0,100,0)
        np.write()
    elif temp <28:
        np[0] = (0,0,100)
        np.write()
    else:
        np[0] = (100,0,0)
        np.write()
    
    if taster.value() == 0:
        mqtt.publish(b"E1FS3/lauer", str(temp))
    print(taster.value())
        
    