troubleshooting Question

Lua FTP upload issues

Avatar of travisryan
travisryanFlag for United States of America asked on
FTPScripting Languages* Camera* Toshiba* Lua
6 Comments1 Solution1426 ViewsLast Modified:
We have a camera with a Toshiba Flash Air card and we are trying to upload pictures via FTP to a network drive with a lua script running on the card.

Our Products:
Canon PowerShot GLPH17015
FlashAir W-03, 16GB running latest firmware
Windows Network

Here is where I pulled the original code from:
https://bitbucket.org/harbortronics/flashair-ftp-upload/downloads/

The card is working, I was able to not only plug it into my computer and view files, but also my iPhone with an OTG adapter.

For the most part, this script is based on 3 files: CONFIG, ftpupload.lua, and settings.lua

The config file is where most of the work happens in the foreground, here is my CONFIG:
**********
[Vendor]

CIPATH=/DCIM/100__TSB/FA000001.JPG
APPNAME=flashair
APPMODE=5
APPSSID=*WIRELESS NETWORK*
APPNETWORKKEY=*WIRELESS NETWORK PASSWORD*
VERSION=FA9CAW3AW3.00.01
CID=02544d535731364740d03b5479010601
PRODUCT=FlashAir
VENDOR=TOSHIBA


MASTERCODE=accf856bbe74
LOCK=1

UPLOAD=1
STA_RETRY_CT=0
LUA_SD_EVENT=/ftpupload.lua
**********

As you can see, the CONFIG points to the ftpupload.lua script, so here is my script:
**********
require("Settings")

local ftpstring = "ftp://"..user..":"..passwd.."@"..server.."/"..serverDir.."/"
local lockdir = "/uploaded/"   -- trailing slash required, and folder must already exist

function exists(path)
    if lfs.attributes(path) then
        return true
    else
        return false
    end
end

function is_uploaded(path)
    local hash = fa.hash("md5", path, "")
    return exists(lockdir .. hash)
end

function set_uploaded(path)
    local hash = fa.hash("md5", path, "")
    local file = io.open(lockdir .. hash, "w")
    file:close()
end

function delete(path)
    -- Both of the following methods cause the next photo to be lost / not stored.
    fa.remove(path)
    -- fa.request("http://127.0.0.1/upload.cgi?DEL="..path)
end

function upload_file(folder, file)
    local path = folder .. "/" .. file
    -- Open the log file
    local outfile = io.open(logfile, "a")
    outfile:write(file .. " ... ")
    local response = fa.ftp("put", ftpstring..file, path)

    --Check to see if it worked, and log the result!
    if response ~= nil then
        print("Success!")
        outfile:write(" Success!\n")
        set_uploaded(path)
        if delete_after_upload == true then
            print("Deleting " .. file)
            outfile:write("Deleting " .. file .. "\n")
            sleep(1000)
            delete(path)
            sleep(1000)
        end
    else
        print(" Fail ")
        outfile:write(" Fail\n")
    end
    --Close our log file
    outfile:close()
end

function walk_directory(folder)
    -- Recursively iterate filesystem
    for file in lfs.dir(folder) do
        local path = folder .. "/" .. file
        local attr = lfs.attributes(path)
        print( "Found "..attr.mode..": " .. path )

        if attr.mode == "file" then
            if not is_uploaded(path) then
                upload_file(folder, file)
            else
                print(path .. " previously uploaded, skipping")
            end
        elseif attr.mode == "directory" then
            print("Entering " .. path)
            walk_directory(path)
        end
    end
end

-- wait for wifi to connect
while string.sub(fa.ReadStatusReg(),13,13) ~= "a" do
    print("Wifi not connected. Waiting...")
    sleep(1000)
end
walk_directory(folder)
**********

And here is the settings.lua code for the ftpupload.lua script:
-- FTP Settings --
logfile   = "/FTPLog.txt"     -- Where to log output on the FA
folder    = "/DCIM"           -- What folder to upload files from
server    = "qualitypic.example.com"                -- The FTP server's IP
serverDir = "/"                -- The path on the FTP server to use.
user      = "FAupload"                -- FTP username
passwd    = "********"                -- FTP password
delete_after_upload = false
ASKER CERTIFIED SOLUTION
David Johnson, CD
The More I know, the more I don't know
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros