#include <WiFi.h>
#include <HTTPClient.h>
// Define el pin de salida
#define PinActuador 12
const char *ssid = "Wokwi-GUEST";
const char *password = "";
// URL servidor
//String serverName = "https://fir-iot-498a3-default-rtdb.firebaseio.com/actuadores/";
String serverName = "https://3515-187-154-232-213.ngrok-free.app/motor";
String id_actuador = "-NkcMolznQKjb7ciCPFC";
// campo
String campo = "valor";
// URI dispositivo
//String URI_actuador= serverName + id_actuador + "/" + campo + ".json";
String URI_actuador= serverName;
// Variable cliente para conectarse con el protocolo HTTP
HTTPClient http;
// Variable para almacenar el status code
int httpStatusCode = 0;
// Variable para almacenar la respuesta del endpoint
String response = "";
void setup()
{
// Configura la velocidad de conexion de la consola
Serial.begin(115200);
// Configura el Led como salida
pinMode(PinActuador, OUTPUT);
// Inicia la conexion con la red wifi
WiFi.begin(ssid, password);
Serial.println("Connecting");
// Bucle que espera hasta que se establezca la conexion con la wifi
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
// Imprime la IP una vez conectado
// Serial.println("Connected to WiFi network with IP Address: " + WiFi.localIP());
} // setup()
void loop()
{
// Valida que haya conexion con la Wifi
if (WiFi.status() == WL_CONNECTED)
{
// Configura la conexion con el endpoint del Led
http.begin(URI_actuador.c_str());
http.addHeader("ngrok-skip-browser-warning","69420");
// Almacena el HTTP status code de la conexion
httpStatusCode = http.GET();
// Valida si el HTTP status code es 200
Serial.println(httpStatusCode);
if (httpStatusCode == 200)
{
// Imprime el HTTP status code
Serial.println("HTTP Response code: " + httpStatusCode);
// Almacena el response del endpoint el valor viene entre ""
response = http.getString();
// Quita los caracteres obtiene el 1 de "1"
String r = response.substring(1, 2);
// Convierte de String a int y compara con 1
Serial.println(r);
if (r.toInt() == 1)
{
// Enciende el LED
digitalWrite(PinActuador, HIGH);
// Imprime el response como validacion
Serial.println("ENCENDER " + response);
}
else
{ // si no se recibio un 1
// Apaga el LED
digitalWrite(PinActuador, LOW);
// Imprime el response como validacion
Serial.println("APAGAR " + response);
}
}
else
{ // Imprime el status code recibido
Serial.println("HTTP status code: " + httpStatusCode);
}
// Cierra la conexion
http.end();
}
else
{ // Si no hay conexion con la Wifi
Serial.println("WiFi Disconnected");
} // WiFi.status()
} // loop()