that_one_nigga_you_k

front

Apr 8th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.33 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Painel de controle ESP32</title>
  5.     <!-- ... (mantenha todos os mesmos estilos) ... -->
  6. </head>
  7. <body>
  8.     <div class="container">
  9.         <h1>Controle remoto do ESP32</h1>
  10.         <h2>Controle do LED</h2>
  11.        
  12.         <div class="led-status">
  13.             Estado atual do LED: <span id="ledState" class="off">DESLIGADO</span>
  14.         </div>
  15.        
  16.         <form action="/control" method="post">
  17.             <input type="hidden" name="action" value="toggle">
  18.             <button type="submit" class="btn-toggle">Alternar LED</button>
  19.         </form>
  20.     </div>
  21.    
  22.     <script>
  23.         document.querySelector('form').addEventListener('submit', function(e) {
  24.             e.preventDefault();
  25.             fetch('/control', {
  26.                 method: 'POST',
  27.                 headers: {
  28.                     'Content-Type': 'application/x-www-form-urlencoded',
  29.                 },
  30.                 body: 'action=toggle'
  31.             })
  32.             .then(response => {
  33.                 if (response.ok) {
  34.                     checkStatus(); // Force immediate status check after toggle
  35.                 }
  36.             })
  37.             .catch(error => console.error('Erro:', error));
  38.         });
  39.  
  40.         function checkStatus() {
  41.             fetch('/status')
  42.                 .then(response => {
  43.                     if (!response.ok) throw new Error('Resposta inválida');
  44.                     return response.text();
  45.                 })
  46.                 .then(state => {
  47.                     const stateElement = document.getElementById('ledState');
  48.                     state = state.toLowerCase().trim();
  49.                    
  50.                     if (state === 'on' || state === 'ligado' || state === 'true' || state === '1') {
  51.                         stateElement.textContent = 'LIGADO';
  52.                         stateElement.className = 'on';
  53.                     } else {
  54.                         stateElement.textContent = 'DESLIGADO';
  55.                         stateElement.className = 'off';
  56.                     }
  57.                 })
  58.                 .catch(error => console.error('Erro ao verificar estado:', error));
  59.         }
  60.  
  61.         // Verifica o estado a cada 2 segundos
  62.         setInterval(checkStatus, 2000);
  63.         checkStatus(); // Verificação inicial
  64.     </script>
  65. </body>
  66. </html>
Add Comment
Please, Sign In to add comment