Estado de relé según temperatura, humedad y presencia de luz

Hola!

Hace poco inicie un proyecto para la automatización de un sistema de riego pero aun me estoy debatiendo de como lograr que el relé se cierre/abra dependiendo de los valores que muestre un sensor DHT22 y una fotorresistencia con entrada digital. Cabe mencionar que estoy trabajando con un microprocesador Adafruit Feather Huzzah ESP8266 y su plataforma online. Agradezco de antemano cualquier sugerencia para poder solucionar este problema.

#include "config.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define HUMTEMP 2
#define SENSLUZ 5
#define SENSHUMSUELO 14
#define VALV1 4

// button state
bool current = false;
bool current2 = false;

// create DHT22 instance
DHT_Unified dht(HUMTEMP, DHT22);

// set up the feeds
AdafruitIO_Feed *temperature = io.feed("temperature");
AdafruitIO_Feed *humidity = io.feed("humidity");
AdafruitIO_Feed *light = io.feed("light");
AdafruitIO_Feed *soil = io.feed("soil");
AdafruitIO_Feed *valve1 = io.feed("valve1");

void setup() {

  pinMode(SENSLUZ, INPUT); //Fotoresistencia
  pinMode(SENSHUMSUELO, INPUT); //Sensor humedad suelo
  pinMode(HUMTEMP, INPUT); //Sensor humedad-temperatura
  pinMode(VALV1, OUTPUT); //Salida valvula 1

  Serial.begin(115200);

  while (! Serial);

  // initialize dht22
  dht.begin();

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

  valve1->onMessage(handleMessage);

  
  // wait for a connection
  while (io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());
  valve1->get();

}

void handleMessage(AdafruitIO_Data *data) {

  Serial.print("Received <- ");

  if (data->toPinLevel() == HIGH)
    Serial.println("HIGH");
  else
    Serial.println("LOW");

  digitalWrite(VALV1, data->toPinLevel());

}

void loop() {

  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();

  sensors_event_t event;
  dht.temperature().getEvent(&event);

  float celsius = event.temperature;

  Serial.println();
  Serial.print("Temperatura: ");
  Serial.print(celsius);
  Serial.println("°C");

  temperature->save(celsius);

  dht.humidity().getEvent(&event);

  Serial.print("Humedad Relativa: ");
  Serial.print(event.relative_humidity);
  Serial.println("%");

  // save humidity to Adafruit IO
  humidity->save(event.relative_humidity);

    if (digitalRead(SENSLUZ) == LOW) {
    current = true;
    Serial.println("Estado del clima: Soleado");
  } else {
    current = false;
    Serial.println("Estado del clima: Nublado / Es de Noche");
  }

   light->save(current);

  if (digitalRead(SENSHUMSUELO) == LOW) {
    current2 = true;
    Serial.println("Estado del suelo: El suelo está húmedo");
  } else {
    current2 = false;
    Serial.println("Estado del suelo: El suelo está seco");
  }

  soil->save(current2);

  delay(10000);
}

:warning:
Código corregido

Tu publicación fue ** MOVIDA ** a su ubicación actual ya que es más adecuada.

¿Podría también tomarse unos minutos para Aprenda a usar el foro .

Aquí puede encontrar más ayuda general y consejos para la resolución de problemas.

Moderador:
Si posteas en el foro en inglés usa idioma inglés para expresarte.
Si escribes en español debes usar el foro Arduino en español.

Prueba este código y observa lo que he agregado para leer la humedad

#include <Arduino.h>

#include "config.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define HUMTEMP 2
#define SENSLUZ 5
#define SENSHUMSUELO 14
#define VALV1 4
#define HUMEDAD_MAX  85.0
#define HUMEDAD_MIN  30.0

// button state
bool current = false;
bool current2 = false;

// create DHT22 instance
DHT_Unified dht(HUMTEMP, DHT22);

// set up the feeds
AdafruitIO_Feed *temperature = io.feed("temperature");
AdafruitIO_Feed *humidity = io.feed("humidity");
AdafruitIO_Feed *light = io.feed("light");
AdafruitIO_Feed *soil = io.feed("soil");
AdafruitIO_Feed *valve1 = io.feed("valve1");

void setup() {

  pinMode(SENSLUZ, INPUT); //Fotoresistencia
  pinMode(SENSHUMSUELO, INPUT); //Sensor humedad suelo
  pinMode(HUMTEMP, INPUT); //Sensor humedad-temperatura
  pinMode(VALV1, OUTPUT); //Salida valvula 1

  Serial.begin(115200);

  while (! Serial);

  // initialize dht22
  dht.begin();

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

  valve1->onMessage(handleMessage);

  
  // wait for a connection
  while (io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());
  valve1->get();

}

void handleMessage(AdafruitIO_Data *data) {

  Serial.print("Received <- ");

  if (data->toPinLevel() == HIGH)
    Serial.println("HIGH");
  else
    Serial.println("LOW");

  digitalWrite(VALV1, data->toPinLevel());

}

void loop() {

  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();

  sensors_event_t event;
  dht.temperature().getEvent(&event);

  float celsius = event.temperature;

  Serial.println();
  Serial.print("Temperatura: ");
  Serial.print(celsius);
  Serial.println("°C");

  temperature->save(celsius);

  dht.humidity().getEvent(&event);

  float humedad = event.relative_humidity;

  Serial.print("Humedad Relativa: ");
  Serial.print(humedad);
  Serial.println("%");

  // save humidity to Adafruit IO
  humidity->save(event.relative_humidity);

  if (humedad > HUMEDAD_MAX) {
    // acción para limite de humedad maxima
  }
  if (humedad < HUMEDAD_MIN) {
    // accion para limite de humedad minima
  }

  if (digitalRead(SENSLUZ) == LOW) {
      current = true;
      Serial.println("Estado del clima: Soleado");
  } 
  else {
      current = false;
      Serial.println("Estado del clima: Nublado / Es de Noche");
  }

  light->save(current);

  if (digitalRead(SENSHUMSUELO) == LOW) {
      current2 = true;
      Serial.println("Estado del suelo: El suelo está húmedo");
  } 
  else {
      current2 = false;
      Serial.println("Estado del suelo: El suelo está seco");
  }
  soil->save(current2);
  delay(10000);
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.