from machine import Pin, I2C, PWM
import time
from onewire import OneWire
from ds18x20 import DS18X20
import ssd1306
from hx711 import HX711
# --- Pin Tanımlamaları ---
BUZZER_PIN = 3
LED_PIN = 0
DS18B20_PIN = 15
HX711_DT_PIN = 18
HX711_SCK_PIN = 19
# --- Başlangıç Ayarları ---
i2c = I2C(0, sda=Pin(16), scl=Pin(17))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
buzzer = PWM(Pin(BUZZER_PIN))
led = Pin(LED_PIN, Pin.OUT)
# --- Sensör Kurulumları ---
ds_sensor = DS18X20(OneWire(Pin(DS18B20_PIN)))
roms = ds_sensor.scan()
hx = HX711(dt_pin=HX711_DT_PIN, sck_pin=HX711_SCK_PIN)
# --- Eşik Değerler (°C ve kg) ---
LOW_TEMP_THRESHOLD = 10
HIGH_TEMP_THRESHOLD = 35
MIN_WEIGHT_THRESHOLD = 15
MAX_WEIGHT_THRESHOLD = 35
# --- Kalibrasyon Dosya Fonksiyonları ---
def save_calibration(offset, scale):
with open("calibration.txt", "w") as f:
f.write(f"{offset},{scale}")
print("Kalibrasyon dosyaya kaydedildi.")
def load_calibration():
try:
with open("calibration.txt", "r") as f:
offset, scale = map(float, f.read().split(","))
print("Kalibrasyon dosyadan yüklendi.")
return offset, scale
except:
print("Kalibrasyon dosyası yok.")
return None, None
# --- Sensör Okumaları ---
def read_temperature():
ds_sensor.convert_temp()
time.sleep_ms(750)
return ds_sensor.read_temp(roms[0])
def read_weight():
return hx.get_units(5)
# --- OLED ve Konsol Gösterimi ---
def display_status(temp, weight, message):
oled.fill(0)
oled.text("Sicaklik:{:.1f}C".format(temp), 0, 0)
oled.text("Agirlik :{:.1f}kg".format(weight), 0, 10)
oled.text("Durum:", 0, 20)
satir1 = message[:16]
satir2 = message[16:32] if len(message) > 16 else ""
oled.text(satir1, 0, 30)
oled.text(satir2, 0, 40)
oled.show()
print("-----")
print("Sıcaklık: {:.1f} °C".format(temp))
print("Ağırlık : {:.1f} kg".format(weight))
print("Durum : {}".format(message))
print("-----")
# --- Uyarı ve Kontrol Fonksiyonu ---
def play_buzzer(freq=1000, duration=0.5):
buzzer.freq(freq)
buzzer.duty_u16(32768)
time.sleep(duration)
buzzer.duty_u16(0)
def check_conditions(temp, weight):
mesaj = "Her sey normal"
if weight < MIN_WEIGHT_THRESHOLD:
play_buzzer()
mesaj = "Sulama Gerekli"
print("UYARI: Bitkinin suya ihtiyacı var.")
elif weight > MAX_WEIGHT_THRESHOLD:
play_buzzer()
mesaj = "Asiri Sulandi"
print("UYARI: Bitki aşırı sulandı.")
if temp < LOW_TEMP_THRESHOLD:
led.on()
mesaj = "Gunes Isigi Gerek"
print("UYARI: Bitkinin güneşe ihtiyacı var.")
elif temp > HIGH_TEMP_THRESHOLD:
led.on()
mesaj = "Golgede Tutun"
print("UYARI: Bitkiyi güneşten uzaklaştırın.")
else:
led.off()
return mesaj
# --- Ana Döngü ---
def main():
hx.power_up()
time.sleep(2)
offset, scale = load_calibration()
if offset is not None and scale is not None:
hx.offset = offset
hx.set_scale(scale)
else:
hx.tare(20)
print("Tartıya 45kg ağırlık koyun ve 5 saniye bekleyin...")
time.sleep(5)
raw_with_weight = sum(hx.read() for _ in range(20)) / 20
known_weight_kg = 45
known_weight_g = known_weight_kg * 1000
scale = (raw_with_weight - hx.offset) / known_weight_g
hx.set_scale(scale)
save_calibration(hx.offset, scale)
while True:
temp = read_temperature()
weight = read_weight()
status = check_conditions(temp, weight)
display_status(temp, weight, status)
time.sleep(2)
# --- Çalıştır ---
if __name__ == "__main__":
main()