Particle photon

Speed of Light

Article from Issue 181/2015
Author(s):

Photon, a tiny WiFi development board backed up by a powerful web platform, is a great device for hardware prototyping and all kinds of interesting projects. We help you to get started with Photon.

Arduino and its countless variants are great for a wide variety of electronics and physical computing projects: from a camera trigger, to an advanced plant watering system, and everything in between. But Arduino is not your only choice when it comes to prototyping your next invention or building a clever gizmo. In fact, the Photon board from Particle [1] might be a better fit depending on your requirements and needs.

Several factors make Photon an attractive alternative to Arduino. For starters, the board is tiny and cheap. Roughly the size of Arduino Nano, Photon can be easily plugged into a standard breadboard for convenient prototyping. The price is sweet, too: Photon costs only $19, which makes it one of the most affordable boards on the market. However, Photon's real killer feature is the on-board WiFi module that supports the 802.11b/g/n standards. This means you can build an Internet-connected prototype or project with a minimum of effort. Better still, Particle provides both tools and services that transform Photon into a cloud-connected device.

Using the Build Web IDE, you can program Photon directly from within the browser, and the Particle Dashboard can be used to monitor and manage multiple Photon devices. The board is programmed using a Wiring-like language, and existing Arduino sketches can be easily adapted for use with Photon. Add to this the fact that Photon is open source hardware, and you have a board worth considering for your next project.

Getting Started with Photon

By default, Photon is configured to work with the accompanying Particle Android app [2] that simplifies the process of connecting Photon to a WiFi network. Power the Photon board via an external power source or a USB port on your machine and wait until the status LED blinks blue. Install the Particle app on your Android device, launch the app, then follow the required steps to create a Particle account and connect the Photon board to the WiFi network of your choice. When the board has successfully established a connection, its status LED should pulse cyan (or breathe, in Photon terminology).

The Particle app also makes it possible to configure and manage Photon's pins, so you can use the app as a rudimentary tool for controlling the board remotely. Because Photon has an on-board LED on pin D7, you can quickly test the board using the Particle app. In the Your Devices section of the app, tap on the active board item, then tap on the D7 pin icon and select digitalWrite. This effectively initializes pin D7 as output. Now tap on the D7 pin icon to change its mode to HIGH, and the LED should turn on. Tap again on the pin icon to toggle it and turn the LED off.

Using the app to read values from devices attached to Photon's analog pins is equally easy. To see this functionality in action, wire a photo resistor and a 10K resistor as shown in Figure 1. In the Particle app, configure the A0 pin as analogRead, then tap on the A0 pin icon to read the value of the photo resistor.

Figure 1: The Particle app can be used to configure the Photon board and control its pins.

Programming Photon

Particle provides a complete integrated development environment (IDE) called Build [3] for writing and compiling programs (called apps) as well as flashing them to Photon. The IDE also provides access to a comprehensive collection of libraries that can be used in Photon programs. Build is rather straightforward in use, and you can master the coding basics by studying the supplied examples. The list of examples includes the classic Blink LED app, which can be a good starting point for learning the ropes.

To add the example to your Build list, click on the Blink LED item in the Example Apps section and press the Fork This Example button. This opens the program in the editor, where you can study and tweak the code. Except for a few minor differences (e.g., pin numbering), Photon's programming language is very similar to Arduino's, so most of what you know about programming Arduino can be used for writing code for Photon.

If you have no experience programming either boards, the detailed comments in the examples supplied with Build can help you get started (see also the Annotated Examples page [4] for more examples and detailed explanations). When you are ready to upload the Blink LED program to the Photon board, press the Verify button to check the code for errors, then press Flash to flash the firmware (i.e., the compiled app) to Photon. See "Upgrading Photon's Firmware on Ubuntu" for details.

Upgrading Photon's Firmware on Ubuntu

Before you flash your program to Photon, you need to upgrade its system firmware to the latest version. There are several ways to do that, but the most straightforward approach on Ubuntu (and most likely on other Linux distributions) is to use the dfu-util tool. First, install the tool using the sudo apt-get install dfu-util command and connect the Photon board to the machine using a USB cable. Then, download the latest firmware release (usually two .bin files) from the project's GitHub repository [6].

Next, you need to put the Photon board into DFU Mode. To do this, press the RESET and SETUP buttons. Release the RESET button, wait till the status LED blinks yellow, then release the SETUP button. In the terminal, switch to the directory containing the downloaded .bin files and run the following commands (replace x.x.x with the actual firmware version):

dfu-util -d 2b04:d006 -a 0 -s 0x8020000 \
  -D system-part1-x.x.x-photon.bin
dfu-util -d 2b04:d006 -a 0 -s 0x8060000:leave \
  -D system-part2-x.x.x-photon.bin

The next project, a simple temperature and humidity logger based on a DHT11 sensor, demonstrates Photon's network capabilities. Wire the sensor as shown in Figure 2, switch to the Build IDE, and create a new app. To read the data from the DHT11 sensor, you need to include the Adafruit_DHT library in the program. To do this, switch to the Libraries section, use the search field to find the library and open it in the editor. Press the Include in the App button to add the library to the program. Switch back to the program and add the code in Listing 1 to it.

Listing 1

DHT11 Sensor Program

 

Figure 2: Photon and the DHT11 sensor wiring diagram.

Use the Verify and Flash buttons to check the code and upload the firmware to Photon. If you have experience programming Arduino, most of the code in the program should look familiar. The interesting part is the Spark.publish function that pushes the sensor data to the Particle cloud platform. You can view these data in real time using the Particle Dashboard (Figure 3) [5]. Switch to the Logs section, and you can view the data as a continuously updated graph and list of events.

Figure 3: Particle Dashboard can be used to view the data received from the Photon board.

In addition to publishing data to the dashboard, you can use the Particle Cloud API to read data directly from the board via HTTP requests that look like regular URLs. To expose specific variables in a Photon program to external requests, you need to declare them using the Spark.variable() function in the void setup() section of the program. The function takes three arguments: the variable name that identifies the variable in a request, the reference variable, and its type. To read, for example, the temperature and humidity values in the DHT11-based program, add the following statements in the void setup() section:

Spark.variable("temp", &temperature, INT);
Spark.variable("hum", &humidity, INT);

To access and read data on the Photon board, a request must contain a device ID and a unique access token. To find out the device ID, switch to the Devices section in the Build IDE, click on the desired device, and note its device ID. Then, switch to the Settings section and note the access token. With all the pieces in place, you can read the exposed values using any tool that can send and receive HTTP requests.

For example, you can use the curl utility to send a request and view the response. The following curl command returns a response containing the value of the temperature variable (replace [DEVICE_ID] and [ACCESS_TOKEN] with the actual device ID and access token):

curl "https://api.particle.io/v1/\
  devices/[DEVICE_ID]/\
  temp?access_token=[ACCESS_TOKEN]"

This returns the result in the JSON format (Listing 2).

Listing 2

Results of Device ID Request

 

To get the temperature value and discard the rest of the response, use the &format=raw parameter in the request:

curl "https://api.particle.io/v1\
  /devices/[DEVICE_ID]/temp?access_token= \
  [ACCESS_TOKEN]&format=raw"

Most modern programming languages can send HTTP requests and process the received JSON responses, so you can write scripts and build applications that can communicate with Photon. For example, a simple PHP-based web app in Listing 3 can be used to fetch and display temperature and humidity data. Looking at the script, you might notice that it uses a function. The problem is that a Cloud API request can't contain multiple variables, so the function provides a simple workaround.

Listing 3

PHP Script for Reading Photo's Values

 

Controlling Photon via Cloud API

As an amateur photographer, I always look for novel ways to control my camera. So, as soon as I got my hands on a Photon board, I devised a Photon-based shutter trigger (Figure 4). It's designed to work with a Sony NEX-3N digital camera, but it can be adapted easily for other camera models, too. Of course, it's possible to use any Arduino board for this project, but Photon's WiFi capabilities and support for the Cloud API opens several interesting possibilities.

Figure 4: Transistor switch wiring diagram.

The Photon board that controls the transistor-based shutter release runs the program in Listing 4. Unlike regular Photon code, this program uses Spark.function to define a function and its identifier that controls the Photon board. In this case, when the shutterRelease function (identified by its shutter name) receives the release command, it triggers the shutter release by toggling the state of the initialized pins in a specific order.

Listing 4

Shutter Release Program

 

Triggering the shutter release is a matter of issuing a request that contains the function's identifier and the command:

curl https://api.particle.io/v1/devices/[DEVICE_ID]/shutter \
  -d access_token=[ACCESS_TOKEN] -d params=release

This command can be integrated easily into shell scripts. The Bash shell script shown in Listing 5, for example, can be used for time-lapse photography.

Listing 5

Bash Shell Script

 

Paste the code into a new text file, save the file under the photon.sh name, and make the script executable using chmod +x photon.sh. The script accepts two arguments: the number of photos to take and the interval between them. So, to take five photos every three seconds, issue the ./photon.sh 5 3 command.

Finally, you can build an HTML-based application to control the Photon-based shutter release. All you need to do is add an HTML form that sends a correct Cloud API request. The simple example in Listing 6 demonstrates how to trigger the shutter release using an HTML button.

Listing 6

HTML Form

 

Code and files for the Photon-based shutter release project are available on GitHub [7].

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News