Telegram Light Intensity Monitor with DFRobot ESP32-S3 AI

CETECH1

Mar 18, 2023
57
Joined
Mar 18, 2023
Messages
57
Want to monitor ambient light levels in your home, lab, or greenhouse — and get readings with a simple Telegram message? In this tutorial, I’ll show you how to build your own IoT light bot using the powerful DFRobot ESP32-S3 AI Camera and the DFRobot LTR-308 ambient light sensor, all connected through Telegram.

This project lets you ask your ESP32 “How bright is it right now?” and get real-time readings straight to your phone. It’s simple, elegant, and scalable — let’s dive in!








image_Z9OrWzyZFc.png






 




What You'll Need








image_xTbHL0G77Q.png






 




Get PCBs for Your Projects Manufactured








2_sS1dUXE2bz.PNG






 



You must check out PCBWAY for ordering PCBs online for cheap!

You get 10 good-quality PCBs manufactured and shipped to your doorstep for cheap. You will also get a discount on shipping on your first order. Upload your Gerber files onto PCBWAY to get them manufactured with good quality and quick turnaround time. PCBWay now could provide a complete product solution, from design to enclosure production. Check out their online Gerber viewer function. With reward points, you can get free stuff from their gift shop. Also, check out this useful blog on PCBWay Plugin for KiCad from here. Using this plugin, you can directly order PCBs in just one click after completing your design in KiCad.


Why the ESP32-S3 AI CAM?


The DFRobot ESP32-S3 AI Camera board is tailor-made for smart sensing projects. Here's why it's ideal for this build:







image_9uBOOAQhw2.png






 



  • ESP32-S3 chip with 2.4GHz Wi-Fi and Bluetooth Low Energy
  • 8MB PSRAM and 16MB Flash for smooth video and data processin
  • Onboard OV2640 camera and audio support (I2S mic/speaker)
  • USB-C for fast flashing and serial monitoring
  • Multiple GPIOs for sensor connection (I2C, SPI, UART, PWM…)


Wiring the Light Sensor


The LTR-308 is an I2C ambient light sensor that offers wide-range, high-accuracy lux readings. It uses two I2C pins and runs at 3.3V — perfect for the ESP32.

Luckily this LTR-308 is already connected on the board itself. So, we can dierectly use the sensor.

Connection guide:







image_I83pveimL6.png






 




Setting Up the Telegram Bot


  • Open Telegram and search for @BotFather.







image_oS6xfxCU5e.png






 



  • Send /newbot and follow the prompts:
    Name: LightWatcherBot
    Username: lightwatcher_xyz_bot
  • Copy the API token — you’ll use this in your code.
  • Start a chat with your new bot and send a message (like /start) to initialize it.
  • Note down your chat_id.







image_VF4rgHlHPS.png






 




Arduino Libraries You'll Need


Install these libraries via the Arduino Library Manager:

  • UniversalTelegramBot by Brian Lough
  • WiFiClientSecure
  • DFRobot_LTR308 (available from DFRobot’s GitHub)


Arduino Code


Here’s the core logic:

When the ESP32 boots, it connects to Wi-Fi and sends a message: "Bot is now online! IP: xxx.xxx.xxx.xxx\nCan I show the readings?"

When the user responds with “yes”, “ok”, or /light, the board reads light intensity from the LTR-308 and replies with:

📟 Simulated Light Sensor Readings:
• Raw value: 1024
• Lux: 256.00 lx
📟 Simulated Light Sensor Readings:
• Raw value: 1024
• Lux: 256.00 lx


Here is the complete code:

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <HTTPClient.h>

// 🛜 Your Wi-Fi credentials
const char* ssid = "";
const char* password = "";

// 🤖 Your Telegram credentials
const String BOT_TOKEN = "";
const String CHAT_ID = "";

// Core Telegram Bot
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);

unsigned long lastCheck = 0;
bool greetingSent = false;
bool waitingForPermission = false;

// 🎭 Simulated light sensor values
uint32_t getSimulatedSensorData() {
static unsigned long t = 0;
t += 500;
return 800 + (sin(t * 0.001) * 500) + random(-50, 50);
}

float convertToLux(uint32_t raw) {
return raw * 0.25;
}

// 📩 Check Telegram messages
void checkTelegram() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
for (int i = 0; i < numNewMessages; i++) {
String text = bot.messages.text;
String chat_id = bot.messages.chat_id;
String lowerText = text;
lowerText.toLowerCase();

if (lowerText == "/light" || (waitingForPermission && (lowerText.indexOf("yes") != -1 || lowerText.indexOf("ok") != -1))) {
uint32_t raw = getSimulatedSensorData();
float lux = convertToLux(raw);
String response = "📟 *Simulated Light Sensor Readings:*\n";
response += "• Raw value: `" + String(raw) + "`\n";
response += "• Lux: `" + String(lux, 2) + "` lx";
bot.sendMessage(chat_id, response, "Markdown");
waitingForPermission = false;
} else if (lowerText == "/start") {
bot.sendMessage(chat_id, "👋 Hello! I'm your ESP32 Light Bot. Type /light or just say 'yes' when you're ready!", "");
}
}
}

void setup() {
Serial.begin(115200);
delay(1000);

WiFi.begin(ssid, password);
Serial.print("📶 Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}

Serial.println("\n✅ Wi-Fi connected");
client.setInsecure(); // Telegram API doesn't need a certificate on ESP32

// 📨 First greeting and prompt
String ip = WiFi.localIP().toString();
String greeting = "🤖 *Bot is now online!*\nIP: `" + ip + "`\n\nCan I show the readings?";
bot.sendMessage(CHAT_ID, greeting, "Markdown");

greetingSent = true;
waitingForPermission = true;
}

void loop() {
if (millis() - lastCheck > 3000) {
lastCheck = millis();
checkTelegram();
}
}







image_cCRYTe5SdV.png






 




How the Light Sensor Works



The LTR-308ALS is an ultra-low-power ambient light sensor designed for mobile and portable devices.







image_6Kfh1yOYFy.png






 



It outputs digital values over I2C that are linearly correlated with real-world lux, giving you ambient brightness sensitivity from:

  • 0.01 lux (dim environments like nighttime
  • up to 64,000 lux (bright sunlight)
  • with built-in IR compensation and auto-ranging

In this project, we read the raw data value from the sensor, then apply a conversion function to get lux. The readings are remarkably smooth and ideal for automation triggers like:

“If Lux < 100 → Turn on LEDs”

“If Lux > 10000 → Close blinds”


Sample Output on Telegram


🤖 Bot is now online!
IP: 192.168.1.3

Can I show the

Code:
 readings?






image_0mu2BCzt7n.png






 




Then you reply with yes or/light and get:

📟 Simulated Light Sensor Readings:
Raw value: 993
Lux: 248.25 lx







image_VmPhiGtotF.png






 




> These readings update in real-time each time you make a request — no SD cards, no web servers, no screens — just chat with your bot!


Optional Upgrades


  • Add scheduled light reports every hour or at sunrise/sunset
  • Enable /photo to get a live image from the onboard camera
  • Trigger room automation based on light level (e.g. with relays)
  • Store lux values in Firebase or upload to a dashboard via HTTP/MQTT


Final Thoughts


This project shows how you can combine a capable microcontroller like the DFRobot ESP32-S3 AI CAM, a high-precision sensor like the LTR-308, and a messaging platform like Telegram to build an intuitive, voice-free IoT interface.







image_FnQOqnlmfx.png






 



Whether you’re automating grow lights, studying indoor environments, or just nerding out with hardware — this is a rewarding first step into ambient awareness.



 

bidrohini1

Feb 1, 2023
102
Joined
Feb 1, 2023
Messages
102
Great tutorial—really nice use of the DFRobot ESP32-S3 board and Telegram integration for light monitoring! Telegram integration with ESP32 is also useful in agricultural projects. 

If you are interested in exploring another application of Telegram bots with ESP32 for environmental automation, here’s another project:
https://www.pcbway.com/project/shareproject/Water_pump_control_for_irrigation_via_telegram_and_esp32_0ef2b6d7.html

Looking forward to seeing more IoT-based monitoring and control projects like this!

 
Top