Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="pt-br">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>ESP32-CAM Controle</title>
- <style>
- body {
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- min-height: 100vh;
- margin: 0;
- background-color: #f0f2f5;
- color: #333;
- }
- .container {
- text-align: center;
- background-color: white;
- padding: 2rem;
- border-radius: 12px;
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
- }
- h1 {
- margin-top: 0;
- color: #1c1e21;
- }
- #photo-display {
- max-width: 100%;
- width: 320px;
- height: 240px;
- border: 1px solid #ddd;
- border-radius: 8px;
- margin-top: 1rem;
- background-color: #eee;
- object-fit: cover;
- }
- #flash-button {
- margin-top: 1.5rem;
- padding: 0.75rem 1.5rem;
- font-size: 1rem;
- font-weight: 600;
- border-radius: 8px;
- border: none;
- cursor: pointer;
- color: white;
- transition: background-color 0.3s, transform 0.1s;
- }
- #flash-button.on {
- background-color: #f5b400; /* Laranja/Amarelo para ON */
- }
- #flash-button.off {
- background-color: #4CAF50; /* Verde para OFF (ação de ligar) */
- }
- #flash-button:active {
- transform: scale(0.98);
- }
- #status {
- margin-top: 1rem;
- font-size: 0.9rem;
- color: #666;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>Controle da Câmera ESP32</h1>
- <img id="photo-display" src="/latest-photo" alt="Última foto da câmera">
- <button id="flash-button">Carregando...</button>
- <p id="status">Buscando estado do flash...</p>
- </div>
- <script>
- const photoDisplay = document.getElementById('photo-display');
- const flashButton = document.getElementById('flash-button');
- const statusEl = document.getElementById('status');
- let isFlashOn = false;
- // Função para atualizar o texto e a cor do botão
- function updateButtonUI() {
- if (isFlashOn) {
- flashButton.textContent = 'Desligar Flash';
- flashButton.className = 'on';
- } else {
- flashButton.textContent = 'Ligar Flash';
- flashButton.className = 'off';
- }
- }
- // Função para enviar o comando do flash para o servidor
- async function toggleFlash() {
- const newCommand = isFlashOn ? 'FLASH_OFF' : 'FLASH_ON';
- try {
- const response = await fetch('/command', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ command: newCommand })
- });
- const result = await response.json();
- if (response.ok) {
- isFlashOn = (result.newCommand === 'FLASH_ON');
- statusEl.textContent = `Comando enviado: ${result.newCommand}.`;
- updateButtonUI();
- } else {
- statusEl.textContent = `Erro: ${result.message}`;
- }
- } catch (error) {
- console.error('Erro ao enviar comando:', error);
- statusEl.textContent = 'Erro de conexão com o servidor.';
- }
- }
- // Função para buscar o estado inicial do flash no servidor
- async function getInitialFlashState() {
- try {
- const response = await fetch('/command'); // GET request
- const command = await response.text();
- isFlashOn = (command === 'FLASH_ON');
- updateButtonUI();
- statusEl.textContent = `Estado atual do flash: ${command}.`;
- } catch (error) {
- console.error('Erro ao buscar estado inicial:', error);
- statusEl.textContent = 'Não foi possível obter o estado do flash.';
- }
- }
- // Função para atualizar a imagem periodicamente
- function refreshImage() {
- // Adiciona um timestamp para evitar que o navegador use a imagem do cache
- photoDisplay.src = `/latest-photo?t=${new Date().getTime()}`;
- }
- // Event listener para o botão
- flashButton.addEventListener('click', toggleFlash);
- // Inicia a aplicação
- // 1. Busca o estado inicial assim que a página carrega
- getInitialFlashState();
- // 2. Define um intervalo para atualizar a imagem a cada 5 segundos
- setInterval(refreshImage, 5000);
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment