Michele Marcionelli's Blog

… about me, my life and what I like to do

×

Tag: ftp

Importing CSV Data from a Sunny WebBox with FTP

Last month we let install a photovoltaic (PV) system of 8.4 kWp with a special module (the Sunny WebBox of the company SMA Solar Technology) that sends all measurements to a portal, which is easily accessible with an iPhone, iPad an of course a normal internet browser. This was in fact for me the main reason to buy this optional module. But only after the installation of the PV-system I discovered that the portal (the place where my data is send) is located in another country, which is not what I wanted. Fortunately I also discovered that the Sunny WebBox, if configured accordingly, can be accessed using FTP (File Transfer Protocol) to transfer files, so that I don’t need anymore to send my data to someone I don’t know ;-)

As “programmer” as I am, I had the idea to use this access to download all the data automatically (at least once a day) and do some statistics by myself. And so I did. I wrote the following small python script which synchronise (in one way) the data on the FTP-Server of the Sunny WebBox with a local directory on my computer.

This script has been tested only wit OS X 10.7+, but should also works with any Linux/Unix distribution. The only requisite is actually the python module ftplib. There are also some limitations (see comment in the script header) which don’t compromise the good functioning in this specific case.

#!/usr/bin/env python

# Author: Michele Marcionelli <michele (at) marcionelli.ch>
#
# Note: this script synchronize a ftp directory with a local directory
# with following limitations:
#   - only filesizes are compared
#   - only filenames without spaces are supported
#
# Last change: 28-Nov-2013

FTP_HOST = 'host_or_ip_of_synny_webbox'
FTP_USER = 'User'
FTP_PASS = '0000'

def ftp_download(path):
    listing = []
    ftp.dir(path, listing.append)
    for entry in listing:
        x = re.compile(" +").split(entry)
        if x[2] == '<DIR>':
            ftp_download(path + '/' + x[3])
        else:
            remotesize = int(x[2])
            datetime = time.mktime(
                time.strptime(x[0] + ' ' + x[1], '%m-%d-%y %H:%M'))
            filename = x[3]
            # check if local copy exists and have same size
            try:
                localsize = os.path.getsize(path + '/' + filename)
            except:
                localsize = 0

            if localsize != remotesize:
                print 'downloading ' + filename
                if not os.path.isdir(path):
                    os.makedirs(path)
                ftp.retrbinary(
                    'RETR ' + path + '/' + filename,
                    open(path + '/' + filename, 'wb').write)
                os.utime(path + '/' + filename, (datetime, datetime))

ftp = FTP(FTP_HOST)
ftp.login(FTP_USER, FTP_PASS)
ftp_download('CSV')

Feel free to copy and modify the script and leave comments.