ProjectsProjeler / IOT-02
IoT · remote control · relay output · data loggingIoT · uzaktan kontrol · röle çıkışı · veri kaydı
Telegram-controlled relay, Morse & thermal logTelegram kontrollü röle, Mors ve sıcaklık kaydı
A Raspberry Pi that listens to a Telegram bot. A command from my phone switches a device's power through a relay module, and any text I send is played back as Morse code audio. A second script logs the Pi's CPU temperature next to the room temperature and draws comparison graphs.
Bir Telegram botunu dinleyen bir Raspberry Pi. Telefonumdan gönderdiğim bir komut, röle modülü üzerinden bir cihazın gücünü açıp kapatıyor; gönderdiğim her metin de Mors kodu sesi olarak çalınıyor. İkinci bir script de Pi'nin CPU sıcaklığını oda sıcaklığıyla birlikte kaydedip karşılaştırma grafikleri çiziyor.
Why Telegram
I wanted to control something at home from anywhere without opening ports on the router or writing a phone app. A Telegram bot solves both: the Pi asks Telegram's servers for new messages over normal outgoing HTTPS, so nothing has to reach the Pi from outside, and the chat window becomes the user interface for free.
Commands
| Message | What the Pi does |
|---|---|
on / off | Sets the GPIO pin that drives the relay, which switches the load's power |
morse <text> | Converts the text to dots and dashes and plays it through the relay-driven sounder |
temp | Replies with the current CPU temperature |
The command names above are illustrative; the logic is the same.
A check on the chat ID makes sure that only my account can send commands. Without it, anyone who finds the bot's name could switch the relay.
Neden Telegram
Evdeki bir şeyi, modemde port açmadan ya da bir telefon uygulaması yazmadan her yerden kontrol etmek istedim. Telegram botu ikisini birden çözüyor: Pi, normal giden HTTPS bağlantısıyla Telegram sunucularına yeni mesaj olup olmadığını soruyor, yani dışarıdan Pi'ye hiçbir şeyin ulaşması gerekmiyor; sohbet penceresi de bedavaya kullanıcı arayüzü oluyor.
Komutlar
| Mesaj | Pi ne yapıyor |
|---|---|
on / off | Röleyi süren GPIO pinini ayarlıyor, röle de yükün gücünü açıp kapatıyor |
morse <metin> | Metni nokta ve çizgilere çevirip röleyle sürülen ses elemanından çalıyor |
temp | Anlık CPU sıcaklığıyla cevap veriyor |
Yukarıdaki komut adları örnek amaçlı; mantık aynı.
Sohbet kimliği (chat ID) kontrolü, komutları sadece benim hesabımın gönderebilmesini sağlıyor. Bu olmadan botun adını bulan herkes röleyi açıp kapatabilirdi.
Morse timing
Morse is defined in units, not seconds: a dot is 1 unit on, a dash is 3, the gap inside a letter is 1 unit off, between letters 3 and between words 7. So the encoder is a lookup table plus one loop:
Mors zamanlaması
Mors saniyeyle değil, birimle tanımlanıyor: nokta 1 birim açık, çizgi 3 birim; harf içindeki boşluk 1 birim kapalı, harfler arası 3, kelimeler arası 7. Yani kodlayıcı bir tablo ve tek bir döngüden ibaret:
MORSE = {"A": ".-", "B": "-...", "O": "---", "S": "...", ...}
UNIT = 0.12 # s
def play(text):
for ch in text.upper():
if ch == " ":
sleep(7 * UNIT); continue
for sym in MORSE.get(ch, ""):
relay.on(); sleep(UNIT if sym == "." else 3 * UNIT)
relay.off(); sleep(UNIT)
sleep(2 * UNIT) # 1 + 2 = 3 units between letters
Relay wiring
A GPIO pin gives 3.3 V and only a few milliamps, which is not enough for a relay coil, and the load side may carry much higher voltage. A relay module solves this with a transistor driver and a flyback diode on the board, so the Pi only drives a logic input. The load runs from its own supply and only shares the switching contact with the circuit. Before connecting anything I checked the module's input with a multimeter to see that it really switches at 3.3 V logic.
Thermal logging
The Pi reports its CPU temperature through the Linux thermal interface, in thousandths of a degree:
Röle bağlantısı
Bir GPIO pini 3.3 V ve sadece birkaç miliamper veriyor; bu bir röle bobini için yetmiyor ve yük tarafında çok daha yüksek bir gerilim olabiliyor. Röle modülü bunu üzerindeki transistör sürücüsü ve flyback diyotuyla çözüyor; böylece Pi sadece bir lojik girişi sürüyor. Yük kendi kaynağından besleniyor ve devreyle sadece anahtarlama kontağını paylaşıyor. Herhangi bir şeyi bağlamadan önce modülün girişini multimetreyle kontrol edip 3.3 V lojikle gerçekten anahtarladığını gördüm.
Sıcaklık kaydı
Pi, CPU sıcaklığını Linux'un thermal arayüzü üzerinden derecenin binde biri cinsinden veriyor:
def cpu_temp():
with open("/sys/class/thermal/thermal_zone0/temp") as f:
return int(f.read()) / 1000 # °C
# every N seconds: timestamp, cpu_temp(), room_temp → CSV → Matplotlib
The script writes a timestamp, the CPU temperature and the room temperature to a CSV file at a fixed interval, and a Matplotlib script draws both curves on the same time axis. The interesting value is the difference between them: it shows how much the Pi heats itself above the room, how fast it rises when the bot is busy, and how slowly it cools down afterwards. It is a small, real example of thermal resistance and a thermal time constant.
Concepts used
REST APIs and long polling, authorisation by chat ID, GPIO output, relay drivers and flyback diodes, timing-based encoding, sysfs on Linux, CSV logging and plotting. See the notes page.
Script, sabit aralıklarla zaman damgasını, CPU sıcaklığını ve oda sıcaklığını bir CSV dosyasına yazıyor; bir Matplotlib scripti de iki eğriyi aynı zaman ekseninde çiziyor. Asıl ilginç olan aralarındaki fark: Pi'nin kendini odanın ne kadar üstüne ısıttığını, bot meşgulken sıcaklığın ne kadar hızlı yükseldiğini ve sonra ne kadar yavaş soğuduğunu gösteriyor. Isıl direnç ve ısıl zaman sabitinin küçük ama gerçek bir örneği.
Kullanılan kavramlar
REST API ve long polling, chat ID ile yetkilendirme, GPIO çıkışı, röle sürücüleri ve flyback diyotları, zamanlamaya dayalı kodlama, Linux'ta sysfs, CSV kaydı ve grafik çizimi. Notlar sayfasına bakabilirsiniz.