#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// LCD Object (0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2); // Ajusta la dirección I2C si es necesario
// WiFi Credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// URL Endpoint for the API
String URL = "http://api.openweathermap.org/data/2.5/weather?";
String ApiKey = "0e5f2b00f7b2f5c4dc61dffb51b2a5a3";
// Location Credentials
String lat = "-36.83298262034296";
String lon = "-73.04985447110695";
// Pin del LED
const int ledPin = 13;
void setup() {
Serial.begin(115200);
// Setup LCD
lcd.init();
lcd.backlight();
lcd.clear();
// Setup LED pin
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Apagar el LED inicialmente
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Wait for WiFi connection
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Set HTTP Request Final URL with Location and API key information
http.begin(URL + "lat=" + lat + "&lon=" + lon + "&units=metric&appid=" + ApiKey);
// Start connection and send HTTP Request
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// Read Data as a JSON string
String JSON_Data = http.getString();
Serial.println(JSON_Data);
// Retrieve some information about the weather from the JSON format
DynamicJsonDocument doc(2048);
deserializeJson(doc, JSON_Data);
JsonObject obj = doc.as<JsonObject>();
// Display the Current Weather Info
const char* description = obj["weather"][0]["description"].as<const char*>();
const float temp = obj["main"]["temp"].as<float>();
const float humidity = obj["main"]["humidity"].as<float>();
lcd.clear();
lcd.print(description);
lcd.setCursor(0, 1);
lcd.print(temp);
lcd.print(" C, ");
lcd.print(humidity);
lcd.print(" %");
// Control the LED based on weather description
if (strcmp(description, "clear sky") == 0) {
digitalWrite(ledPin, HIGH); // Encender el LED
} else {
digitalWrite(ledPin, LOW); // Apagar el LED
}
} else {
Serial.println("Error!");
lcd.clear();
lcd.print("Can't Get DATA!");
digitalWrite(ledPin, LOW); // Apagar el LED si hay un error
}
http.end();
}
// Wait for 30 seconds
delay(30000);
}
El brillo de la pantalla puede modularse
con el potenciometro de la parte trasera.