{"id":14971048,"url":"https://github.com/miso-develop/opniz-sdk-nodejs","last_synced_at":"2025-09-26T16:28:57.785Z","repository":{"id":53607666,"uuid":"348282090","full_name":"miso-develop/opniz-sdk-nodejs","owner":"miso-develop","description":null,"archived":false,"fork":false,"pushed_at":"2023-10-18T09:20:09.000Z","size":3029,"stargazers_count":8,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-09-28T14:01:24.388Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miso-develop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-03-16T09:14:06.000Z","updated_at":"2022-12-11T03:19:58.000Z","dependencies_parsed_at":"2023-01-28T05:40:21.451Z","dependency_job_id":null,"html_url":"https://github.com/miso-develop/opniz-sdk-nodejs","commit_stats":{"total_commits":69,"total_committers":4,"mean_commits":17.25,"dds":0.08695652173913049,"last_synced_commit":"bfe915ee7af387858ac30bd96532128f352eba60"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fopniz-sdk-nodejs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fopniz-sdk-nodejs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fopniz-sdk-nodejs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fopniz-sdk-nodejs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miso-develop","download_url":"https://codeload.github.com/miso-develop/opniz-sdk-nodejs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219862782,"owners_count":16555951,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T13:44:38.769Z","updated_at":"2025-09-26T16:28:52.736Z","avatar_url":"https://github.com/miso-develop.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# opniz SDK for Node.js\n\n\u003cdiv align=\"center\"\u003e\u003cimg src=\"https://user-images.githubusercontent.com/22117028/150321839-e1e73dcb-d4b6-4bac-92b9-b1501ff3b092.png\" alt=\"logo\"\u003e\u003c/div\u003e\n\n\u003e ❗ このプロジェクトは現在アルファ版です。\n\nopnizとはM5StackデバイスをNode.js（JavaScript / TypeScript）からobnizライクに実装できるオープンソースフレームワークです。  \nNode.js SDKおよびArduinoライブラリがあり、WebSocketで相互通信を行います。  \n\nしくみとしてはM5StackデバイスおよびNode.js SDK間にてJSON形式のRPCメッセージをやりとりし、相互に定義されたメソッドを呼び合います。  \n\n![overview](https://user-images.githubusercontent.com/22117028/274111780-1f1e22ec-66ac-4cd0-bee3-1a19ee2eb65c.png)\n\n\n\n## Node.js SDK\n\n本リポジトリはNode.js SDKのリポジトリとなります。  \nデバイスのRead/Writeを実行したり（Pinも動的に指定可能です）、デバイス側からのイベント（たとえばM5Stack系デバイスのボタン等）を受け取って非同期に処理を実行したりできます。  \nまたM5UnifiedおよびM5Atom Arduino LibraryのAPIもいくつか実装しています。  \n\n\n\n## 対応デバイス\n\n* M5Stack BASIC\n* M5Stack Core2\n* M5StickC\n* M5ATOM Matrix\n* M5ATOM Lite\n* M5ATOM Echo\n* M5ATOM U\n* M5ATOMS3\n* M5ATOMS3 Lite\n* M5Stamp Pico\n* M5Stamp S3\n* その他ESP32、ESP32-PICO-D4、ESP32-S3デバイス\n\n\n\n## インストール\n\n```\nnpm install opniz\n```\n\n\n\n## 使い方\n\n\u003e ❗ 事前にデバイスへopniz Arduinoライブラリ（[M5Unified向け](https://github.com/miso-develop/opniz-arduino-m5unified)もしくは[ESP32向け](https://github.com/miso-develop/opniz-arduino-esp32)）を書き込んでください。  \n\u003e [opniz CLI (npm)](https://github.com/miso-develop/opniz-cli) を使用するとかんたんにデバイス書き込みできます。  \n\n以下のコードは最小限のopnizのJavaScript実装コードです。  \nopnizインスタンスの生成、opnizデバイスへの接続、デバイスへの操作の3ステップを実行しています。  \n`address`、`port`をデバイスのIPアドレスと、任意のポート番号に書き換えて実行してみてください。  \n\n```js\nconst { Opniz } = require(\"opniz\")\n\nconst port = 3000 // 任意のポートを指定（opnizデバイスの指定と合わせる）\n\n; (async () =\u003e {\n\tconst opniz = new Opniz.Esp32({ port }) // opnizインスタンス生成\n\tawait opniz.connectWait()                   // opnizデバイスへ接続\n\tconsole.log(await opniz.getFreeHeap())      // opnizデバイスのヒープメモリーサイズを取得して表示\n})()\n```\n\n実際にデバイスを24時間稼働で実装する場合は以下のようにループ実装します。  \n\n```js\nconst { Opniz } = require(\"opniz\")\n\nconst port = 3000 // 任意のポートを指定（opnizデバイスの指定と合わせる）\nconst opniz = new Opniz.Esp32({ port }) // opnizインスタンス生成\n\nconst main = async () =\u003e {\n\t// opnizデバイスへ接続\n\twhile (!(await opniz.connectWait())) console.log(\"connect...\")\n\tconsole.log(\"[connected]\")\n\t\n\ttry {\n\t\t// 1秒おきにデバイスのヒープメモリーサイズを表示\n\t\tfor (;;) {\n\t\t\tconsole.log(await opniz.getFreeHeap())\n\t\t\tawait opniz.sleep(1000)\n\t\t}\n\t\t\n\t// エラー処理\n\t} catch(e) {\n\t\tconsole.log(\"[error]\", e.message)\n\t\tawait main()\n\t}\n}\nmain()\n```\n\n\n\n## 実装例\n\n\u003e ❗ 事前にM5ATOM等の内蔵LED搭載デバイスへ[opniz Arduinoライブラリ（M5Unified向け）](https://github.com/miso-develop/opniz-arduino-m5unified)を書き込んでください  \n\n### M5ATOMでLチカ（内蔵LED）\n\nM5ATOM（内蔵フルカラーLED搭載端末であれば他のM5Stackデバイスでも大丈夫です）でLチカを行ってみます。  \n\n以下のコードを実行すると1秒おきにLEDが点滅します。  \n`await opniz.Led.fillpix(color)`が内蔵LEDを制御するメソッドです。  \nこのようにデバイスをArduinoライブラリを用いて実装するのと同じ感覚でNode.jsにて実装できます。  \n\n```js\nconst { Opniz } = require(\"opniz\")\n\nconst port = 3000 // 任意のポートを指定（opnizデバイスでの指定と合わせる）\nconst opniz = new Opniz.M5Unified({ port }) // opnizインスタンス生成\n\nconst OFF = \"#000000\"\nconst GREEN = \"#00ff00\"\nlet state = true\n\nconst main = async () =\u003e {\n\t// opnizデバイスへ接続\n\twhile (!(await opniz.connectWait())) console.log(\"connect...\")\n\tconsole.log(\"[connected]\")\n\t\n\ttry {\n\t\t// 1秒おきに内蔵LEDを点滅\n\t\tfor (;;) {\n\t\t\tawait opniz.Led.fillpix(state ? GREEN : OFF)\n\t\t\tstate = !state\n\t\t\t\n\t\t\tawait opniz.sleep(1000)\n\t\t}\n\t\t\n\t// エラー処理\n\t} catch (e) {\n\t\tconsole.log(\"[error]\", e.message)\n\t\tawait main()\n\t}\n}\nmain()\n```\n\n### IOに接続したLEDでLチカ\n\n次に内蔵LEDではなく、IOに接続したLEDを制御してみましょう。  \n\n以下のコードを実行すると21番ピンに接続したLEDが1秒おきに点滅します。  \n`await opniz.digitalWrite(pin, state ? HIGH : LOW)`にて変数`pin`に指定したピンをdigitalWriteで制御しています。  \nこのようにNode.js SDKより動的にピンをアサインして制御できます。  \n\n```js\nconst { Opniz } = require(\"opniz\")\n\nconst port = 3000 // 任意のポートを指定（opnizデバイスでの指定と合わせる）\nconst opniz = new Opniz.M5Unified({ port }) // opnizインスタンス生成\n\nconst pin = 21 // LEDを繋いだピン番号を指定\n\nconst HIGH = 1\nconst LOW = 0\nlet state = true\n\nconst main = async () =\u003e {\n\t// opnizデバイスへ接続\n\twhile (!(await opniz.connectWait())) console.log(\"connect...\")\n\tconsole.log(\"[connected]\")\n\t\n\ttry {\n\t\t// 1秒おきに`pin`に接続したLEDを点滅\n\t\tfor (;;) {\n\t\t\tawait opniz.digitalWrite(pin, state ? HIGH : LOW)\n\t\t\tstate = !state\n\t\t\t\n\t\t\tawait opniz.sleep(1000)\n\t\t}\n\t\t\n\t// エラー処理\n\t} catch(e) {\n\t\tconsole.log(\"[error]\", e.message)\n\t\tawait main()\n\t}\n}\nmain()\n```\n\n### デバイスのボタンイベントを拾って処理する\n\n次のコードを実行し、デバイスのボタンを押すとコンソールに`onClicked!`と表示されます。  \n`opniz.Btn.onClicked = (button, count) =\u003e console.log(\"onClicked!\")`のように`onClicked`メソッドを上書くことでボタンイベントを拾い、任意の処理を行います。  \nこのonメソッドはデバイスクラスにて任意に実装可能です。  \n\n```js\nconst { Opniz } = require(\"opniz\")\n\nconst port = 3000 // 任意のポートを指定（opnizデバイスでの指定と合わせる）\nconst opniz = new Opniz.M5Unified({ port }) // opnizインスタンス生成\n\nconst main = async () =\u003e {\n\t// opnizデバイスへ接続\n\twhile (!(await opniz.connectWait())) console.log(\"connect...\")\n\tconsole.log(\"[connected]\")\n\t\n\t// デバイスのボタンが押されたらconsole表示\n\topniz.Btn.onClicked = (button, count) =\u003e console.log(\"onClicked!\")\n\t\n\ttry {\n\t\t// 無限ループ\n\t\tfor (;;) {\n\t\t\tawait opniz.sleep(1000)\n\t\t}\n\t\t\n\t// エラー処理\n\t} catch (e) {\n\t\tconsole.log(\"[error]\", e.message)\n\t\tawait main()\n\t}\n}\nmain()\n```\n\n\n\n## API\n\n#### analogRead(pin: number): Promise\\\u003cnumber\\\u003e\n\n`pin`に指定したピンからアナログ値を読み取ります。  \n\n#### digitalRead(pin: number): Promise\\\u003cnumber\\\u003e\n\n`pin`に指定したピンからデジタル値（0 or 1）を読み取ります。  \n\n#### digitalWrite(pin: number, val: number): Promise\\\u003cboolean\\\u003e\n\n`pin`に指定したピンから`value`に指定したデジタル値（0 or 1）を出力します。  \n\n#### ledcWrite(pin: number, duty: number, channel: number, freq: number, resolutionBits: number): Promise\\\u003cboolean\\\u003e\n\n`pin`に指定したピンからPWM出力します。  \n\n#### restart(): Promise\\\u003cboolean\\\u003e\n\nデバイスを再起動します。  \n\n### M5Unified API\n\n現在[M5Unifiedライブラリ](https://github.com/m5stack/M5Unified)の[`Display`](https://github.com/m5stack/M5GFX)、[`Button`](https://github.com/m5stack/M5Unified/blob/master/src/utility/Button_Class.hpp)、[`IMU`](https://github.com/m5stack/M5Unified/blob/master/src/utility/IMU_Class.hpp)クラスのメソッドをいくつか実装しています。  \n\n* [Display Class](https://miso-develop.github.io/opniz-sdk-nodejs/classes/devices_m5unified_Display.Display-1.html)\n* [Button Class](https://miso-develop.github.io/opniz-sdk-nodejs/classes/devices_m5unified_Button.Button-1.html)\n* [IMU Class](https://miso-develop.github.io/opniz-sdk-nodejs/classes/devices_m5unified_IMU.IMU-1.html)\n\n### RGB LED API\n\nM5Atomライブラリの[`LED_Display`](https://github.com/m5stack/M5Atom/blob/master/src/utility/LED_DisPlay.h)クラスを内部インクルードしいくつか実装しています。  \n\n[LED Class](https://miso-develop.github.io/opniz-sdk-nodejs/classes/devices_m5unified_LED.LED-1.html)\n\n\n\n## ドキュメント\n\n[**TypeDoc**](https://miso-develop.github.io/opniz-sdk-nodejs/)  \n\n* [Class M5Unified](https://miso-develop.github.io/opniz-sdk-nodejs/classes/devices_M5Unified.M5Unified-1.html)\n* [Class M5Atom (Deprecated)](https://miso-develop.github.io/opniz-sdk-nodejs/classes/devices_M5Atom.M5Atom.html)\n* [Class Esp32](https://miso-develop.github.io/opniz-sdk-nodejs/classes/devices_Esp32.Esp32.html)\n\n\n\n## 利用可能な通信プロトコル\n\nopniz Node.js SDKでは以下の通信プロトコルを実装しています。  \nデフォルトでは`WebSocket (Server)`が使用されます。  \n\n* WebSocket (Server)\n* WebSocket (Client)\n* TCP (Server/Client同居)\n\nopniz Arduinoライブラリでは以下の通信プロトコルを実装しています。  \nデフォルトでは`WebSocket (Client)`が使用されます。  \n\n* WebSocket (Client)\n* TCP (Server/Client同居)\n\n使用するプロトコルによりopnizのコンストラクタパラメータが変わってきます。  \n\n|プロトコル|コンストラクタパラメータ|パラメータ内容|\n|---|---|---|\n|WebSocket (Server)|`Opniz.Esp32({ port })`|`port`: listenするポート番号を指定します|\n|WebSocket (Client)|`Opniz.Esp32({ address, port, id?, protocol?: \"WebSocketClient\" })`|`address`: 接続先のopnizのIPアドレスまたはhostnameを指定します\u003cbr\u003e`port`: 接続先のopnizのWebSocket Serverポート番号を指定します\u003cbr\u003e`id`: [opniz Server](https://github.com/miso-develop/opniz-server)を経由してopnizデバイスと接続する場合に、opnizデバイスで指定した`id`と同じ値を指定します（省略可）\u003cbr\u003e`protocol`: \"WebSocketClient\"プロトコルを指定します（`id`が指定されている場合は省略可能です）|\n|TCP (Server/Client同居)|`Opniz.Esp32({ address, port, serverPort?, protocol?: \"TCP\" })`|`address`: 接続先のopnizのIPアドレスを指定します\u003cbr\u003e`port`: 接続先のopnizのTCP Serverポート番号を指定します\u003cbr\u003e`serverPort`: listenするポート番号を指定します（省略可能で、省略時は`port`と同じ値となります）\u003cbr\u003e`protocol`: \"TCP\"を指定します（`serverPort`が指定されている場合は省略可能です）|\n\nまたNode.js SDK同士の接続も可能です。  \n\n\n\n## 関連リポジトリ\n\n* [opniz Arduino Library for M5Unified](https://github.com/miso-develop/opniz-arduino-m5unified)\n\t* M5Unified向けArduinoライブラリ\n* [opniz CLI](https://github.com/miso-develop/opniz-cli)\n\t* opniz Arduino LibraryのBasicスケッチをコマンドから簡単に書き込めるCLIツール\n* [opniz Server](https://github.com/miso-develop/opniz-server)\n\t* opniz Node.js SDKやopnizデバイスからのJSON RPCメッセージを中継するWebSocketサーバ\n\n\n\n## ライセンス\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiso-develop%2Fopniz-sdk-nodejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiso-develop%2Fopniz-sdk-nodejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiso-develop%2Fopniz-sdk-nodejs/lists"}