Objetivo: Este projeto visa o controlo remoto de um LED via Internet. Para a sua implementação, recorreremos a um microcontrolador ESP32 (versões U ou D) e a um servidor de alojamento web para a gestão dos comandos. O projeto será armazenado na diretoria IOT do seu servidor web
1. No Servidor: O ficheiro de estado (led_status.txt) Cria um ficheiro de texto simples chamado led_status.txt na mesma pasta do teu site e dá-lhe permissões de escrita (CHMOD 777). Ele conterá apenas o número 0 ou 1.
2. No ESP32: O código de "Polling" O ESP32 vai ligar-se ao teu servidor a cada 2 segundos, ler o que está escrito no led_status.txt e aplicar ao GPIO 2.
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "O teu SSID de rede";
const char* password = "A tua password de rede";
// URL direta para o ficheiro de texto no teu servidor
const char* serverName = "https://ticmania.net/iot/led_status.txt";
const int ledPin = 2;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println("\nConectado ao Redmi14");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName); // Inicia a ligação ao teu servidor
int httpResponseCode = http.GET(); // Faz o pedido de leitura
if (httpResponseCode > 0) {
String payload = http.getString();
Serial.println("Estado no servidor: " + payload);
if (payload == "1") digitalWrite(ledPin, HIGH);
else if (payload == "0") digitalWrite(ledPin, LOW);
}
http.end();
}
delay(2000); // Verifica a cada 2 segundos para não sobrecarregar o servidor
}
<?php
// Se receber um comando via URL (ex: index.php?comando=1)
if (isset($_GET['comando'])) {
file_put_contents("led_status.txt", $_GET['comando']);
header("Location: index.php"); // Recarrega a página
exit;
}
$estado_atual = file_get_contents("led_status.txt");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Painel TICMANIA</title>
<style>
body { font-family: sans-serif; text-align: center; background: #f0f0f0; }
.btn { padding: 20px; width: 80%; max-width: 300px; font-size: 20px; cursor: pointer; border-radius: 10px; border: none; color: white; margin: 10px; }
.on { background: #27ae60; }
.off { background: #c0392b; }
.status { font-weight: bold; color: <?php echo ($estado_atual == '1') ? '#27ae60' : '#c0392b'; ?>; }
</style>
</head>
<body>
<h1>Controlo Exclusivo Ticmania</h1>
<p>O LED está atualmente: <span class="status"><?php echo ($estado_atual == '1') ? 'LIGADO' : 'DESLIGADO'; ?></span></p>
<a href="?comando=1"><button class="btn on">LIGAR</button></a><br>
<a href="?comando=0"><button class="btn off">DESLIGAR</button></a>
</body>
</html>