NodeManager Motion Sensor: how to use the SensorSwitch class?



  • Hi,

    When using the Node Manager for a Motion Sensor, I read the default interrupt mode is "CHANGE" (see that in NodeManager.h).
    I would like it to replace with "RISING" and I can see it could be in the SensorSwitch class, I just don't know how to code this.
    I took the NodeManager "Motion Sensor" example and tried to add "SensorSwitch.setMode(1);" in the before() routine but I'll get a compilation error of course as I didn't code that the right way.
    Can someone please teach me how to make it right?
    This is my code so far:

    /*
      NodeManager is intended to take care on your behalf of all those common tasks a MySensors node has to accomplish, speeding up the development cycle of your projects.
    
      NodeManager includes the following main components:
      - Sleep manager: allows managing automatically the complexity behind battery-powered sensors spending most of their time sleeping
      - Power manager: allows powering on your sensors only while the node is awake
      - Battery manager: provides common functionalities to read and report the battery level
      - Remote configuration: allows configuring remotely the node without the need to have physical access to it
      - Built-in personalities: for the most common sensors, provide embedded code so to allow their configuration with a single line
    
      Documentation available on: https://github.com/mysensors/NodeManager
    */
    
    
    // load user settings
    #include "config.h"
    // load MySensors library
    #include <MySensors.h>
    // load NodeManager library
    #include "NodeManager.h"
    
    // create a NodeManager instance
    NodeManager nodeManager;
    
    // before
    void before() {
      // setup the serial port baud rate
      Serial.begin(MY_BAUD_RATE);
      /*
         Register below your sensors
      */
      SensorSwitch.setMode(1);
    
      nodeManager.setBatteryMin(1.8);
      nodeManager.setBatteryMax(5.2);
      nodeManager.setSleep(SLEEP, 1, MINUTES);
      nodeManager.registerSensor(SENSOR_MOTION, 3);
    
      /*
         Register above your sensors
      */
      nodeManager.before();
    }
    
    // presentation
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      // call NodeManager presentation routine
      nodeManager.presentation();
    
    }
    
    // setup
    void setup() {
      // call NodeManager setup routine
      nodeManager.setup();
    }
    
    // loop
    void loop() {
      // call NodeManager loop routine
      nodeManager.loop();
    
    }
    
    // receive
    void receive(const MyMessage &message) {
      // call NodeManager receive routine
      nodeManager.receive(message);
    }```

  • Mod

    You don't want to report when pir sensors resets to "no motion"?



  • I'm not sure about that, what I really want is to learn how to use NodeManager.
    Like how can I make such changes as described.
    I want to experiment a bit and don't know how :simple_smile:


  • Mod

    Node manager is made to automate the most common tasks and the pir sensor normally needs to report both status changes, so if you need to experiment you'd probably need to write some code yourself ☺



  • You're absolutely right :simple_smile:
    I am experimenting with mysensors as much as I can. My motion sensor works fine without NodeManager ... but I'm interested in NodeManager too.
    Back to my question however, how can solve my issue?


  • Mod

    Only author can answer that 😀



  • That fine ... I just don't know who's the author, I guess I have to solve this issue first 😄 , or I get lucky and he'll read my post!


  • Mod

    @user2684 is the guy


  • Contest Winner

    Sorry for the delay guys, for some reasons I haven't got the notification of this thread, thanks @gohan for pulling me in! When using SENSOR_MOTION, the default mode is already RISING. To help you reading through the code, the registerSensor() in NodeManager.cpp would create an instance of the SensorMotion class. SensorMotion is derived from SensorSwitch (which has CHANGE has default mode) but in its constructor (SensorMotion::SensorMotion) you will find a setMode(RISING) which is then the default value.

    Whatever parameter you want to change, this requires three steps:

    • registering the sensor
    • retrieving the instance of the class
    • invoking its functions

    SensorSwitch in your example above is a class, you need the instantiated object to invoke its member functions. So something like the following:

    // register the sensor and keep track of its id
    int sensorPIR = nodeManager.registerSensor(SENSOR_MOTION,3);
    // retrieve the instance of the object. Since getSensor() will return a generic Sensor class, you need to cast it accordingly
    SensorMotion* sensorMotion = (SensorMotion*)nodeManager.getSensor(sensorPIR_Id);
    // invoke whatever function you need. Since sensorMotion is a pointer, you need to use the -> notation
    sensorMotion->setMode(CHANGE);
    sensorMotion->setInitial(LOW);
    

    Does it make sense now?
    Thanks



  • Thank you very much sir, it start making sense to me, I still need to "digest" this 😄 as I am a HW engineer ... but trying to improve my SW skills.
    I am comfortable with the basics of C and Arduino but not really with object oriented programming.
    What literature would you suggest me to start with to understand this kind of coding? Java perhaps?


  • Mod

    @iahim67 c++? 😁



  • I thought you may say that 😄 ... is there a good C++ book for beginners that you would recommend?
    On paper I mean, that I can buy?


  • Mod

    I could sell you mine, it is still brand new but it is in Italian 😁


  • Contest Winner

    Agree starting from c++ would make more sense 🙂 Just a silly personal advise: pay attention not to get lost into the tons of capabilities c++ can provide, especially if you want to be just focused on arduino to start with since you can easily get lost. Sometimes I personally prefer just an electronic one-pager with a good amount of examples 🙂



  • @iahim67 Simon Monk's Programming Arduino: Getting Started... book is pretty good.

    Look at the table of contents.

    https://www.amazon.com/Programming-Arduino-Getting-Started-Sketches/dp/1259641635/



  • Thanks for your help, I'll start with Simon Monk's Programming Arduino 😄 !



  • This post is deleted!

Log in to reply
 

Suggested Topics

  • 2
  • 4
  • 1
  • 2
  • 2
  • 2

66
Online

11.4k
Users

11.1k
Topics

112.7k
Posts