Skip to content

Instantly share code, notes, and snippets.

@kou029w
Last active February 6, 2020 09:14
Show Gist options
  • Save kou029w/50f07bb6272363aa1eeeb25e816e51e0 to your computer and use it in GitHub Desktop.
Save kou029w/50f07bb6272363aa1eeeb25e816e51e0 to your computer and use it in GitHub Desktop.
tottori-iot.netlify.com 設定例抜粋

条件

パラメーター 条件式 動作
騒音 70 <= soundNoise && soundNoise < 90 GPIO 20 が一定回数 High/Low の出力を繰り返す
騒音 90 <= soundNoise GPIO 26 が一定回数 High/Low の出力を繰り返す
温度 26 <= temperature 温度${temperature}度です と読み上げる
相対湿度 50 <= relativeHumidity 湿度${relativeHumidity}パーセントです と読み上げる
VOC 100 <= eTVOC 総揮発性有機化合物濃度${eTVOC}ppbです と読み上げる

配線としては、GPIO 20 に黄色のLEDのアノード、GPIO 26 に赤色のLEDのアノードを接続する。 LEDのカソードには100Ωから470Ω程度の抵抗を介してRaspberry PiのGND端子に接続する。

配線図:

GPIO20---|>|---[100-470 Ohm]---GND
GPIO26---|>|---[100-470 Ohm]---GND

加えてスピーカーを接続する

設定例

    speechAgent: {
      enable: true,
      notifyScript: ({ soundNoise, temperature, relativeHumidity, eTVOC }) => {
        const { writeFileSync: write } = require("fs");
        const sleep = require("util").promisify(setTimeout);
        async function blink(pin) {
          try {
            write("/sys/class/gpio/export", pin);
          } catch (e) {}
          try {
            write(`/sys/class/gpio/gpio${pin}/direction`, "out");
          } catch (e) {}

          try {
            for (let i = 0; i < 10; i++) {
              write(`/sys/class/gpio/gpio${pin}/value`, 1);
              await sleep(1000);
              write(`/sys/class/gpio/gpio${pin}/value`, 0);
              await sleep(1000);
            }
          } catch (e) {}
        }
        // LEDをRaspberry PiのGPIO 20, GPIO 26 (BCM)に接続する
        if (70 <= soundNoise && soundNoise < 90) blink(20);
        if (90 <= soundNoise) blink(26);

        return [
          26 <= temperature && `温度${temperature}度です`,
          50 <= relativeHumidity && `湿度${relativeHumidity}パーセントです`,
          100 <= eTVOC && `総揮発性有機化合物濃度${eTVOC}ppbです`
        ]
          .filter(Boolean)
          .join("、");
      }
    }

ピン配置の参考資料: https://www.raspberrypi.org/documentation/usage/gpio/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment