Exercise 2: A simple Sun SPOT application using the Emulator

Expected duration: 15 minutes

Introduction:

The goal of this exercise is to familiarize yourself with the process fo creating a new Sun SPOT Application in NetBeans using the Sun SPOT Application Templates, building and depolying that application to the Built-in Sun SPOT Emulator, and running your application.

You will build, from scratch, a very simple Sun SPOT Application. You will then start the Solarium Application, and a new Virtual Sun SPOT Emulator. YOu will then deploy your application to the emulated Sun SPOT and run your application in the emulator, interacting with the virtual sensor using the Virtual Sun SPOT Sensor Panel.

This exercise is done in NetBeans, and there are no instructions for completing it outside of NetBeans as it relies on the Sun SPOT SDK NetBeans Modules for creating new Sun SPOT applications and for refactoring them.

Exercise:

  1. Start NetBeans by either clicking the NetBeans icon on your desktop, or by typing:
                $ netbeans &
                $
                
    at a terminal prompt.
  2. Create a new NetBeans Project using the 'New Project' button:

  3. Since the Sun SPOT SDK Modules are installed, You can choose the type of the new project to be a Sun SPOT Application:

  4. Name your Application whatever you like:

  5. Your project should now be displayed in the Projects tab of NetBeans. YOu can then expand the project, and see the listing of classes created for you by the New Project Module:

  6. Since we want the 'main' class file named the same as the project, we'll use the Refactoring/renaming facilities in NetBeans to rename the class file. Once the project is fully expanded, right-click on the .java file, and choose "Refactor-->Rename"

  7. Name your class the same as your application name and click 'Refactor"

  8. Your .java file should now have your new class name, and once you open the source, everything should have your new name properly changed.

  9. The Sun SPOT Application Template fills in much of what you need, and even inserts some example code for dealing with the LEDs, the IEEE Radio Address, and the built-in switches.

  10. We will not be using the LED or the Switch code in this example, so delete it. We will be defining a Scalar Input (for use with an analog sensor like the bend sensor we will be using later). So input the following lines of code:
    42         IScalarInput sensor = EDemoBoard.getInstance().getScalarInputs()[EDemoBoard.A0];
    43         while(true){
    44             try {
    45                 int val = sensor.getValue();
    46                 System.out.println("Value: " + val);
    47             } catch (IOException ex) {
    48                 ex.printStackTrace();
    49             }
    50             Utils.sleep(100);
    51         }
    52     }
    

    After which your application code should look like this:

  11. Now we'll build the project to make sure everything compiles correctly, and since we don't have any Sun SPOTs connected, we don't want to "Build Project + Deploy to Sun SPOT" at this point:

  12. The output from the build process should show the successful creation of the application Jar file:

  13. If it is not already running, start the Sun SPOT Manager Tool, and click on the Solarium tab.

  14. We will only be running Virtual SPOTs, so select 'Virtual SPOTs' from the "Select a Sun SPOT" menu:

  15. From the Emulator menu, choose 'New virtual SPOT':

  16. We will need to give input to the Sensor we defined, so right-click on the Virtual SPOT and choose 'Display sensor panel' so that you can interact with the virtual sensor:

  17. The sensor we defined is an 'analog' sensor, so click the 'Analog In' tab, and you will see sliders for each of the Analog pins on the Virtual Sun SPOT.

  18. To deploy your application to the Virtual Sun SPOT, right click on the Virtual Sun SPOT, and choose 'Specify application jar file..." from the pop-up menu

  19. Select the Jar file you built in step 11 above

  20. Once the jar file has been deployed, you can again right-click on the Virtual Sun SPOT and Run the application Midlet:

  21. Once you start the Midlet, you will see a small 'application' displayed next to the Virtual Sun SPOT. When you are running multiple MIDlets on a Sun SPOT this representation is very convenient for identifying and interacting with specific MIDlets

  22. Once again right-click on the Virtual Sun SPOT and choose 'Show application output' to display the printed output from the Virtual Sun SPOT

  23. As you move the slider for the Analog Pin associated with your virtual sensor you can see the output change in the output window

Summary:

This exercise showed the complete process of creating a new Sun SPOT Application, adding new functionality, and deploying the new application to the Solarium Virtual SPOT Emulator. The process for deploying the application to a real Sun SPOT is exactly the same, as we will see in other exercises.

Show Solution

Go on to next exercise