Este código es un programa para un dispositivo que utiliza una pantalla TFT para mostrar la hora actual y la temperatura en Omaha, NE, obtenida de la API de OpenWeatherMap. Aquí hay una explicación en 10 líneas:
1. Se incluyen las bibliotecas necesarias para la pantalla TFT, SPI, WiFi, NTPClient y HTTPClient.
2. Se crea una instancia de la clase TFT_eSPI para controlar la pantalla TFT.
3. Se establecen las credenciales de la red WiFi y se configura el cliente NTP para obtener la hora actual.
4. Se inicializa la pantalla TFT y se conecta a la red WiFi.
5. En el bucle principal, se actualiza la hora cada 10 segundos utilizando el cliente NTP.
6. Se obtiene la temperatura actual de Omaha a través de la API de OpenWeatherMap.
7. La respuesta JSON se procesa para extraer la temperatura en grados Celsius.
8. La temperatura se muestra en la pantalla TFT junto con la hora actual.
9. El bucle principal espera 10 segundos antes de volver a actualizar la hora y la temperatura.
10. Se manejan posibles errores en la obtención de la temperatura, y se imprime información de depuración en el puerto serie.
ELEMENTOS:
- Pantalla lcd spi 3.5 ili9488 ( aliexpress )
- esp32 wroom Teyleten Robot 38pin ( amazon )
(el esp32 es distinto al que aparece en la imagen pero puedes usar los mismos numeros de pines)
Conexiones:
Codigo INO:
#include <TFT_eSPI.h> | |
#include <SPI.h> | |
#include <WiFi.h> | |
#include <NTPClient.h> | |
#include <HTTPClient.h> | |
TFT_eSPI tft = TFT_eSPI(); | |
const char* ssid = "tu ssid"; // coloca el nombre de tu red 2.4g | |
const char* password = "password"; // coloca la contraseña | |
WiFiUDP ntpUDP; | |
NTPClient timeClient(ntpUDP); | |
const char* ntpServerName = "time.nist.gov"; | |
const char* weatherApiUrl = "http://api.openweathermap.org/data/2.5/weather?q=Omaha,NE,US&appid=30cd9db64953cf977e5269aa6a543a06"; | |
unsigned long targetTime = 0; | |
float temperature = 0.0; | |
void setup() { | |
Serial.begin(115200); | |
tft.init(); | |
tft.setRotation(0); | |
tft.fillScreen(TFT_BLACK); | |
tft.setTextColor(TFT_WHITE, TFT_BLACK); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
timeClient.begin(); | |
timeClient.setTimeOffset(-6 * 3600); | |
} | |
void loop() { | |
if (targetTime < millis()) { | |
timeClient.update(); | |
unsigned long epochTime = timeClient.getEpochTime(); | |
int hours = timeClient.getHours(); | |
int minutes = timeClient.getMinutes(); | |
int seconds = timeClient.getSeconds(); | |
int xpos = 0; | |
int ypos = 10; | |
int ysecs = ypos + 24; | |
tft.setTextSize(1); // Cambia el tamaño de fuente a '1' para todos los números | |
if (hours < 10) xpos += tft.drawChar('0', xpos, ypos, 8); | |
xpos += tft.drawNumber(hours, xpos, ypos, 8); | |
xpos += tft.drawChar(':', xpos, ypos - 8, 8); | |
if (minutes < 10) xpos += tft.drawChar('0', xpos, ypos, 8); | |
xpos += tft.drawNumber(minutes, xpos, ypos, 8); | |
xpos += tft.drawChar(':', xpos, ysecs, 6); | |
if (seconds < 10) xpos += tft.drawChar('0', xpos, ysecs, 6); | |
tft.drawNumber(seconds, xpos, ysecs, 6); | |
// Obtén la temperatura de Omaha | |
if (WiFi.status() == WL_CONNECTED) { | |
HTTPClient http; | |
http.begin(weatherApiUrl); | |
int httpResponseCode = http.GET(); | |
Serial.print("HTTP Response Code: "); | |
Serial.println(httpResponseCode); | |
if (httpResponseCode == 200) { | |
String payload = http.getString(); | |
// Procesa el JSON para extraer la temperatura | |
int tempIndex = payload.indexOf("\"temp\":"); | |
if (tempIndex > 0) { | |
tempIndex += 7; | |
String tempValue = payload.substring(tempIndex, tempIndex + 5); | |
Serial.println("Temperature JSON Part:"); | |
Serial.println(tempValue); | |
// Convierte la temperatura de kelvins a grados Celsius | |
float kelvinTemperature = tempValue.toFloat(); | |
float celsiusTemperature = kelvinTemperature - 273.15; | |
// Imprime la cadena JSON completa | |
Serial.println("Full JSON Response:"); | |
Serial.println(payload); | |
// Imprime la temperatura en grados Celsius | |
Serial.print("Temperature in Celsius: "); | |
Serial.println(celsiusTemperature); | |
// Intenta convertir la temperatura solo si no está en el rango de valores incorrectos | |
if (celsiusTemperature > -100 && celsiusTemperature < 100) { | |
temperature = celsiusTemperature; | |
tft.setTextSize(5); | |
tft.setCursor(70, ysecs + 80); | |
//tft.print("Temp: "); | |
tft.print(temperature); | |
tft.print(" C"); // Agrega el símbolo de grados Celsius con "o" | |
} else { | |
Serial.println("Invalid temperature value."); | |
} | |
} else { | |
Serial.println("Temperature JSON part not found."); | |
} | |
} else { | |
Serial.println("Failed to get temperature. Check response code."); | |
} | |
http.end(); | |
} | |
targetTime = millis() + 10000; | |
} | |
} |