import network
import urequests
import ujson
import utime
import dht
from machine import Pin, ADC, I2C
from pico_i2c_lcd import I2cLcd
SSID = 'Wokwi-GUEST'
PASSWORD = ''
API_URL = 'https://omniflow-app-sigma.vercel.app/api/sensors'
DEPOT_ID = 'dep_1'
dht_sensor = dht.DHT22(Pin(15))
pot_co2 = ADC(Pin(26))
pot_nh3 = ADC(Pin(27))
buzzer = Pin(16, Pin.OUT)
led_red = Pin(12, Pin.OUT)
led_yellow = Pin(13, Pin.OUT)
led_green = Pin(14, Pin.OUT)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 4, 20)
TR_CHARS = {
"ı": [0x00, 0x00, 0x0C, 0x04, 0x04, 0x04, 0x0E, 0x00],
"ğ": [0x0E, 0x00, 0x0F, 0x11, 0x11, 0x0F, 0x01, 0x0E],
"ş": [0x0F, 0x10, 0x0E, 0x01, 0x1E, 0x04, 0x0E, 0x00],
"ç": [0x00, 0x0E, 0x10, 0x10, 0x11, 0x0E, 0x04, 0x00],
"ö": [0x0A, 0x00, 0x0E, 0x11, 0x11, 0x11, 0x0E, 0x00],
"ü": [0x0A, 0x00, 0x11, 0x11, 0x11, 0x11, 0x0E, 0x00],
"İ": [0x04, 0x00, 0x0E, 0x04, 0x04, 0x04, 0x0E, 0x00]
}
char_map_indices = {"ı": 0, "ğ": 1, "ş": 2, "ç": 3, "ö": 4, "ü": 5, "İ": 6}
for char, index in char_map_indices.items():
lcd.custom_char(index, TR_CHARS[char])
def tr_fix(text):
replaces = {
"ı": 0, "I": 0, "ğ": 1, "Ğ": 1, "ş": 2, "Ş": 2,
"ç": 3, "Ç": 3, "ö": 4, "Ö": 4, "ü": 5, "Ü": 5, "İ": 6
}
for char, index in replaces.items():
text = text.replace(char, chr(index))
return text
# --- 4. SİSTEM FONKSİYONLARI ---
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
lcd.clear()
lcd.putstr(tr_fix("WiFi Baglaniliyor..."))
print("Kapadokya IoT Sistemine Baglaniliyor", end="")
while not wlan.isconnected():
print(".", end="")
utime.sleep(0.5)
print("\n[SISTEM] Wi-Fi Baglantisi Basarili!")
print("IP Adresi:", wlan.ifconfig()[0])
lcd.clear()
lcd.putstr(tr_fix("WiFi Baglandi!"))
utime.sleep(1)
def alert_system(state):
if state == "CRITICAL":
led_red.value(1); led_green.value(0); led_yellow.value(0); buzzer.value(1)
elif state == "WARNING":
led_yellow.value(1); led_green.value(0); led_red.value(0); buzzer.value(0)
else:
led_green.value(1); led_red.value(0); led_yellow.value(0); buzzer.value(0)
def update_lcd(temp, hum, co2, nh3, lcd_msg):
lcd.move_to(0, 0)
lcd.putstr(tr_fix("Isı:{:.1f}C Nem:%{:.0f} ".format(temp, hum))[:20])
lcd.move_to(0, 1)
lcd.putstr("CO2: {} ppm ".format(co2)[:20])
lcd.move_to(0, 2)
lcd.putstr("NH3 Seviye: {} ".format(nh3)[:20])
lcd.move_to(0, 3)
lcd.putstr(tr_fix("{:<20}".format(lcd_msg[:20])))
# --- EŞİK DEĞERLERİ VE ZAMANLAYICILAR ---
TEMP_LIMIT, CO2_LIMIT, NH3_LIMIT = 2.0, 2500, 40000
last_api_send_time = 0
API_SEND_INTERVAL = 10
# --- BAŞLANGIÇ ---
connect_wifi()
lcd.clear()
print("Sistem Başlatılıyor...")
while True:
try:
# --- 1. ÖLÇÜMLER ---
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
co2_raw = pot_co2.read_u16()
nh3_raw = pot_nh3.read_u16()
co2_ppm = int((co2_raw / 65535) * 4600 + 400)
# --- 2. DURUM ANALİZİ ---
active_warnings = []
lcd_status = "DURUM: NORMAL"
alert_state = "SAFE"
if nh3_raw > NH3_LIMIT:
active_warnings.append("!!! ACİL: Amonyak/Çürüme Tespit Edildi!")
lcd_status = "ACİL: AMONYAK/ÇÜRÜME"
alert_state = "CRITICAL"
if temp < TEMP_LIMIT:
active_warnings.append("UYARI: Düşük Sıcaklık")
if alert_state != "CRITICAL":
lcd_status = "UYARI: DÜŞÜK ISI"
alert_state = "WARNING"
if co2_ppm > CO2_LIMIT:
active_warnings.append("UYARI: Yüksek CO2")
if alert_state != "CRITICAL" and lcd_status == "DURUM: NORMAL":
lcd_status = "UYARI: YÜKSEK CO2"
alert_state = "WARNING"
# --- 3. KONSOL LOGLARI ---
print("\n" + "="*40)
print(f"Sıcaklık: {temp}°C | Nem: %{hum}")
print(f"CO2: {co2_ppm} ppm | NH3: {nh3_raw}")
if active_warnings:
for m in active_warnings: print(m)
else:
print("Sistem Durumu: Normal")
print("="*40)
# --- 4. YEREL ÇIKTILARI UYGULA ---
alert_system(alert_state)
update_lcd(temp, hum, co2_ppm, nh3_raw, lcd_status)
# --- 5. VERCEL API'YE VERİ GÖNDERME ---
current_time = utime.time()
if current_time - last_api_send_time >= API_SEND_INTERVAL:
try:
# PRISMA MODELİNE BİREBİR UYGUN PAYLOAD
payload = {
"co2": float(co2_ppm),
"nh3": float(nh3_raw),
"temp": float(temp), # Prisma'daki temp alanı
"humidity": float(hum), # Prisma'daki humidity alanı
"depotId": DEPOT_ID
}
json_data = ujson.dumps(payload)
headers = {'Content-Type': 'application/json'}
print(f"[API] Sunucuya veri iletiliyor: {json_data}")
response = urequests.post(API_URL, headers=headers, data=json_data)
if response.status_code == 201 or response.status_code == 200:
print("[API-BASARILI] Tüm veriler veritabanina yazildi!")
else:
print(f"[API-HATA] Sunucu reddetti. Kod: {response.status_code}")
print(f"Sunucu Yanıtı: {response.text}")
response.close()
except Exception as api_err:
print(f"[API-HATA] İstek gönderilemedi: {api_err}")
last_api_send_time = current_time
except OSError:
print("SENSÖR HATASI!")
lcd.move_to(0, 3)
lcd.putstr(tr_fix("SENSÖR HATASI! "))
utime.sleep(2)