Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Binary_switch_sleep_sensor sketch does not work

Binary_switch_sleep_sensor sketch does not work

Scheduled Pinned Locked Moved Troubleshooting
4 Posts 2 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sergio_uno
    wrote on last edited by Sergio_uno
    #1

    Hi all,

    As a first step with MySensors I decided to a build binary switch sensor. I found that pull-up on Pin 2&3 does not work. And this is because all void setup() section newer executed (I added a marker Serial.println("Setup section reached "); )
    Needles to say that void loop() does not work either.

    Here is the sketch


    #define MY_NODE_ID 123

    // Enable debug prints to serial monitor
    #define MY_DEBUG

    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69

    #include <SPI.h>
    #include <MySensors.h>

    #define SKETCH_NAME "Binary Sensor"
    #define SKETCH_MAJOR_VER "1"
    #define SKETCH_MINOR_VER "0"

    #define PRIMARY_CHILD_ID 3
    #define SECONDARY_CHILD_ID 4

    #define PRIMARY_BUTTON_PIN 2 // Arduino Digital I/O pin for button/reed switch
    #define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch

    #if (PRIMARY_BUTTON_PIN < 2 || PRIMARY_BUTTON_PIN > 3)
    #error PRIMARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
    #endif
    #if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 3)
    #error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
    #endif
    #if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN)
    #error PRIMARY_BUTTON_PIN and BUTTON_PIN2 cannot be the same
    #endif
    #if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID)
    #error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same
    #endif

    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
    MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);

    void setup()
    {
    Serial.println("Setup section reached ");
    // Setup the buttons
    pinMode(PRIMARY_BUTTON_PIN, INPUT);
    pinMode(SECONDARY_BUTTON_PIN, INPUT);

    // Activate internal pull-ups
    digitalWrite(PRIMARY_BUTTON_PIN, HIGH);
    digitalWrite(SECONDARY_BUTTON_PIN, HIGH);
    }

    void presentation() {
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);

    // Register binary input sensor to sensor_node (they will be created as child devices)
    // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
    // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
    present(PRIMARY_CHILD_ID, S_DOOR);
    present(SECONDARY_CHILD_ID, S_DOOR);
    }

    // Loop will iterate on changes on the BUTTON_PINs
    void loop()
    {
    uint8_t value;
    static uint8_t sentValue=2;
    static uint8_t sentValue2=2;

    // Short delay to allow buttons to properly settle
    sleep(5);

    value = digitalRead(PRIMARY_BUTTON_PIN);
    Serial.print("Value pin2= ");
    Serial.println (value);

    if (value != sentValue) {
    // Value has changed from last transmission, send the updated value
    send(msg.set(value==HIGH ? 1 : 0));
    sentValue = value;
    }

    value = digitalRead(SECONDARY_BUTTON_PIN);

    if (value != sentValue2) {
    // Value has changed from last transmission, send the updated value
    send(msg2.set(value==HIGH ? 1 : 0));
    sentValue2 = value;
    }

    // Sleep until something happens with the sensor
    sleep(PRIMARY_BUTTON_PIN-2, CHANGE, SECONDARY_BUTTON_PIN-2, CHANGE, 0);
    }


    This is the copy from Serial monitor. You can see that there is no print "Setup section reached", meaning that is not reached and program stuck somewhere in the #define section

    Starting sensor (RNNNA-, 2.0.0)
    TSM:INIT
    TSM:RADIO:OK
    TSP:ASSIGNID:OK (ID=123)
    TSM:FPAR
    TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSM:FPAR
    TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSM:FPAR
    TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSM:FPAR
    TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    !TSM:FPAR:FAIL
    !TSM:FAILURE
    TSM:PDT
    TSM:INIT
    TSM:RADIO:OK
    TSP:ASSIGNID:OK (ID=123)
    TSM:FPAR
    TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSM:FPAR
    TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSM:FPAR
    TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSM:FPAR
    TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    !TSM:FPAR:FAIL
    !TSM:FAILURE
    TSM:PDT
    TSM:INIT
    TSM:RADIO:OK
    TSP:ASSIGNID:OK (ID=123)


    I have Arduino IDE 1.6.9. and updated MySensors library (2.0). The board I tested are arduino UNO and Nano, both behave the same.

    Any suggestions?

    mfalkviddM 1 Reply Last reply
    0
    • S Sergio_uno

      Hi all,

      As a first step with MySensors I decided to a build binary switch sensor. I found that pull-up on Pin 2&3 does not work. And this is because all void setup() section newer executed (I added a marker Serial.println("Setup section reached "); )
      Needles to say that void loop() does not work either.

      Here is the sketch


      #define MY_NODE_ID 123

      // Enable debug prints to serial monitor
      #define MY_DEBUG

      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69

      #include <SPI.h>
      #include <MySensors.h>

      #define SKETCH_NAME "Binary Sensor"
      #define SKETCH_MAJOR_VER "1"
      #define SKETCH_MINOR_VER "0"

      #define PRIMARY_CHILD_ID 3
      #define SECONDARY_CHILD_ID 4

      #define PRIMARY_BUTTON_PIN 2 // Arduino Digital I/O pin for button/reed switch
      #define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch

      #if (PRIMARY_BUTTON_PIN < 2 || PRIMARY_BUTTON_PIN > 3)
      #error PRIMARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
      #endif
      #if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 3)
      #error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
      #endif
      #if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN)
      #error PRIMARY_BUTTON_PIN and BUTTON_PIN2 cannot be the same
      #endif
      #if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID)
      #error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same
      #endif

      // Change to V_LIGHT if you use S_LIGHT in presentation below
      MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
      MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);

      void setup()
      {
      Serial.println("Setup section reached ");
      // Setup the buttons
      pinMode(PRIMARY_BUTTON_PIN, INPUT);
      pinMode(SECONDARY_BUTTON_PIN, INPUT);

      // Activate internal pull-ups
      digitalWrite(PRIMARY_BUTTON_PIN, HIGH);
      digitalWrite(SECONDARY_BUTTON_PIN, HIGH);
      }

      void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);

      // Register binary input sensor to sensor_node (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      present(PRIMARY_CHILD_ID, S_DOOR);
      present(SECONDARY_CHILD_ID, S_DOOR);
      }

      // Loop will iterate on changes on the BUTTON_PINs
      void loop()
      {
      uint8_t value;
      static uint8_t sentValue=2;
      static uint8_t sentValue2=2;

      // Short delay to allow buttons to properly settle
      sleep(5);

      value = digitalRead(PRIMARY_BUTTON_PIN);
      Serial.print("Value pin2= ");
      Serial.println (value);

      if (value != sentValue) {
      // Value has changed from last transmission, send the updated value
      send(msg.set(value==HIGH ? 1 : 0));
      sentValue = value;
      }

      value = digitalRead(SECONDARY_BUTTON_PIN);

      if (value != sentValue2) {
      // Value has changed from last transmission, send the updated value
      send(msg2.set(value==HIGH ? 1 : 0));
      sentValue2 = value;
      }

      // Sleep until something happens with the sensor
      sleep(PRIMARY_BUTTON_PIN-2, CHANGE, SECONDARY_BUTTON_PIN-2, CHANGE, 0);
      }


      This is the copy from Serial monitor. You can see that there is no print "Setup section reached", meaning that is not reached and program stuck somewhere in the #define section

      Starting sensor (RNNNA-, 2.0.0)
      TSM:INIT
      TSM:RADIO:OK
      TSP:ASSIGNID:OK (ID=123)
      TSM:FPAR
      TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
      TSM:FPAR
      TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
      TSM:FPAR
      TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
      TSM:FPAR
      TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
      !TSM:FPAR:FAIL
      !TSM:FAILURE
      TSM:PDT
      TSM:INIT
      TSM:RADIO:OK
      TSP:ASSIGNID:OK (ID=123)
      TSM:FPAR
      TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
      TSM:FPAR
      TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
      TSM:FPAR
      TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
      TSM:FPAR
      TSP:MSG:SEND 123-123-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
      !TSM:FPAR:FAIL
      !TSM:FAILURE
      TSM:PDT
      TSM:INIT
      TSM:RADIO:OK
      TSP:ASSIGNID:OK (ID=123)


      I have Arduino IDE 1.6.9. and updated MySensors library (2.0). The board I tested are arduino UNO and Nano, both behave the same.

      Any suggestions?

      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      @Sergio_uno the node is unable to find a way to send messages to the gateway. Do you have a gateway? If not, build one. It might be possible to use MY_PARENT_NODE_ID to test without a gateway but I don't know the details.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Sergio_uno
        wrote on last edited by
        #3

        Thanks @mfalkvidd. I don't have a gateway and home automation controller. My idea is to run a code on a gateway arduino and use MySensor libraries to transport sensors data to the GW. Will it work without a home automation controller?

        1 Reply Last reply
        0
        • mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #4

          It will work, and as long as the project just need little functionality it should be manageable. Managing a lot of functionality will be a pain though, since you won't have as nice development and debug tools available (compared to using a real controller on linux or windows machine).

          You will need to hard-code node IDs for all nodes (except the gateway which always has ID 0) though, just as you have already done with #define MY_NODE_ID 123. I mention it because a lot of people have tried to get auto ID from a gateway, but that won't work since the ID assignment is done by the controller.

          1 Reply Last reply
          0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          15

          Online

          12.0k

          Users

          11.2k

          Topics

          113.4k

          Posts


          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • MySensors
          • OpenHardware.io
          • Categories
          • Recent
          • Tags
          • Popular