#define BLYNK_TEMPLATE_ID "TMPL6PdOKfW8E"
#define BLYNK_TEMPLATE_NAME "ESP32S3"
#define BLYNK_AUTH_TOKEN "PboC-R0dgxkoGiVJeXyd60JbeXGDIk10"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <DHTesp.h>
// Virtual Pins
#define BLYNK_TEMP V0
#define BLYNK_HUMI V1
#define BLYNK_CITY_TEMP V3
#define BLYNK_CITY_HUMI V4
// DHT sensor
const int DHT_PIN = 21;
DHTesp myDHT22;
// Blynk timers
BlynkTimer timer;
BlynkTimer weather_timer;
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// OpenWeatherMap API details
const String api_key = "14a9d4c4e5a579495d7c10b500d77a45";
const String city = "Busan";
const String country_code = "KR";
String server_path = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + country_code + "&appid=" + api_key + "&units=metric";
// Timer delay (30 seconds)
const unsigned long timer_delay = 30000;
void setup() {
Serial.begin(115200);
Serial.println("Hello from ESP32-S3");
// Setup DHT sensor
myDHT22.setup(DHT_PIN, DHTesp::DHT22);
// Connect to Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password, "blynk.cloud", 80);
// Set intervals
timer.setInterval(1000L, readSensor);
weather_timer.setInterval(timer_delay, weatherUpdate);
}
void loop() {
Blynk.run();
timer.run();
weather_timer.run();
}
void readSensor() {
TempAndHumidity values = myDHT22.getTempAndHumidity();
Blynk.virtualWrite(BLYNK_TEMP, values.temperature);
Blynk.virtualWrite(BLYNK_HUMI, values.humidity);
}
void weatherUpdate() {
if (WiFi.status() == WL_CONNECTED) {
String jsonBuffer = httpGETRequest(server_path.c_str());
JSONVar weather_data = JSON.parse(jsonBuffer);
if (JSON.typeof(weather_data) == "undefined") {
Serial.println("Parsing failed!");
return;
}
double temp = double(weather_data["main"]["temp"]);
double humidity = double(weather_data["main"]["humidity"]);
Blynk.virtualWrite(BLYNK_CITY_TEMP, temp);
Blynk.virtualWrite(BLYNK_CITY_HUMI, humidity);
} else {
Serial.println("WiFi not connected for weather update.");
}
}
String httpGETRequest(const char* server_name) {
HTTPClient http;
http.begin(server_name);
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
payload = http.getString();
} else {
Serial.print("HTTP Error: ");
Serial.println(httpResponseCode);
}
http.end();
return payload;
}Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1