Jump to: navigation, search

AutomaticCameraControl

Revision as of 20:37, 24 May 2016 by Brentp (Talk | contribs) (Optional)

GoPro WiFi control

Introduction

Lua Script

The following Lua Script enables WiFi control of your GoPro camera.

Customization

You will need to edit the following fields at the minimum, located at the top of the script

Required

  • GoPro WiFi password: Specify the WiFi password used for the GoPro WiFi adapter
  • GoPro SSID: Specify the GoPro WiFi network name (SSID)

Optional

  • Start trigger Speed: Change this if you want it different from the default start trigger.
  • Stop trigger Speed: Change this if you want it different from the default stop trigger. Ensure this is less than the start trigger
--Specify your GoPro wifi password here
goproPwd = '12345678'
--Specify your GoPro SSID here
goproSsid = 'aslhero1'
--Speed threshold to start recording
goproStart = 10
--Speed threshold to stop recording
goproStop = 5
--How fast we check, in Hz
tickRate = 10
--Set this to 1 to log communications between RCP & WiFi
debug = 0
-----------------------------
--DO NOT EDIT BELOW
-----------------------------
--the serial port where the WiFi is connected
port = 4
--indicates wifiStatus
--0 = not init, 1 = init sent, 2 = got IP, 3 = ready
wifiStatus = 0
lastInitTime = 0
initTimeout = 20000

function logMsg(msg)
  println('[GoProWiFi] ' ..msg)
end

function sendCrlf()
  writeCSer(port, 13)
  writeCSer(port, 10)
end

function sendRaw(val)
  for i=1, #val do
    local c = string.sub(val, i, i)
    writeCSer(port, string.byte(c))
  end
end

function sendAt(val)
  if debug == 1 then logMsg('send: ' ..val) end
  sendRaw(val)
  sendCrlf()
end

function toInt(val)
  return string.sub(val, 1, -3)
end

function httpGet(url)
  sendAt('AT+CIPSTART="TCP","10.5.5.9",80')
  sleep(500)
  local crlf = string.char(13) ..string.char(10)
  local get = 'GET ' ..url ..' HTTP/1.0' ..crlf ..crlf
  sendAt('AT+CIPSEND=' ..toInt(#get))
  sleep(100)
  sendRaw(get)
  sleep(100)
  sendAt("AT+CIPCLOSE") 
end

function sendGoProShutter(cmd)
  httpGet('/bacpac/SH?t=' ..goproPwd ..'&p=%' ..cmd)
end

function startGoPro()
  logMsg('start GoPro')
  sendGoProShutter('01')    
end

function stopGoPro()
  logMsg('stop GoPro')
  sendGoProShutter('00')    
end

recording = 0

function initWiFi()
  logMsg('initializing')
  sendAt('AT+RST')
  sleep(2000)
  sendAt('AT+CWMODE_CUR=1')
  sleep(1000)
  sendAt('AT+CWJAP_CUR="' ..goproSsid ..'","' ..goproPwd ..'"')
  wifiStatus = 1
end

function processIncoming()
  local line = readSer(port, 100)
  if line ~= '' and debug == 1 then print(line) end
  if string.match(line, 'WIFI GOT IP') then 
    wifiStatus = 2
  end
  if wifiStatus == 2 and string.match(line, 'OK') then
    wifiStatus = 3
    logMsg('ready for GoPro')
  end
end

function checkGoPro()
  if wifiStatus == 0 then
    initWiFi()
    lastInitTime = getUptime()
    return
  end
  if wifiStatus == 1 and getUptime() > lastInitTime + initTimeout then
    logMsg('could not connect to GoPro')
    wifiStatus = 0
  end
  processIncoming()
  if wifiStatus ~= 3 then
    return
  end
  trigger = getGpsSpeed()

  if recording == 0 and trigger > goproStart then
    startGoPro()
    recording = 1
  end
  if recording == 1 and trigger < goproStop then
    stopGoPro()
    recording = 0
  end 
end

function onTick()
  checkGoPro()
end

setTickRate(tickRate)