#include <WiFi.h>
#include <WebServer.h>
// --- Configurações de WiFi ---
const char* ssid = "NPITI-IoT";
const char* password = "NPITI-IoT";
// --- Definição dos Pinos dos LEDs ---
const int ledPin1 = 18;
const int ledPin2 = 19;
// --- Configurações de PWM ---
// No Core 3.0, anexamos diretamente ao pino
const int freq = 5000;
const int resolution = 8; // 8 bits (0-255)
// Cria o objeto do servidor na porta 80
WebServer server(80);
// --- Código HTML da Página Web (O mesmo do anterior) ---
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Controle PWM ESP32 v3.0</title>
<style>
body { font-family: Helvetica, sans-serif; text-align: center; margin: 0; padding: 20px; background-color: #f4f4f4; }
h1 { color: #333; }
.card { background: white; max-width: 400px; margin: 0 auto; padding: 30px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); }
.slider-container { margin-bottom: 25px; }
label { font-weight: bold; display: block; margin-bottom: 10px; font-size: 1.2rem;}
input[type=range] { width: 100%; }
span { color: #555; font-size: 0.9rem; }
</style>
</head>
<body>
<div class="card">
<h1>Controle de LEDs</h1>
<div class="slider-container">
<label for="slider1">LED 1 (GPIO 18)</label>
<input type="range" min="0" max="255" value="0" id="slider1" oninput="updateLed(1, this.value)">
<span id="val1">Valor: 0</span>
</div>
<div class="slider-container">
<label for="slider2">LED 2 (GPIO 19)</label>
<input type="range" min="0" max="255" value="0" id="slider2" oninput="updateLed(2, this.value)">
<span id="val2">Valor: 0</span>
</div>
</div>
<script>
function updateLed(id, value) {
document.getElementById('val' + id).innerHTML = 'Valor: ' + value;
var xhr = new XMLHttpRequest();
xhr.open("GET", "/update?led=" + id + "&val=" + value, true);
xhr.send();
}
</script>
</body>
</html>
)rawliteral";
// --- Funções do Servidor ---
void handleRoot() {
server.send(200, "text/html", index_html);
}
void handleUpdate() {
if (server.hasArg("led") && server.hasArg("val")) {
String ledID = server.arg("led");
String ledVal = server.arg("val");
int value = ledVal.toInt();
// MUDANÇA AQUI: ledcWrite agora recebe o PINO, não o canal
if (ledID == "1") {
ledcWrite(ledPin1, value);
Serial.printf("LED 1 (GPIO %d) set to: %d\n", ledPin1, value);
}
else if (ledID == "2") {
ledcWrite(ledPin2, value);
Serial.printf("LED 2 (GPIO %d) set to: %d\n", ledPin2, value);
}
server.send(200, "text/plain", "OK");
} else {
server.send(400, "text/plain", "Bad Request");
}
}
void setup() {
Serial.begin(115200);
// MUDANÇA AQUI: ledcAttach substitui ledcSetup e ledcAttachPin
// Sintaxe: ledcAttach(pino, frequência, resolução)
if (!ledcAttach(ledPin1, freq, resolution)) {
Serial.println("Falha ao configurar LED 1");
}
if (!ledcAttach(ledPin2, freq, resolution)) {
Serial.println("Falha ao configurar LED 2");
}
// Conexão WiFi
Serial.print("Conectando em ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi conectado. IP: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/update", handleUpdate);
server.begin();
}
void loop() {
server.handleClient();
}