#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
int pinPotenciometro = 34;
int ledPin = 14; // Pin del LED
// URL de la API en Heroku
String serverName = "https://login-cf67c-default-rtdb.firebaseio.com/iot";
unsigned long lastTime = 0;
unsigned long timerDelay = 500;
int lastValorPotenciometro = -1;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
// Conectar a la red WiFi
WiFi.begin(ssid, password);
Serial.println("Conectando");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.print("Conectado a la red WiFi con la dirección IP: ");
Serial.println(WiFi.localIP());
Serial.println("El temporizador tiene 5 segundos.");
}
void loop() {
// Leer el valor del potenciómetro
int valorPotenciometro = analogRead(pinPotenciometro);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Crear una URL para la API en Heroku, incluyendo el valor del potenciómetro
String serverPathPotenciometro = serverName + "/2/" + String(valorPotenciometro);
// Iniciar la conexión HTTP para el potenciómetro
http.begin(serverPathPotenciometro.c_str());
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Enviar una solicitud HTTP PUT con el valor del potenciómetro
int httpResponseCodePotenciometro = http.PUT("");
if (httpResponseCodePotenciometro > 0) {
Serial.println(httpResponseCodePotenciometro);
Serial.print("Valor del potenciómetro: ");
Serial.println(valorPotenciometro);
} else {
Serial.print("Código de error (Potenciómetro): ");
Serial.println(httpResponseCodePotenciometro);
Serial.print("Valor del potenciómetro no enviado. Error.");
}
// Liberar recursos para la solicitud del potenciómetro
http.end();
}
}