One DHT22 and a relay

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
plutonium
Posts: 8
Joined: 05 Mar 2017 19:39

Re: One DHT22 and a relay

Post by plutonium »

Thanks again, sorry for disturbing so much.

Now I've got everything working!

I need just a little help to get my IF to work.

When the waterlevel switch return zero I don't want the relay to turn ON. How can I manage this? I can't get the variable currentWaterValue to function in my void setterRelay at the bottom.

Code: Select all


#define DEBUG
#include "ZUNO_DHT.h"

// pin number, where relay is connected
#define DHTPIN 9        // DHT22 sensor
#define RELAY_PIN 12
#define WATER_PIN 10

DHT dht(DHTPIN, DHT11);

// variable to store current relay state
byte relaylastSetValue;
byte currentWaterValue;
int currentTemperatureValue;
int currentHumValue;
int lastTemperatureValue;
int lastHumValue;
int lastWaterValue;

ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_BINARY(getterRelay, setterRelay),
  ZUNO_SENSOR_MULTILEVEL_TEMPERATURE(getterTemp),
  ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_RELATIVE_HUMIDITY,
                           SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE,
                           SENSOR_MULTILEVEL_SIZE_ONE_BYTE,
                           SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS,
                           getterHum),
  ZUNO_SWITCH_BINARY(getterWater, setterWater)  

);

#define ZUNO_CHANNEL_RELAY        1
#define ZUNO_CHANNEL_TEMP         2
#define ZUNO_CHANNEL_HUM          3
#define ZUNO_CHANNEL_WATER        4

void setup() {
  pinMode(RELAY_PIN, OUTPUT); // set up relay pin as output
  pinMode(WATER_PIN,INPUT_PULLUP); // Control button 1
  dht.begin();
  Serial.begin(9600);
  Serial.println("start");
}

void loop() {
//------------------------ Read temperature value --------------------------------
currentTemperatureValue = dht.readTemperature();
    if ((currentTemperatureValue > (lastTemperatureValue + 0)) || (currentTemperatureValue < (lastTemperatureValue - 0))) {
      lastTemperatureValue = currentTemperatureValue;
      zunoSendReport(ZUNO_CHANNEL_TEMP);        // report the temperature
    }
#ifdef DEBUG
    Serial.print("temperature = ");
    Serial.println(currentTemperatureValue);
#endif
//------------------------ Read humidity value --------------------------------
currentHumValue = dht.readHumidity();
    if ((currentHumValue > (lastHumValue + 1)) || (currentHumValue < (lastHumValue - 1))) {
      lastHumValue = currentHumValue;
      zunoSendReport(ZUNO_CHANNEL_HUM);         // report the relative humidity
    }
#ifdef DEBUG
    Serial.print("humidity = ");
    Serial.println(currentHumValue);
#endif
//------------------------ Read waterlevel value --------------------------------
currentWaterValue = digitalRead(WATER_PIN);
  if (currentWaterValue != lastWaterValue) {
    lastWaterValue = currentWaterValue;
    zunoSendReport(ZUNO_CHANNEL_WATER);
  }
#ifdef DEBUG
  Serial.print("WaterSwitch = ");
  Serial.println(currentWaterValue);
#endif

  delay(5000);
}

byte getterTemp() {
  return lastTemperatureValue;
}
byte getterHum() {
  return lastHumValue;
}

// Water switch
byte getterWater() {
  return lastWaterValue;
}

byte setterWater() {
  return lastWaterValue;
}

// RELAY

byte getterRelay() {
  return relaylastSetValue;
}

void setterRelay(byte newValue) {
  // newValue is a variable, holding a "value"
  // which came from the controller or other Z-Wave device
//  if (newValue > 0) { // if greater then zero
  if (newValue > 0){                            // I TRIED the currentWaterValue HERE!
    digitalWrite(RELAY_PIN, LOW); //turn relay off
  } else {            // if equals zero
    digitalWrite(RELAY_PIN, HIGH);  //turn relay on
  }
  // save the new value in a variable
  relaylastSetValue = newValue;
}
one thing that bother me. Why can domoticz two relay units? Have it with cinnamon are doing?
User avatar
10der
Posts: 80
Joined: 08 Jul 2016 00:23
Location: Ukraine - Berkeley, CA

Re: One DHT22 and a relay

Post by 10der »

1st notice for u:
actually, relay turned OFF with command HIGH nor LOW
2nd:
actually, setterSwitchs voids should do only simple actions...
only turned ON/OFF relay
void setterSwitch1(byte value)

Code: Select all

void setterSwitch(byte value) {
#ifdef DEBUG
  Serial.print("Switch1: ");
  Serial.println(value);
#endif
  digitalWrite(RELAY_PIN, (value > 0) ? LOW : HIGH );
  relayValue = value;
}

byte  getterSwitch() {
#ifdef DEBUG
  Serial.print("+Switch1: ");
  Serial.println(relayValue);
#endif
  return relayValue;
}
all business logic for water control etc you should develop in your controller via LUA / etc.

IMHO

... or your question about water not clear for me... sorry
plutonium
Posts: 8
Joined: 05 Mar 2017 19:39

Re: One DHT22 and a relay

Post by plutonium »

Okey,

But if I want something like this?
if (newValue > (0) && currentWaterValue > (0)){
User avatar
10der
Posts: 80
Joined: 08 Jul 2016 00:23
Location: Ukraine - Berkeley, CA

Re: One DHT22 and a relay

Post by 10der »

sure!

send a report to a controller if a light value was changed only (!) more than 5 lux

Code: Select all

    currentLumiValue = bh1750.readLightLevel(BH1750_I2CADDR);
    if ((currentLumiValue > (lastLumiValue + 5)) || (currentLumiValue < (lastLumiValue - 5))) {
      lastLumiValue = currentLumiValue;
      zunoSendReport(ZUNO_CHANNEL_LUM);         // report the luminance
    }
plutonium
Posts: 8
Joined: 05 Mar 2017 19:39

Re: One DHT22 and a relay

Post by plutonium »

I don't get it. What do you mean?
The problem that I have is that I get undeclair variable when try to use the current switch state OUTside the loop.
I cannot declare my variable outside the loop. So how can I manage this? I still want rapid response on my relay when changing the state BUT I don't want it to turned on when the switch == 1.

But if I move my digitalWrite to my loop-section i get delayed because of delay 5000, And the the whole idea with "rapid response" is not that I want.

Look at my code again. How can I manage to solve this?

Code: Select all


#define DEBUG
#include "ZUNO_DHT.h"

// pin number, where relay is connected
#define DHTPIN 9        // DHT22 sensor
#define RELAY_PIN 12
#define WATER_PIN 10

DHT dht(DHTPIN, DHT11);

// variable to store current relay state
byte relaylastSetValue;
byte currentWaterValue;
int currentTemperatureValue;
int currentHumValue;
int lastTemperatureValue;
int lastHumValue;
int lastWaterValue;

ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_BINARY(getterRelay, setterRelay),
  ZUNO_SENSOR_MULTILEVEL_TEMPERATURE(getterTemp),
  ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_RELATIVE_HUMIDITY,
                           SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE,
                           SENSOR_MULTILEVEL_SIZE_ONE_BYTE,
                           SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS,
                           getterHum),
  ZUNO_SWITCH_BINARY(getterWater, setterWater)  

);

#define ZUNO_CHANNEL_RELAY        1
#define ZUNO_CHANNEL_TEMP         2
#define ZUNO_CHANNEL_HUM          3
#define ZUNO_CHANNEL_WATER        4

void setup() {
  pinMode(RELAY_PIN, OUTPUT); // set up relay pin as output
  pinMode(WATER_PIN,INPUT_PULLUP); // Control button 1
  dht.begin();
  Serial.begin(9600);
  Serial.println("start");
}

void loop() {
//------------------------ Read temperature value --------------------------------
currentTemperatureValue = dht.readTemperature();
    if ((currentTemperatureValue > (lastTemperatureValue + 0)) || (currentTemperatureValue < (lastTemperatureValue - 0))) {
      lastTemperatureValue = currentTemperatureValue;
      zunoSendReport(ZUNO_CHANNEL_TEMP);        // report the temperature
    }
#ifdef DEBUG
    Serial.print("temperature = ");
    Serial.println(currentTemperatureValue);
#endif
//------------------------ Read humidity value --------------------------------
currentHumValue = dht.readHumidity();
    if ((currentHumValue > (lastHumValue + 1)) || (currentHumValue < (lastHumValue - 1))) {
      lastHumValue = currentHumValue;
      zunoSendReport(ZUNO_CHANNEL_HUM);         // report the relative humidity
    }
#ifdef DEBUG
    Serial.print("humidity = ");
    Serial.println(currentHumValue);
#endif
//------------------------ Read waterlevel value --------------------------------
currentWaterValue = digitalRead(WATER_PIN);
  if (currentWaterValue != lastWaterValue) {
    lastWaterValue = currentWaterValue;
    zunoSendReport(ZUNO_CHANNEL_WATER);
  }
#ifdef DEBUG
  Serial.print("WaterSwitch = ");
  Serial.println(currentWaterValue);
#endif

  delay(5000);
}

byte getterTemp() {
  return lastTemperatureValue;
}
byte getterHum() {
  return lastHumValue;
}

// Water switch
byte getterWater() {
  return lastWaterValue;
}

byte setterWater() {
  return lastWaterValue;
}

// RELAY

byte getterRelay() {
  return relaylastSetValue;
}

void setterRelay(byte newValue) {
  // newValue is a variable, holding a "value"
  // which came from the controller or other Z-Wave device
//  if (newValue > 0) { // if greater then zero
  if (newValue > 0){                            // I TRIED the currentWaterValue HERE!
    digitalWrite(RELAY_PIN, LOW); //turn relay off
  } else {            // if equals zero
    digitalWrite(RELAY_PIN, HIGH);  //turn relay on
  }
  // save the new value in a variable
  relaylastSetValue = newValue;
}
Post Reply