{"id":22612453,"url":"https://github.com/miso-develop/github-pages-test","last_synced_at":"2025-03-28T23:45:08.891Z","repository":{"id":44804040,"uuid":"350731396","full_name":"miso-develop/github-pages-test","owner":"miso-develop","description":null,"archived":false,"fork":false,"pushed_at":"2022-01-23T15:18:50.000Z","size":495,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-03T10:12:25.455Z","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}},"created_at":"2021-03-23T13:53:41.000Z","updated_at":"2021-03-23T17:28:14.000Z","dependencies_parsed_at":"2022-09-08T23:40:41.837Z","dependency_job_id":null,"html_url":"https://github.com/miso-develop/github-pages-test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fgithub-pages-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fgithub-pages-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fgithub-pages-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fgithub-pages-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miso-develop","download_url":"https://codeload.github.com/miso-develop/github-pages-test/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246117690,"owners_count":20726068,"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-12-08T17:12:58.834Z","updated_at":"2025-03-28T23:45:08.851Z","avatar_url":"https://github.com/miso-develop.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# opniz SDK for Node.js\n\n`❗ このプロジェクトは現在アルファ版です。`\n\nopnizとはM5StackといったESP32デバイスをNode.jsからobnizライクに制御するための、**Node.js SDK**および**Arduinoライブラリ**です。  \nざっくりいうとサーバーを介さない、オープンソースな、obnizっぽい実装ができるなにかです。  \n\nしくみとしてはESP32デバイスおよびNode.js SDKにて**TCPサーバー/クライアントを実行**し、相互にTCP経由で**JSONメッセージ**をやりとりしています。  \n\n![overview](./extras/images/overview.png)\n\n現在Node.js SDK、Arduinoライブラリともに**ESP32**および**M5ATOM**クラスを実装しています。  \nM5ATOMクラスで**M5Stack、M5StickC、M5ATOM Lite、M5ATOM Matrixでの動作を確認しています。**  \n\n新たなデバイスクラスを簡単に拡張できる設計となっています。  \nおってリファレンスも作成予定ですが、クラス拡張ハンズオンも作成予定です。  \n\n\n\n## Node.js SDK\n\n本リポジトリはNode.js SDKのリポジトリとなります。  \nデバイスのRead/Writeを実行したり（Pinも動的に指定可能です）、デバイス側からのイベント（たとえばM5Stack系デバイスのボタン等）を受け取って非同期に処理を実行したりできます。  \n\n\n\n## インストール方法\n\nnpmにはまだリリースしていないため、GitHubリポジトリをcloneのうえ`npm install`にてcloneディレクトリを指定しインストールします。  \n\n```\nmkdir opniz \u0026\u0026 cd opniz\ngit clone https://github.com/miso-develop/opniz-sdk-nodejs opniz\nnpm install ./opniz\n```\n\n別途デバイスへも[Arduinoライブラリ](https://github.com/miso-develop/opniz-device)を使用したスケッチの書き込みが必要です。  \n\n\n\n## 使い方\n\n❗ 事前にデバイスへ[opniz Arduinoライブラリ](https://github.com/miso-develop/opniz-device)を書き込んでください  \n\n以下のコードは最小限のopnizのJavaScript実装コードです。  \nopnizインスタンスの生成、opnizデバイスへの接続、デバイスへの操作の3ステップを実行しています。  \n`address`、`port`をデバイスのIPアドレスと、任意のポート番号に書き換えて実行してみてください。  \n\n```js\nconst { Opniz } = require(\"opniz\")\n\nconst address = \"192.168.0.1\" // opnizデバイスのIPアドレスを指定\nconst port = 3000             // 任意のポートを指定（opnizデバイスでの指定と合わせる）\n\nconst main = async () =\u003e {\n\tconst opniz = new Opniz.M5Atom({ address, port }) // opnizインスタンス生成\n\tawait opniz.connect()                                 // opnizデバイスへ接続\n\tconsole.log(await opniz.getFreeHeap())                // opnizデバイスのヒープメモリーサイズを取得して表示\n}\nmain()\n```\n\n実際にデバイスを24時間稼働で実装する場合は以下のようにループ実装します。  \n\n```js\nconst { Opniz } = require(\"opniz\")\n\nconst address = \"192.168.0.1\" // opnizデバイスのIPアドレスを指定\nconst port = 3000             // 任意のポートを指定（opnizデバイスでの指定と合わせる）\n\nconst main = async () =\u003e {\n\t// opnizインスタンス生成\n\tconst opniz = new Opniz.M5Atom({ address, port })\n\t\n\t// opnizデバイスへ接続\n\twhile (!(await opniz.connect())) {\n\t\tconsole.log(\"connect...\")\n\t\tawait opniz.sleep(1000)\n\t}\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❗ 事前にM5ATOMへ[opniz Arduinoライブラリ](https://github.com/miso-develop/opniz-device)を書き込んでください  \n\n### M5ATOMでLチカ（内蔵LED）\n\nM5ATOM（Lite、Matrixどちらでもかまいません）でLチカを行ってみます。  \nまずはM5ATOMに内蔵されているLEDを制御します。  \n※M5Stack、M5StickCは「M5ATOMでLチカ（ピンに挿したLED）」をお試しください  \n\n以下のコードを実行すると1秒おきにLEDが青く点滅します。  \n25行目の`await opniz.drawpix(0, color)`が内蔵LEDを制御するメソッドです。  \nこれはM5ATOMのArduinoライブラリで実装されている関数`M5.dis.drawpix`と名前、引数とも同じです。  \nこのようにデバイスをArduinoライブラリを用いて実装するのと同じ感覚でNode.jsにて実装できます。  \n\n```js\nconst { Opniz } = require(\"opniz\")\n\nconst address = \"192.168.0.1\" // opnizデバイスのIPアドレスを指定\nconst port = 3000             // 任意のポートを指定（opnizデバイスでの指定と合わせる）\n\nconst OFF = \"#000000\"\nconst BLUE = \"#0000ff\"\nlet color = OFF\n\nconst main = async () =\u003e {\n\t// opnizインスタンス生成\n\tconst opniz = new Opniz.M5Atom({ address, port })\n\t\n\t// opnizデバイスへ接続\n\twhile (!(await opniz.connect())) {\n\t\tconsole.log(\"connect...\")\n\t\tawait opniz.sleep(1000)\n\t}\n\tconsole.log(\"[connected]\")\n\t\n\ttry {\n\t\t// 1秒おきに内蔵LEDを青色で点滅\n\t\tfor (;;) {\n\t\t\tcolor = color === OFF ? BLUE : OFF\n\t\t\tawait opniz.drawpix(0, color)\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### M5ATOMでLチカ（ピンに挿したLED）\n\n次に内蔵LEDではなく、ピンに挿したLEDを制御してみましょう。  \nM5ATOM Lite、Matrixの場合、21番ピンとGNDにLEDを挿します（必要に応じて抵抗を添えてください）。  \n※M5Stack、M5StickCはピン番号を任意に置き換えてお試しください  \n\n以下のコードを実行すると1秒おきにLEDが点滅します。  \n25行目の`await opniz.digitalWrite(21, value)`にて21番ピンをdigitalWriteで制御しています。  \nこのようにNode.js SDKより動的にピンをアサインして制御できます。  \n\n```js\nconst { Opniz } = require(\"opniz\")\n\nconst address = \"192.168.0.1\" // opnizデバイスのIPアドレスを指定\nconst port = 3000             // 任意のポートを指定（opnizデバイスでの指定と合わせる）\n\nconst HIGH = 1\nconst LOW = 0\nlet value = LOW\n\nconst main = async () =\u003e {\n\t// opnizインスタンス生成\n\tconst opniz = new Opniz.M5Atom({ address, port })\n\t\n\t// opnizデバイスへ接続\n\twhile (!(await opniz.connect())) {\n\t\tconsole.log(\"connect...\")\n\t\tawait opniz.sleep(1000)\n\t}\n\tconsole.log(\"[connected]\")\n\t\n\ttry {\n\t\t// 1秒おきに21番ピンとGNDに挿したLEDを点滅\n\t\tfor (;;) {\n\t\t\tvalue = value === LOW ? HIGH: LOW\n\t\t\tawait opniz.digitalWrite(21, value)\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### M5ATOMのボタンイベントを拾って処理する\n\n次のコードを実行し、M5ATOMのボタンを押すとconsoleに`on button!`と表示されます。  \n18行目の`opniz.onbutton = () =\u003e console.log(\"on button!\")`のように`onbutton`メソッドを上書くことでボタンイベントを拾い、任意の処理を行います。  \nこのonメソッドはデバイスクラスにて任意に実装可能です。  \n\n```js\nconst { Opniz } = require(\"opniz\")\n\nconst address = \"192.168.0.1\" // opnizデバイスのIPアドレスを指定\nconst port = 3000             // 任意のポートを指定（opnizデバイスでの指定と合わせる）\n\nconst main = async () =\u003e {\n\t// opnizインスタンス生成\n\tconst opniz = new Opniz.M5Atom({ address, port })\n\t\n\t// opnizデバイスへ接続\n\twhile (!(await opniz.connect())) {\n\t\tconsole.log(\"connect...\")\n\t\tawait opniz.sleep(1000)\n\t}\n\tconsole.log(\"[connected]\")\n\t\n\t// M5ATOMのボタンが押されたらconsole表示\n\topniz.onbutton = () =\u003e console.log(\"on button!\")\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## ライセンス\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiso-develop%2Fgithub-pages-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiso-develop%2Fgithub-pages-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiso-develop%2Fgithub-pages-test/lists"}