jollen / blog

Jollen's Blog
http://www.jollen.org/blog
66 stars 4 forks source link

如何用 Wio Link 快速自製 GoPro Remote #6

Closed jollen closed 8 years ago

jollen commented 8 years ago

這次在「LinkIt Smart 7688 與 Wio Link(ESP8266)技術沙龍」活動上,閃電展示了如何使用 Wio Link 自製 GoPro 的 Remote 控制器,整個過程只需要大約 30 分鐘,以下分享製作方法。

實作原理

本專案的技術原理非常簡單:

本文使用 Wio Link 或 NodeMCU 開發板,並以 Lua 來撰寫 HTTP request 程式碼。如果改用 Node.js 來實作 HTTP request,就可以改用近期火紅的 MediaTek LinkIt Smart 7688 (Duo) 開發板來製作 GoPro 控制器。

Step 1: 材料準備

下載 GoPro Studio 安裝後,將 GoPro 連接到電腦。GoPro Studio 會自動更新 GoPro firmware。

Step 3: 更新 Wio Link Firmware

使用 NodeMCU 的開發者可忽略這個步驟。

Wio Link 預載 Seeed Stduio 專門打造的 firmware,可搭配 Wio Link App[1] 快速打造 IoT application。在這個小專題裡,我們會使用 Lua 來撰寫簡單的 GoPro 控制器,因此要更換為 NodeMCU firmware。

更新 NodeMCU firmware 的詳細步驟,請參考(ESP8266 & NodeMCU 開發入門 (Part 5) - 編譯並更新 NodeMCU Firmware)[https://wotcity.com/blog/2015/11/13/esp8266-nodemcu-iot-starter-part-5/]。

Step 4: 連接 GPIO Button

如圖 1,將 Grove Kit 的 Button 接到 Wio Link 的 Digital 插糟。以圖 1 為例,Button 將經由 GPIO14 來控制,後續撰寫程式時,須將 GPIO14 設定為中斷觸發。

wio-link-gopro-1 圖 1:連接 GPIO Button

Step 5: 認識 Wio Link GPIOs

Wio Link 設計了 3 個 Digital 插糟,每個插糟上都有 2 根 GPIO 腳位。以上圖為例,觀察黃色接線,可以看出,Grove Kit 的 Button 是經由 GPIO14 來控制。Wio Link 的 GPIO 腳位,相容於 NodeMCU,因此應用上可視為 NodeMCU 的替代品。

Wio Link 腳位名稱 NodeMCU 腳位名稱 Note
GPIO-13 GPIO-13 D7
GPIO-12 GPIO-12 D6
GPIO-14 GPIO-14 D5

以圖 1 為例,撰寫 Lua 程式將 GPIO-14(D5)設定為中斷觸發模式:

pin = 5
gpio.mode(pin, gpio.INT)

再定義中斷觸發的 callback function:

--set the interrupt callback function
gpio.trig(pin, "both", button)

對 Wio Link 與 NodeMCU 的 Lua 開發環境不熟悉的話,可以參考以下文章:

WoT.City 提供的ESP8266 & NodeMCU 開發入門系列文章,可做為初學者的自學教材。

Step 4: 撰寫 Lua 程式

將以下程式碼上傳至 Wio Link:

--
-- GoPro Remote Simple
-- See: https://github.com/jollen/blog/issues/6
--

-- Configs
host = "10.5.5.9"
port = 80
ssid = "yiigopro"
pass = "moko0721"
pin = 5
api = "/gp/gpControl/command/shutter?p=1"

-- Configure the ESP as a station (client)
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid, pass)
wifi.sta.autoconnect(1)

-- Create a TCP socket
sk = net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) 
  print(c)
end)

-- Set GPIO
gpio.mode(pin, gpio.INT)

-- Poll GPIO
now = 0
duration = 0

function button(level)
  duration = tmr.now()-now
  print(duration)
  now = tmr.now()

  if level == 1 then 
    print("down")
    sk:send(headers)
  else 
    print("up")
  end
end

--set the interrupt callback function
gpio.trig(pin, "both", button)

-- Print IP address
local ip = wifi.sta.getip()

-- Make HTTP request headers
if (ip == nil) then
  ip = "localhost"
end
headers = "GET " .. api .. " HTTP/1.1\r\nHost: " .. ip .. "\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"
print(headers)

-- Connect to GoPro host
sk:connect(port, host)

可將程式碼儲存為 init.lua 後上傳至 Wio Link。Wio Link 開機時,會自動執行 init.lua

Step 5: 開啟 GoPro Wifi 並進行測試

  1. 開啟 GoPro Wifi
  2. 將 GoPro 設定為拍照模式
  3. 上傳上述程式碼至 Wio Link
  4. 按下 Button 拍照

實境測試影片如下。

網路資源

[1]: Wio Link App, https://itunes.apple.com/tw/app/wio-link/id1054893491?mt=8

jollen commented 8 years ago

其它相關資源