from machine import Pin, I2C
import time
import dht
import framebuf
from sh1106 import SH1106_I2C
# OLED bağlantısı
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
oled = SH1106_I2C(128, 64, i2c)
# DHT22 sensör
sensor = dht.DHT22(Pin(2))
# DIP switch (GP3–GP10)
dip_switches = [Pin(i, Pin.IN, Pin.PULL_UP) for i in range(3, 11)]
# Stepper motor pinleri
step_pins = [Pin(11, Pin.OUT), Pin(12, Pin.OUT), Pin(13, Pin.OUT), Pin(14, Pin.OUT)]
# Step sıralaması
step_seq = [
[1, 0, 1, 0],
[0, 1, 1, 0],
[0, 1, 0, 1],
[1, 0, 0, 1]
]
def read_switch():
return 0 if dip_switches[0].value() == 0 else 1
def step_motor(direction):
steps = step_seq if direction == 1 else step_seq[::-1]
for _ in range(50):
for step in steps:
for pin, val in zip(step_pins, step):
pin.value(val)
time.sleep(0.01)
# Buffer oluştur (128x64)
buf = bytearray(128 * 64 // 8)
fbuf = framebuf.FrameBuffer(buf, 128, 64, framebuf.MONO_VLSB)
def draw_rotated_text(temp, hum):
# Buffera yazı yaz
fbuf.fill(0)
fbuf.text( "Sicaklik: {:>2} " .format(int(temp)), 0, 0)
fbuf.text("Nem : {:>2}".format(int(hum)), 0, 12)
# Buffer'ı 180 derece döndür
rotated = bytearray(len(buf))
for y in range(64):
for x in range(128):
bit = fbuf.pixel(x, y)
new_x = 127 - x
new_y = 63 - y
i = new_x + (new_y // 8) * 128
if bit:
rotated[i] |= (1 << (new_y % 8))
# OLED'e bastır
oled.buffer = rotated
oled.show()
while True:
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
draw_rotated_text(temp, hum)
direction = read_switch()
step_motor(direction)
time.sleep(2)
except Exception as e:
print("HATA:", e)
time.sleep(2)