Best practice for sending data from sensors.....?



  • Just wondering if this is the best way to send data within mys node.
    Currently this node has 2 sensors, both with temp and hum. This is how I am using it.....

    #define CHILD_ID_FGEHUM 0
    #define CHILD_ID_FGETEMP 2
    #define CHILD_ID_FZRHUM 1
    #define CHILD_ID_FZRTEMP 3
    
    
    MyMessage msgFgeHum (CHILD_ID_FGEHUM,  V_HUM);
    MyMessage msgFgeTemp(CHILD_ID_FGETEMP, V_TEMP);
    MyMessage msgFzrHum (CHILD_ID_FZRHUM,  V_HUM);
    MyMessage msgFzrTemp(CHILD_ID_FZRTEMP, V_TEMP);
    
    
    void presentation()  
    { 
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_FGEHUM, S_HUM, "Fridge Hum");
      present(CHILD_ID_FZRHUM, S_HUM, "Freezer Hum");
      present(CHILD_ID_FGETEMP, S_TEMP, "Fridge Temp");
      present(CHILD_ID_FZRTEMP, S_TEMP, "Freezer Temp");
    }
    
     
    
    void loop(){
      // distracting stuff removed !!!
        send(msgFgeHum.set(fgehum));
        send(msgFzrHum.set(fzrhum));
        send(msgFgeTemp.set(fgetemp, 1));
        send(msgFzrTemp.set(fzrtemp, 1));
        }
    

    I wonder if this is considered the 'normal' or 'best' way to do this or if it can be made simpler?


  • Mod

    @skywatch yes this is the normal way



  • @mfalkvidd

    Thanks, I was getting in a bit of a muddle as I am only sending 2 types of data (temp and hum) and wondered if could I save memory by doing it differently? Perhaps not! 🙂


  • Mod

    @skywatch yes you probably could. But why?


  • Mod

    @skywatch You could remove the MyMessage declarations at the top and rewrite the loop() to:

    void loop(){
      send( MyMessage(CHILD_ID_FGEHUM,  V_HUM ).set(fgehum) );
      send( MyMessage(CHILD_ID_FGETEMP, V_TEMP).set(fzrhum) );
      send( MyMessage(CHILD_ID_FZRHUM,  V_HUM ).set(fgetemp, 1) );
      send( MyMessage(CHILD_ID_FZRTEMP, V_TEMP).set(fzrtemp, 1) );
    }
    

    This frees RAM as the mymessage structures are declared temporarily on the stack for each invocation of send().



  • @yveaux

    nice tip; I am still getting into the proper mindset for memory conservation.

    To be clear, do you mean the following?

    void loop(){
      // for example
      float fgehum = myHumidity.readHumidity()
      send( MyMessage(CHILD_ID_FGEHUM,  V_HUM ).set(fgehum) );
    ...
    }
    

    Also, @mfalkvidd, why would you prefer to declare a MyMessage object instead of on the fly? I guess that there's overhead for creation of the object anew everytime. So there would be powersavings due to having a single and MyMessage object and then reusing it. Am I in the ballpark?


  • Mod

    @oneyb said in Best practice for sending data from sensors.....?:

    To be clear, do you mean the following?

    Yes!

    Also, @mfalkvidd, why would you prefer to declare a MyMessage object instead of on the fly? I guess that there's overhead for creation of the object anew everytime. So there would be powersavings due to having a single and MyMessage object and then reusing it. Am I in the ballpark?

    Yes, you could save some cpu power (and energy) by preallocating and configuring the mymessage instances. It all depends on your requirements 😉


  • Mod

    @yveaux said in Best practice for sending data from sensors.....?:

    Yes, you could save some cpu power (and energy) by preallocating and configuring the mymessage instances. It all depends on your requirements 😉

    This is my reason for asking "why". There are a lot of trade-offs. Cpu/energy usage, ram usage, flash usage, code readability, code maintainability...



  • @mfalkvidd I would think ‘best practice’ for a temperature (and humidity) sensor is to “send only if changed”.
    I have many Dallas 1-wire temperature sensors on one Arduino and I have an array to store last value sent
    I also sleep for a second after a polling loop. And every minute I send the temperatures regardless of whether they have changed or not.
    I have not done this but for truly best practice I would add ‘anti-flapping’ logic to prevent constant updates when a thermometer is reading ‘between’ two values.



  • Domoticz have a sensor timout, and makes the icon red if the timeout appears, so regardless it's required to occasionally (1 or 2 times per day) to send a "heartbeat"/alive message. I know this timeout in Domoticz can be changed, but it's a nice feature.



  • Thanks for some interesting info!

    The reason is 1. To save memory for adding more sensors, signing etc and 2. To save battery energy on battery nodes. So there is likely more than one solution to this problem.

    Also, I have used 'for' loops as well and wonder how this affects memory and battery drain?

    Thanks


Log in to reply
 

Suggested Topics

  • 4
  • 15
  • 2
  • 4
  • 3
  • 2

13
Online

11.4k
Users

11.1k
Topics

112.7k
Posts