新人資訊
技術-開發板 #4-用 Arduino 呼叫 REST Api
前三回合完成抓 sensor 資料,顯示小條 LCD 螢幕,連上 wifi 熱點,傳送資料到 MQTT,都進行得很順利,沒想到這一次玩 http client 欲連 REST api 卻讓我吃足苦頭,到處踩坑危機四伏!對於這種小板子而言 http 已經算是很複雜的協議,尤其又加上 https 的講究,雲端伺服器環境整個安全性要求都持續而全面的提升中,可隨意呼叫的伺服器越來越少!但掙扎一天後中還是搞定了,開始來記錄這段過程。
先測試最簡單的 GET,首先尋找可用的函示庫,宣告必要的總體變數,找一個公開且支援 http 的伺服器網址 (現在已經很難找了,大部分都限制 https 了)。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在 setup() 區塊必須完成 wifi 的連接,這部分可參閱 第二篇,然後 loop() 程式如下,一切就很正常的跑出來了。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
接下來測試一個 https 的網站
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
想當然爾一定錯誤的,但跳出 -5 錯誤碼就不知甚麼鬼了,也不是標準的 http 狀態碼,開始進入折磨的階段。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
找到一個新的函示庫,在相對位置處做取代。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
還好這次沒有卡很久,已經成功抓到 https server 的回傳資料。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
這樣看來已經沒問題了,那就改成我自己的 server 吧,前一陣子所玩的 azure function app 現在可以串起來了,試著把網址改成如下:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
結果又是一個讓人莫名奇妙的錯誤碼!再度確認此網址用 browser 是可以的。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
撞牆一陣子之後想起 https 也有版本之分,會不會是這個問題?到雲端先把板本改到較舊的 TLS 1.0
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
終於成功了。後來反覆測試,這個 client 端只支援到 TLS 1.0 版,伺服器端若要求 1.1 版以上的安全限制,目前就只能望之興嘆了。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
既然 GET() 成功了,那 POST() 也需要測一下,以下參數改成用 POST 夾帶。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
結果一樣可以跑出預期的結果。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
到此開發板與雲端的溝通方法已經搞定 MQTT and Http Get Post 大致上已經夠用來建一個簡單的系統,接下來來想應用吧,同時也要解決電源供應問題。
By Newman Chen 2022/4/12
參考資料
https://zhuanlan.zhihu.com/p/113310435
https://maakbaas.com/esp8266-iot-framework/logs/https-requests/
https://randomnerdtutorials.com/esp8266-nodemcu-http-get-post-arduino/