Skip to content

Instantly share code, notes, and snippets.

@jo0707
Last active July 9, 2025 10:37
Show Gist options
  • Save jo0707/1c583966fc80b6022353438c7a1e8526 to your computer and use it in GitHub Desktop.
Save jo0707/1c583966fc80b6022353438c7a1e8526 to your computer and use it in GitHub Desktop.
This gist is a sample ESP32 code to test Arduino IDE and PlatformIO IDE compilation performance while using Firebase and Telegram library. Read the blog here: https://blog.itsjo.works/improve-iot-development-from-arduino-ide-to-platformio-ide
#include <Arduino.h>
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// --- Replace with your credentials ---
#define WIFI_SSID "YOUR_WIFI_SSID"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"
#define FIREBASE_API_KEY "YOUR_FIREBASE_API_KEY"
#define FIREBASE_DATABASE_URL "YOUR_FIREBASE_DATABASE_URL"
#define BOT_TOKEN "YOUR_TELEGRAM_BOT_TOKEN"
#define CHAT_ID "YOUR_TELEGRAM_CHAT_ID"
// --- End of credentials ---
// Firebase objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
// Telegram Bot objects
WiFiClient client;
UniversalTelegramBot bot(BOT_TOKEN, client);
unsigned long lastTimeBotChecked;
const unsigned long botRequestDelay = 1000; // Check for new messages every 1 second
// Function to handle new Telegram messages
void handleNewMessages(int numNewMessages) {
Serial.println("Handling new messages...");
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
Serial.print("Received message from ");
Serial.print(from_name);
Serial.print(": ");
Serial.println(text);
if (text.startsWith("/update_firebase")) {
// Command to update Firebase. Example: /update_firebase myValue
String value = text.substring(text.indexOf(" ") + 1);
if (value != "") {
if (Firebase.RTDB.setString(&fbdo, "/telegram/data", value)) {
bot.sendMessage(chat_id, "Firebase updated successfully with value: " + value, "");
} else {
bot.sendMessage(chat_id, "Failed to update Firebase.", "");
Serial.println(fbdo.errorReason());
}
} else {
bot.sendMessage(chat_id, "Please provide a value to update.", "");
}
} else {
bot.sendMessage(chat_id, "Unknown command. Use /update_firebase [value]", "");
}
}
}
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
// Firebase configuration
config.api_key = FIREBASE_API_KEY;
config.database_url = FIREBASE_DATABASE_URL;
// Assign the user sign-in credentials
auth.user.email = "[email protected]"; // You can use a dummy email for anonymous auth
auth.user.password = "password";
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
// Set up a stream to listen for Firebase changes (optional)
// if (!Firebase.RTDB.beginStream(&fbdo, "/telegram/data")) {
// Serial.println("Could not begin Firebase stream");
// }
bot.sendMessage(CHAT_ID, "ESP32 Bot is now online!", "");
}
void loop() {
if (millis() > lastTimeBotChecked + botRequestDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotChecked = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment