#include <WiFi.h>
#include <HTTPClient.h>
#include <PubSubClient.h>
#define LED_BUILTIN 14
#define LDR_PIN 34
// Credenciales de red Wi-Fi
const char* ssid = "Wokwi-GUEST"; // Reemplaza con el nombre de tu red Wi-Fi
const char* password = ""; // Reemplaza con la contraseña de tu red Wi-Fi
// Token y URL de Ubidots
const char* token = "BBUS-EzAC9CxIKGMm09i3hM6FlpMR6IU0z4"; // Reemplaza con tu token de Ubidots
const char* device_label = "Esp"; // Nombre de tu dispositivo
const char* variable_label = "resistor"; // Nombre de la variable
WiFiClient wifi_client;
PubSubClient mqttClient(wifi_client);
const char* mqtt_server = "mqtt-dashboard.com";
const int mqtt_port = 1883;
// Características del LDR
const float GAMMA = 0.7;
const float RL10 = 50;
void reconnect() {
Serial.println("Connecting to MQTT Broker...");
while (!mqttClient.connected()) {
Serial.println("Reconnecting to MQTT Broker...");
String clientId = "ESP32Client-";
clientId += String("sefsefsefaesf");
mqttClient.setKeepAlive(60);
if (mqttClient.connect(clientId.c_str())) {
Serial.println("Connected.");
} else {
Serial.println("Not connected.");
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Callback - ");
Serial.print("Message: ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
}
void setupMQTT() {
mqttClient.setServer(mqtt_server, mqtt_port);
mqttClient.setCallback(callback);
}
void enviarUbidots(float iluminacion) {
HTTPClient http;
String url = String("http://industrial.api.ubidots.com/api/v1.6/devices/") + device_label;
http.begin(url.c_str());
http.addHeader("Content-Type", "application/json");
http.addHeader("X-Auth-Token", token);
// Crear el payload con la variable y el valor
String payload = "{\"" + String(variable_label) + "\": " + String(iluminacion) + "}";
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Datos enviados a Ubidots:");
Serial.println(response);
} else {
Serial.print("Error enviando datos a Ubidots, código: ");
Serial.println(httpResponseCode);
}
http.end();
}
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LDR_PIN, INPUT);
// Conectar a la red Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando a WiFi...");
}
Serial.println("Conectado a WiFi");
setupMQTT();
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
// Leer el valor del sensor LDR
int analogValue = analogRead(LDR_PIN);
float voltage = analogValue / 1024.0 * 5.0;
float resistance = 2000.0 * voltage / (1.0 - voltage / 5.0);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1.0 / GAMMA));
// Visualización por el puerto serie
Serial.print("Iluminacion: ");
Serial.println(lux);
// Enviar datos a Ubidots
if (WiFi.status() == WL_CONNECTED) {
enviarUbidots(lux);
} else {
Serial.println("Error de conexión WiFi");
}
if (!mqttClient.connected()) {
reconnect();
}
String mensaje = String(lux);
mqttClient.publish("ugr/josep/wokwi/iluminacion", mensaje.c_str());
delay(5000); // Enviar datos cada 5 segundos
}