from machine import Pin, I2C
from neopixel import NeoPixel
import ssd1306, ds18x20, onewire
import time

# ESP32 Pin assignment 
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
ow = onewire.OneWire(Pin(12))
ds = ds18x20.DS18X20(ow)
np_pin = Pin(13, Pin.OUT)
np = NeoPixel(np_pin, 16)

# Setup Display device
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

# Start text
oled.text('Hello, Thermo!', 10, 10)      
oled.show()
'''
np[0] =(50,174,0)
np.write()

roms = ds.scan()
ds.convert_temp()
time.sleep_ms(750)
for rom in roms:
    print(ds.read_temp(rom))

oled.fill(0)
oled.text('temp: {0}'.format(ds.read_temp(rom)), 0, 0)
oled.show()
'''

def tempreader():
    roms = ds.scan()
    ds.convert_temp()
    time.sleep_ms(750)
    for rom in roms:
        # print(ds.read_temp(rom))
        messung = ds.read_temp(rom)
    return messung

while True:
    
    messwert = tempreader()
    
    if messwert <= -10:
        oled.fill(0)
        oled.text('Saukalt!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0, 0, 0)
        np[2] = (0, 0, 0)
        np[3] = (0, 0, 0)
        np[4] = (0, 0, 0)
        np[5] = (0, 0, 0)
        np[6] = (0, 0, 0)
        np[7] = (0, 0, 0)
        np[8] = (0, 0, 0)
        np[9] = (0, 0, 0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 0:
        oled.fill(0)
        oled.text('Kalt!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0, 0, 0)
        np[3] = (0, 0, 0)
        np[4] = (0, 0, 0)
        np[5] = (0, 0, 0)
        np[6] = (0, 0, 0)
        np[7] = (0, 0, 0)
        np[8] = (0, 0, 0)
        np[9] = (0, 0, 0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 10:
        oled.fill(0)
        oled.text('Frisch!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0, 0, 0)
        np[4] = (0, 0, 0)
        np[5] = (0, 0, 0)
        np[6] = (0, 0, 0)
        np[7] = (0, 0, 0)
        np[8] = (0, 0, 0)
        np[9] = (0, 0, 0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 15:
        oled.fill(0)
        oled.text('Kühl!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (0, 0, 0)
        np[5] = (0, 0, 0)
        np[6] = (0, 0, 0)
        np[7] = (0, 0, 0)
        np[8] = (0, 0, 0)
        np[9] = (0, 0, 0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 16:
        oled.fill(0)
        oled.text('Wird besser!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (0, 0, 0)
        np[6] = (0, 0, 0)
        np[7] = (0, 0, 0)
        np[8] = (0, 0, 0)
        np[9] = (0, 0, 0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 19:
        oled.fill(0)
        oled.text('Angenehm!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (0, 0, 0)
        np[7] = (0, 0, 0)
        np[8] = (0, 0, 0)
        np[9] = (0, 0, 0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 22:
        oled.fill(0)
        oled.text('Gemütlich!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (50,174,0)
        np[7] = (0, 0, 0)
        np[8] = (0, 0, 0)
        np[9] = (0, 0, 0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 25:
        oled.fill(0)
        oled.text('Warm!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (50,174,0)
        np[7] = (50,174,0)
        np[8] = (0, 0, 0)
        np[9] = (0, 0, 0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 26:
        oled.fill(0)
        oled.text('Sommerlich!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (50,174,0)
        np[7] = (50,174,0)
        np[8] = (253,166,0)
        np[9] = (0, 0, 0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 29:
        oled.fill(0)
        oled.text('Sommerlich!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (50,174,0)
        np[7] = (50,174,0)
        np[8] = (253,166,0)
        np[9] = (253,166,0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 32:
        oled.fill(0)
        oled.text('Sommerlich!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (50,174,0)
        np[7] = (50,174,0)
        np[8] = (253,166,0)
        np[9] = (253,166,0)
        np[10] = (0, 0, 0)
        np[11] = (0, 0, 0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 35:
        oled.fill(0)
        oled.text('Sommerlich!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (50,174,0)
        np[7] = (50,174,0)
        np[8] = (253,166,0)
        np[9] = (253,166,0)
        np[10] = (253,166,0)
        np[11] = (253,166,0)
        np[12] = (0, 0, 0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 36:
        oled.fill(0)
        oled.text('Heiß!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (50,174,0)
        np[7] = (50,174,0)
        np[8] = (253,166,0)
        np[9] = (253,166,0)
        np[10] = (253,166,0)
        np[11] = (253,166,0)
        np[12] = (201,0,0)
        np[13] = (0, 0, 0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 40:
        oled.fill(0)
        oled.text('Heiß!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (50,174,0)
        np[7] = (50,174,0)
        np[8] = (253,166,0)
        np[9] = (253,166,0)
        np[10] = (253,166,0)
        np[11] = (253,166,0)
        np[12] = (201,0,0)
        np[13] = (201,0,0)
        np[14] = (0, 0, 0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert <= 45:
        oled.fill(0)
        oled.text('Zu heiß!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (50,174,0)
        np[7] = (50,174,0)
        np[8] = (253,166,0)
        np[9] = (253,166,0)
        np[10] = (253,166,0)
        np[11] = (253,166,0)
        np[12] = (201,0,0)
        np[13] = (201,0,0)
        np[14] = (201,0,0)
        np[15] = (0, 0, 0)
        np.write()
    elif messwert >= 46:
        oled.fill(0)
        oled.text('Viel zu heiß!', 0,0)
        oled.show()
        np[0] = (0,56,255)
        np[1] = (0,56,255)
        np[2] = (0,56,255)
        np[3] = (0,56,255)
        np[4] = (50,174,0)
        np[5] = (50,174,0)
        np[6] = (50,174,0)
        np[7] = (50,174,0)
        np[8] = (253,166,0)
        np[9] = (253,166,0)
        np[10] = (253,166,0)
        np[11] = (253,166,0)
        np[12] = (201,0,0)
        np[13] = (201,0,0)
        np[14] = (201,0,0)
        np[15] = (201,0,0)
        np.write()
time.sleep(5)