Telegram Bot Controlled Raspberry Pi

This project demonstrates how to control a Raspberry Pi using a Telegram bot. The bot allows you to remotely control the Pi's GPIO pins, such as turning on an LED or reading sensor data from anywhere in the world.

1. Introduction to Telegram Bot Controlled Raspberry Pi

In this project, we will create a Telegram bot that allows you to control your Raspberry Pi remotely. You can send commands to the bot to perform actions like turning an LED on or off, reading sensor data, or executing any other GPIO-based task on the Raspberry Pi.

2. What You Need: Components and Tools

Required Components:

  • Raspberry Pi (any model with GPIO pins, preferably Pi 3 or Pi 4)
  • LED
  • 220Ω Resistor
  • Jumper Wires
  • Breadboard
  • Power Supply for Raspberry Pi
  • Telegram account and Telegram Bot API token

3. Setting Up the Telegram Bot

To control your Raspberry Pi using Telegram, first, you need to create a Telegram bot and get its API token. Follow these steps:

  1. Open Telegram and search for the 'BotFather'.
  2. Start a chat with 'BotFather' and type `/newbot` to create a new bot.
  3. Follow the prompts to choose a name and username for your bot.
  4. Once your bot is created, you will receive an API token. Save this token for use in the code.

4. Wiring Diagram: Raspberry Pi and LED

LED to Raspberry Pi Connections:

  • Anode (long leg) → GPIO17 (Pin 11)
  • Cathode (short leg) → 220Ω Resistor → GND (Pin 6)

5. Setting Up the Raspberry Pi for Telegram Bot Control

  1. Install Python Telegram library: `pip install python-telegram-bot`
  2. Install RPi.GPIO library: `sudo apt-get install python-rpi.gpio`
  3. Create a new Python script to control the Raspberry Pi's GPIO based on messages received by the Telegram bot.
import telebotimport RPi.GPIO as GPIOimport time# Set up GPIO pinGPIO.setmode(GPIO.BCM)GPIO.setup(17, GPIO.OUT)# Replace with your Telegram bot tokenTOKEN = 'YOUR_BOT_API_TOKEN'bot = telebot.TeleBot(TOKEN)# Handle '/on' command to turn on the LED@bot.message_handler(commands=['on'])def turn_on_led(message):    GPIO.output(17, GPIO.HIGH)    bot.reply_to(message, 'LED turned ON!')# Handle '/off' command to turn off the LED@bot.message_handler(commands=['off'])def turn_off_led(message):    GPIO.output(17, GPIO.LOW)    bot.reply_to(message, 'LED turned OFF!')# Handle '/status' command to check LED status@bot.message_handler(commands=['status'])def status(message):    led_status = 'ON' if GPIO.input(17) == GPIO.HIGH else 'OFF'    bot.reply_to(message, f'LED is {led_status}.')# Polling to listen for messagesbot.polling()

6. Code Explanation

  • The code starts by importing necessary libraries and setting up the GPIO pin.
  • We use the `telebot` library to interact with the Telegram Bot API. The `TOKEN` variable is where you put your bot's API token.
  • We define three handlers for the commands `/on`, `/off`, and `/status` that control the LED connected to GPIO17. The bot responds to these commands by turning the LED on or off, or by reporting its current status.

7. Running the Telegram Bot on Raspberry Pi

  1. Save the Python script and run it on your Raspberry Pi: `python3 telegram_bot.py`
  2. Open your Telegram app and start a chat with your bot.
  3. Use the following commands to control the LED on the Raspberry Pi:
  4. - `/on` to turn the LED on.
  5. - `/off` to turn the LED off.
  6. - `/status` to check if the LED is on or off.

8. Applications

  • Remote control of home automation systems
  • Monitor and control devices over Telegram
  • IoT-based communication and control systems
  • Remote sensor data collection and management

9. FAQs: Telegram Bot Controlled Raspberry Pi

Q: How can I add more devices to control?

A: You can add more GPIO pins and write new command handlers in the code to control additional devices, such as fans, motors, or sensors.

Q: Can I use this bot to monitor sensor data?

A: Yes, you can modify the code to read data from sensors (e.g., temperature sensors) and send the readings to the Telegram bot.

Q: How do I run the bot in the background?

A: You can run the bot as a background service on your Raspberry Pi using tools like `screen`, `tmux`, or by setting it up as a system service.

10. Conclusion: What You’ve Learned

  • How to create and set up a Telegram bot for remote control.
  • How to interact with Raspberry Pi GPIO pins using Telegram messages.
  • How to remotely control devices like LEDs using a Telegram bot.

11. Resources and References