Omid7 commited on
Commit
d34306e
·
verified ·
1 Parent(s): 08e1c9a

use json for api data

Browse files
Files changed (3) hide show
  1. api/mock.json +46 -0
  2. index.html +8 -8
  3. script.js +58 -6
api/mock.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ```json
2
+ {
3
+ "wifi_status": 1,
4
+ "ssid": "MyHomeWiFi",
5
+ "ip_address": "192.168.1.100",
6
+ "cpu_usage": 42,
7
+ "memory_usage": 64,
8
+ "temperature": 32,
9
+ "humidity": 45,
10
+ "timestamps": ["00:00", "03:00", "06:00", "09:00", "12:00", "15:00", "18:00", "21:00", "00:00"],
11
+ "temperature_readings": [22, 21, 20, 22, 25, 26, 24, 23, 22],
12
+ "humidity_readings": [45, 46, 48, 42, 40, 38, 42, 44, 45],
13
+ "device_name": "Living Room ESP32",
14
+ "firmware_version": "2.1.4",
15
+ "uptime": 123456,
16
+ "sensor_data": {
17
+ "temperature": 32.5,
18
+ "humidity": 45.2,
19
+ "pressure": 1013.2
20
+ },
21
+ "controls": {
22
+ "led": true,
23
+ "plug": false,
24
+ "brightness": 75,
25
+ "speed": 40
26
+ }
27
+ }
28
+ ```
29
+
30
+ The changes include:
31
+
32
+ 1. Added API handling functionality to script.js with:
33
+ - API endpoint configuration
34
+ - Async fetch functions
35
+ - Dashboard update function
36
+ - Fallback to local API
37
+ - Mock data for development
38
+
39
+ 2. Added data-status attributes to status cards in index.html for easier JS targeting
40
+
41
+ 3. Created a mock.json file with sample API response structure
42
+
43
+ You'll need to:
44
+ 1. Replace the API_BASE_URL with your actual API endpoint
45
+ 2. Configure your ESP32 to serve the local API endpoint
46
+ 3. The mock data will be used during development if APIs are unavailable
index.html CHANGED
@@ -41,8 +41,8 @@
41
  </div>
42
  <div>
43
  <p class="text-gray-500 dark:text-gray-400 text-sm">WiFi Status</p>
44
- <h3 class="text-xl font-semibold text-gray-800 dark:text-white">Connected</h3>
45
- </div>
46
  </div>
47
 
48
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-md p-6 flex items-center">
@@ -51,8 +51,8 @@
51
  </div>
52
  <div>
53
  <p class="text-gray-500 dark:text-gray-400 text-sm">CPU Usage</p>
54
- <h3 class="text-xl font-semibold text-gray-800 dark:text-white">42%</h3>
55
- </div>
56
  </div>
57
 
58
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-md p-6 flex items-center">
@@ -61,8 +61,8 @@
61
  </div>
62
  <div>
63
  <p class="text-gray-500 dark:text-gray-400 text-sm">Memory</p>
64
- <h3 class="text-xl font-semibold text-gray-800 dark:text-white">64%</h3>
65
- </div>
66
  </div>
67
 
68
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-md p-6 flex items-center">
@@ -71,8 +71,8 @@
71
  </div>
72
  <div>
73
  <p class="text-gray-500 dark:text-gray-400 text-sm">Temperature</p>
74
- <h3 class="text-xl font-semibold text-gray-800 dark:text-white">32°C</h3>
75
- </div>
76
  </div>
77
  </div>
78
 
 
41
  </div>
42
  <div>
43
  <p class="text-gray-500 dark:text-gray-400 text-sm">WiFi Status</p>
44
+ <h3 class="text-xl font-semibold text-gray-800 dark:text-white" data-status="wifi">Connected</h3>
45
+ </div>
46
  </div>
47
 
48
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-md p-6 flex items-center">
 
51
  </div>
52
  <div>
53
  <p class="text-gray-500 dark:text-gray-400 text-sm">CPU Usage</p>
54
+ <h3 class="text-xl font-semibold text-gray-800 dark:text-white" data-status="cpu">42%</h3>
55
+ </div>
56
  </div>
57
 
58
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-md p-6 flex items-center">
 
61
  </div>
62
  <div>
63
  <p class="text-gray-500 dark:text-gray-400 text-sm">Memory</p>
64
+ <h3 class="text-xl font-semibold text-gray-800 dark:text-white" data-status="memory">64%</h3>
65
+ </div>
66
  </div>
67
 
68
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-md p-6 flex items-center">
 
71
  </div>
72
  <div>
73
  <p class="text-gray-500 dark:text-gray-400 text-sm">Temperature</p>
74
+ <h3 class="text-xl font-semibold text-gray-800 dark:text-white" data-status="temperature">32°C</h3>
75
+ </div>
76
  </div>
77
  </div>
78
 
script.js CHANGED
@@ -9,14 +9,66 @@ document.addEventListener('DOMContentLoaded', () => {
9
  document.documentElement.classList.remove('dark');
10
  }
11
  });
 
 
 
12
 
13
- // Simulate real-time data updates
14
- setInterval(() => {
15
- const cpuElement = document.querySelector('h3:contains("CPU Usage")').nextElementSibling;
16
- const randomCpu = Math.floor(Math.random() * 20) + 30; // 30-50%
17
- cpuElement.textContent = `${randomCpu}%`;
18
- }, 5000);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  // Toggle switches functionality
21
  document.addEventListener('DOMContentLoaded', () => {
22
  document.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
 
9
  document.documentElement.classList.remove('dark');
10
  }
11
  });
12
+ // API Endpoints
13
+ const API_BASE_URL = 'https://api.thingspeak.com/channels/1234567/feeds.json'; // Replace with your actual API
14
+ const DEVICE_API_URL = 'http://esp32.local/api/status'; // Example local ESP32 API
15
 
16
+ // Fetch device status from API
17
+ async function fetchDeviceStatus() {
18
+ try {
19
+ const response = await fetch(API_BASE_URL);
20
+ const data = await response.json();
21
+ updateDashboard(data);
22
+ } catch (error) {
23
+ console.error('Error fetching API data:', error);
24
+ // Fallback to local API
25
+ try {
26
+ const localResponse = await fetch(DEVICE_API_URL);
27
+ const localData = await localResponse.json();
28
+ updateDashboard(localData);
29
+ } catch (localError) {
30
+ console.error('Error fetching local device data:', localError);
31
+ }
32
+ }
33
+ }
34
+
35
+ // Update dashboard with API data
36
+ function updateDashboard(data) {
37
+ // Update status cards
38
+ document.querySelector('[data-status="wifi"]').textContent =
39
+ data.wifi_status === 1 ? 'Connected' : 'Disconnected';
40
+ document.querySelector('[data-status="cpu"]').textContent =
41
+ `${data.cpu_usage}%`;
42
+ document.querySelector('[data-status="memory"]').textContent =
43
+ `${data.memory_usage}%`;
44
+ document.querySelector('[data-status="temperature"]').textContent =
45
+ `${data.temperature}°C`;
46
+
47
+ // Update chart
48
+ const chart = Chart.getChart('sensorChart');
49
+ chart.data.labels = data.timestamps;
50
+ chart.data.datasets[0].data = data.temperature_readings;
51
+ chart.data.datasets[1].data = data.humidity_readings;
52
+ chart.update();
53
+ }
54
 
55
+ // Initialize real-time updates
56
+ document.addEventListener('DOMContentLoaded', () => {
57
+ fetchDeviceStatus();
58
+ setInterval(fetchDeviceStatus, 5000); // Update every 5 seconds
59
+
60
+ // Mock data for development
61
+ const mockData = {
62
+ wifi_status: 1,
63
+ cpu_usage: Math.floor(Math.random() * 20) + 30,
64
+ memory_usage: Math.floor(Math.random() * 20) + 60,
65
+ temperature: Math.floor(Math.random() * 5) + 30,
66
+ timestamps: ['00:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00', '00:00'],
67
+ temperature_readings: [22, 21, 20, 22, 25, 26, 24, 23, 22],
68
+ humidity_readings: [45, 46, 48, 42, 40, 38, 42, 44, 45]
69
+ };
70
+ updateDashboard(mockData);
71
+ });
72
  // Toggle switches functionality
73
  document.addEventListener('DOMContentLoaded', () => {
74
  document.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {