#include <DHTesp.h>
#include <WiFiClient.h>
#include <WiFi.h>
#include <WebServer.h>
#include <LiquidCrystal_I2C.h>//libreria de LCD
/* Put your SSID & Password */
#define DHTPIN 4//pin del sesnor de temperatura
//tipo de sensor
#define DHTTYPE DHT22
DHTesp dhtSensor;
const char* ssid = "INFINITUMTHPY_2.4"; // Enter SSID here
const char* password = "TUuhaJ379A"; //Enter Password here
//asignacion de sensor
LiquidCrystal_I2C lcd(0x27, 16, 2);//asiognacion de LCD
WebServer server(80);
const int Luz3 = 18;
const int Luz2 = 19;
const int Luz1 = 23;
const int MotorOnOff = 15;
const int motorAltaBaja = 5;
const int Bomba = 12;
const int AutoLed = 13;
const int PIR = 36;
const int POT = 39;
const int btnMode = 35;
const int btnMotorOnOff = 32;
const int btnMotorAltaBaja = 33;
const int btnAuto = 25;
const int btnLuz1 = 26;
const int btnLuz2 = 27;
const int btnLuz3 = 14;
int luz1ONOFF = 0;
int luz2ONOFF = 0;
int luz3ONOFF = 0;
int marcaAuto = 0;
int marcaMode = 0;
int marcaONOFF = 0;
int marcaAire = 0;
int display1 = 0;
float valuePot;
float PotTemp;
float Temp;
float Hum;
float difTemp;
void ISR1()
{
if (marcaMode == 0)
{
marcaMode = 1;
}
else
{
if (marcaMode == 1)
{
marcaMode = 0;
}
}
}
//funcion de interrupciones para boton de encendido y apagado de motor
void ISR2()
{
if (marcaONOFF == 0)
{
marcaONOFF = 1;
display1 = 1;
}
else if (marcaONOFF == 1)
{
marcaONOFF = 0;
display1 = 2;
}
}
void LUZ1()
{
if (luz1ONOFF == 0)
{
luz1ONOFF = 1;
}
else
{
if (luz1ONOFF == 1)
{
luz1ONOFF = 0;
}
}
}
//funcion de control de luz 2
void LUZ2()
{
if (luz2ONOFF == 0)
{
luz2ONOFF = 1;
}
else
{
if (luz2ONOFF == 1)
{
luz2ONOFF = 0;
}
}
}
//funcion de control de luz 3
void LUZ3()
{
if (luz3ONOFF == 0)
{
luz3ONOFF = 1;
}
else
{
if (luz3ONOFF == 1)
{
luz3ONOFF = 0;
}
}
}
//funcion de control de luces manual
void luz_manual()
{
digitalRead(PIR);
if (digitalRead(PIR) == LOW)
{
luz1ONOFF = 0;
luz2ONOFF = 0;
luz3ONOFF = 0;
}
if (luz1ONOFF == 1)
{
digitalWrite(Luz1, LOW);
}
if (luz1ONOFF == 0)
{
digitalWrite(Luz1, HIGH);
}
if (luz2ONOFF == 1)
{
digitalWrite(Luz2, LOW);
}
if (luz2ONOFF == 0)
{
digitalWrite(Luz2, HIGH);
}
if (luz3ONOFF == 1)
{
digitalWrite(Luz3, LOW);
}
if (luz3ONOFF == 0)
{
digitalWrite(Luz3, HIGH);
}
delay(200);
}
void AUTO()
{
if (marcaAuto == 0)
{
marcaAuto = 1;
}
else if (marcaAuto == 1)
{
marcaAuto = 0;
}
}
void DISPLAY_SHOW(int STATE)
{
String val1;
String val2;
int val3;
switch (STATE)
{
case 1:
val1 = "Encendiendo Aire";
val2 = "Bajo";
val3 = 0;
break;
case 2:
val1 = "Encendiendo Aire";
val2 = "Alto";
val3 = 0;
break;
case 3:
val1 = "Apagando Aire";
val2 = "";
val3 = 4;
break;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(val1);
lcd.setCursor(2, 1);
lcd.print(val2);
display1 = val3;
HUM();
delay(3000);
}
void HUM ()
{
if (marcaAire == 1)
{
if (Hum > 40)
{
digitalWrite(Bomba, HIGH);
}
if (Hum < 35)
{
digitalWrite(Bomba, LOW);
}
}
else if (marcaAire == 0)
{
digitalWrite(Bomba, HIGH);
}
}
void AIRE()
{
if ( marcaAuto == 1 & marcaONOFF == 1)
{
if (Temp > PotTemp)
{
if (difTemp > 1 & difTemp < 3 )
{
digitalWrite(motorAltaBaja, HIGH);
digitalWrite(MotorOnOff, LOW);
marcaAire = 1;
if (display1 == 1)
{
DISPLAY_SHOW(1);
display1 = 0;
}
}
if (difTemp >= 4)
{
digitalWrite(motorAltaBaja, LOW );
digitalWrite(MotorOnOff, LOW);
marcaAire = 1;
if (display1 == 1)
{
DISPLAY_SHOW(2);
display1 = 0;
}
}
}
if (Temp < PotTemp)
{
if (difTemp < 0 & difTemp > -1)
{
marcaAire = 1;
digitalWrite(MotorOnOff, LOW);
digitalWrite(motorAltaBaja, HIGH);
}
if (difTemp < -1.5)
{
marcaAire = 0;
digitalWrite(MotorOnOff, HIGH);
digitalWrite(motorAltaBaja, HIGH);
}
}
}
//Manual
if (marcaAuto == 0 & marcaONOFF == 1)
{
if (Temp > 27)
{
digitalWrite(motorAltaBaja, LOW);
}
else
{
digitalWrite(motorAltaBaja, HIGH);
}
digitalWrite(MotorOnOff, LOW);
marcaAire = 1;
if (display1 == 1)
{
DISPLAY_SHOW(1);
display1 = 0;
}
}
//Off
if (marcaONOFF == 0 )
{
digitalWrite(motorAltaBaja, HIGH);
digitalWrite(MotorOnOff, HIGH);
marcaAire = 0;
if (display1 == 2 )
{
DISPLAY_SHOW(3);
display1 = 1;
marcaAuto = 0;
}
}
}
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHTPIN, DHTesp::DHT22);
lcd.init();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("ENCENDIENDO");
lcd.setCursor(3, 2);
lcd.print("COOLLIGHT");
pinMode (MotorOnOff, OUTPUT);
pinMode (motorAltaBaja, OUTPUT);
pinMode (Bomba, OUTPUT);
pinMode (AutoLed, OUTPUT);
pinMode (Luz1, OUTPUT);
pinMode (Luz2, OUTPUT);
pinMode (Luz3, OUTPUT);
pinMode (POT, INPUT);
pinMode (btnMotorOnOff , INPUT);
pinMode (btnMotorAltaBaja, INPUT);
pinMode (btnAuto, INPUT);
pinMode (btnLuz1, INPUT);
pinMode (btnLuz2, INPUT);
pinMode (btnLuz3, INPUT);
pinMode (btnMode, INPUT);
pinMode (PIR, INPUT);
digitalWrite(MotorOnOff, HIGH );
digitalWrite(motorAltaBaja, HIGH );
digitalWrite(Bomba, HIGH );
digitalWrite(AutoLed, HIGH );
digitalWrite(Luz1, HIGH );
digitalWrite(Luz2, HIGH );
digitalWrite(Luz3, HIGH );
attachInterrupt(btnMode, ISR1, FALLING);
attachInterrupt(btnMotorOnOff, ISR2, FALLING);
attachInterrupt(btnAuto, AUTO, FALLING);
attachInterrupt(btnLuz1, LUZ1, RISING);
attachInterrupt(btnLuz2, LUZ2, RISING);
attachInterrupt(btnLuz3, LUZ3, RISING);
delay(1000);
Serial.println("Connecting to ");
Serial.println(ssid);
//connect to your local wi-fi network
WiFi.begin("Wokwi-GUEST", "", 6);
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
server.on("/", handle_OnConnect);
server.on("/luz1on", handle_luz1on);
server.on("/luz1off", handle_luz1off);
server.on("/luz2on", handle_luz2on);
server.on("/luz2off", handle_luz2off);
server.on("/luz3on", handle_luz3on);
server.on("/luz3off", handle_luz3off);
server.on("/AutoOn", handle_AutoOn);
server.on("/AutoOff", handle_AutoOff);
server.on("/AireOn", handle_AireOn);
server.on("/AireOff", handle_AireOff);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop()
{
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin("Wokwi-GUEST", "", 6);
}
TempAndHumidity data = dhtSensor.getTempAndHumidity();
valuePot = analogRead(POT);
PotTemp = (((valuePot / 4095) * 15) + 15);
difTemp = Temp - PotTemp;
Temp = data.temperature;
Hum = data.humidity;
HUM();
AIRE();
luz_manual();
if (marcaAuto == 0)
{
digitalWrite(AutoLed, LOW );
}
else if (marcaAuto == 1)
{
digitalWrite(AutoLed, HIGH );
}
if (marcaMode == 0)
{
lcd.setCursor(0, 0);
lcd.print(" Temp: " + String(Temp, 2) + " " + "C ");
lcd.setCursor(0, 1);
if (marcaONOFF == 1)
{
lcd.setCursor(0, 1);
lcd.print(" Aire Encendido ");
}
if (marcaONOFF == 0)
{
lcd.print(" Aire Apagado ");
}
delay(100);
}
if (marcaMode == 1)
{
valuePot = analogRead(POT);
PotTemp = (((valuePot / 4095) * 15) + 15);
lcd.setCursor(0, 0);
lcd.print(" CAMBIE LA TEMP ");
lcd.setCursor(0, 1);
lcd.print(" Temp: " + String(PotTemp, 1) + " " + "C ");
delay(100);
}
server.handleClient();
luz_manual();
Serial.println(String(luz1ONOFF) + String(luz2ONOFF) + String (luz3ONOFF) + String (digitalRead(PIR)));
}
void handle_OnConnect() {
luz1ONOFF = luz1ONOFF;
luz2ONOFF = luz2ONOFF;
luz3ONOFF = luz3ONOFF;
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Temp = data.temperature;
server.send(200, "text/html", SendHTML(Temp, PotTemp, luz1ONOFF, luz2ONOFF, luz3ONOFF, marcaAuto, marcaONOFF));
}
void handle_luz1on() {
luz1ONOFF = 1;
server.send(200, "text/html", SendHTML(Temp, PotTemp, 1, luz2ONOFF, luz3ONOFF, marcaAuto, marcaONOFF));
}
void handle_luz1off() {
luz1ONOFF = 0;
server.send(200, "text/html", SendHTML(Temp, PotTemp, 0, luz2ONOFF, luz3ONOFF, marcaAuto, marcaONOFF));
}
void handle_luz2on() {
luz2ONOFF = 1;
server.send(200, "text/html", SendHTML(Temp, PotTemp, luz1ONOFF, 1, luz3ONOFF, marcaAuto, marcaONOFF));
}
void handle_luz2off() {
luz2ONOFF = 0;
server.send(200, "text/html", SendHTML(Temp, PotTemp, luz1ONOFF, 0, luz3ONOFF, marcaAuto, marcaONOFF));
}
void handle_luz3on() {
luz3ONOFF = 1;
server.send(200, "text/html", SendHTML(Temp, PotTemp, luz1ONOFF, luz2ONOFF, 1, marcaAuto, marcaONOFF));
}
void handle_luz3off() {
luz3ONOFF = 0;
server.send(200, "text/html", SendHTML(Temp, PotTemp, luz1ONOFF, luz2ONOFF, 0, marcaAuto, marcaONOFF));
}
void handle_AutoOn() {
marcaAuto = 1;
server.send(200, "text/html", SendHTML(Temp, PotTemp, luz1ONOFF, luz2ONOFF, luz3ONOFF, 1 , marcaONOFF));
}
void handle_AutoOff() {
marcaAuto = 0;
server.send(200, "text/html", SendHTML(Temp, PotTemp, luz1ONOFF, luz2ONOFF, luz3ONOFF, 0 , marcaONOFF));
}
void handle_AireOn () {
marcaONOFF = 1;
server.send(200, "text/html", SendHTML(Temp, PotTemp, luz1ONOFF, luz2ONOFF, luz3ONOFF, marcaAuto, 1));
}
void handle_AireOff () {
marcaONOFF = 0;
server.send(200, "text/html", SendHTML(Temp, PotTemp, luz1ONOFF, luz2ONOFF, luz3ONOFF, marcaAuto, 0));
}
void handle_NotFound() {
server.send(404, "text/plain", "Not found");
}
String SendHTML(float Temp, float Pot, int luz1ONOFF, int luz2ONOFF, int luz3ONOFF, int marcaAuto, int marcaONOFF)
{
String ptr = "<!DOCTYPE html> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>COOLLIGHT</title>\n";
ptr += "<link href='https://fonts.googleapis.com/css?family=Open+Sans:300,400,600' rel='stylesheet'>";
ptr += "<style>html { font-family: 'Open Sans', sans-serif; display: block; margin: 5px auto 5px; text-align: center;color: #444444;}";
ptr += "body{margin-top: 10px;} h1 {color: #444444;margin: 10px auto 20px;} h3 {color: #444444;margin-bottom:10px;}\n";
ptr += ".buttonAuto {display: block; width: 150px;background-color: #FF1616;border: black;color: white;padding: 10px 25px;text-decoration: none;font-size: 30px;margin: 0px auto 10px;cursor: pointer;border-radius: 15px;}\n";
ptr += ".buttonAuto-on {background-color: #49E82C;}\n";
ptr += ".buttonAuto-on:active {background-color: #3CBE24;}\n";
ptr += ".buttonAuto-off {background-color: #E82C2C;}\n";
ptr += ".buttonAuto-off:active {background-color: #B82424;}\n";
ptr += ".buttonAire {display: block; width: 150px;background-color: #FF1616;border: black;color: white;padding: 10px 25px;text-decoration: none;font-size: 30px;margin: 0px auto 10px;cursor: pointer;border-radius: 15px;}\n";
ptr += ".buttonAire-on {background-color: #49E82C;}\n";
ptr += ".buttonAire-on:active {background-color: #3CBE24;}\n";
ptr += ".buttonAire-off {background-color: #E82C2C;}\n";
ptr += ".buttonAire-off:active {background-color: #B82424;}\n";
ptr += ".button {display: block; width: 35px;background-color: #1D1D26;border: black;color: white;padding: 10px 30px;text-decoration: none;font-size: 30px;margin: 1px auto 10px;cursor: pointer;border-radius: 15px;}\n";
ptr += ".button-on {background-color: #3498db;}\n";
ptr += ".button-on:active {background-color: #2980b9;}\n";
ptr += ".button-off {background-color: #FF0A0F;}\n";
ptr += ".button-off:active {background-color: #D70F0F;}\n";
ptr += "p {font-size: 15px;color: #171312;margin-bottom: 2px;}\n";
ptr += ".side-by-side{display: table-cell;vertical-align: middle; margin: auto;}";
ptr += ".text{font-weight: 600;font-size: 15px;width: 120px;}";
ptr += ".reading{font-weight: 300;font-size: 40px;padding-right: 20px;}";
ptr += ".Temp .reading{color: #C80000;}";
ptr += ".Pottemp.reading{color: #0A3CC8;}";
ptr += ".data{padding: 10px;}";
ptr += ".container{display: table;margin: 0 auto;}";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1>Coollight control</h1>\n";
ptr += "<h3>Aire</h3>\n";
ptr += "<div class='container'>";
ptr += "<div class='data Temp'>";
ptr += "<div class='side-by-side icon'>";
ptr += "<svg enable-background='new 0 0 19.438 54.003'height=54.003px id=Layer_1 version=1.1 viewBox='0 0 19.438 54.003'width=19.438px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><g><path d='M11.976,8.82v-2h4.084V6.063C16.06,2.715,13.345,0,9.996,0H9.313C5.965,0,3.252,2.715,3.252,6.063v30.982";
ptr += "C1.261,38.825,0,41.403,0,44.286c0,5.367,4.351,9.718,9.719,9.718c5.368,0,9.719-4.351,9.719-9.718";
ptr += "c0-2.943-1.312-5.574-3.378-7.355V18.436h-3.914v-2h3.914v-2.808h-4.084v-2h4.084V8.82H11.976z M15.302,44.833";
ptr += "c0,3.083-2.5,5.583-5.583,5.583s-5.583-2.5-5.583-5.583c0-2.279,1.368-4.236,3.326-5.104V24.257C7.462,23.01,8.472,22,9.719,22";
ptr += "s2.257,1.01,2.257,2.257V39.73C13.934,40.597,15.302,42.554,15.302,44.833z'fill=#F22121 /></g></svg>";
ptr += "</div>";
ptr += "<div class='side-by-side text'>Temperatura</div>";
ptr += "<div class='side-by-side reading'>";
ptr += Temp;
ptr += "<span class='superscript'>°C</span></div>";
ptr += "<div class='data Pottemp'>";
ptr += "<div class='side-by-side icon'>";
ptr += "<svg enable-background='new 0 0 19.438 54.003'height=54.003px id=Layer_1 version=1.1 viewBox='0 0 19.438 54.003'width=19.438px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><g><path d='M11.976,8.82v-2h4.084V6.063C16.06,2.715,13.345,0,9.996,0H9.313C5.965,0,3.252,2.715,3.252,6.063v30.982";
ptr += "C1.261,38.825,0,41.403,0,44.286c0,5.367,4.351,9.718,9.719,9.718c5.368,0,9.719-4.351,9.719-9.718";
ptr += "c0-2.943-1.312-5.574-3.378-7.355V18.436h-3.914v-2h3.914v-2.808h-4.084v-2h4.084V8.82H11.976z M15.302,44.833";
ptr += "c0,3.083-2.5,5.583-5.583,5.583s-5.583-2.5-5.583-5.583c0-2.279,1.368-4.236,3.326-5.104V24.257C7.462,23.01,8.472,22,9.719,22";
ptr += "s2.257,1.01,2.257,2.257V39.73C13.934,40.597,15.302,42.554,15.302,44.833z'fill=#1EAEF6 /></g></svg>";
ptr += "</div>";
ptr += "<div class='side-by-side text'>Pre-Set de Temperatura</div>";
ptr += "<div class='side-by-side reading'>";
ptr += (int)PotTemp;
ptr += "<span class='superscript'>°C</span></div>";
if (marcaAuto == 1)
{
ptr += "<a class=\"buttonAuto buttonAuto-on\" href=\"/AutoOff\">Auto ON</a>\n";
}
else
{
ptr += "<a class=\"buttonAuto buttonAuto-off\" href=\"/AutoOn\">Auto OFF</a>\n";
}
if (marcaONOFF == 1)
{
ptr += "<a class=\"buttonAire buttonAire-on\" href=\"/AireOff\">Aire ON</a>\n";
}
else
{
ptr += "<a class=\"buttonAire buttonAire-off\" href=\"/AireOn\">Aire OFF</a>\n";
}
ptr += "<h3>Luces</h3>\n";
if (luz1ONOFF == 1)
{
ptr += "<p>LUZ1: ON</p><a class=\"button button-off\" href=\"/luz1off\">OFF</a>\n";
}
else
{
ptr += "<p>LUZ1: OFF</p><a class=\"button button-on\" href=\"/luz1on\">ON</a>\n";
}
if (luz2ONOFF == 1)
{
ptr += "<p>LUZ2: ON</p><a class=\"button button-off\" href=\"/luz2off\">OFF</a>\n";
}
else
{
ptr += "<p>LUZ2: OFF</p><a class=\"button button-on\" href=\"/luz2on\">ON</a>\n";
}
if (luz3ONOFF == 1)
{
ptr += "<p>LUZ: ON</p><a class=\"button button-off\" href=\"/luz3off\">OFF</a>\n";
}
else
{
ptr += "<p>LUZ3: OFF</p><a class=\"button button-on\" href=\"/luz3on\">ON</a>\n";
}
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}