M5stack Card Computer Project

PHOTO EMBED

Fri Apr 04 2025 14:32:52 GMT+0000 (Coordinated Universal Time)

Saved by @yummyo

/* M5Card Basic Dashboard - Time, Temperature, Weather */
#include <M5Cardputer.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

// WiFi Credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// OpenWeather API
const char* apiKey = "YOUR_OPENWEATHER_API_KEY";
const char* city = "Kuala Lumpur";
String weatherURL = "http://api.openweathermap.org/data/2.5/weather?q=" + String(city) + "&appid=" + apiKey + "&units=metric";

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 28800, 60000);

void setup() {
    M5Cardputer.begin();
    M5Cardputer.Display.setTextFont(2);
    M5Cardputer.Display.clear();
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
    }
    timeClient.begin();
}

void loop() {
    timeClient.update();
    M5Cardputer.Display.clear();
    M5Cardputer.Display.setCursor(10, 20);
    M5Cardputer.Display.printf("Time: %s", timeClient.getFormattedTime().c_str());

    if (WiFi.status() == WL_CONNECTED) {
        HTTPClient http;
        http.begin(weatherURL);
        int httpCode = http.GET();
        if (httpCode > 0) {
            String payload = http.getString();
            DynamicJsonDocument doc(1024);
            deserializeJson(doc, payload);
            float temp = doc["main"]["temp"];
            String weather = doc["weather"][0]["description"];
            M5Cardputer.Display.setCursor(10, 50);
            M5Cardputer.Display.printf("Temp: %.1f C", temp);
            M5Cardputer.Display.setCursor(10, 80);
            M5Cardputer.Display.printf("Weather: %s", weather.c_str());
        }
        http.end();
    }
    delay(10000);
}
content_copyCOPY