---
# System prepended metadata

title: L9110s遠隔化答え

---

# L9110s遠隔化答え
## CodeSandBox

HTML
```html
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
</head>
<script type="module" src="pc.js"></script>
<body>
<h3 id="deviceTitle">Remote Example - Controller</h3>
<input type="button" onclick="OnLED_A()" value="LED A ON"></input>
<input type="button" onclick="OnLED_B()" value="LED B ON"></input>
<input type="button" onclick="OffLED()" value="LED A, B OFF"></input>
<div id="messageDiv" ></div>
</body>
</html>
```

JavaScript
```javascript
import { RelayServer } from "https://chirimen.org/remote-connection/js/beta/RelayServer.js";

window.OnLED_A = OnLED_A;
window.OnLED_B = OnLED_B;
window.OffLED = OffLED;

var channel;
onload = async function () {
  // webSocketリレーの初期化
  var relay = RelayServer("chirimentest", "chirimenSocket");
  channel = await relay.subscribe("chirimenLED");
  messageDiv.innerText = "web socketリレーサービスに接続しました";
  channel.onmessage = getMessage;
};

function getMessage(msg) {
  // メッセージを受信したときに起動する関数
  messageDiv.innerText = msg.data;
}

function OnLED_A() {
  console.log("OnLED_A()");
  channel.send("LED_A ON");
}
function OnLED_B() {
  console.log("OnLED_B()");
  channel.send("LED_B ON");
}
function OffLED() {
  console.log("OffLED()");
  channel.send("LED OFF");
}

```

## ラズパイ
Node.js
```javascript
import { requestGPIOAccess } from "./node_modules/node-web-gpio/dist/index.js";
import nodeWebSocketLib from "websocket"; // https://www.npmjs.com/package/websocket
import { RelayServer } from "./RelayServer.js";

var channel;
let portA1A;
let portA1B;

const A1A = 20;
const A1B = 26;

const LOW = 0;
const HIGH = 1;

const led = async (char, portA, portB) => {
    if (char === "R") {
        await portA.write(LOW);
        await portB.write(HIGH);
    } else if (char == "L") {
        await portA.write(HIGH);
        await portB.write(LOW);
    } else {
        await portA.write(LOW);
        await portB.write(LOW);
    }
};

async function connect() {
    const gpioAccess = await requestGPIOAccess();
    portA1A = await gpioAccess.ports.get(A1A);
    portA1B = await gpioAccess.ports.get(A1B);
    await portA1A.export("out");
    await portA1B.export("out");

    // webSocketリレーの初期化
    var relay = RelayServer("chirimentest", "chirimenSocket", nodeWebSocketLib, "https://chirimen.org");
    channel = await relay.subscribe("chirimenLED");
    console.log("web socketリレーサービスに接続しました");
    channel.onmessage = controlLED;
}

async function controlLED(message) {
    const text = message.data
    if (text === "LED_A ON") {
        await led("R", portA1A, portA1B);
        console.log("LED A ON");
        channel.send("LED A をオンにしました");
    } else if (text === "LED_B ON") {
        await led("L", portA1A, portA1B);
        console.log("LED B ON");
        channel.send("LED B をオンにしました");
    } else if (text == "LED OFF") {
        await led("OFF", portA1A, portA1B)
        console.log("LED A, B OFF");
        channel.send("LED A, Bをオフにしました");
    } else {
        throw Error("不正なメッセージ")
    }
}

connect();
```