Showing posts with label 3. Internet - Yun communications. Show all posts
Showing posts with label 3. Internet - Yun communications. Show all posts

Thursday, December 26, 2013

ftp using Linino CPU and Python

I started with ftp-ing directly from the MCU using Curl.
Obviously this isn't very optimal given the Yun architecture.

I changed this so that the Linino gets the Temperature (or other measurements) from the MCU and then the Linino CPU will do the ftp-ing. Using Python this is very straightforward.

The only tricky part is that the ftp expects a file, but I don't want to put the data in a file. Luckily there exists a StringIO class that can take care of that. The code becomes something like:
import StringIO
from ftplib import FTP

myFileIO = StringIO.StringIO()

myFileIO.write(now_str)
myFileIO.write(",")
myFileIO.write(temp)
myFileIO.seek(0)
ftp=FTP("ftp.homebrew.be")
ftp.login(User,Pass)
ftp.cwd("www.homebrew.be/Yafa")
ftp.storlines("STOR dat.csv",myFileIO)
ftp.close()
myFileIO.close()

Use APPE instead of STOR if you want to append to the file.

Monday, October 21, 2013

runShellCommand

I just learned that there exists a runShellCommand as well :-)

The difference with the addParameter is that it does not escape characters. But that is no issue at all.

The clumsy code has just become:


p.runShellCommand("date > /mnt/sda1/arduino/time.txt");

p.runShellCommand("curl -T /mnt/sda1/arduino/time.txt ftp://username:password@webpage/ -a");


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




First ideas regarding Yun-Internet communication

Challenge: observe and control the setup over internet.

I want to be able to follow the fermentation (temperature, CO2 production, etc...) online. But I also want to be able to control or setup the device remotely.
I do not have a lot of expertise with this, so I will start with something very simple.

My first idea was to run a webserver or so on the Yun and store everything on the SD-card.
But I have no idea how I can do this, especially how to give (safe) internet access of the Yun through my (home) router/firewall, and how to manage my non-static IP-address.
So I will start with something more simple.
I have solar-panels on my house with a Solarlog. This box regularly uploads (via ftp) production numbers to my site. Using any browser (and some Java I believe) you can get graphics about the production.
Something similar is good enough for now.

The reverse (controlling the Yun) looks more difficult. I saw the Yun should be able to read emails. This seems like a good start: to control the Yun you just sent it an email with some configuration file (probably XML). This will allow me to start by just manually sending emails. Later I can easily make a windows or android GUI to create and send the emails more human friendly.