WebThingsIO/example-adapter

how send new value

Closed this issue · 3 comments

Hello,
I will try to create an adapter for DHT22 (AM2302).
I use this library to read data from the DHT sensor. => node-dht-sensor

var sensor = require('node-dht-sensor');
sensor.read(22, 4, function(err, temperature, humidity) {
if (!err) {
console.log('temp: ' + temperature.toFixed(1) + '°C, ' +
'humidity: ' + humidity.toFixed(1) + '%'
);
}
});
The temperature.toFixed variable (1) contains the temperature number.

Now I downloaded example-adapter code. But I do not know where to put the temperature.toFixed (1) into the example-adapter code.

Will you please help me?

You'll probably want to set up an interval with setInterval() in your Device or Property class (whichever makes the most sense to you) to read the temperature value periodically. After reading, you can do something like this:

// If inside Device class:
const value = temperature.toFixed(1);
temperatureProperty.setCachedValue(value);
this.notifyPropertyChanged(temperatureProperty);

// If inside Property class:
const value = temperature.toFixed(1);
this.setCachedValue(value);
this.device.notifyPropertyChanged(this);

If you haven't already read it, this blog post is an excellent reference as well.

Hello,

thank you very much for your help. Your advice helped me a lot. It works. thanks