#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "http://api.thingspeak.com/update";
const char* apiKey = "G5WMQBY8N6051W3K"; // Reemplazar con la Write API Key
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27
const int portPin= 34;
int potValor = 0;
const int Led1= 4;
const int Led2= 16;
const int Led3= 17;
const int Led4= 5;
const int Led5= 18;
const int Led6= 19;
const int DHT_PIN = 15;
DHTesp dhtSensor;
void setup() {
// put your setup code here, to run once:
lcd.init(); // initialize the lcd
lcd.backlight();
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
pinMode(Led4, OUTPUT);
pinMode(Led5, OUTPUT);
pinMode(Led6, OUTPUT);
Serial.begin(9600);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(WiFi.status());
Serial.print("");
Serial.println("Wifi Conectado");
//delay(2000);
}
void loop() {
potValor = analogRead(portPin);
//Serial.println(potValor);
lcd.clear(); // clear display
lcd.setCursor(2, 0); // move cursor to (0, 0)
lcd.print("IOT Control"); // print message at (0, 0)
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
float t= data.temperature;
float h= data.humidity;
Serial.print("Viento: "); Serial.print(potValor/40.0); Serial.println(" Km/h");
if (WiFi.status() == WL_CONNECTED){
HTTPClient http;
String url = String(server) + "?api_key=" + apiKey + "&field1=" + String(potValor/40.0) +
"&field2=" + String(t) +
"&field3=" + String(h);
http.begin(url);http.GET();
http.end();
int httpResponseCode = http.GET();
if (httpResponseCode > 0){
Serial.println("Datos enviados a ThingSpeak");
}else{
Serial.println("Error de Conexion");
}
}
lcd.setCursor(0, 1);
lcd.print("Temp: " + String(data.temperature, 2) + " C");
delay(2000);
lcd.setCursor(0, 1);
lcd.print("Humedad: " + String(data.humidity, 1) + " %");
delay(2000);
lcd.setCursor(0, 1);
lcd.print("Viento: ");
lcd.print(potValor/40);lcd.print(" Km/h");
delay(2000);
if (potValor>=682){
digitalWrite(Led1, HIGH);
} else if(potValor < 682){
digitalWrite(Led1, LOW);
}
if (potValor>=1364){
digitalWrite(Led2, HIGH);
} else if(potValor < 1364){
digitalWrite(Led2, LOW);
}
if (potValor>=2046){
digitalWrite(Led3, HIGH);
} else if(potValor < 2046){
digitalWrite(Led3, LOW);
}
if (potValor>=2728){
digitalWrite(Led4, HIGH);
} else if(potValor < 2728){
digitalWrite(Led4, LOW);
}
if (potValor>=3410){
digitalWrite(Led5, HIGH);
} else if(potValor < 3410){
digitalWrite(Led5, LOW);
}
if (potValor>=3800){
digitalWrite(Led6, HIGH);
} else if(potValor < 3800){
digitalWrite(Led6, LOW);
}
delay(15000); // this speeds up the simulation
}