dawidchyrzynski / arduino-home-assistant

ArduinoHA allows to integrate an Arduino/ESP based device with Home Assistant using MQTT.
https://dawidchyrzynski.github.io/arduino-home-assistant/
GNU Affero General Public License v3.0
508 stars 119 forks source link

Temperature sensor #84

Closed jbcorreia72 closed 2 years ago

jbcorreia72 commented 2 years ago

I am trying to go through the Temperature Sensor example, and a couple of questions came to my mind. I am sorry if these questions seem very basic or even folish, but I am not a professional programmer. Just trying to figure out the library in order to integrate an ESP32 located in a different WiFi network than the one where I have HA running.

1.) I was expecting to read the values of a temperature sensor in one of the Analog inputs of Arduino and publish those values using mqtt in HA broker (I am using Mosquitto). Instead, I notice you are publishing the following values:

    lastValue = lastValue + 0.5;
    temp.setValue(lastValue);

Shouldn't 'lastValue' be based on the reading of the temperature sensor in one of the analogue inputs of the arduino?

2.) You have a callback function 'onBeforeSwitchStateChanged()' that you mention to be called before publishing the new value of the sensor. Going through the source code, I find that the constructor used in the example does not have a callback function defined:

HAMqtt::HAMqtt(Client& netClient, HADevice& device) :
    _netClient(netClient),
    _device(device),
    _messageCallback(nullptr),
    _connectedCallback(nullptr),
    _connectionFailedCallback(nullptr),
    _initialized(false),
    _discoveryPrefix(DefaultDiscoveryPrefix),
    _dataPrefix(nullptr),
    _mqtt(new PubSubClient(netClient)),
    _username(nullptr),
    _password(nullptr),
    _lastConnectionAttemptAt(0),
    _devicesTypesNb(0),
    _devicesTypes(nullptr),
    _lastWillTopic(nullptr),
    _lastWillMessage(nullptr),
    _lastWillRetain(false)
{
    _instance = this;
}

Is this callback function used in this example?

Thanks for any help you can provide regarding these topics.

jbcorreia72 commented 2 years ago

Ok, I got it.