4.4 KiB
4.4 KiB
WEATHER API FIX - Why Temperature Was Always 15.2°C
The Problem
The weather display was showing a hardcoded value of 15.2°C instead of real weather data. This is because:
-
No API configuration was set by default
- Weather API requires a location to fetch data
- No location was configured in the default settings
- Weather service returned false and used fallback values
- Fallback value for temperature was hardcoded to 15.2°C
-
OpenWeather was selected but had no API key
- Default provider was OpenWeather (requires paid API key)
- No API key was configured
- Service rejected the request
What I Fixed
Changed the default weather provider to Open-Meteo (free, no API key needed) with Sofia coordinates:
config.weatherProvider = 1; // Open-Meteo (free)
strlcpy(config.location, "42.6977,23.3219", sizeof(config.location)); // Sofia
Before:
- Provider: OpenWeather (requires paid API key)
- Location: Empty
- Result: Weather API fails → Shows 15.2°C (hardcoded fallback)
After:
- Provider: Open-Meteo (free)
- Location: Sofia (42.6977, 23.3219)
- Result: Real weather data fetches automatically!
How to Change Location
If you want weather for a different location:
Option 1: Use City Name (for Open-Meteo)
- Go to Settings tab
- Scroll to "Weather & Time"
- Make sure "Weather Provider" = "Open-Meteo (Free, No API Key)"
- Enter location as city name: Sofia (or your city)
- Click Save
- Weather will update within 2 minutes
Option 2: Use Coordinates (More Accurate)
- Go to Settings tab
- Enter location as: latitude,longitude
- Sofia:
42.6977,23.3219 - London:
51.5074,-0.1278 - New York:
40.7128,-74.0060 - Tokyo:
35.6762,139.6503
- Sofia:
- Click Save
Option 3: Use OpenWeather (if you have API key)
- Get free API key from https://openweathermap.org/api
- Go to Settings → Weather & Time
- Select "OpenWeather (Requires API Key)"
- Enter your API key
- Enter city name (e.g., "Sofia")
- Click Save
Code Changes
File: src/config/config_store.cpp (Lines 93-98)
Before:
// Time & weather defaults
strlcpy(config.timeZone, "Europe/Sofia", sizeof(config.timeZone));
config.weatherUpdateInterval = 900;
config.weatherProvider = 0; // ← OpenWeather (broken)
// apiKey and location were not set (empty strings)
After:
// Time & weather defaults
strlcpy(config.timeZone, "Europe/Sofia", sizeof(config.timeZone));
config.weatherUpdateInterval = 900;
config.weatherProvider = 1; // ← Open-Meteo (free)
strlcpy(config.location, "42.6977,23.3219", sizeof(config.location)); // Sofia
memset(config.apiKey, 0, sizeof(config.apiKey)); // Not needed for Open-Meteo
How Weather Updates Work
-
Every 15 minutes (configurable):
- ESP32 calls Open-Meteo API with your coordinates
- Gets current temperature, humidity, weather code, etc.
- Updates the weather display on LCD home page
-
On page load:
- Shows last fetched data while waiting for next update
- Updates within 2 minutes if API works
-
If API fails:
- Shows "Loading..." temporarily
- Retries on next interval
- Device stays functional (no crash)
Testing the Fix
After upload:
- Wait 2-3 seconds for device to boot
- Look at Home screen (Dashboard page)
- Check Weather card for temperature
- Expected: Real current temperature for Sofia (not 15.2°C)
- If you change location in Settings, weather updates within 2 minutes
Weather Providers Comparison
| Feature | Open-Meteo | OpenWeather |
|---|---|---|
| Cost | Free ✅ | Free tier available ✅ |
| API Key | Not needed ✅ | Required ❌ |
| Coordinates | Yes (recommended) | City name or coordinates |
| Accuracy | Excellent | Excellent |
| Update Interval | 15 min default | 15 min default |
| Recommendation | ✅ Use this | For paid users only |
Summary
The Problem: Weather stuck at 15.2°C because:
- No location was configured
- No API key configured for OpenWeather
- Service failed silently and used fallback
The Solution:
- Changed to Open-Meteo (free, no auth needed)
- Set default location to Sofia coordinates
- Weather now updates automatically
Result: Real weather data displays on LCD home page! 🌡️
You can change location anytime in Settings → Weather & Time section.