Compare commits
	
		
			3 Commits
		
	
	
		
			4ce0244bd7
			...
			61fd8a4c45
		
	
	| Author | SHA1 | Date | 
|---|---|---|
|  | 61fd8a4c45 | |
|  | d89bf5b0a6 | |
|  | d97cb1f9b0 | 
|  | @ -0,0 +1,109 @@ | ||||||
|  | #include <ESP8266WiFi.h> | ||||||
|  | #include <PubSubClient.h> | ||||||
|  | 
 | ||||||
|  | // Update these with values suitable for your network.
 | ||||||
|  | 
 | ||||||
|  | const char* ssid = "TP-LINK_F621EA"; | ||||||
|  | const char* password = "Caracas01."; | ||||||
|  | const char* mqtt_server = "192.168.1.108"; | ||||||
|  | 
 | ||||||
|  | WiFiClient espClient; | ||||||
|  | PubSubClient client(espClient); | ||||||
|  | unsigned long lastMsg = 0; | ||||||
|  | #define MSG_BUFFER_SIZE	(50) | ||||||
|  | char msg[MSG_BUFFER_SIZE]; | ||||||
|  | int value = 0; | ||||||
|  | 
 | ||||||
|  | void setup_wifi() { | ||||||
|  | 
 | ||||||
|  |   delay(10); | ||||||
|  |   // We start by connecting to a WiFi network
 | ||||||
|  |   Serial.println(); | ||||||
|  |   Serial.print("Connecting to "); | ||||||
|  |   Serial.println(ssid); | ||||||
|  | 
 | ||||||
|  |   WiFi.mode(WIFI_STA); | ||||||
|  |   WiFi.begin(ssid, password); | ||||||
|  | 
 | ||||||
|  |   while (WiFi.status() != WL_CONNECTED) { | ||||||
|  |     delay(500); | ||||||
|  |     Serial.print("."); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   randomSeed(micros()); | ||||||
|  | 
 | ||||||
|  |   Serial.println(""); | ||||||
|  |   Serial.println("WiFi connected"); | ||||||
|  |   Serial.println("IP address: "); | ||||||
|  |   Serial.println(WiFi.localIP()); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void callback(char* topic, byte* payload, unsigned int length) { | ||||||
|  |   Serial.print("Message arrived ["); | ||||||
|  |   Serial.print(topic); | ||||||
|  |   Serial.print("] "); | ||||||
|  |   for (int i = 0; i < length; i++) { | ||||||
|  |     Serial.print((char)payload[i]); | ||||||
|  |   } | ||||||
|  |   Serial.println(); | ||||||
|  | 
 | ||||||
|  |   // Switch on the LED if an 1 was received as first character
 | ||||||
|  |   if ((char)payload[0] == '1') { | ||||||
|  |     digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
 | ||||||
|  |     // but actually the LED is on; this is because
 | ||||||
|  |     // it is active low on the ESP-01)
 | ||||||
|  |   } else { | ||||||
|  |     digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void reconnect() { | ||||||
|  |   // Loop until we're reconnected
 | ||||||
|  |   while (!client.connected()) { | ||||||
|  |     Serial.print("Attempting MQTT connection..."); | ||||||
|  |     // Create a random client ID
 | ||||||
|  |     String clientId = "ESP8266Client-"; | ||||||
|  |     clientId += String(random(0xffff), HEX); | ||||||
|  |     // Attempt to connect
 | ||||||
|  |     if (client.connect(clientId.c_str())) { | ||||||
|  |       Serial.println("connected"); | ||||||
|  |       // Once connected, publish an announcement...
 | ||||||
|  |       client.publish("invernadero_inteligente", "hello world"); | ||||||
|  |       // ... and resubscribe
 | ||||||
|  |       client.subscribe("invernadero_inteligente"); | ||||||
|  |     } else { | ||||||
|  |       Serial.print("failed, rc="); | ||||||
|  |       Serial.print(client.state()); | ||||||
|  |       Serial.println(" try again in 5 seconds"); | ||||||
|  |       // Wait 5 seconds before retrying
 | ||||||
|  |       delay(5000); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void setup() { | ||||||
|  |   pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
 | ||||||
|  |   Serial.begin(115200); | ||||||
|  |   setup_wifi(); | ||||||
|  |   client.setServer(mqtt_server, 1883); | ||||||
|  |   client.setCallback(callback); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void loop() { | ||||||
|  | 
 | ||||||
|  |   if (!client.connected()) { | ||||||
|  |     reconnect(); | ||||||
|  |   } | ||||||
|  |   client.loop(); | ||||||
|  | 
 | ||||||
|  |   unsigned long now = millis(); | ||||||
|  |   if (now - lastMsg > 2000) { | ||||||
|  |     lastMsg = now; | ||||||
|  |     ++value; | ||||||
|  |     snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value); | ||||||
|  |     Serial.print("Publish message: "); | ||||||
|  |     Serial.println(msg); | ||||||
|  |     client.publish("invernadero_inteligente", msg); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | @ -0,0 +1,224 @@ | ||||||
|  | #include <RTClib.h> | ||||||
|  | #include <Wire.h> | ||||||
|  | 
 | ||||||
|  | float calibration_value = 21.34; | ||||||
|  | int phval = 0; | ||||||
|  | unsigned long int avgval; | ||||||
|  | int buffer_arr[10], temp; | ||||||
|  | int relePin1 = 2; | ||||||
|  | int relePin2 = 3; | ||||||
|  | RTC_DS3231 rtc; | ||||||
|  | bool inicio_bombeo = true; | ||||||
|  | bool fin_bombeo = true; | ||||||
|  | 
 | ||||||
|  | void setup() | ||||||
|  | { | ||||||
|  |   Serial.begin(9600); | ||||||
|  | } | ||||||
|  | void loop() { | ||||||
|  |   for (int i = 0; i < 10; i++) | ||||||
|  |   { | ||||||
|  |     buffer_arr[i] = analogRead(A0); | ||||||
|  |     delay(30); | ||||||
|  |   } | ||||||
|  |   for (int i = 0; i < 9; i++) | ||||||
|  |   { | ||||||
|  |     for (int j = i + 1; j < 10; j++) | ||||||
|  |     { | ||||||
|  |       if (buffer_arr[i] > buffer_arr[j]) | ||||||
|  |       { | ||||||
|  |         temp = buffer_arr[i]; | ||||||
|  |         buffer_arr[i] = buffer_arr[j]; | ||||||
|  |         buffer_arr[j] = temp; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |   avgval = 0; | ||||||
|  |   for (int i = 2; i < 8; i++) | ||||||
|  |     avgval += buffer_arr[i]; | ||||||
|  |   float volt = (float)avgval * 5.0 / 1024 / 6; | ||||||
|  |   float ph_act = -5.70 * volt + calibration_value; | ||||||
|  |   if (ph_act >= 6.50){ | ||||||
|  |     digitalWrite (relePin1, HIGH); | ||||||
|  |   }else { | ||||||
|  |     digitalWrite (relePin1, LOW); | ||||||
|  |   DateTime horario = rtc.now(); | ||||||
|  | 
 | ||||||
|  |   //Riego de las 00:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 0 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("00:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 0 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("00:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |  //Riego de las 06:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 6 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("06:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 6 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("06:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |  //Riego de 08:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 8 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("08:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 8 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("08:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |  //Riego de 10:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 10 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("10:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 10 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("10:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |  //Riego de 11:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 11 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("11:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 11 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("11:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |  //Riego de 12:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 12 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("12:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 12 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("12:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |  //Riego de 13:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 13 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("13:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 13 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("13:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |  //Riego de 14:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 14 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("14:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 14 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("14:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |  //Riego de 15:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 15 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("15:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 15 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("15:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |  //Riego de 17:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 17 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("17:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 17 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("17:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |  //Riego de 20:00
 | ||||||
|  |   if (relePin1, LOW && horario.hour() == 20 && horario.minute() == 0){ | ||||||
|  |     if (inicio_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, HIGH); | ||||||
|  |       Serial.println("20:00 ON"); | ||||||
|  |       inicio_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   if (relePin1, LOW && horario.hour() == 20 && horario.minute() == 5){ | ||||||
|  |     if (fin_bombeo = true){ | ||||||
|  |       digitalWrite (relePin2, LOW); | ||||||
|  |       Serial.println("20:05 OFF"); | ||||||
|  |       fin_bombeo = false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   delay(1000); | ||||||
|  |  } | ||||||
|  | } | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | Subproject commit 97c1a18cfd6f125cad9e1483a302f8c9e928f3ce | ||||||
|  | @ -0,0 +1,38 @@ | ||||||
|  | #include <DHT.h> | ||||||
|  | #include <DHT_U.h> | ||||||
|  | 
 | ||||||
|  | DHT SenHumTemp (1, DHT11); | ||||||
|  | float tempc, hume; | ||||||
|  | int temp_limite = 29; | ||||||
|  | int ventiladores = 3; | ||||||
|  | bool ventiladores_activados = false; | ||||||
|  | unsigned long start_time = 0; | ||||||
|  | 
 | ||||||
|  | void leerDatos(){ | ||||||
|  |   tempc = SenHumTemp.readTemperature(); | ||||||
|  |   hume = SenHumTemp.readHumidity(); | ||||||
|  |   Serial.println("Temperatura: " + String(tempc)+ "Humedad: " + String (hume)); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void setup() { | ||||||
|  |   Serial.begin(115200); | ||||||
|  |   SenHumTemp.begin(); | ||||||
|  |   pinMode (ventiladores, OUTPUT); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void loop() { | ||||||
|  |   if(millis() - start_time >= 2000){ | ||||||
|  |   leerDatos(); | ||||||
|  |   start_time = millis();   | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   if (tempc >= temp_limite){ | ||||||
|  |   digitalWrite (ventiladores, HIGH); | ||||||
|  |   ventiladores_activados = true; | ||||||
|  |   Serial.println ("Los ventiladores se han activado"); | ||||||
|  |   }else { | ||||||
|  |   digitalWrite (ventiladores, LOW); | ||||||
|  |   ventiladores_activados = false; | ||||||
|  |   Serial.println ("Los ventiladores se han desactivado"); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | @ -0,0 +1,38 @@ | ||||||
|  | #include <DHT.h> | ||||||
|  | #include <DHT_U.h> | ||||||
|  | 
 | ||||||
|  | DHT SenHumTemp (1, DHT11); | ||||||
|  | float tempc, hume; | ||||||
|  | int temp_limite = 29; | ||||||
|  | int ventiladores = 3; | ||||||
|  | bool ventiladores_activados = false; | ||||||
|  | unsigned long start_time = 0; | ||||||
|  | 
 | ||||||
|  | void leerDatos(){ | ||||||
|  |   tempc = SenHumTemp.readTemperature(); | ||||||
|  |   hume = SenHumTemp.readHumidity(); | ||||||
|  |   Serial.println("Temperatura: " + String(tempc)+ "Humedad: " + String (hume)); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void setup() { | ||||||
|  |   Serial.begin(115200); | ||||||
|  |   SenHumTemp.begin(); | ||||||
|  |   pinMode (ventiladores, OUTPUT); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void loop() { | ||||||
|  |   if(millis() - start_time >= 2000){ | ||||||
|  |   leerDatos(); | ||||||
|  |   start_time = millis();   | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   if (tempc >= temp_limite){ | ||||||
|  |   digitalWrite (ventiladores, HIGH); | ||||||
|  |   ventiladores_activados = true; | ||||||
|  |   Serial.println ("Los ventiladores se han activado"); | ||||||
|  |   }else { | ||||||
|  |   digitalWrite (ventiladores, LOW); | ||||||
|  |   ventiladores_activados = false; | ||||||
|  |   Serial.println ("Los ventiladores se han desactivado"); | ||||||
|  |   } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue