#include <WiFi.h>
#include <HTTPClient.h>
// Variables de acceso a la RED
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// URI para hacer peticiones
String serverName = "https://backendesp-c2ae18e5982d.herokuapp.com/dispositivos";
String payload;
// Variables del potenciometro
const int potPin = 32;
int potValue = 0;
int prevpotValue = -1;
// Variables del LED
const int ledPin = 26;
int ledState = LOW;
//int prevLedState = -1;
// Manejo de Tiempo
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 5000; // Intervalo de actualización en milisegundos (5 segundos)
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
pinMode(potPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - lastUpdateTime >= updateInterval) {
potValue = analogRead(potPin);
Serial.println(potValue);
ledState = (ledState);
//digitalWrite(ledPin, ledState);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Definir las rutas del servidor para el LED y el potenciómetro
String ledPath = serverName + "/1";
String potPath = serverName + "/2";
// Realizar solicitud HTTP para el LED
http.begin(ledPath.c_str());
//http.addHeader("accept", "application/json");
//http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
//String jsonBody = "{\"id\":\"1\",\"dispositivo\":\"led\",\"valor\":\"" + String(ledState) + "\"}";
//Serial.println(jsonBody);
//int httpResponseCode = http.PUT(jsonBody);
//if (httpResponseCode > 0) {
Serial.print("HTTP Response code (LED): ");
Serial.println(httpResponseCode);
String payload = http.getString();
char valor = payload[36];
if (valor == '1'){
digitalWrite(26, HIGH);
} else if (valor == '0'){
digitalWrite(26, LOW);
} else {
digitalWrite(26, LOW);
}
//} else {
//Serial.print("HTTP Error (LED): ");
//Serial.println(httpResponseCode);
//Serial.println(http.getString());
//}
//prevLedState = ledState;
}
http.end(); // Finalizar la conexión HTTP para el LED
// Realizar solicitud HTTP para el potenciómetro
http.begin(potPath.c_str());
http.addHeader("accept", "application/json");
http.addHeader("Content-Type", "application/json");
if (potValue != prevpotValue) {
String jsonBody = "{\"id\":\"2\",\"pot\":\"potenciometro\",\"led\":\"" + String(potValue) + "\"}";
Serial.println(jsonBody);
int httpResponseCode = http.PUT(jsonBody);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code (Potenciómetro): ");
Serial.println(httpResponseCode);
} else {
Serial.print("HTTP Error (Potenciómetro): ");
Serial.println(httpResponseCode);
Serial.println(http.getString());
}
prevpotValue = potValue;
}
http.end(); // Finalizar la conexión HTTP para el potenciómetro
} else {
Serial.println("WiFi Disconnected");
}
lastUpdateTime = currentMillis;
}
}