#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#include "DHTesp.h"
const int DHT_PIN = 12;
DHTesp dhtSensor;
#define S0 14
#define S1 27
#define PIR 33
int SW0, SW1;
#include <WiFi.h>
#include <PubSubClient.h>
void setup_wifi();
void procesar_mensajes(char *topic, byte *mensaje, unsigned int longitud);
void publicar();
void tiempo();
void humedad();
void temperatura();
void movimiento();
WiFiClient BoardESP32;
PubSubClient client_mqtt(BoardESP32);
// WiFi network
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// MQTT Broker
const char *broker = "broker.mqtt-dashboard.com";
int puerto = 1883; // Puerto no SSL
const char *id_cliente = "XIMEBRT";
// Variables
const char *topico1 = "Equipo2MUX";
const char *topico2 = "Equipo2"; // Nombre cambiado
void setup() {
Serial.begin(115200);
// Init LCD
lcd.init();
lcd.backlight();
// Print something
lcd.setCursor(0, 0);
lcd.print("XIMENA,A01746987");
delay(3500);
lcd.clear();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(S0, INPUT);
pinMode(S1, INPUT);
pinMode(PIR, INPUT);
setup_wifi();
// Connection to the broker
client_mqtt.setServer(broker, puerto);
if (client_mqtt.connect(id_cliente)) {
Serial.println("Connected to MQTT Broker!");
} else {
Serial.println("Connected to MQTT failed...");
}
// Activate the routine to receive messages
client_mqtt.setCallback(procesar_mensajes);
// Subscribe to a topic
if (client_mqtt.subscribe(topico1)) {
Serial.printf("Suscrito a topico %s\n", topico1);
}
}
void loop() {
client_mqtt.loop(); // Manejar mensajes MQTT
// Agregar ifs para determinar qué función llamar basándote en el contenido del mensaje
if (SW0 == 0 && SW1 == 0) {
tiempo();
} else if (SW0 == 0 && SW1 == 1) {
temperatura();
} else if (SW0 == 1 && SW1 == 0) {
humedad();
} else if (SW0 == 1 && SW1 == 1) {
movimiento();
}
}
void tiempo() {
DateTime now = rtc.now();
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
int year = now.year();
lcd.setCursor(3, 0);
lcd.print(year);
lcd.print("/");
Serial.print(now.month(), DEC);
Serial.print('/');
int month = now.month();
lcd.print(month);
lcd.print('/');
Serial.print(now.day(), DEC);
int day = now.day();
lcd.print(day);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
lcd.setCursor(4, 1);
int hour = now.hour();
lcd.print(hour);
lcd.print(':');
Serial.print(now.minute(), DEC);
int minute = now.minute();
lcd.print(minute);
lcd.print(':');
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
int second = now.second();
lcd.print(second);
delay(1000);
}
void humedad() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float humedad = data.humidity;
Serial.println("Humidity: " + String(humedad) + "%");
Serial.println("---");
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Humedad");
lcd.setCursor(6, 1);
lcd.print(humedad);
delay(1000);
}
void temperatura() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temperatura = data.temperature;
Serial.println("Temp: " + String(temperatura) + "°C");
Serial.println("---");
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Temperatura");
lcd.setCursor(6, 1);
lcd.print(temperatura);
delay(1000);
}
void movimiento() {
int move = digitalRead(PIR);
if (move == HIGH) {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Movimiento");
lcd.setCursor(3, 1);
lcd.print("detectado");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("NO Movimiento");
lcd.setCursor(3, 1);
lcd.print("detectado");
}
delay(1000);
}
void setup_wifi() {
delay(50);
// Connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
void publicar() {
int EdoSensor = 23;
char EdoSensorString[5];
itoa(EdoSensor, EdoSensorString, 10);
client_mqtt.publish(topico2, EdoSensorString);
}
void procesar_mensajes(char *topic, byte *mensaje, unsigned int longitud) {
char mensaje_local[100] = {0};
Serial.print("Topic: ");
Serial.println(topic);
Serial.print("Mensaje recibido: ");
for (int i = 0; i < longitud; i++) {
Serial.write(mensaje[i]);
mensaje_local[i] = mensaje[i];
}
Serial.println();
SW0 = int(mensaje_local[0]) - 48;
SW1 = int(mensaje_local[1]) - 48;
Serial.print("SW0=");
Serial.print(SW0);
Serial.print(" SW1=");
Serial.print(SW1);
Serial.println();
int DatoRecibido = strtol(mensaje_local, 0, 10);
Serial.print("Dato recibido tipo long: ");
Serial.println(DatoRecibido);
}