import time
time.sleep(0.1) # Wait for USB to become ready
print("Hello, Pi Pico!")
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
from dht import DHT22
import sys,time
pix_res_x = 128 #SSD1306 horizontal resolution
pix_res_y = 64 #SSD1306 vertical resolution
#nappi = machine.Pin(20, machine.Pin.IN, machine.Pin.PULL_DOWN)
i2c_dev = I2C(1,scl=Pin(7),sda=Pin(6),freq=200000) # start I2C on I2C1 (GPIO 26/27)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()] # get I2C address in hex format
if i2c_addr==[]:
print('No I2C Display Found')
sys.exit() # exit routine if no dev found
else:
print("I2C Address : {}".format(i2c_addr[0])) # I2C device address
print("I2C Configuration: {}".format(i2c_dev)) # print I2C params
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev) # oled controller
#oled.write_cmd(0xc0) # flip display to place 0,0 at lower-left corner
adc_2 = machine.ADC(2) # ADC channel 2 for input
"""
while True:
oled.fill(0) # clear the display
if nappi.value()==True:
oled.text("Hello Dude",0,0)#Tulostetaan näytölle, kohtaan X=0 ja Y=0
else:
oled.text("Goodbye Dude",0,0)
oled.show()#Näytetään puskurissa oleva data näytölle
time.sleep(1)
"""
dht_pin = Pin(11, Pin.IN, Pin.PULL_UP) # Muuta pinni tarpeen mukaan
# Luo DHT-objekti
dht_sensor = DHT22(dht_pin)
while True:
try:
# Lue lämpötila ja kosteus
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
# Tulosta tulokset
print("Lämpötila: {:.2f}°C".format(temperature))
print("Kosteus: {:.2f}%".format(humidity))
# Puhdista näyttö
oled.fill(0)
# Näytä tiedot näytöllä
oled.text("Temp: {:.2f}C".format(temperature), 0, 0)
oled.text("Humidity: {:.2f}%".format(humidity), 0, 16)
# Päivitä näyttö
oled.show()
except Exception as e:
print("Virhe:", str(e))
# Pieni viive ennen seuraavaa mittauskertaa
time.sleep(2)
"""
oled.fill(0) # clear the display
for jj in range(pix_res_x): # loop through horizontal display resolution
adc_pt = adc_2.read_u16() # read adc and get 16-bit data point
plot_pt = (adc_pt/((2*16)-1))(pix_res_y-1) # convert to OLED pixels
oled.text('.',jj,int(plot_pt)) # update x=jj with data point
oled.show() # show the new text and image
"""