AutomaticCameraControl

Revision as of 15:30, 25 May 2016 by Brentp (talk | contribs)

GoPro WiFi control

GoProCameraControl RaceCapture.jpg

Introduction

This guide shows you how to enable automatic start / stop control of WiFi enabled GoPro cameras using RaceCapture/Pro.

How it works

When WiFi is enabled on your GoPro camera it creates a network that other devices can connect to. RaceCapture/Pro with the optional WiFi module can connect to this network and issue the appropriate commands to control various camera functions - in this case, starting and stopping video recording.

Starting and stopping the GoPro can controlled by a variety of means: speed, RPM or based on other sensor data, enabled by a Lua Script running on RaceCapture/Pro.

This guide focuses on using GPS speed to trigger the camera; for triggering on other sensors see the Lua scripting guide as a reference for what other sensor data is available.

Requirements

  • RaceCapture/Pro MK2 running firmware 2.9.1 or higher
  • RaceCapture/Pro WiFi module
  • WiFi enabled GoPro Hero camera, with WiFi activated
  • GPS Connectivity (for speed measurement)

Steps

Upgrade your firmware

Upgrade to the latest firmware if necessary. This guide was developed with firmware version 2.9.1.

Configure your GoPro

Using the GoPro app, configure the WiFi network, making a note of the network name (SSID) and password.

Connect the WiFi module

Connect the RaceCapture/Pro WiFi module to the outermost port of RaceCapture/Pro. When powered up, you should see the power LED illuminate.

Enable Lua script for GoPro connectivity

Customize the Lua script shown at the bottom with the GoPro's WiFi network name (SSID) and password, then copy and paste the Lua script shown at the bottom into the scripting window of your RaceCapture/Pro.

Steps:

  • Connect RaceCapture/Pro to the RaceCapture app and read the current configuration.
  • Copy the Lua script into the Scripting window:
    • Ensure the script window is first blank by removing the default script by deleting the text in the window.
      • Note: If you have an existing script you want to keep, you will need to blend the camera functionality with your existing script. How to do this is beyond the scope of this guide; see the Lua Scripting Guide for more information.
    • Copy the script into the window by using the ctrl-v keyboard shortcut.
  • Write the configuration back to RaceCapture/Pro.

Gopro script.png

Bench testing

Once the Lua script is written to RaceCapture/Pro, it will attempt to connect to the network created by the GoPro camera. Once connected, it will be ready to issue start / stop commands to the camera.

Before road testing, ensure RaceCapture/Pro can connect to the GoPro's WiFi network by observing the messages in the window below the script.

  • Tip 1: Touch or click the Poll Log to see the current message in the RaceCapture/Pro log
  • Tip 2: To restart the Lua script, touch or click the 'Restart' button.

Road testing

Mount the GoPro in the car as usual and power up RaceCapture/Pro, waiting for GPS achieve a lock.

  • Drive carefully above the speed threshold and observe the GoPro - you should hear and see the familiar beep and red light activate, just as if you manually started recording using the shutter button on the camera.
  • When you drop below the threshold, the GoPro should stop recording.

As RaceCapture/Pro starts and stops the camera, you will see the following information in the log:

[GoProWiFi] start GoPro
[GoProWiFi] stop GoPro

Troubleshooting

If RaceCapture/Pro cannot control the camera, check the following:

  • GoPro WiFi network name (SSID) and password: Ensure the name and password currently in the script matches how your GoPro was configured.
  • Enable WiFi on the GoPro: The blue LED on the GoPro should be blinking periodically
  • RaceCapture/Pro WiFi module connection: Ensure the module is plugged into the outermost RJ11 port and the green LED is illuminated.
  • Verify WiFi connection: Ensure RaceCapture/Pro can connect to the GoPro WiFi network. You should see the following messages in the log upon power-up:
[lua] Successfully loaded script.
[GoProWiFi] initializing
[GoProWiFi] ready for GoPro

If RaceCapture/Pro cannot connect to the GoPro's network you will see this in the log:

[GoProWiFi] initializing
[GoProWiFi] could not connect to GoPro
[GoProWiFi] initializing
[GoProWiFi] could not connect to GoPro
[GoProWiFi] initializing
[GoProWiFi] could not connect to GoPro
[GoProWiFi] initializing

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

  • goproStart: Change this if you want to start recording at a different threshold.
  • goproStop: Change this if you want to stop recording at a different threshold. Ensure this is less than the start trigger
  • tickRate: Adjust this to update how often you want RaceCapture/Pro to perform a start/stop check. Value is in Hz. If uncertain, leave this alone
  • debug: If things aren't working as expected you can set this to 1 to see the conversation between the WiFi module and the GoPro.
    • This information shows up in the log window below the script window - check the box Poll log to see this information.
--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)