Created
July 9, 2025 09:33
-
-
Save jo0707/b285bb8e50b205dfa6841f7f091398c7 to your computer and use it in GitHub Desktop.
This gist used to test Arduino IDE and PlatformIO IDE performance on my blog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 | |
| WiFiClientSecure 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()); | |
| // Set up Telegram bot | |
| client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org | |
| // 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