2026年8月23日日曜日

groveシステムのメモ obniz and grove-sht35

 https://qiita.com/nanbuwks/items/b79f53b46f289ec9d411

pin 1 - Yellow (for example, SCL on I2C Grove Connectors)

pin 2 - White (for example, SDA on I2C Grove Connectors)

pin 3 - Red - VCC on all Grove Connectors

pin 4 - Black - GND on all Grove Connectors

これだけわかればOK

------アプリ開発で成功 書斎ではhuaweiのアクセスポイントでのみ成功 電波よわいか-------

html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://unpkg.com/obniz@3.x/obniz.js" crossorigin="anonymous" ></script>
</head>
<body>

<div class="container">
<h1>SHT35 温湿度モニター</h1>
<div id="status" class="status">obnizに接続中...</div>
<div class="data">
温度: <span id="temp">--.-</span>
</div>
<div class="data">
湿度: <span id="humi">--.-</span> %
</div>
</div>

<script>

// 1. 自分のobniz IDを指定
const obniz = new Obniz("OBNIZ_ID_HERE");

// 2. obnizに接続完了したときの処理
obniz.onconnect = async function () {

document.getElementById("status").innerText =
"接続成功。データ取得中...";

// 3. SHT35のピン割り当て
const sht35 = obniz.wired("Grove_SHT35Sensor", {
gnd: 0,
vcc: 1,
sda: 2,
scl: 3
});

// 4. 3秒ごとに測定
setInterval(async function () {

try {

// ★ getAllData() ではなく getAllWait()
const data = await sht35.getAllWait();

if (data) {

document.getElementById("temp").innerText =
data.temperature.toFixed(1);

document.getElementById("humi").innerText =
data.humidity.toFixed(1);

document.getElementById("status").innerText =
"データ更新完了(3秒間隔)";

console.log(
"温度:", data.temperature,
"湿度:", data.humidity
);
}

} catch (error) {

document.getElementById("status").innerText =
"データ取得エラー";

console.error(error);
}

}, 3000);
};

// 切断時
obniz.onclose = function () {

document.getElementById("status").innerText =
"obnizとの接続が切断されました";
};
</script>
</body>
</html>

0 件のコメント:

コメントを投稿