#include <ESP32Servo.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const int channel = 11;
const String apiUrl = "http://192.168.1.11:8120/ramal";
Servo myservo;
int pos = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(ssid, password, channel);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Conectando a WiFi...");
}
}
void loop() {
// put your main code here, to run repeatedly:
HTTPClient http;
http.begin(apiUrl);
int httpResponseCode = http.GET();
if (httpResponseCode > 0)
{
String respuestaAPI = http.getString();
Serial.println(respuestaAPI);
// Analizar la respuesta JSON
DynamicJsonDocument doc(1024);
deserializeJson(doc, respuestaAPI);
// Acceder a los elementos del objeto JSON
JsonArray ramal = doc.as<JsonArray>();
for (JsonObject obj : ramal)
{
const char *idRamal = obj["idRamal"];
const char *ubicacion = obj["ubicacion"];
const char *referencia = obj["referencia"];
int numeroReferencia = obj["numeroReferencia"];
// Imprimir los valores en el monitor serial
Serial.print("idRamal: ");
Serial.println(idRamal);
Serial.print("Ubicacion: ");
Serial.println(ubicacion);
Serial.print("Referencia: ");
Serial.println(referencia);
Serial.print("Numero de Referencia: ");
Serial.println(numeroReferencia);
Serial.println();
}
}
else
{
Serial.print("Error en la solicitud HTTP: ");
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
if (!myservo.attached()) {
myservo.attach(33); // Attach the servo after it has been detatched
}
myservo.write(0);
delay(1000);
if(){
for (pos = 0; pos <= 180; pos += 90) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.println(myservo.read());
}
}
delay(1000); // this speeds up the simulation
}