from machine import Pin, I2C, ADC
import time
from ssd1306 import SSD1306_I2C

from dht import DHT22

ambiente=DHT22(Pin(15))
sensor = ADC(Pin(34))

ancho = 128
alto = 64

i2c = I2C(0, scl=Pin(22), sda=Pin(23))
oled = SSD1306_I2C(ancho, alto, i2c)

# print(i2c.scan())

def saludo():
    oled.text("*"*16,0,0)
    oled.text("Hola Mundo",24,10)
    oled.text("Bienvenido", 24, 20)
    oled.text("Python - Devnet", 0, 30)
    oled.text("*"*16, 0, 40)
    oled.show()
    time.sleep(2)
    
def imprimir_pote():
    val = int((sensor.read_u16())/500)
    oled.fill_rect(1,15,val,25,1)
    oled.show()
    oled.fill_rect(1,15,val,25,0)
    time.sleep(0.25)
def grafica_sensor():
    global acum
    acum+=1
    val = int((sensor.read_u16())/2200)
    oled.pixel(acum,val+24,1)
    print (val)
    oled.show()
    time.sleep(0.02)
    if acum==128:
        acum=0
        oled.fill(0)
        oled.show()
saludo() 
oled.fill(1)
oled.show()
time.sleep(1)
oled.fill(0)
oled.show()
acum=0
while True:
    ambiente.measure()
    temperatura=ambiente.temperature()
    humedad=ambiente.humidity()
    print (f"T:{temperatura}\t H:{humedad}")
    oled.text(f"     T:{temperatura}     ",0,0,1)
    oled.text(f"     H:{humedad}    ",0,8,1)
    oled.show()
    time.sleep(0.03)
    #oled.fill(0)
    #imprimir_pote()
    grafica_sensor()