// ==============================================
// CMPE30274 - FAct5 - Part C: Blynk Cloud + DHT22
// Group #: ___ Section: ___
// Members: [List names]
// Description: Reads temperature and humidity from DHT22 every 2 seconds
// using BlynkTimer and pushes values to Blynk Cloud.
// V0 = Temperature (°C), V1 = Humidity (%)
//
// ── Blynk Setup (Required Before Uploading)
//──────────────────────────
// 1. Log in at https://blynk.cloud
// 2. Create a new Template → name: "ESP32-DHT22", hardware: ESP32, connection: WiFi
// 3. Create Datastreams:
// V0 - Name: Temperature, Data Type: Double, Units: °C, Min: -10, Max: 50
// V1 - Name: Humidity, Data Type: Double, Units: %, Min: 0, Max: 100
// 4. Create a Web Dashboard → add 2 Gauge widgets (V0 and V1)
// 5. Create a Device from the template → copy the Auth Token
// 6. Copy BLYNK_TEMPLATE_ID and BLYNK_TEMPLATE_NAME from theTemplate Settings
//
// ── Library Installation (Arduino IDE)
//───────────────────────────────
// - Sketch → Include Library → Manage Libraries
// Search: "Blynk" → install "Blynk" by Volodymyr Shymanskyy
// Search: "DHT sensor library" → install by Adafruit
// Search: "Adafruit Unified Sensor" → install by Adafruit
//
// ── IMPORTANT: #define ORDER
//──────────────────────────────────
// BLYNK_TEMPLATE_ID and BLYNK_TEMPLATE_NAME MUST appear BEFORE any #include.
// secrets.h (which defines BLYNK_AUTH_TOKEN) must be included AFTER those
// two defines but BEFORE <BlynkSimpleEsp32.h>.
//
// ── Debugging Exercise
//───────────────────────────────────────
// BUG: The BlynkTimer interval is set to 2L milliseconds instead of 2000L.
// This means sendSensorData() runs thousands of times per second,
// flooding Blynk and likely causing disconnections or rate-limit errors.
// Fix the timer interval to send data every 2 seconds (2000L ms).
// ─────────────────────────────────────────────────────
// ==============================================
// ── Step 1: Blynk defines MUST come first (before any #include) ──────
#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID" // TODO: paste your Template ID
#define BLYNK_TEMPLATE_NAME "YOUR_TEMPLATE_NAME" // TODO: paste your Template Name
// ── Step 2: Local credentials (keeps passwords out of source code) ───
#include "secrets.h"
//Defines: WIFI_SSID, WIFI_PASS, BLYNK_AUTH_TOKEN
// ── Step 3: Library includes
//─────────────────────────────────────
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
// ── DHT22 configuration
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
// ── Sensor read + Blynk push
────────────────────────────────────
void sendSensorData() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Validate readings before sending
if (isnan(temperature) || isnan(humidity)) {
Serial.println("[DHT22] Read failed — check wiring and pull-up resistor.");
return;
}
Blynk.virtualWrite(V0, temperature);
Blynk.virtualWrite(V1, humidity);
Serial.printf("[Blynk] Sent → Temp: %.1f °C Humidity: %.1f %%\n",
temperature, humidity);
}
void setup() {
Serial.begin(115200);
delay(1000);
dht.begin();
// Connect to WiFi and Blynk Cloud
Blynk.begin(BLYNK_AUTH_TOKEN, WIFI_SSID, WIFI_PASS);
// BUG: Change 2L to 2000L — BlynkTimer uses milliseconds, not seconds.
// 2L = 2 ms (way too fast); 2000L = 2000 ms = 2 seconds (correct).
timer.setInterval(2L, sendSensorData);
Serial.println("✓ Connected to Blynk Cloud");
Serial.println(" Check your Blynk dashboard for live readings.");
}
void loop() {
Blynk.run(); // Maintain Blynk connection
timer.run(); // Execute scheduled tasks
}