孫祥恩
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: 作業 --- # 物聯網 Lab5 物聯網目錄: * [物聯網環境建置與測試](/9cAzkUsBRZCozYU6PdEXDA) * [物聯網 Lab1](/jV8TXnQKREeo2GTfujuyRQ) * [物聯網 Lab2](/I4zHw3uDTneV2Jw3G829kQ) * [物聯網 Lab4](/Tm65KQpQR0aUrsrUTlGgzQ) * 本篇 (Lab5) ## 遠端燈泡同步控制 ### 前備知識 打開 NSCL 與 GSCL 後,在 GSCL 部分打入 `ss` 出現如下視窗,再輸入 `start 25` 開始燈泡模擬 `ipu.sample` ![](https://i.imgur.com/VFwQZ8x.png) ### 達成目標 基本功能: - [x] 手機控制燈泡亮暗 - [x] 手機同步顯示燈泡資訊 額外功能: - [x] 手動同步改成定時同步 - [x] 按鈕控制改成 `Switch`,且配合狀態同步顯示 ### 版面設計 <p float="left"> <img src="https://i.imgur.com/Xa25hwS.png" width="50%" /> <img src="https://i.imgur.com/EBHgBmW.png" width="40%" /> </p> 一樣需注意元件初始值 * `send`:`Url` 填入 `http://電腦的 IPv4:1880/LAMP` * `recv`:`Url` 填入 `http://電腦的 IPv4:1880/STATUS` * `Clock1`:`TimeInterval` 填入 700 毫秒 ### 程式設計 這次主要因為 `Switch` 同步的關係,在程式上需要考慮的點會比較多 * `recv` 主要是進行定期同步,根據 OM2M 紀錄更新 APP 狀態,同時也更新 `Switch` * 兩個變數 `lamp0` 與 `lamp1` 為紀錄下一輪同步前的狀態 * 由於 OM2M 端切換燈泡時會使得 `Switch` 一起改變,而切換又會造成事件觸發,所以需要比較同步前的狀態是否有更動,如果不同就表示是 APP 主動切換燈泡 * 控制全部按鈕的部分比較複雜,同步基本上就是與電路的 AND 同理,在事件部分比對的則是兩個 `Switch` 目前狀況,不同就表示主動控制全部 * 傳送控制指令以 `control` 為主要關鍵字 ![](https://i.imgur.com/qVklbkA.png) Node-RED 部分拉出以下所示 * `/LAMP` 接收 APP 的控制訊息,根據數字執行對應的 OM2M 操作 * `/STATUS` 接收 APP 的同步訊息,先依序獲取燈泡最新狀態,再擷取對應的 true/false,最終合併傳回 ![](https://i.imgur.com/SwmgBoM.png) `CONTROL` 這邊使用 `status` 變數作為 `switch` 判斷,且使 `payload` 為空,以此傳送空指令 ```javascript= msg.status = msg.payload.control; msg.payload = ""; return msg; ``` 這邊的路徑與 OM2M 上的捷徑一致,ALL 的部分與其他的稍為不同 > 前面的號碼表示 `control` 所對應的指令 * (0) LAMP0 ON:`http://localhost:8181/om2m/gscl/applications/LAMP_0/lamps/true` * (1) LAMP0 OFF:`http://localhost:8181/om2m/gscl/applications/LAMP_0/lamps/false` * (2) LAMP1 ON:`http://localhost:8181/om2m/gscl/applications/LAMP_1/lamps/true` * (3) LAMP1 OFF:`http://localhost:8181/om2m/gscl/applications/LAMP_1/lamps/false` * (4) ALL ON:`http://localhost:8181/om2m/gscl/groups/ON_ALL/membersContent` * (5) ALL FF:`http://localhost:8181/om2m/gscl/groups/OFF_ALL/membersContent` * LAMP0 STAT: `http://localhost:8181/om2m/gscl/applications/LAMP_0/containers/DATA/contentInstances/latest/content` * LAMP1 STAT: `http://localhost:8181/om2m/gscl/applications/LAMP_1/containers/DATA/contentInstances/latest/content` `EXTRACT` 是一樣的 `function`,主要為了取得燈泡最新狀態的 true/false ```javascript= msg.payload = msg.payload.obj.bool[0].$.val; return msg; ``` `join` 這邊使用 `,` 分隔,且設定接受到兩個訊息後自動合併 > 需自行注意燈泡誰前誰後 ![](https://i.imgur.com/vc54a7J.png) ### 實際畫面 ![](https://i.imgur.com/V0n9AUi.png) ![](https://i.imgur.com/akJibVw.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully