Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Lua FTP upload issues

Avatar of travisryan
travisryanFlag for United States of America asked on
Scripting LanguagesFTP* Toshiba* Camera* 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
Avatar of David Johnson, CD
David Johnson, CDFlag of Canada imageThe More I know, the more I don't know
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answers