Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to transfer data to PC

edited December 2012 in Examples Posts: 2

Hi, I'm new to Codea
I would like to write a simple program to collect a fair amount of accelerometer data and analyze the data on a PC using Excel or other tools.
Would someone point me to an example on how to collect data and more importantly how to transfer data to PC?
Thanks

Comments

  • edited December 2012 Posts: 688

    The easiest way is to save the data locally in the file format of your choice and then use one of several apps (both available for PC and Mac) like FunBox, or iExplore to connect your iPad to your computer, you then have to browse the app directory structure, find the Codea folder, inside that find the .codea folder and inside that you'll find the local documents folder where 's data files are stored.

    For later analysis on the PC using Excel, you might want to save your data as a CSV (each value separated by a comma) file.

    Hope this helps.

  • BortelsBortels Mod
    Posts: 1,557

    Other, slightly more difficult alternatives:

    1) Jailbreak, then use normal unix copy commands - rsync is my favorite. This has the advantage of being able to be done untethered.

    2) Use http.request() and send your data to a website.

    As for how to collect the data - go look at the example programs (if you haven't already) to see how to get the acceleration data; I'd then simply accrue it in a table, or if you're doing (2) above just post it on the fly (depending on how often you want to sample)

  • Jmv38Jmv38 Mod
    Posts: 3,295

    .@bortels could you be more detailed on how to send a file to a website? I have a web site, but i don't know how to send a file to my site with codea, nor what to do on the sever side...

  • BortelsBortels Mod
    Posts: 1,557

    Heh - I knew someone would ask.

    So - here's a basic skeleton, the minimal amount you'd need to do; and you can build on it. This is running on my ubuntu box at home:


    bortels@duo:/var/www/codea$ ls -al total 16 drwxrwxr-x 2 bortels bortels 4096 Dec 6 10:13 . drwxr-xr-x 46 bortels bortels 4096 Dec 6 10:11 .. -rw-rw-rw- 1 bortels bortels 26 Dec 6 10:13 data.txt -rwxrwxr-x 1 bortels bortels 489 Dec 6 10:12 index.cgi bortels@duo:/var/www/codea$ cat index.cgi #!/usr/bin/perl use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); print header; print start_html; $d = param('data'); if ($d) { print "Data received.\n"; } else { print "no data"; } open (F, ">>data.txt"); print F scalar(localtime) . ' ' . $d . "\n"; close F; print end_html;

    You will likely need to "touch data.txt ; chmod og+w data.txt" to set up the permissions on the data file.

    You can hit this with a browser - go to http://home.bortels.us/codea/data.txt to see the data, and you can hit http://home.bortels.us/codea/?data=Something to add data. Please keep it clean. :-)

    In codea, http.get() the above URL(s), to either read data.txt or to add data.

    In the case of the original post, you could sample your accelerometer data, and do http.get with data=results and it would go to the text file, which you could then import into a spreadsheet or such; change the print statements in the perl script to use a reasonable format.

    I hope this makes sense - it's as simple as I could make it, but I've been doing this for what, 20 years? so I don't know what's clear and what's not. Ask questions if you want.

  • Jmv38Jmv38 Mod
    Posts: 3,295

    Ok... Btsdfg""& hsol snzuenp sksi? Snhuejso lmsp mmmpsjd! Hahaha! Gdbsu,a ghuigh?
    Hem, that's a little bit what your post sound like to me.... (No offense, i'm just an uneducated moron...) what langage is it? 'ubuntu'means linux i guess? Any suggestion for people who know only javascript/html and run their web site on the ipad from textastic...?

  • BortelsBortels Mod
    Posts: 1,557

    Heh - yeah, the real problem is that the above assumes you have some passing familiarity with perl (the language it's in), ubuntu (a linux operating system, although the above should work on basically anything but windows), and some basic sysadmin skills.

    This is actually a fairly suitable situation for a site that was in another thread - take a look at https://www.webscript.io/examples/leaderboard for a semi-complex example (but at least it's in lua!). Modifying this to collect data, and spit it all back out, should be straightforward, and for temporary non-giant amounts of data, you'll be able to use their free usage tier.

    If that's still greek, maybe I'll try to work up the same thing I did above, but on webscript.io as an example.

  • BortelsBortels Mod
    Posts: 1,557

    So here's a very lame first crack using webscript.io:

    d = request.query.data
    if (d) then
         log(d)
         return d, {["Content-Type"]="text/plain"}
    else
         return "Boo!", {["Content-Type"]="text/plain"}
    end
    

    You can go there, make a new script, paste this in, then use the link they give you - for example, mine is http://demo-tthzve.webscript.io/script?data=monkey to log "monkey". You as the owner can then look at the logs.

    You could also do things like save the data to persistent storage, and have a different URL in the same domain spit back out a csv file of the data or such. Again - look at the leaderboard for examples of both saving data and spitting out custom results.

  • Jmv38Jmv38 Mod
    Posts: 3,295

    Thanks for all this interesting info @bortels. I had a look at webscript link: the code is nice and simple, but i have not got the big picture of what they do: they sell webscript.io and/or we have to pay an account to use their code? And this webscript.io is LUA, but is not installed in codea, is it?
    I'll take the time to look at your examples and will do some experiments. Thanks.

  • BortelsBortels Mod
    Posts: 1,557

    They sell it (it's what they call PaaS, Platform as a Service), with a free tier; in a nutshell, you can do what you want for free, but they nuke it after a week. For $5 a month, they'll not nuke it. I don't think it's a great business model, but they seem to like it, and if you don't want to get deep into server-side stuff, it does fit the bill.

    There are other ways to get free server-side code - run your own box like I do, or use google appscript or redhat's new openshift stuff (google for them) - but they tend to be more complex. (I actually really like openshift - it hits the sweet spot between ease and power, and it's what I'd use if I didn't have a server already)

    And yes - that lua code is running on their servers, in response to what your lua code does.

  • Jmv38Jmv38 Mod
    Posts: 3,295

    Ok, thanks.

  • dave1707dave1707 Mod
    Posts: 7,522

    Does anyone know how to decode a PNG image file. I'm able to pack data into an image on the iPad, save it in Dropbox, sync Dropbox with my PC and see the file with a .PNG extension on my PC. If I dump the file with a hex editor, I can find the different chunks of information, but I haven't found anything that says how to decode the actual RGB information. I know the information in the file is correct on my PC. I checked this by loading the PNG file into a photo editor and increased the size of the image until each pixel was a square. I then took the eye dropper to display the RGB values of each pixel. If I start at the lower left pixel of the image and move up then repeat, each pixels RGB value is the value I packed in the image. If I can decode the PNG image, then I can create the file on the PC.

  • Thank you all for the suggestions.
    As a beginner I'll try TechDojo's method first. :)

  • dave1707dave1707 Mod
    edited December 2012 Posts: 7,522

    Continuing from above. Actually, if I save the PNG file as a RAW file using a photo editor, the RAW file has the RGB values for each pixel as individual bytes. The RAW file just needs to be rearranged to match the original file.

  • dave1707dave1707 Mod
    Posts: 7,522

    Continuing my post from above, if I create the image file in the correct order for the RAW file on the PC, then there won't be any reason to convert it at all. The steps would be, create the file as an image on the iPad, save it in Dropbox, sync Dropbox with the PC, load the PNG file into a photo editor, save it as a RAW file, and the RAW file should be the original file on the iPad. The only goofy process will be using the photo editor to convert the file. Sounds good in theory, I'll have to wait and see if it actually works.

  • dave1707dave1707 Mod
    Posts: 7,522

    Continuing again from above. Everything worked out just fine. I was able to create the .PNG file so that the .RAW file being created was in the correct order. No converting of the RAW file on the PC was necessary. The .RAW file can be renamed to .txt or .bin depending on the type. So far, the program will create an ASCII file that's terminated with HEX zeroes, or a HEX file with the file length as the first 3 bytes of the file. To test, I created an ASCII file of random values separated with commas that I imported into Excel without any problems. I'm still cleaning up the program and testing it, but so far everything looks OK. As I said above, the only goofy part is using a photo editor to convert the .PNG file to a .RAW file. But I guess that's no more goofy than having to hook the iPad up to the PC and using a program to find the file on the iPad. So far the steps are, create a table/string of the info you want, pass the string into my program that creates a .PNG file in Dropbox, sync Dropbox with the PC, load the .PNG file into a photo editor, save the file as .RAW and rename the file and extension if necessary.

  • dave1707dave1707 Mod
    edited December 2012 Posts: 7,522

    .. @thirtyeight If you're still looking on how to save acceleration information in a table and to load it into Excel, this might help you. See my other post "Send file to a PC" for more details on what you need to do. As for saving acceleration data, change the interval for how often you want to read data, and change sample for the number of samples you want. In this example, it's set up for a sample every 1/2 second and it will get 10 samples. So you will get 5 seconds of information. If this isn't what you want, maybe it will give you an idea.

    EDIT: I had an error in the table.insert lines, it's been fixed.


    function setup()     info={}     et=ElapsedTime          -- 10 samples at 1/2 second intervals,  5 seconds run time     interval=.5    -- interval in seconds     samples=10     -- number of samples     print("\nstart\n") end function draw()     background(40,40,50)          if samples>0 then         if (ElapsedTime - et) > interval then                          x=UserAcceleration.x             table.insert(info,x..",")                      y=UserAcceleration.y             table.insert(info,y..",")                      z=UserAcceleration.z             table.insert(info,z..",")                      samples = samples - 1                          et=ElapsedTime         end     end           if samples==0 then         createPNG(info)         samples=-1         print("\nfinished")     end end -- this function is all that's needed to create the .png image file function createPNG(strng)    -- pass a string or table of information     local fileName="Dropbox:CodeaFile"    -- file name for .png file     local img1,size      local count=0     local x,y,v1,v2,v3,s1,s2,b1,b2,b3,z     local a1=256; local a2=256*256     local ascii=true          if type(strng)=="table" then         strng=table.concat(strng)    -- convert a table to a string     end          -- check for all ascii or a mix of ascii and hex     size=#strng        for z=1,size do         x=string.byte(strng,z,z)         if x<32 or x>126 then    -- non ascii value             ascii=false         end     end           -- calculate width and height for image size and create img1     x=math.ceil((size+6)/3)     s1=math.ceil(math.sqrt(x))     s2=math.ceil(x/s1)      img1=image(s1,s2)                  -- calculate 3 byte size (b1+b2*256+b3*256*256) for hex file     b1=size; b2=0; b3=0      if b1>=a2 then         b3=math.floor(size/a2)         b1=b1-b3*a2     end     if b1>=a1 then         b2=math.floor(size/a1)         b1=b1-b2*a1     end          -- move string data into r,g,b values and set x,y point of img1     for y=s2,1,-1 do            for x=1,s1 do             if x==1 and y==s2 and not ascii then                 img1:set(1,s2,b1,b2,b3,255)    -- put size in 1st rgb entry of a hex file                          else                 v1=0;v2=0;v3=0                 count = count + 1                 if count<=size then                     v1=string.byte(strng,count,count)                     count = count + 1                     if count<=size then                         v2=string.byte(strng,count,count)                         count = count + 1                         if count<=size then                             v3=string.byte(strng,count,count)                         end                      end                                       img1:set(x,y,v1,v2,v3,255)    --save rgb values per x,y point                 else                     img1:set(x,y,0,0,0,255)    -- fill remaining image with 0's                 end             end         end     end          saveImage(fileName,img1) end
  • Posts: 666

    Two suggestions:
    Save your file in the documents: directory instead of DropBox:. I don't think the DropBox for has a bidirectional sync.
    Create a class that is a vec3 and assign the acceleration data to a class instance, and store that in the table. Makes it easier to manage the data both from a read and write perspective.

  • dave1707dave1707 Mod
    Posts: 7,522

    Hi @aciolino The Documents directory doesn't have a sync option, so saving the file there won't help to sync it with the PC. Also, saving the data as a vec3 probably won't help any either because the .png file is just a stream of bytes. It might save table space on the iPad, but in the .png file each RGB value is just a byte of data. You're correct about the bidirectional sync. That takes care of writing code to send a file from the PC to the iPad using Dropbox sync.

  • Posts: 666

    OIC, I thought that the user was saving it to the iPad and trying to sync to the dropbox system to get it to the PC. That's what I'm doing with my DropBox library, but it's not what was being requested.

  • Posts: 1

    Hi, you can use some tools to collect data, and use iPhone Transfer to transfer CSV/HTML/TXT file to PC.

  • Posts: 103

    @CarlJimmy this thread is almost four years old. In the future it may be more useful to start a new thread rather than resurrecting old ones. We appreciate your enthusiasm, though! Welcome to the forum!

  • Posts: 167

    If you're talking about a Mac, the simplest way to put your code into there is to install Dropbox in your iPad and Mac, have them linked to the same account, and then long press and click export, then you can open it in Dropbox, if you want to view the code in an easier way, install Xcode from the Mac App Store, it's free. If you're using Windows, copy all of your code and paste it into an email to yourself, then open the email on your computer.

Sign In or Register to comment.