4-3 Create Photon App

Your team needs to create a Photon app that will run on your smart device to perform the functions envisioned in your team’s solution design.

You will need to use online references (such as: References for Wiring & Coding, Photon Experiment Guide, Wiring Framework Reference, etc.) to figure out how to program a Photon app that will read data from your device’s inputs, analyze the data to make decisions, send commands to your device’s outputs, and interact with your device’s web app by sharing data through the Internet (via Particle Cloud).

The Photon app will be created in the Particle Build online code editor using the Wiring programming language. You will need to plan out your Photon app, and then program it in stages, testing and revising the code as you go.

YOUR TASK

  1. Plan out Photon app

    1. Use your team’s system model to identify all the inputs and outputs connected to your smart device, the function of each part, and the interactions between parts of the system.

      • If your team's system model does NOT clearly and correctly describe the functions and interactions of the parts, then revise your system model to be accurate.

    2. Answer these questions to help plan out your Photon app. Don't worry about creating "code" for the answers – answer in plain English.

  2. Code Photon app - Part 1 (add libraries, declare global variables, code setup() function, test each part)

    1. CREATE NEW APP: Login to your team's Particle Build account. Create a new app, and give it a title that makes sense. Particle Build will automatically insert a blank setup() function and blank loop() function for your new app.

    2. LIBRARIES: Determine whether any parts (such as: OLED display, servo motor, etc.) need a special code library added to the Photon app. If so, then search for the necessary library in Particle Build, and add the library to your Photon app. Particle Build will insert an #include statement for the library at the very top of your app code.

    3. GLOBAL VARIABLES: Declare (create) global variables near the top of your app (after any library #include statements but before the setup() function). Each variable must have a unique name. You get to decide what the names are, but try to use names that will make sense to someone else reading your code. Remember that variable names cannot have spaces (and cannot be a keyword in the programming language).

      • Create global variables for each part’s I/O pin. This I/O pin number should have been recorded by the person who wired and connected the parts. If necessary, visually verify the I/O pin number used by the part on your Photon device.

      • Create global variables for any object variables that are needed. Parts that needed a special code library usually require an object variable.

      • Create global variables to store any other data (numbers, text, etc.) that your app needs to track or needs to share with your web app.

    4. SETUP( ) FUNCTION: Within the setup() function, add code needed to prepare your device for use. Remember that the setup() function runs only one time when the device is first started (or is reset).

      • Set the pinMode() for each input or output. Be aware that some parts do not need a pinMode() statement.

      • Parts that needed a library (such as: OLED display, etc.) may need certain commands added within the setup() function. For example, the OLED display needs oled.begin(); added within the setup() function.

      • Add any other commands that you want to occur one time when the device is first started. For example, if you are using an LED light, you may want to either turn it on (or turn it off) when the device is first started.

      • Later (in step 3 below), you will add Particle Cloud statements within the setup() function, so your Photon app can send data to your web app and/or receive commands from the web app.

    5. TEST EACH PART: Within the loop() function, add temporary code to test each part, in order to verify the part is wired correctly and working. This "test" code should make the part do something simple that you can observe or verify in some way.

      • OUTPUT examples: turn on LED (or make it blink), have speaker produce a tone, make servo motor rotate, display message on OLED screen, etc.

      • INPUT examples: turn on built-in D7 LED when button is pushed (and turn off when button is released), turn on built-in D7 LED when motion detected (and turn off when no motion detected), display measurement from accelerometer on OLED screen, etc.

      • After adding the "test" code, "flash" (download) the app onto your Photon device to run it. If the part doesn't work properly, then first check your code in the Photon app to make sure it is complete and accurate. For example, does the pin number you listed for the part's global variable match the actual pin that it is wired to? Did you set the correct pinMode() for the part? If necessary, revise the code, and re-flash the Photon app to your device. If you've verified the code is correct but the part is still not working, then check whether the part is wired correctly to the Photon device.

      • Once the parts are tested to work properly, the "test" code can be removed from the loop() function (but do not remove the libraries, global variables, or code in the setup() function) – or the "test" code can be turned into comments by typing two forward slashes at the beginning of each line of "test" code: //

  3. Code Photon app - Part 2 (code loop() function, code any custom functions, add Particle Cloud statements)

    1. LOOP( ) FUNCTION: More details coming soon...

    2. CUSTOM FUNCTIONS: More details coming soon...

    3. PARTICLE CLOUD: More details coming soon...

  4. Once the smart device, Photon app, and web app have all been completed, conduct integration testing to verify they work together properly.

Last updated