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.