//Incluimos las librerias
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include "DHTesp.h"
//Es la página y ruta a la que se conecta
const char* apiEndpoint = "https://stunning-eureka-v4gjr4jw4x92wr5j-5000.preview.app.github.dev/sensor_data";
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//Decaramos la variable que almacena el pin a conectar el DHT11
int pinDHT = 13;
//Declaramos las variables y su pin
#define LIGHT_SENSOR_PIN 15 // ESP32 pin GIOP36 (ADC0)
const int ledHumed = 21; // GPIO del led azul claro
const int ledTemp = 19; //GPIO del led rojo
const int ledLuz= 18; //GPIO del led blanco
//Instanciamos el DHT
DHTesp dht;
void setupWifi()
{
Serial.begin(9600);
//Se conecta al internet
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
//Imprime "." cada segundo mientras se conecta al internet
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(ledHumed, OUTPUT);
pinMode(ledTemp, OUTPUT);
pinMode(ledLuz, OUTPUT);
setupWifi();
}
void sendData(float temperature, float humidity, int analogValue)
{
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;
// Add the current date and time to the JSON document. This will change to a date from the proper sensor in the future
doc["date_time"] = "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() {
// reads the input on analog pin (value between 0 and 4095)
int analogValue = analogRead(LIGHT_SENSOR_PIN);
//Obtenemos el arreglo de datos (humedad y temperatura)
TempAndHumidity data = dht.getTempAndHumidity();
// We'll have a few threshholds, qualitatively determined
if (analogValue > 156) {
Serial.println(" => Dark");
digitalWrite(ledLuz, HIGH);
} else{
digitalWrite(ledLuz, LOW);
}
//Si la temperatura es mayor a 50%, se enciende el foco
if(data.temperature>=50){
digitalWrite(ledTemp, HIGH);
}
else{
digitalWrite(ledTemp, LOW);
}
//Si la humedad es mayor a 50%, se enciende el foco
if(data.humidity>=50){
digitalWrite(ledHumed, HIGH);
}
else{
digitalWrite(ledHumed, LOW);
}
//Mostramos los datos de la temperatura, humedad y luz
Serial.println("Temperatura: " + String(data.temperature, 2) + "°C");
Serial.println("Humedad: " + String(data.humidity, 1) + "%");
Serial.print("Luz: " + String(analogValue) + "lux");
Serial.println("---");
delay(1000);
sendData(data.temperature, data.humidity, analogValue);
}