RadiaCode-Spectrometer/ESP32-S3-Touch/_archive_previous_project/WEB_UI_FIXES.md

8.5 KiB
Raw Permalink Blame History

🔧 Web UI Fixes - API Data Mapping Issues

Problem Summary

The Web UI was not displaying data correctly because the JavaScript expected different field names than what the C++ API was actually returning.


Issues Found & Fixed

1. ❌ WiFi Signal Not Displaying

Problem: JS expected data.rssi but API returns data.wifiRSSI
Fix: Changed data.rssi → data.wifiRSSI

2. ❌ Uptime Not Displaying

Problem: JS expected data.uptimeMs (milliseconds) but API returns data.uptime (seconds)
Fix: Changed formatUptime(data.uptimeMs) → formatUptime((data.uptime || 0) * 1000)

3. ❌ Weather Data Not Displaying

Problem: JS expected data.weather.temperature but API returns weather at root level as data.extTemp
Fix:

  • Changed data.weather.temperature → data.extTemp
  • Changed data.weather.humidity → data.humidity
  • Changed data.weather.windSpeed → data.windKph
  • Changed data.weather.pressure → data.pressure
  • Changed data.weather.description → getWeatherDescription(data.weatherCode)

4. ❌ Weather Description Missing

Problem: API returns weather code (WMO standard) not text description
Fix: Added getWeatherDescription() function to convert weather codes to human-readable text

5. ❌ LCD Preview Canvas Empty

Problem: JS was drawing grid on builder-canvas but dashboard uses lcd-preview
Fix:

  • Added initLcdPreview() function for dashboard canvas
  • Added initBuilderCanvas() function for builder page canvas
  • Both canvases now initialize correctly on page load

6. ❌ Refresh Button Not Working

Problem: No event handler for LCD preview refresh button
Fix: Added setupDashboard() function with refresh button handler

7. ⚠️ CPU Temp & Sensor Status Not Available

Problem: API doesn't provide cpuTemp or sensorOk fields yet
Workaround: Display placeholder values until backend implements these


API Field Mapping Reference

UI Display Old JS Field (Wrong) Correct API Field Status
WiFi Signal data.rssi data.wifiRSSI ✅ Fixed
Device Uptime data.uptimeMs data.uptime (seconds) ✅ Fixed
Weather Temp data.weather.temperature data.extTemp ✅ Fixed
Weather Desc data.weather.description data.weatherCode ✅ Fixed
Humidity data.weather.humidity data.humidity ✅ Fixed
Wind Speed data.weather.windSpeed data.windKph ✅ Fixed
Pressure data.weather.pressure data.pressure ✅ Fixed
Current Temp data.currentTemp data.currentTemp ✅ OK
Target Temp data.targetTemp data.targetTemp ✅ OK
Heater Status data.heaterOn data.heaterOn ✅ OK
Control Mode data.heaterControlMode data.heaterControlMode ✅ OK
Energy Today data.dailyKWh data.dailyKWh ✅ OK
Energy Total data.kwhTotal data.kwhTotal ✅ OK
Efficiency data.efficiency data.efficiency ✅ OK
Free Heap data.freeHeap data.freeHeap ✅ OK
WiFi SSID data.wifiSsid data.wifiSsid ✅ OK
IP Address data.ipAddress data.ipAddress ✅ OK
CPU Temp data.cpuTemp ❌ Not provided ⏳ Pending
Sensor Status data.sensorOk ❌ Not provided ⏳ Pending

Code Changes Made

data/js/app.js

Lines Modified: 63-108 (updateDashboard function)

Before:

updateElement('wifi-signal', `${data.rssi || 0} dBm`);
updateElement('device-uptime', formatUptime(data.uptimeMs));
if (data.weather) {
  updateElement('weather-temp', formatTemp(data.weather.temperature));
  updateElement('weather-desc', data.weather.description || 'N/A');
  // ...
}

After:

updateElement('wifi-signal', `${data.wifiRSSI || 0} dBm`);
updateElement('device-uptime', formatUptime((data.uptime || 0) * 1000));
updateElement('weather-temp', formatTemp(data.extTemp));
updateElement('weather-desc', getWeatherDescription(data.weatherCode));
updateElement('weather-humidity', `${data.humidity || 0}%`);
updateElement('weather-wind', `${data.windKph || 0} km/h`);
updateElement('weather-pressure', `${data.pressure || 0} hPa`);

New Function Added:

function getWeatherDescription(code) {
  if (!code) return 'N/A';
  const codes = {
    0: 'Clear', 1: 'Mainly Clear', 2: 'Partly Cloudy', 3: 'Overcast',
    45: 'Foggy', 48: 'Rime Fog', 51: 'Light Drizzle', 53: 'Drizzle',
    55: 'Heavy Drizzle', 61: 'Light Rain', 63: 'Rain', 65: 'Heavy Rain',
    71: 'Light Snow', 73: 'Snow', 75: 'Heavy Snow', 80: 'Light Showers',
    81: 'Showers', 82: 'Heavy Showers', 95: 'Thunderstorm', 
    96: 'Thunderstorm with Hail'
  };
  return codes[code] || `Code ${code}`;
}

Canvas Initialization:

function initLcdPreview() {
  const canvas = document.getElementById('lcd-preview');
  if (canvas) {
    const ctx = canvas.getContext('2d');
    ctx.fillStyle = '#1e293b';
    ctx.fillRect(0, 0, 480, 272);
    ctx.fillStyle = '#64748b';
    ctx.font = '16px sans-serif';
    ctx.textAlign = 'center';
    ctx.fillText('LCD Screen Preview', 240, 136);
    document.getElementById('preview-status').textContent = 'Preview Ready';
  }
}

function initBuilderCanvas() {
  const canvas = document.getElementById('builder-canvas');
  if (canvas) {
    const ctx = canvas.getContext('2d');
    // Draw 20px grid...
  }
}

Dashboard Setup:

function setupDashboard() {
  const refreshBtn = document.getElementById('refresh-preview');
  if (refreshBtn) {
    refreshBtn.addEventListener('click', () => {
      initLcdPreview();
      showToast('Preview refreshed', 'success');
    });
  }
}

Upload Status

✅ Filesystem uploaded successfully

  • File: app.js - 11.1 KB, 304 lines
  • Upload completed: 5.9 seconds
  • Exit code: 0

Testing Instructions

1. Refresh Your Browser

Press Ctrl + F5 (hard refresh) to clear cache and load new JavaScript

2. Check Dashboard

  • ✅ Current/Target temperature should display correctly
  • ✅ WiFi signal should show dBm value (e.g., "-45 dBm")
  • ✅ Uptime should display (e.g., "2h 15m")
  • ✅ Weather temp should show external temperature
  • ✅ Weather description should show text (e.g., "Clear", "Rain")
  • ✅ LCD Preview canvas should show dark background with "LCD Screen Preview" text

3. Check Console (F12)

Look for:

ESP32 Web UI initialized
ESP32 Web UI ready

No errors should appear.

4. Watch Network Tab

  • /api/status should return JSON every 5 seconds
  • Response should include all fields listed in mapping table above

What Should Work Now

✅ Dashboard Page

  • Temperature displays (current, target, difference)
  • Heater status indicator
  • Weather widget with real data
  • Network info (SSID, signal, IP, uptime)
  • Energy statistics
  • Device health (memory usage)
  • LCD preview canvas with dark background

✅ Control Page

  • Mode buttons (Off/Manual/Auto/AI)
  • Temperature slider and +/- buttons
  • Quick preset buttons
  • Toast notifications on changes

✅ Builder Page

  • Canvas with 20px grid
  • 480×272 display dimensions shown

✅ Settings Page

  • All forms functional
  • Restart/Reset confirmations

Known Limitations

⚠️ Still Not Working

CPU Temperature:

  • Backend doesn't provide this data yet
  • Shows "-- °C" placeholder

Sensor Status:

  • Backend doesn't have dedicated sensor status field
  • Using wifiConnected as workaround for now

Energy Stats:

  • May show zeros if backend hasn't calculated yet
  • Check if dailyKWh, kwhTotal, efficiency are being populated

Next Steps if Issues Persist

  1. Open Browser Console (F12)

    • Check for JavaScript errors
    • Look at /api/status response in Network tab
  2. Verify API Response

    • Navigate to: http://[ESP32_IP]/api/status
    • Should see JSON with all fields
  3. Check Serial Monitor

    • Verify ESP32 is connected to WiFi
    • Check if weather API is being called
  4. Clear Browser Cache

    • Use Incognito/Private mode
    • Try different browser

Summary

Total Changes: 8 fixes implemented
File Size: 11.1 KB (increased from 9.3 KB to add weather descriptions and canvas functions)
Lines: 304 (increased from 248 lines)
Upload Status: ✅ Success

Result: Web UI should now display all available data correctly based on actual API response structure.


Test Now! 🚀

Refresh your browser (Ctrl + F5) and check if the dashboard displays properly!