getConfig().isMetric not updated correctly



  • Hey,

    I just started to create a MySensors controller, and I face a problem relating the metric/imperial config, using this sketch:

    #include <SPI.h>
    #include <EEPROM.h>
    #include <MySensor.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define TEMPERATURE_ID 1
    
    #define ONE_WIRE_BUS 4
    #define TEMPERATURE_INTERVAL 120
    
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature temperatureSensors(&oneWire);
    MySensor gw;
    MyMessage msgTemperature(TEMPERATURE_ID, V_TEMP);
    
    unsigned long time_temperature_sent;
    
    void setup() {
      temperatureSensors.begin();
      gw.begin(handleMessage);
      gw.sendSketchInfo("Temperature", "0.1");
      gw.present(TEMPERATURE_ID, S_TEMP, "Room temperature");
    }
    
    void loop() {
      gw.process();
      if (millis() - time_temperature_sent >= TEMPERATURE_INTERVAL * 1000UL) {
        temperatureSensors.requestTemperaturesByIndex(0); 
        float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?temperatureSensors.getTempCByIndex(0):temperatureSensors.getTempFByIndex(0)) * 10.)) / 10.;
        gw.send(msgTemperature.set(temperature, 1));
        time_temperature_sent = millis();
      }
    }
    
    void handleMessage(const MyMessage& receivedMessage) {
    }
    

    Once the controller receives the I_CONFIG message, it replies with a M to indicate we want a value in Celcius. I am an hundred percent sure the reply is received by the sensor.
    The first value is sent after two minutes, so the sensor has received and processed the I_CONFIG before sending out its first value.

    But the fact is the first value sent is in Fahrenheit, and then all subsequent values sent are in Celcius. It's like if the isMetric is not updated, even after having received the config from the controller, until the first send. This happens after each reboot of the sensor.
    Maybe I am missing something?

    Thanks for your answers.



  • i think you need add ||| metric = gw.getConfig().isMetric; ||| in void setup and declare ||| boolean metric = true; |||



  • No, it would be a workaround, not a real solution.
    I know what happens... It's 30°C where I am, which is about 85°F. Well, the first value I received was 85.0, a value that I thought was the Fahrenheit one, but it is not. It appears that +85.0°C is the power-on reset value of the temperature register... which is ignored in the official temp sketch since 17 June, I didn't see.

    So there is no problem finally!


  • Hero Member

    @Marvin-Roger One suggestion, I would move the gw.getConfig() request into the setup function, rather than in the loop. Unless you are frequently changing whether you want results in F or C, requesting it once and storing it in a variable should suffice.

    Cheers
    Al


  • Admin

    Calling getConfig() only returns a cached copy of the config requested by node at startup (or from eeprom if the request failed).

    Requested here:
    https://github.com/mysensors/Arduino/blob/master/libraries/MySensors/MySensor.cpp#L279

    And reply is handled here:
    https://github.com/mysensors/Arduino/blob/master/libraries/MySensors/MySensor.cpp#L697

    So the node is always handling incoming CONFIG updates (when awake).
    So either the controller must send out a new config to the nodes when updated or the node must be restarted to send a new request.

    Currently there aren't any API function exposed to re-request this during runtime. I can't imagine many switches between Metric/Imperial frequently.


  • Hero Member

    @hek Thanks for clarifying!

    Cheers
    Al



  • Great, and as I see it waits two seconds waiting for the configuration reply. So when you enter in the loop, you're quite sure everything is set! Thanks.


Log in to reply
 

Suggested Topics

  • 33
  • 9
  • 3
  • 2
  • 11
  • 17

53
Online

11.4k
Users

11.1k
Topics

112.7k
Posts