"Error sending switch command, check device/hardware !"

  • hello .
    I have problem with switch command.i am using this sketch:

     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     * Version 1.0 - Henrik Ekblad
     * Example sketch for a "light switch" where you can control light or something
     * else from both HA controller and a local physical button
     * (connected between digital pin 3 and GND).
     * This node also works as a repeader for other nodes
     * http://www.mysensors.org/build/relay
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    // Enabled repeater feature for this node
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    #define RELAY_PIN1  6  // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN1  3  // Arduino Digital I/O pin number for button 
    #define CHILD_ID1 1   // Id of the sensor child
    #define RELAY_PIN2  7  // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN2  4  // Arduino Digital I/O pin number for button 
    #define CHILD_ID2 2   // Id of the sensor child
    #define RELAY_PIN3  8  // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN3  5  // Arduino Digital I/O pin number for button 
    #define CHILD_ID3 3   // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    Bounce debouncer1 = Bounce();
    int oldValue1 = 0;
    bool state1;
    Bounce debouncer2 = Bounce();
    int oldValue2 = 0;
    bool state2;
    Bounce debouncer3 = Bounce();
    int oldValue3 = 0;
    bool state3;
    MyMessage msg1(CHILD_ID1, V_LIGHT);
    MyMessage msg2(CHILD_ID2, V_LIGHT);
    MyMessage msg3(CHILD_ID3, V_LIGHT);
    void setup()
      // Setup the button
      pinMode(BUTTON_PIN1, INPUT);
      digitalWrite(BUTTON_PIN1, HIGH);
      pinMode(BUTTON_PIN2, INPUT);
      digitalWrite(BUTTON_PIN2, HIGH);
      pinMode(BUTTON_PIN3, INPUT);
      digitalWrite(BUTTON_PIN3, HIGH);
      // After setting up the button, setup debouncer
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN1, RELAY_OFF);
      pinMode(RELAY_PIN1, OUTPUT);
      digitalWrite(RELAY_PIN2, RELAY_OFF);
      pinMode(RELAY_PIN2, OUTPUT);
      digitalWrite(RELAY_PIN3, RELAY_OFF);
      pinMode(RELAY_PIN3, OUTPUT);
      // Set relay to last known state (using eeprom storage)
      state1 = loadState(CHILD_ID1);
      digitalWrite(RELAY_PIN1, state1 ? RELAY_ON : RELAY_OFF);
      state2 = loadState(CHILD_ID2);
      digitalWrite(RELAY_PIN2, state2 ? RELAY_ON : RELAY_OFF);
      state3 = loadState(CHILD_ID3);
      digitalWrite(RELAY_PIN3, state3 ? RELAY_ON : RELAY_OFF);
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay & Button", "1.0");
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID1, S_LIGHT);
      present(CHILD_ID2, S_LIGHT);
      present(CHILD_ID3, S_LIGHT);
    *  Example on how to asynchronously check for new messages from gw
    void loop()
      { debouncer1.update();
        // Get the update value
        int value1 = debouncer1.read();
        if (value1 != oldValue1 && value1 == 0) {
          send(msg1.set(state1 ? false : true), true); // Send new state and request ack back
        oldValue1 = value1;
      { debouncer2.update();
        // Get the update value
        int value2 = debouncer2.read();
        if (value2 != oldValue2 && value2 == 0) {
          send(msg2.set(state2 ? false : true), true); // Send new state and request ack back
        oldValue2 = value2;
      { debouncer3.update();
        // Get the update value
        int value3 = debouncer3.read();
        if (value3 != oldValue3 && value3 == 0) {
          send(msg3.set(state3 ? false : true), true); // Send new state and request ack back
        oldValue3 = value3;
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
        Serial.println("This is an ack from gateway");
      if (message.type == V_LIGHT) {
        switch (message.sensor) {
          case 1:
            state1 = message.getBool();
            digitalWrite(message.sensor + 2, state1 ? RELAY_ON : RELAY_OFF);
          case 2:
            state2 = message.getBool();
            digitalWrite(message.sensor + 2, state2 ? RELAY_ON : RELAY_OFF);
          case 3:
            state3 = message.getBool();
            digitalWrite(message.sensor + 2, state3 ? RELAY_ON : RELAY_OFF);
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(", New status: ");

    this is a 3 channel relay with button.
    some time commands sent and some time could not be sent and show "Error sending switch command, check device/hardware !" I did take steps in accordance site and use a 4.7uf capacitor between vcc and gnd in radio . now some time command sent . some time could not be sent and i wait for a time (1 min , 2 min and more) and again send command and this is ok ! also when for test i sent some consecutive command (on/off/on/off/on) so again i see "Error sending switch command, check device/hardware !" i wait again and after a while work again and ok !
    I did search between topics and i see some suggest to use this module:
    link text
    so i use this but the problem did not solve for me .
    also i use a repeater between node and gateway but...
    Does anyone a proposal for my problem ?

    Best Regards S_m .

  • Contest Winner

    @Reza just stop creating new accounts please.

  • @Anticimex
    when some people is living in a building so this is not all account are one person ! sama is my friend . i introduce mysensors to her . so he had problem and question about this with me and i told to her i dont know go and register a account or use my account ! i have just 2 account one account is this and another account is for when i lost my user pass (this ) after found i comeback to this account ....

  • Hardware Contributor

    looks funky but maybe..

    This is a problem you have already asked for..

    So the best you can do is:

    • remove your relays and connect leds instead
    • test this for few days.

    If there is no issues, it means that's on your hardware. And we can't scope it at distance. Can be a bad power supply input on your hardware (no filtering), parasites due to relay switch (better to use relay driver, tvs diode, capa for filtering) etc..
    I suggest you to read some docs on internet about this. That's the diy side, when using simple chinese relays modules.

  • @sama, I had similar problems, always with relay nodes. The solution that has worked 100% of the time for me is to add a delay(100) as described by @kk02067 at the end of the forum thread of the same name:
    Don't know if this will solve your problem, but worth a try.

Log in to reply

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.