ESP8266 Telegram LED Control IoT Project
Controlling an LED using an ESP8266 and a Telegram bot is a great Internet of Things (IoT) project that allows you to remotely control an LED using your smartphone. Here are the basic steps you need to follow:
Step 1: Gather the Materials
- ESP8266 board
- LED
- Resistor (220 ohm)
- Breadboard
- Jumper wires
- USB cable
- Computer with Arduino IDE software installed
- Smartphone with Telegram app installed
Step 2: Set up the Telegram bot
- Create a new bot on Telegram by following the instructions on the Telegram documentation.
- Obtain the API token for your bot.
- Start a conversation with your bot on Telegram.
- Obtain your chat ID by sending a message to the bot and then visiting the following URL in your web browser: https://api.telegram.org/bot<API_token>/getUpdates (replace <API_token> with your actual API token). Look for the "chat" section in the response and copy the chat ID.
Step 3: Set up the ESP8266 board
- Connect the LED to the ESP8266 board with the resistor in series with the LED. The resistor limits the current flowing through the LED, which protects it from burning out.
- Connect the ESP8266 board to your computer using a USB cable.
- Open the Arduino IDE and select the appropriate board and port in the "Tools" menu.
- Download and install the Telegram bot library for Arduino by following the instructions on the GitHub repository: https://github.com/witnessmenow/arduino-telegram-bot
- Copy and paste the following code into the Arduino IDE:
arduino
#include <UniversalTelegramBot.h>
#include <ESP8266WiFi.h>
#define LED_PIN 2
// Replace with your Wi-Fi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// Replace with your Telegram bot token and chat ID
#define BOT_TOKEN "YOUR_BOT_TOKEN"
#define CHAT_ID YOUR_CHAT_ID
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
bot.begin();
}
void loop() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("New message(s) received:");
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
if (text == "/on") {
digitalWrite(LED_PIN, HIGH);
bot.sendMessage(chat_id, "LED turned on");
} else if (text == "/off") {
digitalWrite(LED_PIN, LOW);
bot.sendMessage(chat_id, "LED turned off");
} else {
bot.sendMessage(chat_id, "Invalid command");
}
}
bot.last_message_received += numNewMessages;
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
delay(1000);
}
- Replace "YOUR_SSID", "YOUR_PASSWORD", "YOUR_BOT_TOKEN", and "YOUR_CHAT_ID" with your actual Wi-Fi credentials, Telegram bot token, and chat ID, respectively.
- Upload the code to the ESP8266 board.
Step 4: Test the project
- Open the Telegram app on your smartphone and start a conversation
Posting Komentar untuk "ESP8266 Telegram LED Control IoT Project"