Z-Uno to readout wallswitches and send state to domoticz

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
hschokker
Posts: 4
Joined: 10 May 2018 11:25

Z-Uno to readout wallswitches and send state to domoticz

Post by hschokker »

In my home I have put signal wire to each wallswitch leading to 1 point in my house. (wire lengths 1 meter up to 15 meter)

The wallswitches have 2 switch positions, 1 that connects the 2 wires and the other one that leaves them unconnected.

In domoticz I would like to know when the state of the switch changes (at that point I will turn on or off the hue light that is appointed to that wallswitch).

In what way can I best connect the 2-wires comming from each of the switches to the Z-Uno and how would my sketch look like? I suppose I need something with 'lifeline'?

Edit: I think I need something similar to this: https://z-uno.z-wave.me/examples/SimpleSensor/

Edit2: If im correct I can just connect the wallswitches like this: https://ctheds.wordpress.com/2007/10/30 ... e-arduino/

Edit3: With the internal pullup as used by the SimpleSensor example I do not need to pull the input pin up or down, I can just leave it "floating" (internal pullup) and connect it with the switch to ground. That seems like a good option, I will just have to test if it works with long wire lengths.
hschokker
Posts: 4
Joined: 10 May 2018 11:25

Re: Z-Uno to readout wallswitches and send state to domoticz

Post by hschokker »

First hurdle, it seems z-uno only has 10 channels I can use.
https://z-uno.z-wave.me/Reference/ZUNO_SETUP_CHANNELS/

This would mean I need either 2 z-uno for all of my ~18 wallswitches or I need to use 1 channel to 'activate' a button press and 1 or multiple other channels to send the state of all switches. Any ideas on this?

I think I have a working sketch for the 10 wallswitches now. Will test this in my home this weekend. Is it possible to connect 2 z-uno's to 1 controller and use 2 x 10 channels that way?
hschokker
Posts: 4
Joined: 10 May 2018 11:25

Re: Z-Uno to readout wallswitches and send state to domoticz

Post by hschokker »

Ok, I will try that.

Which pins are used for programming sketches? Is that on TX 0 / RX 0 (pins 24/25) or are pins 7 and 8 (TX 1 / RX 1) also used?
hschokker
Posts: 4
Joined: 10 May 2018 11:25

Re: Z-Uno to readout wallswitches and send state to domoticz

Post by hschokker »

It kinda works with the large (ugly and inefficient) code below:

Code: Select all

// LED pin number
// 13 pin - user LED of Z-Uno board
#define LED_PIN     13

// Define buttons
// 18 pin - button(BTN) of Z-Uno board
#define BTN1_PIN     9
#define BTN2_PIN     10
#define BTN3_PIN     11
#define BTN4_PIN     12
#define BTN5_PIN     14
#define BTN6_PIN     15
#define BTN7_PIN     16
#define BTN8_PIN     19
#define BTN9_PIN     20
#define BTN10_PIN    21

//extra button pins
#define BTN11_PIN    17
#define BTN12_PIN    0
#define BTN13_PIN    1
#define BTN14_PIN    2
#define BTN15_PIN    3
#define BTN16_PIN    4
#define BTN17_PIN    5
#define BTN18_PIN    6
#define BTN19_PIN    7
#define BTN20_PIN    8

// channel numbers
#define ZUNO_CHANNEL_NUMBER_ONE     1
#define ZUNO_CHANNEL_NUMBER_TWO     2
#define ZUNO_CHANNEL_NUMBER_THREE   3
#define ZUNO_CHANNEL_NUMBER_FOUR    4
#define ZUNO_CHANNEL_NUMBER_FIVE    5
#define ZUNO_CHANNEL_NUMBER_SIX     6
#define ZUNO_CHANNEL_NUMBER_SEVEN   7
#define ZUNO_CHANNEL_NUMBER_EIGHT   8
#define ZUNO_CHANNEL_NUMBER_NINE    9
#define ZUNO_CHANNEL_NUMBER_TEN     10

// extra channel numbers
#define ZUNO_CHANNEL_NUMBER_ELEVEN     11
#define ZUNO_CHANNEL_NUMBER_TWELVE     12
#define ZUNO_CHANNEL_NUMBER_THIRTEEN   13
#define ZUNO_CHANNEL_NUMBER_FOURTEEN   14
#define ZUNO_CHANNEL_NUMBER_FIFTEEN    15
#define ZUNO_CHANNEL_NUMBER_SIXTEEN    16
#define ZUNO_CHANNEL_NUMBER_SEVENTEEN  17
#define ZUNO_CHANNEL_NUMBER_EIGHTEEN   18
#define ZUNO_CHANNEL_NUMBER_NINETEEN   19
#define ZUNO_CHANNEL_NUMBER_TWENTY     20

// variables to store current button states
byte lastButton1State;
byte lastButton2State;
byte lastButton3State;
byte lastButton4State;
byte lastButton5State;
byte lastButton6State;
byte lastButton7State;
byte lastButton8State;
byte lastButton9State;
byte lastButton10State;

byte lastButton11State;
byte lastButton12State;
byte lastButton13State;
byte lastButton14State;
byte lastButton15State;
byte lastButton16State;
byte lastButton17State;
byte lastButton18State;
byte lastButton19State;
byte lastButton20State;

ZUNO_SETUP_CHANNELS(ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter1),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter2),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter3),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter4),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter5),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter6),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter7),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter8),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter9),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter10),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter11),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter12),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter13),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter14),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter15),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter16),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter17),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter18),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter19),
                    ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE, getter20)
                   );

// the setup routine runs once when you press reset:
void setup() {
  pinMode(LED_PIN, OUTPUT); // set LED pin as output
  pinMode(BTN1_PIN, INPUT_PULLUP);
  pinMode(BTN2_PIN, INPUT_PULLUP);
  pinMode(BTN3_PIN, INPUT_PULLUP);
  pinMode(BTN4_PIN, INPUT_PULLUP);
  pinMode(BTN5_PIN, INPUT_PULLUP);
  pinMode(BTN6_PIN, INPUT_PULLUP);
  pinMode(BTN7_PIN, INPUT_PULLUP);
  pinMode(BTN8_PIN, INPUT_PULLUP);
  pinMode(BTN9_PIN, INPUT_PULLUP);
  pinMode(BTN10_PIN, INPUT_PULLUP);

  pinMode(BTN11_PIN, INPUT_PULLUP);
  pinMode(BTN12_PIN, INPUT_PULLUP);
  pinMode(BTN13_PIN, INPUT_PULLUP);
  pinMode(BTN14_PIN, INPUT_PULLUP);
  pinMode(BTN15_PIN, INPUT_PULLUP);
  pinMode(BTN16_PIN, INPUT_PULLUP);
  pinMode(BTN17_PIN, INPUT_PULLUP);
  pinMode(BTN18_PIN, INPUT_PULLUP);
  pinMode(BTN19_PIN, INPUT_PULLUP);
  pinMode(BTN20_PIN, INPUT_PULLUP);
}

// the loop routine runs over and over again forever:
void loop() {
  // sample current button state
  byte currenButton1State = digitalRead(BTN1_PIN);
  byte currenButton2State = digitalRead(BTN2_PIN);
  byte currenButton3State = digitalRead(BTN3_PIN);
  byte currenButton4State = digitalRead(BTN4_PIN);
  byte currenButton5State = digitalRead(BTN5_PIN);
  byte currenButton6State = digitalRead(BTN6_PIN);
  byte currenButton7State = digitalRead(BTN7_PIN);
  byte currenButton8State = digitalRead(BTN8_PIN);
  byte currenButton9State = digitalRead(BTN9_PIN);
  byte currenButton10State = digitalRead(BTN10_PIN);

  byte currenButton11State = digitalRead(BTN11_PIN);
  byte currenButton12State = digitalRead(BTN12_PIN);
  byte currenButton13State = digitalRead(BTN13_PIN);
  byte currenButton14State = digitalRead(BTN14_PIN);
  byte currenButton15State = digitalRead(BTN15_PIN);
  byte currenButton16State = digitalRead(BTN16_PIN);
  byte currenButton17State = digitalRead(BTN17_PIN);
  byte currenButton18State = digitalRead(BTN18_PIN);
  byte currenButton19State = digitalRead(BTN19_PIN);
  byte currenButton20State = digitalRead(BTN20_PIN);

  if (currenButton1State != lastButton1State) { // if state changes
    lastButton1State = currenButton1State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_ONE); //Send report and blink LED
  }

  if (currenButton2State != lastButton2State) { // if state changes
    lastButton2State = currenButton2State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_TWO); //Send report and blink LED
  }

  if (currenButton3State != lastButton3State) { // if state changes
    lastButton3State = currenButton3State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_THREE); //Send report and blink LED
  }

  if (currenButton4State != lastButton4State) { // if state changes
    lastButton4State = currenButton4State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_FOUR); //Send report and blink LED
  }

  if (currenButton5State != lastButton5State) { // if state changes
    lastButton5State = currenButton5State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_FIVE); //Send report and blink LED
  }

  if (currenButton6State != lastButton6State) { // if state changes
    lastButton6State = currenButton6State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_SIX); //Send report and blink LED
  }

  if (currenButton7State != lastButton7State) { // if state changes
    lastButton7State = currenButton7State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_SEVEN); //Send report and blink LED
  }

  if (currenButton8State != lastButton8State) { // if state changes
    lastButton8State = currenButton8State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_EIGHT); //Send report and blink LED
  }

  if (currenButton9State != lastButton9State) { // if state changes
    lastButton9State = currenButton9State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_NINE); //Send report and blink LED
  }

  if (currenButton10State != lastButton10State) { // if state changes
    lastButton10State = currenButton10State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_TEN); //Send report and blink LED
  }

  //extra channels:

  if (currenButton11State != lastButton11State) { // if state changes
    lastButton11State = currenButton11State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_ELEVEN); //Send report and blink LED
  }

  if (currenButton12State != lastButton12State) { // if state changes
    lastButton12State = currenButton12State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_TWELVE); //Send report and blink LED
  }

  if (currenButton13State != lastButton13State) { // if state changes
    lastButton13State = currenButton13State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_THIRTEEN); //Send report and blink LED
  }

  if (currenButton14State != lastButton14State) { // if state changes
    lastButton14State = currenButton14State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_FOURTEEN); //Send report and blink LED
  }

  if (currenButton15State != lastButton15State) { // if state changes
    lastButton15State = currenButton15State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_FIFTEEN); //Send report and blink LED
  }

  if (currenButton16State != lastButton16State) { // if state changes
    lastButton16State = currenButton16State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_SIXTEEN); //Send report and blink LED
  }

  if (currenButton17State != lastButton17State) { // if state changes
    lastButton17State = currenButton17State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_SEVENTEEN); //Send report and blink LED
  }

  if (currenButton18State != lastButton18State) { // if state changes
    lastButton18State = currenButton18State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_EIGHTEEN); //Send report and blink LED
  }

  if (currenButton19State != lastButton19State) { // if state changes
    lastButton19State = currenButton19State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_NINETEEN); //Send report and blink LED
  }

  if (currenButton20State != lastButton20State) { // if state changes
    lastButton20State = currenButton20State; // save new state
    SendButtonReport(ZUNO_CHANNEL_NUMBER_TWENTY); //Send report and blink LED
  }

}

void SendButtonReport(byte ZunoChannel)
{
  digitalWrite(LED_PIN, HIGH);  // turn the LED on
  zunoSendReport(ZunoChannel); // send report over the Z-Wave to the controller
  delay(100);
  digitalWrite(LED_PIN, LOW);   // turn the LED off
}

byte getter1() {
  if (lastButton1State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter2() {
  if (lastButton2State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter3() {
  if (lastButton3State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter4() {
  if (lastButton4State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter5() {
  if (lastButton5State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter6() {
  if (lastButton6State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter7() {
  if (lastButton7State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter8() {
  if (lastButton8State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter9() {
  if (lastButton9State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter10() {
  if (lastButton10State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

// New channels:

byte getter11() {
  if (lastButton11State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter12() {
  if (lastButton12State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter13() {
  if (lastButton13State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter14() {
  if (lastButton14State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter15() {
  if (lastButton15State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter16() {
  if (lastButton16State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter17() {
  if (lastButton17State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter18() {
  if (lastButton18State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter19() {
  if (lastButton19State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}

byte getter20() {
  if (lastButton20State == 0) { // if button is pressed
    return 0xff;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return 0;                 // return "Idle" state to the controller
  }
}
At least the light blinks, I still have to test it connected to my zwave controller. The problem is that many pins such as pins 8, 19, 20, 21 take a long time to respond after I remove the ground connected to that pin. When I connect the ground to those pins the light immediately blinks but when I remove this gnd connection it takes a second or 2 for the light to blink again. With other pins suchs as 9,10,11 etc. it works very speedy.

Anybody knows what is causing this behaviour? The SPI and fast pins are fast and all the other pins are slow.

EDIT: And to keep answering my own questions, a 10k external pullup did the job. I will use that on the pins that are slow.
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Z-Uno to readout wallswitches and send state to domoticz

Post by PoltoS »

Just in case you want to optimize your code for size (in ccase you want to use S2 in future), please have a look on dynamic channels - many channels of the same type can share the getter and setter.

http://z-uno.z-wave.me/examples/multipl ... e-sensors/
Post Reply