Run at specific time...

edited July 2017 in Library Questions

Hi I have the code below. Basically I want to run this ftp upload at certain non-precise time intervals. There's another portion that I haven't combined with it yet that runs a loop that updates the file in question. So I figured the easiest way to do the first part is to just make a counter. The problem I have is that the sketch appears to only execute the ftp.put on the first run (as far as I can tell). I'm aware that the insertion of ftp login info is baaaaad as is the uploads every 9 seconds but I'm just trying to get something rolling here for testing purposes. Any suggestions on how to improve this would be greatly appreciated. Thanks! Mike

import com.enterprisedt.net.ftp.*; 
FTPClient ftp; 
String host,user,pass,remotePath,localPath,timestamp,runAtTime; 
String [] nameList; 

void setup(){ 
  size(640, 360);
  host="*********"; 
  user="********"; 
  pass="*********"; 
  remotePath="/DataFTP/"; 
  localPath="/Users/***/***/Processing/test.rtf";
  timestamp = year() + nf(month(),2) + nf(day(),2) + "-"  + nf(hour(),2) + nf(minute(),2) + nf(second(),2);


} 

void draw(){
  for (int i=0; i<90000; i=i+1) { 
    if (i==89999) {
       try { 
          ftp=new FTPClient(host); 

          ftp.login(user, pass); 
          ftp.chdir(remotePath); // go to  folder on the server 

          // copy file to server
          println ("Putting file");
          ftp.put(localPath, "/DataFTP/test-"+timestamp+".rtf");
          nameList=ftp.dir(".", false); //  get list of what exists under remotePath 

          println(nameList); 

          ftp.quit(); 
       } 
       catch (Exception e) { 
         try { 
          if(ftp!=null) ftp.quitImmediately(); 
        } 
        catch (Exception e1) { 
          e1.printStackTrace(); 
        } 
      } 
    }
  }

}

Answers

  • draw() runs every 60th of a second.

    and it'll run all 90000 times through that loop every time. so your ftp command will run every time draw() does, again and again, many time a second (if it can). not what you want.

    look at frameCount() or millis() for timing things.

  • _vk_vk
    Answer ✓

    a timer example:

    PFont font;
    String time = "000";
    int initialTime;
    int interval = 1000;
    int fontSize;
    
    void setup()
    {
      size(300, 300);
      fontSize = 40;
      font = createFont("Arial", fontSize);
      background(255);
      fill(0);
      smooth();
      noStroke();
      textFont(font);
      initialTime = millis();
    }
    
    void draw()
    {
      background(255);
    
      // if current time minus lastStored bigger than interval 
      if (millis() - initialTime > interval){
    
        // increment time as an int and back to string again
        time = nf(int(time) + 1, 3);
    
        // reset counter
        initialTime = millis();
      }
    
      // just  cosmetic using the counter ...
      if (int(time) % 5 != 0) {
        fill(210);
      }else{
        fill(170, 100, 100);
      }
    
      ellipse(width/2, height/2, 120, 120);
    
      fill(255);
      ellipse(width/2, height/2, 100, 100);
    
    
      //display time
      fill(0);
      text(time, width/2 - textWidth(time)/2, height/2 + fontSize/2.8 );
    }
    
  • Moreover, load resources, instantiate heavy classes inside setup()! /:)

  • Excellent. Thanks for pointing me in the right direction. i got it working :) Point taken on the setup tip also. Thanks Mike

Sign In or Register to comment.