FlagMaker
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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: `FM631A` # LAB15 - iPhone 範例 :::info 此範例除了 iPhone 以外, 也同樣適用於 Android 手機。 ::: ## AIO 建立 Feed 因為要將資料上傳到 AIO, 所以建立一個新的 Feed 來存放資料。請參考第 7 章到 [Adafruit IO](https://io.adafruit.com/) 建立一個名為 **door** 的 Feed : ![](https://i.imgur.com/h8tR8rh.png) ## 增加名稱資料夾 1. 開啟**下載範例檔案**, 並切換到『網頁資料/door_lock_iphone』資料夾: ![](https://i.imgur.com/0AxbF25.png) 2. 切換到 images 資料夾, 並建立**名稱資料夾**。此範例為 Chuan、Teddy 兩個人: ![](https://i.imgur.com/TAx0vMK.png) 3. 在名稱資料夾內各自**放入 3 張對應的圖片**。圖片名稱一定要各自命名為 1、2、3 (圖片格式限制為 jpg 和 png): **Chuan 資料夾** ![](https://i.imgur.com/5WnHETw.png) **Teddy 資料夾** ![](https://i.imgur.com/Xah1jOZ.png) :::info 我們提供的範例網頁中, 只會讀取檔名為 1、2、3 的圖片, 所以可以多放圖片(程式不會讀取到), 不能少放。如果想要網頁多讀取一些圖片, 可以到 door_lock_iphone 資料夾裡的 script.js 內 (184 行) 更改: <br> ```=184 for (let i = 1; i <= 3; i++) { ``` ::: ## 上傳網頁資料夾 1. 連上 [GitHub 網站](https://github.com/), 切換到第 8 章建立的倉庫, 並點選 **Add file/Upload files**: ![](https://i.imgur.com/U6UjIxz.png) 2. 將 **door_lock_iphone 資料夾** 拖曳至 GitHub: ![](https://i.imgur.com/kUyYcVB.png) 3. 等待檔案全部上傳完畢後, 按下 **Commit changes**: ![](https://i.imgur.com/WceIgzH.png) 4. 即可看到 **door_lock_iphone 資料夾**: ![](https://i.imgur.com/7ETVvQ8.png) ## 開啟網頁 1. 在 Chrome 瀏覽器輸入以下網址開啟網頁: ``` https://使用者名稱.github.io/door_lock_iphone/index.html ``` :::success 使用者名稱是 **GitHub 名稱**, 例如筆者的 GitHub 名稱是 **flagweng**, 網址就會是: <br> ``` https://flagweng.github.io/door_lock_iphone/index.html ``` ::: ![](https://i.imgur.com/fOgWZm3.png) 2. 輸入**前面建立放圖片的資料夾名稱**, 如果有多個資料夾請以**英文逗號隔開**, 輸入完畢後按下**確定**: ![](https://i.imgur.com/UzGGZfc.png) 3. 輸入 **AIO 使用者名稱**。並按下**確定**: ![](https://i.imgur.com/1QD4yIj.png) 4. 點選**允選**: ![](https://i.imgur.com/u2BoRce.png) 5. 稍等一下即可看到畫面: ![](https://i.imgur.com/E0MOvJJ.png) 6. 輸入 **AIO 的金鑰**, 並按下**辨識按鈕**: ![](https://i.imgur.com/HVUX24t.png) 7. 切換到 Adafruit IO, 點選剛剛建立的 door。如果有成功辨識, 則會出現資料: ![](https://i.imgur.com/eAwCACa.png) ## 執行程式 現在已經將資料上傳到 AIO 平台, 接著使用 ESP32 取得資料: ```python= from umqtt.robust import MQTTClient # mqtt函式庫 from servo import Servo from machine import Pin import math import network import time # 名稱列表 name_list = ["Teddy","Chuan"] # 建立伺服馬達物件 my_servo = Servo(Pin(22)) # 連線至無線網路 sta=network.WLAN(network.STA_IF) sta.active(True) sta.connect('無線網路名稱','無線網路密碼') while not sta.isconnected() : pass print('Wi-Fi連線成功') # MQTT 參數 mqtt_client_id = 'door' # 用戶端識別名稱(可以隨意取名) AIO_URL = 'io.adafruit.com' # 主機網址 AIO_USERNAME = '請填入 Adafruit IO 使用者名稱' # 帳戶名稱 AIO_KEY = '請填入 Adafruit IO 金鑰' # 金鑰 client = MQTTClient(client_id=mqtt_client_id, # 用戶端識別名稱 server=AIO_URL, # 中介伺服器網址 user=AIO_USERNAME, # 帳戶名稱 password=AIO_KEY) # 金鑰 # 連線至 MQTT 伺服器 client.connect() print('MQTT連線成功') # 從 MQTT 伺服器獲得資料 def get_cmd(topic,msg): face = float(msg) name_index = int(math.ceil(face))-1 dis = face-name_index if((name_index >= 0) and (dis < 0.4)): print(name_list[name_index],dis) print("開啟") my_servo.write_angle(0) time.sleep(3) my_servo.write_angle(90) else: print("我不認識你!!") client.set_callback(get_cmd) client.subscribe(str.encode(AIO_USERNAME)+b"/feeds/door") while True: # 確定是否有新資料 client.check_msg() ``` :::info 第 9 行:更改為**自己的名稱列表**。要與網頁上輸入的**順序相同**。 第 17 行:輸入**無線基地台名稱、密碼**。 第 25-26 行:輸入 **AIO 使用者名稱、密碼**。 ::: ## 測試程式 執行程式後, 如果 ESP32 連上**指定基地台**並**訂閱 MQTT** 則會出現以下畫面: ![](https://i.imgur.com/laAZkaD.png) 接下來就可以使用手機辨識: ![](https://i.imgur.com/wj9G0HG.png) 只要辨識到**名稱正確**且**差距小於 0.4**, 伺服馬達就會轉動。相反的, 只要其中一個條件沒達成, 互動環境就會顯示『**我不認識你!!**』。

    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