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

One DHT22 and a relay

Post by plutonium »

Hi :)

I think this problem is quite simple to you.
I'm new to z-uno and I cannot figure out this problem.

I have a relay and one DHT22 sensor. But I cannot get my temperatur or humidity to shown in domoticz but in "serial monitor" it shows my realy works like a charm!

What have I done wrong?

This is my code:

Code: Select all

#include "ZUNO_DHT.h"

// pin number, where relay is connected
#define RELAY_PIN     12
#define DHTPIN 9 

DHT dht(DHTPIN, DHT11);

// variable to store current relay state
byte lastSetValue;
int humidity;      // here we will store the humidity
int temperature;   // here we will store the temperature


// next macro sets up the Z-Uno channels
ZUNO_SETUP_CHANNELS(
                    ZUNO_SWITCH_BINARY(getter, setter),
                    ZUNO_SENSOR_MULTILEVEL_TEMPERATURE(getterTemperature),
                    ZUNO_SENSOR_MULTILEVEL_HUMIDITY(getterHumidity)
                    );     

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

void loop() {
  // loop is empty, because all the control comes over the Z-Wave
   // obtaining readings from the sensor DHT
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  Serial.print("Humidity = "); 
  Serial.print(humidity);
  Serial.print(" %  ");
  Serial.print("Temperature = "); 
  Serial.print(temperature);
  Serial.println(" *C");
  // send data to channels
  zunoSendReport(1);
  zunoSendReport(2); 
  // send every 30 second
  delay(30000);
}

byte getterTemperature() {
  return temperature;
}

byte getterHumidity() {
  return humidity;
}
// function, which returns the previously saved relay value
// this function runs only once the controller asks
byte getter() {
    return lastSetValue;
}


// function, which sets new relay state
// this function runs only once the controller sends new value
void setter(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
    digitalWrite(RELAY_PIN, LOW); //turn relay on
  } else {            // if equals zero
    digitalWrite(RELAY_PIN, HIGH); //turn relay off
  }

  // save the new value in a variable
  lastSetValue = newValue;
}
droll
Posts: 48
Joined: 20 Dec 2013 01:37

Re: One DHT22 and a relay

Post by droll »

You may also report channel 3 that is used for the humidity value:

Code: Select all

zunoSendReport(3);
plutonium
Posts: 8
Joined: 05 Mar 2017 19:39

Re: One DHT22 and a relay

Post by plutonium »

Thanks droll! You did the trick :)

Now I have added a basic switch that I want to monitor on or off state, but I get some debug. And I don't really know how to solve it.

debug:

Code: Select all

unreferenced function argument : 'i'
LLCore_arduino.c:256: warning 85: in function delayLoops unreferenced function argument : 'v'
Custom.h:21: error 20: Undefined identifier 'setterWater'
sdcc: Calling preprocessor...
I can understand that my setup channel is wrong but I don't know why..


Code:

Code: Select all

#include "ZUNO_DHT.h"


// Pin number
#define RELAY_PIN 12    // Realy pin
#define DHTPIN 9        // DHT22 sensor
#define WATER_PIN 10    // Water level sensor
DHT dht(DHTPIN, DHT11);


// variable to store current state
byte relayValue;      // here we will store the realy state
byte waterValue;      // here we will store the water level state
int  humidity;        // here we will store the humidity
int  temperature;     // here we will store the temperature


// Sets up the Z-Uno channels
ZUNO_SETUP_CHANNELS(
                    ZUNO_SWITCH_BINARY(getterRelay1, setterRelay1),
                    ZUNO_SWITCH_BINARY(getterWater, setterWater),
                    ZUNO_SENSOR_MULTILEVEL_TEMPERATURE(getterTemperature),
                    ZUNO_SENSOR_MULTILEVEL_HUMIDITY(getterHumidity)
                    );     


void setup() {
  // set up I/O pins
  pinMode(RELAY_PIN, OUTPUT); // set up relay pin as output
  pinMode(WATER_PIN, INPUT_PULLUP); // Control water level
  
  dht.begin();
  Serial.begin(); 
  Serial.println("start");
}

void loop() {
  // obtaining readings from the sensor DHT
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  Serial.print("Humidity = "); 
  Serial.print(humidity);
  Serial.print(" %  ");
  Serial.print("Temperature = "); 
  Serial.print(temperature);
  Serial.println(" *C");

  // obtaining readings from switches
  Serial.print("But1=");
  Serial.print(digitalRead(WATER_PIN));
  // send every 10 second
  delay(10000);
}


// function, which returns the previously saved value
// this function runs only once the controller asks
byte getterRelay1() {
      return relayValue;
     // send data to channel
     zunoSendReport(1);
}

byte getterWater() {
      return waterValue;
     // send data to channel
     zunoSendReport(2);
}

byte getterTemperature() {
      return temperature;
     // send data to channel
     zunoSendReport(3);
}

byte getterHumidity() {
      return humidity;
     // send data to channel
     zunoSendReport(3);
}



// function, which sets new relay state
// this function runs only once the controller sends new value
void setterRelay1(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
    digitalWrite(RELAY_PIN, LOW); //turn relay on
  } else {            // if equals zero
    digitalWrite(RELAY_PIN, HIGH); //turn relay off
  }

  // save the new value in a variable
  relayValue = newValue;
}
User avatar
10der
Posts: 80
Joined: 08 Jul 2016 00:23
Location: Ukraine - Berkeley, CA

Re: One DHT22 and a relay

Post by 10der »

sample and internal remarks

Code: Select all

ZUNO_SETUP_CHANNELS(
  ZUNO_SENSOR_MULTILEVEL_TEMPERATURE(getterTemp)                          //1

  , 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)                                     //2

  , ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_LUMINANCE,
                           SENSOR_MULTILEVEL_SCALE_LUX,
                           SENSOR_MULTILEVEL_SIZE_TWO_BYTES,
                           SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS,
                           getterLum)                                     //3

  , ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_MOTION, getterMotion)      //4

  , ZUNO_SWITCH_BINARY(getterSwitch1, setterSwitch1)                      //5
  , ZUNO_SWITCH_BINARY(getterSwitch2, setterSwitch2)                      //6
  , ZUNO_SWITCH_BINARY(getterSwitch3, setterSwitch3)                      //7
  , ZUNO_SWITCH_BINARY(getterSwitch4, setterSwitch4)                      //8

);

#define ZUNO_CHANNEL_TEMP         1
#define ZUNO_CHANNEL_HUM          2
#define ZUNO_CHANNEL_LUM          3
#define ZUNO_CHANNEL_MOTION       4

#define ZUNO_CHANNEL_SWITCH_1     5
#define ZUNO_CHANNEL_SWITCH_2     6
#define ZUNO_CHANNEL_SWITCH_3     7
#define ZUNO_CHANNEL_SWITCH_4     8

#define ZUNO_CHANNEL_RESERVED_1   9
#define ZUNO_CHANNEL_RESERVED_2   10
[...]

Code: Select all

 
//------------------------ Read temperature value --------------------------------

    currentTemperatureValue = dht22_sensor.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 = dht22_sensor.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
User avatar
10der
Posts: 80
Joined: 08 Jul 2016 00:23
Location: Ukraine - Berkeley, CA

Re: One DHT22 and a relay

Post by 10der »

about relay setters & getters - another sample

Code: Select all

#define MAX_RELAYS 4
struct RelayState_s
{
  BYTE  pin;
  BYTE  state;
};

// Cooling and Heating switches
#define SWITCH1_PIN   11
#define SWITCH2_PIN   12
[...]

Code: Select all

// ------------- Switches -------------------
void setterSwitch1(byte value) {
#ifdef DEBUG
  Serial.print("Switch1: ");
  Serial.println(value);
#endif
  digitalWrite(relays[0].pin, (value > 0) ? LOW : HIGH );
  relays[0].state = value;
}

byte  getterSwitch1() {
#ifdef DEBUG
  Serial.print("+Switch1: ");
  Serial.println(relays[0].state);
#endif
  return relays[0].state;
}

void setterSwitch2(byte value) {
#ifdef DEBUG
  Serial.print("Switch2: ");
  Serial.println(value);
#endif
  digitalWrite(relays[1].pin, (value > 0) ? LOW : HIGH );
  relays[1].state = value;
}

byte  getterSwitch2() {
#ifdef DEBUG
  Serial.print("+Switch2: ");
  Serial.println(relays[1].state);
#endif
  return relays[1].state;
}
plutonium
Posts: 8
Joined: 05 Mar 2017 19:39

Re: One DHT22 and a relay

Post by plutonium »

Hi 10der.

Thanks for your reply! I cannot migrate your code correctly. I can't understand the whole the syntax. I'm a noob :)

Do you think you can help me some more with this?
User avatar
10der
Posts: 80
Joined: 08 Jul 2016 00:23
Location: Ukraine - Berkeley, CA

Re: One DHT22 and a relay

Post by 10der »

it's simple "C" lang :) it's easy :)

>>Do you think you can help me some more with this?

ok. you can ask your questions here.
User avatar
10der
Posts: 80
Joined: 08 Jul 2016 00:23
Location: Ukraine - Berkeley, CA

Re: One DHT22 and a relay

Post by 10der »

copy-past pattern detected :)

in declaration you have
DHT dht(DHTPIN, DHT22);
so variable name - dht

but in code, you are trying to use dht22_sensor

so, simple replace or in declaration dht on dht22_sensor or in code dht22_sensor on dht
User avatar
10der
Posts: 80
Joined: 08 Jul 2016 00:23
Location: Ukraine - Berkeley, CA

Re: One DHT22 and a relay

Post by 10der »

User avatar
10der
Posts: 80
Joined: 08 Jul 2016 00:23
Location: Ukraine - Berkeley, CA

Re: One DHT22 and a relay

Post by 10der »

1) Serial.begin(9600); instead of Serial.begin();
2) you have first line in your sketch
#define DEBUG
Post Reply