Sunday, October 20, 2013

ftp the current time and date to a webpage

The first thing I want to try is uploading something to a webpage. Later this will be processed sensor data. For now I will try to log the current time date.

For the ftp-ing I looked at the pre-installed curl package.
I didn't immediately find how I could pass a string or environment variable to curl, so I decided to put the time and date in a file, and ftp this file to my webpage.

I created a folder arduino on my SD card. Using the terminal I could immediately see the folder /mnt/sda1/arduino. Writing to the file using simple linux commands was a bit more difficult. I read about the process library and its begin/run/addparameter methods. I simply wanted to call

date > /mnt/sda1/arduino/time.txt

I tried several combinations of what strings to put in begin()/run() but didn't manage to get it to work :-(
As alternative I made a simple shell script (go.sh) that does just that:


#!/bin/sh
date > /mnt/sda1/arduino/time.txt
echo "hoi"

Calling this script from the sketch is as simple as:

Process p;        // Create a process and call it "p"
p.begin("/mnt/sda1/arduino/go.sh");
p.run();

Great :-)

Now uploading the file to my webbage. Doing this on the terminal was pretty straightforward, but I again struggled with the begin/addparameter syntax. Eventually I got it to work using the following piece of code:

p.begin("curl");
p.addParameter("-T");
p.addParameter("/mnt/sda1/arduino/time.txt");
p.addParameter("ftp://username:password@my-web-location");
p.addParameter("-a");
p.run();

The -a is to append the file.
I don't know why I had to use 4 addParameter calls though...but OK it works.
Probably I could do something similar for the file creation.

These 2 codes together and a delay of about 2 minutes in between create a file on my webpage looking as follows:

Sun Oct 20 20:40:58 CEST 2013
Sun Oct 20 20:43:04 CEST 2013
Sun Oct 20 20:45:10 CEST 2013
Sun Oct 20 20:47:16 CEST 2013
Sun Oct 20 20:49:21 CEST 2013
Sun Oct 20 20:51:28 CEST 2013
Sun Oct 20 20:53:34 CEST 2013
Sun Oct 20 20:55:40 CEST 2013
Sun Oct 20 20:57:46 CEST 2013
Sun Oct 20 20:59:52 CEST 2013
Sun Oct 20 21:01:58 CEST 2013
Sun Oct 20 21:04:04 CEST 2013
Sun Oct 20 21:06:10 CEST 2013
Sun Oct 20 21:08:15 CEST 2013




No comments:

Post a Comment