Skip to content

Instantly share code, notes, and snippets.

@kwbr
Forked from digitaltrails/olyWifiSync.sh
Created April 20, 2024 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwbr/617ada62aab82c513b275149607d3b42 to your computer and use it in GitHub Desktop.
Save kwbr/617ada62aab82c513b275149607d3b42 to your computer and use it in GitHub Desktop.
Olympus camera WIFI download/mirror script
#!/bin/bash
##
## olyWifiSync - Mirror Olympus camera via WIFI
##
## Usage: olyWifiSync [-d|-n|-f|-p|-h] ESSID PASSWORD [wifidevice]
## -d download only, don't try to configure the WIFI device)
## -n no disconnect, do not disconnect the WIFI on completion
## -f full download, do not skip over files that already exist
## -p power off camera at end of transfer
## -h help
##
## Examples:
## olyWifiSync E-M5II-P-XYZZY123 12341234 wlan0
##
## Mirrors SD contents to $HOME/SD-Mirror/$CAMERA_NET_ESSID
## Only copies files they are not already in the destination folder.
## The ESSID and PASSWORD are those displayed on the camera LCD
## when wifi is enabled. Defaults to wlan0.
##
## Requires full root sudo access if configuring the WIFI device.
## Wifidevice defaults to wlan0. Pass -d to skip WIFI configuration,
## in which case root is not required.
##
## Limitations: Sometimes wpa_supplicant can fail to exit or just be
## cranky - if this occurs, try sudo killall wpa_supplicant (but only
## if you have one WIFI device). The test of file existence is just
## by name match - resetting the numbering sequence would defeat this
## simple approach. The camera can timeout if there are too many
## files to transfer. The camera sometimes seems to stop responding
## over the wifi and its wifi will have to be restarted. Needs more
## error handling and error trapping.
##
## Notes: to find out what commands a camera supports do:
## wget -O cmdlist.txt http://192.168.0.10/get_commandlist.cgi
## (see https://www.dpreview.com/forums/post/57165274 )
## Originally based on reading the C++ code at:
## https://sourceforge.net/projects/wifi-downloader-for-oly-e-m1/
## that project looks like a good GUI if you don't need WIFI
## to be configured. This script was written on OpenSUSE 42.3,
## but should run elsewhere too.
##
## MIT License
##
## Copyright (c) 2018 Michael Hamilton
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in all
## copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.Copyright 2018 Michael Hamilton
##
DOWNLOAD_ONLY=N
NO_DISCONNECT=N
FULL_DOWNLOAD=N
POWER_OFF_CAMERA=N
while [[ "$1" =~ ^-[dhnpf] ]]
do
[ "$1" == "-d" ] && DOWNLOAD_ONLY=Y
[ "$1" == "-n" ] && NO_DISCONNECT=Y
[ "$1" == "-f" ] && FULL_DOWNLOAD=Y
[ "$1" == "-p" ] && POWER_OFF_CAMERA=Y
[ "$1" == "-h" ] && egrep "^##" $0 && exit 0
shift
done
CAMERA_NET_ESSID=${1:-E-M1MKII-P-DefaultSerialNumber}
CAMERA_NET_PASSWORD=${2:-DefaultWifiPassword}
CAMERA_NET_PREFIX=192.168.0
CAMERA_NET_CAMERA_ADDRESS=$CAMERA_NET_PREFIX.10
CAMERA_NET_PC_ADDRESS=$CAMERA_NET_PREFIX.5
CAMERA_NET_DEVICE=${3:-wlan0}
MIRROR_DIR="$HOME/SD-mirror/$CAMERA_NET_ESSID"
WPA_CTL=/tmp/wpa_ctl_$CAMERA_NET_ESSID
WPA_PID_FILE=/tmp/wpa_pid_$CAMERA_NET_ESSID
function download {
if ! ping -c 1 $CAMERA_NET_CAMERA_ADDRESS
then
echo "ERROR: no device responding at IP address CAMERA_NET_CAMERA_ADDRESS"
return 1
fi
echo "INFO: mirroring to $MIRROR_DIR"
mkdir -p $MIRROR_DIR
url="http://$CAMERA_NET_CAMERA_ADDRESS/get_imglist.cgi?DIR=/DCIM/100OLYMP"
echo "INFO: retrieving file list via $url"
downloadCount=0
wget -q "$url" -O - | while read line
do
IFS=, read dir filename rest <<< $line
if [ -n "$filename" ]
then
if [ $FULL_DOWNLOAD == "N" -a -f $MIRROR_DIR/$filename ]
then
echo "WARN: skipping existing file: $filename"
else
url="http://$CAMERA_NET_CAMERA_ADDRESS/DCIM/100OLYMP/$filename"
echo "INFO: downloading $filename"
wget -O $MIRROR_DIR/$filename "$url" || return
downloadCount=$[downloadCount + 1]
fi
fi
done
echo "INFO: downloaded $downloadCount files"
if [ $POWER_OFF_CAMERA == "Y" ]
then
url="http://$CAMERA_NET_CAMERA_ADDRESS/exec_pwoff.cgi"
wget "$url" -O -
fi
}
function connect_wifi {
[ $DOWNLOAD_ONLY == "Y" ] && return 0
# Do Linux WIFI configuration for a WIFI device dedicated to the camera.
sudo ifconfig $CAMERA_NET_DEVICE up
sleep 2
if sudo iwlist $CAMERA_NET_DEVICE scan | egrep "ESSID:\"$CAMERA_NET_ESSID\""
then
sudo bash -c "wpa_supplicant -B -g $WPA_CTL -P $WPA_PID_FILE -i $CAMERA_NET_DEVICE -c <(/usr/sbin/wpa_passphrase $CAMERA_NET_ESSID $CAMERA_NET_PASSWORD)"
sleep 2
sudo ifconfig $CAMERA_NET_DEVICE $CAMERA_NET_PC_ADDRESS netmask 255.255.255.0
sleep 2
else
echo "ERROR: failed to connect to WIFI for $CAMERA_NET_DEVICE $CAMERA_NET_ESSID" && return 1
fi
}
function disconnect_wifi {
[ $DOWNLOAD_ONLY == "Y" ] && return
[ $NO_DISCONNECT == "Y" ] && return
sudo ifconfig $CAMERA_NET_DEVICE down
[ -f $WPA_CTL ] && sudo wpa_cli -g $WPA_CTL terminate
sleep 2
sudo kill `cat $WPA_PID_FILE`
sudo rm $WPA_CTL $WPA_PID_FILE
}
connect_wifi && download && disconnect_wifi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment