Quick Start - Getting Started with Spark Core and Photon (2015)

Getting Started with Spark Core and Photon (2015)

Chapter 2. Quick Start

In this chapter you will launch right into using the Spark Photon or Core. If you are new to programming and this kind of technology, then just work through the instructions here and do not worry too much if there are things that you don’t understand, these will all be explained in much more detail later in the book.

Signing Up

One of the things that you get when you buy a Spark device is access to Spark’s Cloud Service. You will need to register to gain access to this. Registering with the cloud service is essential as it allows you to manage your Photons and Cores over the Internet, including programming them remotely.

The cloud service is also what a Photon/Core uses to communicate with other Photons, Cores and browser applications.

Your first step should therefore be to sign up at www.spark.io. Click on the LAUNCH button to begin the sign up process. Don’t worry, this is strictly just to use your Photon over the Internet, you will not be bombarded with spam.

Once you are all signed up, you will find yourself in the Web IDE ready to start writing programs. Before you can do that, you need to connect your Photon to the Internet.

Connecting to WiFi

Although it is possible to program a Photon or Core over its USB connection (see Chapter 9), it makes more sense to program an Internet device like this over the Internet using the Web IDE. To do that, the Photon or Core needs to be connected to the Internet over WiFi. To be able to connect using WiFi, it needs to know the SSID (network name) and passphrase of your home network.

As you may have noticed, the Core and Photon have no keyboard or display, so you need a way of telling them these details.

Connecting a Core

To help you get your Core connected, Spark has produced a smartphone app available for Android and iPhone. This app (TinkerApp) actually does more than allow you to connect your Core to the Internet; it also allows you to control the Core, turning pins on and off, etc. If you do not have a suitable smartphone, then all is not lost; you can configure both the Core and Photon using the USB cable (see Chapter 9).

When you plug in a brand-new Core, it should blink dark blue, indicating that it is “listening” for a WiFi network to connect to. Unless it is in this state, you will not be able to connect it to your wireless network. You can put your device back into this state by doing a factory reset on it as described in Chapter 9.

You will need an Android or iOS (iPhone/iPad) device to use the Tinker app to connect your device, and the phone or tablet must be connected to the WiFi network that you want to join the Core to.

When you first start the Tinker app you will be prompted to log into your Spark account. Sign in using the same email and password that you gave when you signed up to Spark.

After you are logged in, you will be prompted to enter your WiFi password. The SSID (wireless network name) will be pre-filled as your device is already connected to that wireless network.

The app should then discover your Core and prompt you to supply a name for your Core. At this point the Core will be “blinking rainbows at you” as it says in the dialog.

The Core should then reset, and when its finished the LED should be gently breathing in cyan and ready to start using.

Connecting a Photon

The documentation for connecting a Photon is not yet available. It may be exactly the same as the Core and use the Tinker App. But there is also talk of a SoftAP (Soft Access Point) method of connecting that may mean that the Photon temporarily hosts a wireless access point, to which you can connect and then enter credentials for your normal wireless network. Please check on www.spark.io for the latest information.

Controlling Pins with the Tinker App

Before trying out our first program, we can use the smartphone Tinker app to control the Photon or Core. This is the same smartphone app that you used to configure your Photon/Core, but having configured the device, you can use the same app to interact with it.

In this case, you will use it to turn the built-in blue LED attached to pin D7 on and off.

The first step is to launch the app on your Android phone or iPhone. Log in to Spark if you are prompted and then you should find yourself at the main control screen.

Select the option digitalWrite. Now, if you tap on the D7 pin on the screen, the blue LED next to pin 7 will toggle on and off.

Project 1. Blink the Tiny Blue LED

Programmers have a tradition that the first program that they write in any language should be called “Hello World”. This usually involves just getting the program to display the words “Hello World”. Clearly the Photon/Core doesn’t have anywhere to display those words, so we will have to think of something else as our easiest possible program.

Arduino and other similar boards have adopted the task of blinking an LED as the most basic type of thing that you might want to do with a board like the Photon, so lets start with that.

Do not worry about how this program works, all will be explained in the next chapter, for now we just want to see that LED blink, so click on the icon in the top left of the IDE that looks like a lightning bolt. This is the “Flash” button that will flash the blink program onto your Photon.

You should see a message appear in the status area at the bottom of the screen that says: Flash successful! Please wait a moment while your Photon is updated...

Occasionally, the flashing will appear to hang and nothing happens. Eventually, the IDE will time-out and you can try again. It may help to reset the Photon/Core by clicking on the Photon’s reset button.

At this point, the RGB LED on the device should start blinking away in purple, indicating that the Photon/Core is being updated. After the device has received the program, it will reset itself and the RGB LED will blink green for a little while. Once the Photon/Core restarts, the tiny blue LED next to pin 7 will flash on and off every two seconds.

You will meet this program in the next chapter, where all will be explained, but before that, in Project 2, you will learn how to create a new program and type in the code for it.

Project 2. Control the Photon’s LED

Like the tiny blue LED attached to pin D7 of the Raspberry Pi, you can also control the RGB LED that the Photon uses to indicate its status. In this mini-project, you will type a short program into the Web IDE that will make the RGB LED light first red, then change to green, then after a further two seconds turn blue and so on.

As you can see from Figure 2-new-app1 you are being prompted for a name for the app. This needs to be unique and whatever you type here as a name will be converted into uppercase. If you want to separate the words of the app name, then you can use an _ character. Call the app “RGB_LED.”

When you create a new project, the Web IDE will automatically create a blank template into which you can write your code. At the moment, the program for the app looks like this:

void setup() {

}

void loop() {

}

Change this text so that it appears as below:

void setup() {

RGB.control(true);

}

void loop() {

RGB.color(255, 0, 0);

delay(2000);

RGB.color(0, 255, 0);

delay(2000);

RGB.color(0, 0, 255);

delay(2000);

}

All the text that you are going to add into the template needs to be tabbed in one tab space, so on each new line, press the TAB key once before typing the line of code. Notice that each line of code must end in a semi-colon.

When you have typed in all of the code, save the app by clicking on the Save icon (looks like a folder) and then hit the flash button (lightning bolt) to program the Photon.

It does not matter whether the Photon is plugged into the breadboard or not. However, keeping your Photon on the breadboard will protect its pins against getting bent or accidentally shorting together if they touch something metal.

ERROR MESSAGES

If you mistyped something then you will see an error message. A common mistake is a missing semi-colon on the end of a line. If this is your error, then if you look carefully at the error message, there is a lot of incomprehensible stuff before you get to the line that says:

the_user_app.cpp:3:1: error: expected ';' before '}' token

This is just saying that there is a semi-colon missing somewhere. So go back and check over your code carefully.

Once the Photon/Core has finished flashing purple and has restarted, you should see the RGB LED cycle through red, green and blue.

Summary

Congratulations, you have made your Photon or Core do something!

In the next chapter you will revisit both the apps that you have run on your core to understand a bit more about how they work and how to go about writing your own apps.