Executing a Processing Script from Arduino Uno

edited April 2017 in Arduino

Hi guys,

Having spent sometime learning from tutorials and exploring library examples, I am left scratching my head figuring out the following:

I want to shutdown Windows 10 from an Arduino UNO via a button push connected to the Arduino.

I have a script using Processing that shuts down windows which I found from another online forum. A simple run of the script achieves the desired result of shutting down Windows.

I require a script for the Arduino that would run the Processing sketch via serial communication after a button is pushed. Whilst I appreciate the concepts involved and have experimented with serial communication between Arduino and Processing, I am having difficulty going further. Any help would be greatly welcome.

The Processing sketch for shutting down Windows I found, is pasted below and works as required, but no doubt requires some editing to have it read a command from the Arduino button push.

void setup() { killBill(); }

void killBill() { try { Runtime.getRuntime().exec("shutdown.exe -s -f -t 0"); } catch (IOException e) { e.printStackTrace(); } }

Answers

  • Why in the world would you need Processing for this? Are you planning on anything more than this?

  • @Lord_of_the_Galaxy For a project with arduino I will say its fine. Not sure if it is doable. You will need to have a Processing sketch running in the background, so I am not sure if it is possible. I am looking fwd to seeing other people's suggestion.

    Kf

  • It is very much possible to run a Processing sketch in the background.

  • You must use surface.setVisible(false);, it will automatically run in the background.

  • Nice trick... I don't think it performs well with P2D though.

    Kf

    boolean vis=false;
    
    void draw(){ 
      if(frameCount%90==0){
        background(random(256));
        surface.setVisible(vis=!vis);
      }
    }
    
  • edited April 2017

    You don't need P2D if you aren't even having a screen to draw to, unless you're interested in 3D on a seperate graphics object. Besides, "not performing well" is quite a bit different from "not working".

    And I'd use it in setup. It is a bit slow.

  • So to the question, all you need to do is get the Arduino to send some "secret code" on the Serial (after connecting on both sides, of course) and get Processing to recognise it and do the shutdown. Use serialEvent().

  • Hi guys,

    Really appreciate the comments. This struggling newbie is playing catch up!

    Ok, as for the reason to do this: The project calls for a button on the face-plate to communicate with Windows and shut down the system prior to turning off power by a single button press. I do not want to have to use a physical re-wire of the power switch on the computer but rather have Arduino send a command to a suitable application to run the shutdown script. I have looked into serial communication and what host application might run the shutdown script and Processing was discussed, so I looked into using this.

    However, without deeper understanding on the subject I may have opened a can of worms for myself! The most I have achieved so far is invoking such functions as 'draw' and 'mousePressed' from Processing examples from serial communication from the Arduino. I presume there are functions in Processing that run scripts once a serial command is received but it is a lot to ask for someone to hold my hand every step of the way. I totally understand all the eye rolling I must be causing by asking such naive questions!. At the very least it is interesting to know it is feasible in theory, and interesting to hear that Processing can be run in the background too.

  • Processing can be run in the background as mentioned before. You could call a bat file to execute processing via command line. Now processing, if set to invisible, will be running on the background and it will be there unless somebody closes the application. This is a hacky way to do it. In windows, there must be a better way to do this as for example, registering the application as a service to run on startup.

    When the application runs, it will occupy the serial port listening for any command. If it get the shutdown instruction, it will execute your one liner above. This is of course, assuming you have admin privileges to run your command, which it seems you have that figure out.

    For the actual code, you could explore simple examples from previous posts: https://forum.processing.org/two/search?Search=arduino
    https://forum.processing.org/two/search?Search=redraw
    Specifically:
    https://forum.processing.org/two/discussion/comment/92348/#Comment_92348

    Kf

  • Hi KF... Thanks very much that! I would dig into the links and have a look....talk about running before I can even walk!

  • In windows, there must be a better way to do this as for example, registering the application as a service to run on startup.

    Indeed. You must register the program to run on startup (all this is assuming you'll use only Windows, otherwise I have no idea, perhaps create multiple versions), unless you want to rely on the user running the program every time on switching on the computer. Also, the program shall have to be one that runs in the background, so you'll need to use surface.setVisible(false). Then, using serialEvent(), you "listen" to the connected port (see here for details on connecting to a port, or ask me if you need better code, since this has errors - https://forum.processing.org/two/discussion/5740/is-there-a-way-to-check-a-serial-port-until-a-connection-is-made#Item_2), waiting for it to tell you to shut down. And then, you shut down, using a method you already knew.

  • To register your program to run on startup, you need to put a shortcut to it in the %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup folder.
    You may do it manually, or contact me if you need help to do it automatically, though beware that my methods are mostly "workarounds" that I found with experimentation.

  • Thanks everyone... Let me have a look at what I can do and if I get stuck I appreciate being able to ask... Priceless resource of talent here!

Sign In or Register to comment.