Hong En
    • 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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Lab16 Communicate with IoT Platform ###### tags: `2024工業物聯網` 助教:睿翔 # IoT Platform 使用說明 (ThingSpeak) ThingSpeak是一個提供物聯網分析平台服務的網站,讓你能夠在雲端收集、可視化和分析實時的資料。此外,ThingSpeak也能即時地將你的設備發佈到它的平台上進行數據的可視化。 ![](https://i.imgur.com/VvGCu7V.png) ### 點擊連結註冊並且登入 https://thingspeak.com/ <font color='red'>請使用學校Email註冊</font> ![](https://i.imgur.com/cx7UvXy.jpg) ![](https://i.imgur.com/vhW21yf.jpg) ![](https://i.imgur.com/4ok3cOp.jpg) ## 樹莓派串接 IoT Platform 即時監測實例 (DHT11) ### 新增channel ![](https://i.imgur.com/WD5aDFw.jpg) ![](https://i.imgur.com/bSfflbb.jpg) ![](https://i.imgur.com/OLfA1HY.jpg) ![](https://i.imgur.com/lrCKqXY.jpg) ### 查看API Key ![](https://i.imgur.com/ZZJJbIX.jpg) ## IoT Platform 溝通方式 (HTTP request) ![](https://i.imgur.com/jopRr3E.jpg) | 方法 | 目標 | 說明 | HTTP 回應狀態碼 | | ------ | ------------ | ----------------------------------------------------------------------------------- | --------------- | | GET | 單個資源 URL | 取得資源 | 200 | | GET | 資源集合 URL | 取得資源集合(或是如果伺服器有進行分頁,取得它的一頁) | 200 | | POST | 資源集合 URL | 建立一個新資源並將它加入集合,伺服器會選擇新資源的 URL,在回應的Location 標頭回傳它 | 201 | | PUT | 單個資源 URL | 修改既有的資源,或者當用戶端可以選擇資源 URL 時,這個方法也可以用來建立新資源 | 200 or 204 | | DELETE | 單個資源 URL | 刪除一個資源 | 200 or 204 | | DELETE | 資源集合 URL | 刪除集合的所有資源 | 200 or 204 | 延伸資料: [HTTP request methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) [淺談 HTTP Method:表單中的 GET 與 POST 有什麼差別?](https://blog.toright.com/posts/1203/%E6%B7%BA%E8%AB%87-http-method%EF%BC%9A%E8%A1%A8%E5%96%AE%E4%B8%AD%E7%9A%84-get-%E8%88%87-post-%E6%9C%89%E4%BB%80%E9%BA%BC%E5%B7%AE%E5%88%A5%EF%BC%9F.html) 參考資料: [Flask 網頁開發](https://www.books.com.tw/products/0010793455) ## 關鍵程式碼片段 由於 mini conda 把樹莓派的 python 版本改為 3.4,強烈建議將樹莓派重灌以徹底移除 conda 記得先備份程式碼!!! 請參考[Lab03 DHT11](https://hackmd.io/@MCAS/IIoT_Lab03),修改時請改為 'BCM2711' __將資料透過 HTTP request 發送至 ThinkSpeak__ ```python= # 改成你創建的 ThingSpeak channel 所顯示的 <Write-API-Key> ThingSpeak_WriteApiKey = '3HVRLATG7INCEH2U' # 每 `detection_period` 秒偵測一次 detection_period = 5 # 取得目前的溫濕度 humidity, temperature = get_sensor_data() # 若有讀取到資料就轉成格式化字串型態,並透過 API key 以 GET request 方式上傳至 ThinkSpeak if humidity is not None and temperature is not None: # 若 `DHT.read_retry` 失敗,會回傳 None ThingSpeak_API_URL = f'https://api.thingspeak.com/update?api_key={ThingSpeak_WriteApiKey}&' \ f'field1={"{0:0.1f}".format(temperature)}&' \ f'field2={"{0:0.1f}".format(humidity)}' query = requests.get(ThingSpeak_API_URL) print(f'HTTP: {query.status_code}') # 查看 HTTP 狀態碼,若為 200 表示傳送成功 print(now+',', 'temperature:', temperature, '度C,', 'humidity:', humidity, '%') # 標準輸出溫溼度於本地端 else: # 感測器讀取失敗就立即重讀 print('Failed to get reading. Try again!') continue # 上傳完數據之後就暫停 `detection_period` 秒 time.sleep(detection_period) ``` ## 輸出範例 __命令列標準輸出範例__ ![](https://i.imgur.com/yn6OoNE.png) __ThinkSpeak 監控畫面__ ![](https://i.imgur.com/ayrcCwF.png) # 本週作業說明與繳交規範 1. 根據前述關鍵程式碼的教學,自行更改 Lab2 DHT11 的程式碼,完成下列要求: * 使用 while loop 使程式能持續監測 (監測頻率自行設定) * 程式要可以使用 Keyboard Interrupt (Ctrl+C) 終止程式 * 透過 HTTP GET request 上傳資料至 ThingSpeak 2. 作業繳交規範 * 每組繳交一份壓縮檔即可,檔名請命名為「groupX_lab06」 * 壓縮檔請包含 程式碼檔案 (.py檔) 與 實作影片檔 * 實作影片需拍攝樹莓派連接DHT11的電路接法、程式運作畫面 (命令列標準輸出)、ThinkSpeak 即時監測畫面 3. 繳交期限:05/14 23:59

    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