//Incluimos las librerias
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,4);
const char* apiEndpoint = "https://super-space-spork-7v9v9rgjp9rj3q79-5000.app.github.dev/sensor_data";
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//Decaramos el variable que almacena el pin a conectar el DHT11
int pinDHT = 17;
//Instanciamos el DHT
DHTesp dht;
const int ventilador = 32; // azul
const int deshumidificador = 33; //Azul clar
const int luz = 25; // blanca
const int movimiento = 34; //Sensor de movimiento
const int botoni = 14;
void setupWifi() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.print(" Connected: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
//Inicializamos el dht
dht.setup(pinDHT, DHTesp::DHT22);
pinMode(ventilador, OUTPUT);
pinMode(deshumidificador, OUTPUT);
pinMode(luz, OUTPUT);
pinMode(movimiento, INPUT);
pinMode(botoni, INPUT);
digitalWrite(ventilador, LOW);
digitalWrite(deshumidificador, LOW);
digitalWrite(luz, LOW);
setupWifi();
lcd.init();
lcd.backlight();
}
void sendData(float temperature, float humidity, int movement){
Serial.print("Sending data to API: ");
// Set up HTTP connection with the API endpoint
HTTPClient http;
http.begin(apiEndpoint);
http.addHeader("Content-Type", "application/json");
// Create a JSON document using the ArduinoJson library
StaticJsonDocument<200> doc;
// Add the data to the JSON document
doc["temperature"] = temperature;
doc["humidity"] = humidity;
doc["movement"] = movement;
// Add the current date and time to the JSON document. This will change to a date from the proper sensor in the future
doc["tiempo"] = "2021-01-01 00:00:00";
// Serialize the JSON document to a string
String json;
serializeJson(doc, json);
// Send the POST request to the API endpoint
int httpResponseCode = http.POST(json);
if (httpResponseCode > 0) {
// Print the response from the server
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String responseString = http.getString();
Serial.println("Received response: " + responseString);
}
else {
// Print the error code
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
void loop() {
//Obtenemos el arreglo de datos (humedad y temperatura)
TempAndHumidity data = dht.getTempAndHumidity();
float temp = data.temperature;
float humid = data.humidity;
float mov = digitalRead(movimiento);
float boton = digitalRead(botoni);
if(temp > 30){
digitalWrite(ventilador, HIGH);
}
else{
if(temp <= 22){
digitalWrite(ventilador, LOW);
}
}
if(humid > 55){
digitalWrite(deshumidificador, HIGH);
}
else{
if(humid <= 25){
digitalWrite(deshumidificador, LOW);
}
}
if(mov || boton){
digitalWrite(luz, HIGH);
}
else{
digitalWrite(luz, LOW);
}
//Mostramos los datos de la temperatura y humedad
Serial.println("Temperatura: " + String(temp, 2) + "°C");
Serial.println("Humedad: " + String(humid, 1) + "%");
Serial.println("Movimiento: " + String(mov, 1) + "!");
Serial.println("---");
delay(1000);
lcd.setCursor(0,0);
lcd.print("Humedad: ");
lcd.print(String(humid,1) + "%");
lcd.setCursor(0,1);
lcd.print("Temperatura: ");
lcd.print(String(temp,1) + "%");
lcd.setCursor(0,2);
lcd.print("Movimiento: ");
lcd.print(String(mov,1) + "%");
delay(1000);
lcd.print("Enviando datos API");
sendData(temp, humid, mov);
lcd.clear();
}