Reading INPUTS from arduino in domoticz

  • Hello,

    Can you help me with reading inputs (like door, window open)?

    I try to use mega2560 to read inputs.

    With this code, i see in domoticz input1 and input2 always on...
    Do you know what can be wrong?
    My sketch:

    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    // Enable and select radio type attached
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    // Set LOW transmit power level as default, if you have an amplified NRF-module and
    // power your radio separately with a good regulator you can turn up PA level. 
    //#define MY_RF24_PA_LEVEL RF24_PA_LOW
    // Enable serial gateway
    // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
    #if F_CPU == 12000000L
    #define MY_BAUD_RATE 115200
    // Flash leds on rx/tx/err
    // Set blinking period
    // #define MY_DEFAULT_LED_BLINK_PERIOD 300
    // Inverses the behavior of leds
    // Enable inclusion mode
    // Enable Inclusion mode button on gateway
    // Inverses behavior of inclusion button (if using external pullup)
    // Set inclusion mode duration (in seconds)
    // Digital pin used for inclusion mode button
    // Uncomment to override default HW configurations
    //#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED
    #include <SPI.h>
    #include <MySensors.h>  
    #include <Bounce2.h>
    // Enable repeater functionality for this node
    #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_2  5
    #define RELAY_3  6
    #define RELAY_4  7
    #define RELAY_5  8
    #define RELAY_6  9
    #define RELAY_7  10
    #define RELAY_8  11
    #define RELAY_9  12
    #define RELAY_10  13
    #define RELAY_11  14
    #define RELAY_12  15
    #define NUMBER_OF_RELAYS  12 // Total number of attached relays                             dodać na próbę wejścia
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    #define CHILD_ID_1 20
    #define CHILD_ID_2 21
    #define INPUT_1  20
    #define INPUT_2  21
    Bounce debouncer1 = Bounce(); 
    Bounce debouncer2 = Bounce(); 
    int oldValue1 = -1;
    int oldValue2 = -1;
    void before() { 
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
    void setup()  {
    void presentation()  
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay", "1.0");
     present(CHILD_ID_1, S_DOOR);
     present(CHILD_ID_2, S_DOOR);
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        present(sensor, S_LIGHT);
    MyMessage msg(1, V_LIGHT);
    MyMessage msg2(2, V_LIGHT);
    MyMessage msg3(3, V_LIGHT);
    MyMessage msg4(4, V_LIGHT);
    MyMessage msg5(5, V_LIGHT);
    MyMessage msg6(6, V_LIGHT);
    MyMessage msg7(7, V_LIGHT);
    MyMessage msg8(8, V_LIGHT);
    MyMessage msg9(9, V_LIGHT);
    MyMessage msg10(10, V_LIGHT);
    MyMessage msg11(11, V_LIGHT);
    MyMessage msg12(12, V_LIGHT);
    MyMessage msg13(CHILD_ID_1, V_TRIPPED);
    MyMessage msg14(CHILD_ID_2, V_TRIPPED);
    void loop() {
      int value1 =;
      if (value1 != oldValue1) {
         send(msg13.set(value1 == HIGH ? 1 : 0));
         oldValue1 = value1;
      int value2 =;
      if (value2 != oldValue2) {
         send(msg14.set(value2 == HIGH ? 1 : 0));
         oldValue2 = value2;
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(", New status: ");

  • Hi @Mati_Smart,
    Why are you using the relay actuator sketch if you want to read inputs?

    First rule in debugging things, is to narrow down the problem.
    Make your sketch to contain only the things you need.
    Verify if your sketch is meant to work with the hardware you are using.
    If the sketch is written originally for a Arduino Uno and you are using it on a Arduino Mega 2560, there is a chance that the used port numbers are not correct.
    For example I see that on a mega, the D20 and D21 pins are also pins used for SDA and SCL. Try to use first the normal digital pins, like D2, D3, ...

  • Thank you for your answer.

    I wan to use this relay outputs and this outputs works good.
    So, I can't use pins D20 and D21?
    I want to use about 20-25 digital inputs but for testing i use this two.
    I write this sketch on the faith of examples "digital inputs gateway"

  • Hi @Mati_Smart, I didn't say that you can't use the pins D20 and D21.
    But for now they are not working in your current setup, so try first to modify your sketch to include only the code necessary to use digital pins as input. Nothing more, nothing less.

    Do you have experience with arduino hardware and arduino programming?
    There are plenty of arduino sketches out there in the wild, but without knowledge how they work, makes it very difficult to find solutions if they don't work on your hardware setup.

    For example you can try first to understand this very simple mysensors sketch :
    Even simpler, try an arduino sketch without mysensors code
    Try to understand the basics of Arduino programming and hardware before getting to more complicated hardware and sketches.

  • @Mati_Smart

    What troubleshooting steps have you all ready taken?

    I have loaded your sketch onto a mega2560 and looked at the serial output.
    the binary switch child's 20 and 21 are switching.


    have you checked this?

    also why do you have all MY_INCLUSION defines active when there is no radio attached?

    // Enable inclusion mode
    // Enable Inclusion mode button on gateway
    // Inverses behavior of inclusion button (if using external pullup)
    // Set inclusion mode duration (in seconds)
    // Digital pin used for inclusion mode button

    and you have the repeater function enabled

    // Enable repeater functionality for this node

    this is sloppy programming and can cause problems.

    Have you checked the basic things like the connections to the inputs?
    You have the internal PULLUP enabled so the input signal should switch to ground.

  • @hard-shovel

    I think, my problem is in MyMessage function.
    It looks fine, but it not working with domoticz.

    In "hardware'' - "MySensorsUSBGateway" - "Setup" - "Nodes" I can see taht my child ID 20 and 21 are switching
    But in "devices" tab both are always on

    I tried to do like this simple example:
    I moved "MyMessage" from "void presentation" and put in before "void before"
    It did't help.

    Of course this sketch: without my relays it works fine.

    @hard-shovel thanks for the suggestion about My Inclusion and my repeater - I'm not using this, I have to disable it.

    @evb I understand the basics (only basisc) of Arduino programming and hardware.
    I have 10 years of expirience in PLC programming and a little experience in "C" language.

  • @Mati_Smart, if the messages from the sensor are arriving in domoticz, it must be some configuration in domoticz itself.
    Can't help you here, I don't know domoticz.
    Did you search already on the domoticz forum?

Log in to reply

Suggested Topics

  • 7
  • 1
  • 4
  • 1
  • 19
  • 20
  • 6
  • 4