Open13

obnizで温湿度

n0bisuken0bisuke

DHT20使えるか
=> DHT12は対応しているらしいけどそのままいけるか。。 とりあえずいけなかった

obnizのピン

  • 0: vcc
  • 1: gnd
  • 10: sda
  • 11: scl

で割り当て

const obniz = new Obniz("");

obniz.onconnect = async function () {

    obniz.io0.output('5v');
    obniz.io1.output(false);

    let device = obniz.wired("DHT12", { sda: 10, scl: 11 });
    let data = await device.getAllDataWait();
    console.log(data);
  
}
n0bisuken0bisuke

obniz js 3.31.0 (最新 今日アップデート?)

  • obniz board 1yの場合
'use strict';

const Obniz = require('obniz');
const obniz = new Obniz('1234-5678');
    
obniz.onconnect = async () => {
    let device = obniz.wired("DHT20",{vcc:0, sda:1, gnd:2,  scl:3});
    obniz.onloop = async () => {
        let data = await device.getAllDataWait();
        console.log(data);
    }
}

  • obniz boardの場合

voltage: "5v"を指定

'use strict';

const Obniz = require('obniz');
const obniz = new Obniz('1234-5678');
    
obniz.onconnect = async () => {
    let device = obniz.wired("DHT20",{vcc:0, sda:1, gnd:2,  scl:3 ,voltage: "5v"});
    obniz.onloop = async () => {
        let data = await device.getAllDataWait();
        console.log(data);
    }
}

取れた。。!!

n0bisuken0bisuke

Node-RED版

obnizボードで試してます。 not 1Y

obnizノード(グローバル)

obnizParts.dht20 = obniz.wired("DHT20",{vcc:0, sda:1, gnd:2,  scl:3 ,voltage: "5v"});

obniz repeatノード

msg.payload = await obnizParts.dht20.getAllDataWait();

return msg;

全体フロー

n0bisuken0bisuke

1Y + 通常ピン

  • 1Yで試したけど通常ピンを使うと5v指定がないとうまく数字が取れない
  • 初期化コードこれだとうまくいかない
obnizParts.dht20 = obniz.wired("DHT20",{vcc:0, sda:1, gnd:2,  scl:3});


やはりこっちだとうまくとれる

obnizParts.dht20 = obniz.wired("DHT20",{vcc:0, sda:1, gnd:2,  scl:3 ,voltage: "5v"});
n0bisuken0bisuke

DHT20からのダッシュボードでモニタリング

  • データの置換 => text
  • データの追加 => chat

ノード

  • chageノード

  • 全体
[ { "id": "94cad79e705b3b89", "type": "debug", "z": "714c358eb78676c9", "name": "debug 1", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 840, "y": 240, "wires": [] }, { "id": "b5dee7c3213a152b", "type": "obniz-repeat", "z": "714c358eb78676c9", "obniz": "c099b466db7d76cf", "name": "", "interval": 100, "code": "msg.payload = await obnizParts.dht20.getAllDataWait();\n\nreturn msg;", "x": 130, "y": 140, "wires": [ [ "2d119d386a3fcc68", "9f72261db942eefa", "0f2c0d683537c2d4", "aa1e748ae0b2b91e" ] ] }, { "id": "2d119d386a3fcc68", "type": "ui_text", "z": "714c358eb78676c9", "group": "05a0dc532d4e0de6", "order": 1, "width": 0, "height": 0, "name": "", "label": "温度", "format": "{{msg.payload.temperature}} °C", "layout": "row-spread", "className": "", "style": false, "font": "", "fontSize": 16, "color": "#000000", "x": 570, "y": 340, "wires": [] }, { "id": "1e75891e746ce9f6", "type": "ui_chart", "z": "714c358eb78676c9", "name": "", "group": "3a79bf34cc15cd96", "order": 2, "width": 0, "height": 0, "label": "chart - 湿度", "chartType": "line", "legend": "false", "xformat": "HH:mm:ss", "interpolate": "monotone", "nodata": "", "dot": false, "ymin": "", "ymax": "", "removeOlder": 1, "removeOlderPoints": "", "removeOlderUnit": "3600", "cutout": 0, "useOneColor": false, "useUTC": false, "colors": [ "#1f77b4", "#aec7e8", "#ff7f0e", "#2ca02c", "#98df8a", "#d62728", "#ff9896", "#9467bd", "#c5b0d5" ], "outputs": 1, "useDifferentColor": false, "className": "humidity", "x": 570, "y": 100, "wires": [ [ "94cad79e705b3b89" ] ] }, { "id": "9f72261db942eefa", "type": "change", "z": "714c358eb78676c9", "name": "湿度の値", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "payload.humidity", "tot": "msg" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 380, "y": 80, "wires": [ [ "1e75891e746ce9f6" ] ] }, { "id": "e25c6a70c101fc54", "type": "ui_chart", "z": "714c358eb78676c9", "name": "", "group": "05a0dc532d4e0de6", "order": 2, "width": 0, "height": 0, "label": "chart - 温度", "chartType": "line", "legend": "false", "xformat": "HH:mm:ss", "interpolate": "monotone", "nodata": "", "dot": false, "ymin": "", "ymax": "", "removeOlder": 1, "removeOlderPoints": "", "removeOlderUnit": "3600", "cutout": 0, "useOneColor": false, "useUTC": false, "colors": [ "#1f77b4", "#aec7e8", "#ff7f0e", "#2ca02c", "#98df8a", "#d62728", "#ff9896", "#9467bd", "#c5b0d5" ], "outputs": 1, "useDifferentColor": false, "className": "", "x": 590, "y": 260, "wires": [ [ "94cad79e705b3b89" ] ] }, { "id": "0f2c0d683537c2d4", "type": "change", "z": "714c358eb78676c9", "name": "温度の値", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "payload.temperature", "tot": "msg" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 400, "y": 200, "wires": [ [ "e25c6a70c101fc54" ] ] }, { "id": "aa1e748ae0b2b91e", "type": "ui_text", "z": "714c358eb78676c9", "group": "3a79bf34cc15cd96", "order": 0, "width": 0, "height": 0, "name": "", "label": "湿度", "format": "{{msg.payload.humidity}} %", "layout": "row-spread", "className": "humidity", "style": false, "font": "", "fontSize": 16, "color": "#000000", "x": 570, "y": 160, "wires": [] }, { "id": "c099b466db7d76cf", "type": "obniz", "obnizId": "67598631", "deviceType": "obnizboard", "name": "無印 - ds-office", "accessToken": "", "code": "obnizParts.dht20 = obniz.wired(\"DHT20\",{vcc:0, sda:1, gnd:2,  scl:3 ,voltage: \"5v\"});" }, { "id": "05a0dc532d4e0de6", "type": "ui_group", "name": "温度 - temp", "tab": "8efd9a328f31df7a", "order": 2, "disp": true, "width": "8", "collapse": false, "className": "" }, { "id": "3a79bf34cc15cd96", "type": "ui_group", "name": "湿度 - humidity", "tab": "8efd9a328f31df7a", "order": 1, "disp": true, "width": "8", "collapse": false, "className": "" }, { "id": "8efd9a328f31df7a", "type": "ui_tab", "name": "オフィスモニタリング", "icon": "dashboard", "disabled": false, "hidden": false } ]
n0bisuken0bisuke

トラフィックライト検証 なぜか黄色が動かなかった

obnizParts.dht20 = obniz.wired("DHT20",{vcc:0, sda:1, gnd:2,  scl:3 ,voltage: "5v"});
obnizParts.light = obniz.wired("Keyestudio_TrafficLight", {gnd:8, green:9, yellow:10, red:11});

// obnizParts.light.single("yellow");

obnizParts.light.green.on();
obnizParts.light.yellow.on();
obnizParts.light.red.on();

return msg;

動いたコードこちら

8,9,10,11のピン指定だったものを5,6,7,8に変更したら動いた。
10番ピンが何かおかしかったのかも

obnizParts.dht20 = obniz.wired("DHT20",{vcc:0, sda:1, gnd:2,  scl:3 ,voltage: "5v"});
obnizParts.light = obniz.wired("Keyestudio_TrafficLight", {gnd:5, green:6, yellow:7, red:8});
n0bisuken0bisuke

複数のobniz repeat

複数のセンサー使いたくてこんな試し方をしてみた

  • 1つめ dhtのobniz repeat
msg.payload = await obnizParts.dht20.getAllDataWait();
return msg;
  • 2つめ cds用のad読み込み
    (テンプレ読み込みした段階)
const voltage = await obniz.ad1.getWait();

obniz.display.print(voltage)
msg.payload = `changed to ${voltage} v`;
return msg;

これだと2個目に入れたCdSしか機能しなかった。

詳しい人に聞いたら二つ使うとバグるらしい。(仕様)

修正

こんな感じに一つにまとめないといけないです。
ただmsg.payloadに詰めると値が上書きされてしまうので
msg.payload.cdsみたいなプロパティに値を詰めないといけないです。

  • ダメな例
msg.payload = await obnizParts.dht20.getAllDataWait();

const voltage = await obniz.ad1.getWait();

obniz.display.print(voltage)
msg.payload = `changed to ${voltage} v`; //上書きされちゃう

return msg;
  • 良き
msg.payload = await obnizParts.dht20.getAllDataWait();

const voltage = await obniz.ad1.getWait();

obniz.display.print(voltage)
msg.payload.cds = `changed to ${voltage} v`;

return msg;

n0bisuken0bisuke

CdS(1MΩ) に対しての抵抗器

オフィスでの変化

  • 1MΩ(1/4W)の抵抗器 => 4.7v~4.9vくらい

  • 100kΩ(1/6W)の抵抗器 => 4.5~4.6vくらい

  • 10kΩ(1/4W)の抵抗器 => 3.4~3.7vくらい

  • 330Ω(1/2W)の抵抗器 => 0.3~0.4vくらい

n0bisuken0bisuke

トラフィックライトと温湿度例
https://twitter.com/n0bisuke/status/1787505984644251689

[{"id":"aa89eea0859520b9","type":"obniz-repeat","z":"79c0d73b5a5130c9","obniz":"c099b466db7d76cf","name":"温湿度 - repeat","interval":100,"code":"msg.payload = await obnizParts.dht20.getAllDataWait();\n\nconst voltage = await obniz.ad11.getWait();\n\nobniz.display.print(voltage)\nmsg.payload.cds = voltage;\n\nreturn msg;\n","x":140,"y":80,"wires":[["99c75aaf5dd950dc","74be2b2d414e84ab","66b4f05ee59ac891","a80548a41596ba33","ecce98b47d239c69","d2b191a460f236c5"]]},{"id":"74be2b2d414e84ab","type":"change","z":"79c0d73b5a5130c9","name":"湿度の値","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.humidity","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":40,"wires":[["6abaea9d375d7ae0","45eac3c9c3ba4e5f","e510e9c2eb12c6e1","754b771a98e5260b"]]},{"id":"754b771a98e5260b","type":"switch","z":"79c0d73b5a5130c9","name":"湿度でLED分岐","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"49","vt":"str"},{"t":"btwn","v":"47","vt":"num","v2":"48.9","v2t":"num"},{"t":"lte","v":"46.9","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":760,"y":180,"wires":[["1692a0b0790c0cfc"],["909c3e53f3ac6bbe"],["0caa546946faeac2"]]},{"id":"1692a0b0790c0cfc","type":"obniz-function","z":"79c0d73b5a5130c9","obniz":"c099b466db7d76cf","name":"LED緑","code":"// obnizParts.light.single(\"yellow\");\n\nobnizParts.light.green.on();\n\nreturn msg;","x":990,"y":280,"wires":[[]]},{"id":"909c3e53f3ac6bbe","type":"obniz-function","z":"79c0d73b5a5130c9","obniz":"c099b466db7d76cf","name":"LED黄色","code":"// obnizParts.light.single(\"yellow\");\n\nobnizParts.light.yellow.on();\n\n\nreturn msg;","x":980,"y":340,"wires":[[]]},{"id":"0caa546946faeac2","type":"obniz-function","z":"79c0d73b5a5130c9","obniz":"c099b466db7d76cf","name":"LED赤","code":"// obnizParts.light.single(\"yellow\");\n\nobnizParts.light.red.on();\n\n\nreturn msg;","x":970,"y":400,"wires":[[]]},{"id":"c099b466db7d76cf","type":"obniz","obnizId":"67598631","deviceType":"obnizboard","name":"無印 - ds-office","accessToken":"","code":"obnizParts.dht20 = obniz.wired(\"DHT20\",{vcc:0, sda:4, gnd:2,  scl:3 ,voltage: \"5v\"});\nobnizParts.light = obniz.wired(\"Keyestudio_TrafficLight\", {gnd:5, green:6, yellow:7, red:8});"}]