Iot / index.html
veltrixcode's picture
Upload 5 files
1329aed verified
Raw History Blame Contribute Delete
10.6 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IoT LED Control</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: rgba(255, 255, 255, 0.95);
border-radius: 20px;
padding: 40px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
max-width: 500px;
width: 100%;
}
h1 {
text-align: center;
color: #333;
margin-bottom: 30px;
font-size: 28px;
}
.status {
text-align: center;
padding: 10px;
border-radius: 10px;
margin-bottom: 30px;
font-weight: bold;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
}
.status-indicator {
width: 12px;
height: 12px;
border-radius: 50%;
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.status.connected {
background: #d4edda;
color: #155724;
}
.status.connected .status-indicator {
background: #28a745;
}
.status.disconnected {
background: #f8d7da;
color: #721c24;
}
.status.disconnected .status-indicator {
background: #dc3545;
animation: none;
}
.status.connecting {
background: #fff3cd;
color: #856404;
}
.status.connecting .status-indicator {
background: #ffc107;
}
.led-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
margin-bottom: 30px;
}
.led-card {
background: #f8f9fa;
border-radius: 15px;
padding: 25px;
text-align: center;
transition: all 0.3s ease;
cursor: pointer;
border: 3px solid transparent;
}
.led-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}
.led-card.active {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-color: #fff;
}
.led-card.disabled {
opacity: 0.5;
cursor: not-allowed;
}
.led-icon {
font-size: 48px;
margin-bottom: 10px;
transition: all 0.3s ease;
}
.led-card.active .led-icon {
filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.8));
}
.led-name {
font-size: 18px;
font-weight: bold;
margin-bottom: 5px;
color: #333;
}
.led-card.active .led-name {
color: white;
}
.led-status {
font-size: 14px;
color: #666;
}
.led-card.active .led-status {
color: rgba(255, 255, 255, 0.9);
}
.chase-section {
margin-bottom: 20px;
}
.chase-card {
background: #fff3cd;
border-radius: 15px;
padding: 25px;
text-align: center;
cursor: pointer;
border: 3px solid transparent;
transition: all 0.3s ease;
}
.chase-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}
.chase-card.active {
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a6f 100%);
border-color: #fff;
}
.chase-icon {
font-size: 48px;
margin-bottom: 10px;
}
.chase-name {
font-size: 20px;
font-weight: bold;
margin-bottom: 5px;
color: #856404;
}
.chase-card.active .chase-name {
color: white;
}
.chase-description {
font-size: 12px;
color: #856404;
}
.chase-card.active .chase-description {
color: rgba(255, 255, 255, 0.9);
}
.all-off-btn {
width: 100%;
padding: 15px;
font-size: 16px;
font-weight: bold;
border: none;
border-radius: 10px;
background: #dc3545;
color: white;
cursor: pointer;
transition: all 0.3s ease;
}
.all-off-btn:hover {
background: #c82333;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(220, 53, 69, 0.4);
}
.all-off-btn:active {
transform: translateY(0);
}
</style>
</head>
<body>
<div class="container">
<h1>πŸ”Œ IoT LED Control</h1>
<div class="status connecting" id="status">
<div class="status-indicator"></div>
<span id="status-text">Connecting...</span>
</div>
<div class="chase-section">
<div class="chase-card" id="chase-card" onclick="toggleChase()">
<div class="chase-icon">πŸŒ€</div>
<div class="chase-name">Chase Mode</div>
<div class="chase-description">LED1 β†’ LED2 β†’ LED3 (500ms)</div>
</div>
</div>
<div class="led-grid">
<div class="led-card" data-led="led1" onclick="toggleLED('led1')">
<div class="led-icon">πŸ’‘</div>
<div class="led-name">LED 1</div>
<div class="led-status">OFF</div>
</div>
<div class="led-card" data-led="led2" onclick="toggleLED('led2')">
<div class="led-icon">πŸ’‘</div>
<div class="led-name">LED 2</div>
<div class="led-status">OFF</div>
</div>
<div class="led-card" data-led="led3" onclick="toggleLED('led3')">
<div class="led-icon">πŸ’‘</div>
<div class="led-name">LED 3</div>
<div class="led-status">OFF</div>
</div>
<div class="led-card" data-led="led4" onclick="toggleLED('led4')">
<div class="led-icon">πŸ’‘</div>
<div class="led-name">LED 4</div>
<div class="led-status">OFF</div>
</div>
</div>
<button class="all-off-btn" onclick="allOff()">
Turn All OFF
</button>
</div>
<script>
let ledStates = {
led1: false,
led2: false,
led3: false,
led4: false,
chase: false
};
// Fetch initial LED states and connection status
async function fetchLedStates() {
try {
const response = await fetch('/api/leds');
const data = await response.json();
ledStates = data;
updateUI();
checkConnectionStatus();
} catch (error) {
console.error('Error fetching LED states:', error);
updateConnectionStatus(false);
}
}
// Check ESP8266 connection status
async function checkConnectionStatus() {
try {
const response = await fetch('/api/status');
const data = await response.json();
updateConnectionStatus(data.connected);
} catch (error) {
console.error('Error checking connection status:', error);
updateConnectionStatus(false);
}
}
// Update connection status display
function updateConnectionStatus(connected) {
const statusDiv = document.getElementById('status');
const statusText = document.getElementById('status-text');
if (connected) {
statusDiv.className = 'status connected';
statusText.textContent = 'ESP8266 Connected';
} else {
statusDiv.className = 'status disconnected';
statusText.textContent = 'ESP8266 Disconnected';
}
}
// Toggle chase mode
async function toggleChase() {
const newState = !ledStates.chase;
try {
const response = await fetch('/api/led/chase', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ state: newState })
});
const data = await response.json();
if (data.success) {
ledStates = data.ledStates;
updateUI();
}
} catch (error) {
console.error('Error toggling chase:', error);
}
}
// Toggle individual LED
async function toggleLED(ledName) {
// Prevent toggling LED1-3 when chase is active
if (ledStates.chase && (ledName === 'led1' || ledName === 'led2' || ledName === 'led3')) {
return;
}
const newState = !ledStates[ledName];
try {
const response = await fetch(`/api/led/${ledName}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ state: newState })
});
const data = await response.json();
if (data.success) {
ledStates = data.ledStates;
updateUI();
}
} catch (error) {
console.error('Error toggling LED:', error);
}
}
// Turn all LEDs off
async function allOff() {
try {
const response = await fetch('/api/all-off', {
method: 'POST'
});
const data = await response.json();
if (data.success) {
ledStates = data.ledStates;
updateUI();
}
} catch (error) {
console.error('Error turning off all LEDs:', error);
}
}
// Update UI based on LED states
function updateUI() {
// Update chase card
const chaseCard = document.getElementById('chase-card');
if (ledStates.chase) {
chaseCard.classList.add('active');
} else {
chaseCard.classList.remove('active');
}
// Update LED cards
Object.keys(ledStates).forEach(ledName => {
if (ledName === 'chase') return;
const card = document.querySelector(`[data-led="${ledName}"]`);
const statusText = card.querySelector('.led-status');
// Disable LED1-3 cards when chase is active
if (ledStates.chase && (ledName === 'led1' || ledName === 'led2' || ledName === 'led3')) {
card.classList.add('disabled');
statusText.textContent = 'CHASE MODE';
} else {
card.classList.remove('disabled');
if (ledStates[ledName]) {
card.classList.add('active');
statusText.textContent = 'ON';
} else {
card.classList.remove('active');
statusText.textContent = 'OFF';
}
}
});
}
// Initialize
fetchLedStates();
// Check connection status every 3 seconds
setInterval(checkConnectionStatus, 3000);
// Refresh states every 5 seconds as backup
setInterval(fetchLedStates, 5000);
</script>
</body>
</html>