import machine
from machine import Pin, I2C, PWM
import dht
import time
from i2c_lcd import I2cLcd
import network
import urequests
import json
# Konfigurasi WiFi
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
# Konfigurasi Bot Telegram
BOT_TOKEN = "8027026546:AAHBWELBKIBbGho8OZQXd2NL92NWopQ6dSw"
CHAT_ID = 1326019983
# Setup WiFi
def connect_wifi():
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
if not wifi.isconnected():
print('Menghubungkan ke WiFi...')
wifi.connect(WIFI_SSID, WIFI_PASSWORD)
while not wifi.isconnected():
pass
print('Terhubung ke WiFi')
print('IP Address:', wifi.ifconfig()[0])
# Fungsi untuk mengirim pesan ke Telegram
def send_telegram_message(message):
try:
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage" # Menggunakan BOT_TOKEN dari variabel
headers = {'Content-Type': 'application/json'}
data = {
"chat_id": CHAT_ID,
"text": message,
"parse_mode": "HTML" # Tambahkan support untuk formatting HTML
}
print(f"Mengirim pesan ke Telegram: {message}") # Debug print
response = urequests.post(url, headers=headers, data=json.dumps(data))
print(f"Status respon Telegram: {response.status_code}") # Debug print
print(f"Respon Telegram: {response.text}") # Debug print
response.close()
return True
except Exception as e:
print(f"Error saat mengirim pesan Telegram: {str(e)}")
return False
# Setup I2C dan LCD
i2c = I2C(scl=Pin(19), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, I2cLcd.LCD_I2C_ADDR, 4, 20)
# Setup alarm dan sensor
alarm_pin = machine.Pin(15, machine.Pin.OUT)
sensor = dht.DHT22(Pin(13))
# Setup servo dengan frekuensi 50Hz
servo = PWM(Pin(17), freq=50)
def set_servo_angle(angle):
min_duty = 20
max_duty = 120
duty = int(((max_duty - min_duty) * angle / 180) + min_duty)
servo.duty(duty)
# Hubungkan ke WiFi saat startup
connect_wifi()
# Inisialisasi status api dan posisi awal servo
fire_detected = False
current_angle = 90 # Posisi awal 90 derajat (kondisi normal)
set_servo_angle(current_angle) # Set posisi awal servo
# Variable untuk tracking waktu notifikasi terakhir
last_notification_time = 0
NOTIFICATION_INTERVAL = 2 # Interval minimal antar notifikasi (dalam detik)
while True:
try:
current_time = time.time()
sensor.measure()
temp = sensor.temperature()
humi = sensor.humidity()
# Menampilkan data suhu dan kelembapan pada LCD
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Temp: {:.1f}C".format(temp))
lcd.move_to(0, 1)
lcd.putstr("Humidity: {:.1f}%".format(humi))
# Kondisi deteksi api
if temp >= 60 or humi <= 20:
if not fire_detected: # Status baru berubah menjadi terdeteksi api
fire_detected = True
alarm_pin.value(1)
set_servo_angle(180)
current_angle = 180
# Kirim notifikasi ke Telegram
if current_time - last_notification_time >= NOTIFICATION_INTERVAL:
message = f"⚠️ PERINGATAN KEBAKARAN! ⚠️\nSuhu: {temp:.1f}°C\nKelembaban: {humi:.1f}%\nLokasi: Ruang Server"
send_telegram_message(message)
last_notification_time = current_time
lcd.move_to(0, 2)
lcd.putstr("Status: Fire!")
else:
if fire_detected: # Status baru berubah menjadi normal
fire_detected = False
alarm_pin.value(0)
set_servo_angle(90)
current_angle = 90
# Kirim notifikasi kondisi aman ke Telegram
if current_time - last_notification_time >= NOTIFICATION_INTERVAL:
message = f"✅ Kondisi Kembali Normal\nSuhu: {temp:.1f}°C\nKelembaban: {humi:.1f}%\nLokasi: Ruang Server"
send_telegram_message(message)
last_notification_time = current_time
lcd.move_to(0, 2)
lcd.putstr("Status: Normal")
time.sleep(1)
except Exception as e:
print("Error:", e)
time.sleep(2)
continue