Advanced Photon - Getting Started with Spark Core and Photon (2015)

Getting Started with Spark Core and Photon (2015)

Chapter 9. Advanced Photon

In this chapter you will learn about some of the more advanced features of the Photon and Core.

Configuring a Photon Using USB

Although the Tinker App is a very convenient way of setting up a new Photon/Core, it is not the only way of telling a new device your WiFi credentials. The USB socket on a Photon or Core is not just for supplying it with power, it can also be used to communicate with your computer over USB.

You can pass WiFi credentials to your Photon/Core using the USB connection and a serial communication program. If you have a Mac or Linux computer, then you already have a built-in utility (called screen) for this kind of serial communication over USB.

WINDOWS USERS

If you are a Windows user, you will need to install Putty (http://www.chiark.greenend.org.uk/~sgtatham/putty/) and a USB driver for the Photon/Core (https://s3.amazonaws.com/spark-website/Spark.zip).

Extract the driver archive (Spark.zip) to your Desktop, you will need to point the Found New Hardware wizard at this folder later. Plug the Photon/Core in and when prompted for a driver, navigate to the folder you just downloaded.

To use Putty to communicate over USB with your Photon/Core, start Putty up and then in the row of radio buttons for “Connection type” select the option “Serial” (Figure 9-1).

Figure 9-1. Figure 9-1. Setting-up Putty

The Photon/Core will probably be allocated to COM7

If you are using Linux or a Mac, open a terminal window and issue the command:

$ screen screen /dev/cu.usbmodem1451 9600

When selecting the device (i.e., cu.usbmodem1451) the number on the end may be different for you, so press TAB after you have typed cu.usbmodem and your device should be autocompleted.

Whether you are using Putty or screen, you should now have an empty screen waiting for a command. Press the “i” key and you should see a message like this:

Your core id is 54ff6f065572524851401167

Make a note of this number, this is the ID for your Core. You will need it later when you come to manually claim your Core.

The other command that you can send to the Photon/Core is “w." This lets you tell the Photon/Core what your wireless network is called and its password. So, press the “w” key and you will be prompted for the SSID, security type (probably WPA2) and then your password.

SSID: mymobilenetwork

Security 0=unsecured, 1=WEP, 2=WPA, 3=WPA2: 3

Password: mypassword

Thanks! Wait about 7 seconds while I save those credentials...

Awesome. Now we'll connect!

If you see a pulsing cyan light, your Spark Core

has connected to the Cloud and is ready to go!

If your LED flashes red or you encounter any other problems,

visit https://www.spark.io/support to debug.

Spark <3 you!

If all is well, your Photon/Core should reboot itself and then connect itself to the Spark Cloud indicated by a rhythmic cyan “breathing” of the RGB LED.

Although your core is now connected to the Spark Cloud, it does not know who it belongs to, so you need to “claim” it by logging into Spark.io and clicking on the “Cores” icon (it looks like a target). Then click the button ADD NEW CORE and paste in the long number you copied earlier after issuing the “i” command (Figure 9-2). Then click CLAIM A CORE. You will then be prompted for a name for the Photon/Core.

Figure 9-2. Figure 9-2. Configuring a Photon / Core Over USB

You should now be able to use your Photon/Core as normal.

Factory Reset

From time to time, your Photon/Core will misbehave and need resetting. A simple press of the reset button or powering it on and off will restart the device and cure many problems; however, if the problem is something in your code, then it may stop you flashing replacement code onto the app. This is especially common if you are experimenting with control of the WiFi or making the device sleep to save power.

When all else fails, you can perform a full factory reset. To do this, press and hold down both buttons on the device and then release the Reset button. There will be some yellow flashing and then eventually it will start to flash white. When it starts flashing white, you can release the Setup (Mode on the Core) button and wait while the device does a full factory reset.

Eventually the white flashing will end and your device should be back to its original state. You will now have to tell it your WiFi credentials again.

Programming a Photon Using Spark Dev

I quite like the Web IDE and find it a convenient way to program and manage my devices. Spark also provides a desktop version of the IDE that you can download onto your computer. This is called Spark Dev and in many ways looks and operates in a very similar manner to the Web IDE.

At the time of writing, this tool is not fully off-line as it uses a web service to actually compile the app and flash it onto the device; however, this is a work in progress and full off-line programming of Photons and Cores over USB will be available in the future.

Debugging with the Serial Monitor

One problem with using an IoT device like the Photon or Core is that it can be tricky to work out what is happening when things go wrong with your code.

A common way to find out what is going on in a program is to add trace to your code. Trace is nothing more than program commands that print out some text somewhere so that you can see when the device runs a certain line of the program.

The problem with this on a device like the Photon or Core is that there is no screen on which the messages can appear. But what you can do is arrange for these messages to be sent over USB to a terminal application on your computer like Putty or the screen command that you used in “Configuring a Photon Using USB”.

As an example, try uploading the following app to your device. You can find the code in the file ch_09_Trace_Example in the PHOTON_BOOK library.

void setup() {

Serial.begin(9600);

}

void loop() {

if (Serial.available()) {

char ch = Serial.read();

if (ch == '?') {

Serial.print("Hello millis()=");

Serial.println(millis());

}

}

}

Now open screen or Putty just as you did in #configuring-a-photon. Enter “?” in the terminal window, once its connected and you will see your device report the number of milliseconds since it last rebooted. Press ? again to repeat the process.

Hello millis()=101285

Hello millis()=107317

To start the communication the line Serial.println(9600) starts communication at the baud rate (communication speed) of 9600.

The loop function checks for an incoming communication from screen or Putty and if the character received is a “?” first a message string and then the result of calling millis() are printed over the serial connection.

The difference between print and println is just that println starts a new line at the end of the value it prints out, whereas print does not.

The Electron

A new Spark product is the Electron. At the time of writing, this is still at the stage of a Kickstarter project, but this device will function very like a Photon or Core, but instead of communicating using WiFi, the Electron communicates using a cellphone-stype GSM modem. This means that the device can be truly mobile. For more news and updates on this interesting device, check back on the Spark.io website.

Power Management

WiFi uses quite a lot of power (generally up to say 200mA). To put that into perspective, this means that if you were powering your Photon or Core from a pack of four AA batteries, you might expect it to get about 10 hours use out of the batteries. With the WiFi turned off, you might easily get ten times as much time before your batteries are empty.

You can arrange for your device to put itself to sleep for period of time to increase the battery life of the project using the sleep command.

The sleep takes a number of seconds as its parameter and turns WiFi off for that period of time. For example, to put WiFi to sleep for 5 seconds, you would do:

Spark.sleep(5);

There are other commands to explicitly turn WiFi on and off, but remember that if WiFi is off, there is no way to flash a replacement app on the device, so you may well need to do a factory reset. if things go wrong.

Check out the Spark documentation http://docs.spark.io/firmware for the latest documentation on these features.

Summary

There are many more advanced features of the Photon and Core that will need to wait for a more advanced book. This book is after all a getting started book. But, having mastered the basics check out the Spark documentation that is very thorough and well written. You will also find a helpful and informed community in the Spark forum at community.spark.io