WebThingsIO / example-adapter

Example adapter add-on for WebThings Gateway
Mozilla Public License 2.0
34 stars 20 forks source link

how send new value #12

Closed rool80 closed 6 years ago

rool80 commented 6 years ago

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?

mrstegeman commented 6 years ago

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.

rool80 commented 6 years ago

Hello,

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

rool80 commented 6 years ago

Hello,

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

st 17. 10. 2018 o 13:40 Michael Stegeman notifications@github.com napísal(a):

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 https://hacks.mozilla.org/2018/02/making-a-clap-sensing-web-thing/ is an excellent reference as well.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mozilla-iot/example-adapter/issues/12#issuecomment-430595080, or mute the thread https://github.com/notifications/unsubscribe-auth/ARtTy4fA0vxk0zNVUP2s5PDkz1vwYHkbks5ulxdHgaJpZM4XjOnZ .