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

Getting Started with Spark Core and Photon (2015)

Chapter 1. The Spark Photon

In this chapter, you will learn a little about the Internet of Things in general, as well as the Spark Photon in particular. The Photon and its older brother, the Core, are explored along with some background about where it has come from and where it sits in the pantheon of developer boards.

The Internet of Things

It used to be that the only way you could interact with the Internet was to use a browser on your computer. A browser would allow it to send requests to a web server that would send back information to be displayed.

The browser would display this information using a computer monitor, and the “user” would type text on his or her keyboard and follow hyperlinks with the click of a mouse. As far as inputs and outputs were concerned, those were your options.

The Internet of Things (abbreviated to IoT) has changed all this. Now all sorts of sensors and appliances can be connected to the Internet. The IoT encompasses a wide range of systems including:

§ Home automation systems where lighting, heating and doors are controlled using web browser or network-enabled smartphone applications. These may be used to control systems over the local area network, or over the Internet using WiFi or a cellular network

§ Arrays of sensors, such as the Safecast open radiation monitoring system that was developed following the Fukushima nuclear disaster

Products and maker projects that will become part of the IoT are springing up all over the place. These include successful projects like the Nest smart thermostat as well as many IoT products that use the accelerometer, location services and communication features of smartphones to capture information about people’s health and activity levels.

Since so many people are creating IoT projects, it makes perfect sense to provide a simple modular framework for both hardware and software that provides an easy-to-use IoT technology kit. This is exactly where the Spark team come in. They provide IoT technology in a box. In a very small and low cost box. What is more, the technology is easy to use, open source and based on the very popular Arduino software framework.

Sparks in the Clouds

The hardware component of this IoT framework is the Spark Photon. The Photon is the next generation of Spark’s IoT platform that began with the Spark Core. The Spark Photon is backwards compatible with the Spark Core, and so much of what is detailed in this book about the Photon will also work with the older Core.

Although other technologies exist to help you build IoT devices, they often neglect the all-important software framework that allows the device to communicate with other devices and browsers over the Internet. The Spark approach, by contrast, integrates the hardware and software seamlessly.

An IoT device using a Photon/Core might provide remote unlocking of a door. In such a case, a user would access a web page on their browser that has an “Unlock” button. This page will have been served from a web server somewhere on the Internet. When the user clicks on the Unlock button in their browser, a message is sent from the browser to the IoT device via a cloud service that is aware of the Photon running the IoT device. The message is then passed to the Photon/Core that controls an electromechanical door latch to unlock the door.

If, on the other hand, the IoT device was acting as a sensor, lets say for temperature, then the Photon/Core could send temperature readings to a cloud service. Those readings could be stored temporarily until the user’s browser has a chance to pick them up and display the latest reading on the browser.

To use Spark’s cloud service, you first register with Spark and then identify each of your Photons/Cores, which will have registered themselves with the cloud service as being yours. All the Photon needs to do to register itself is to have access to your WiFi network. This process not only allows you to ensure that you know which Photon or Core you are interacting with, but also allows you to program your Photons and Cores over the air from the comfort of your web browser.

Other IoT Platforms

Before plunging into those delightfully warm and pleasant waters of the Spark Photon pool, its is worth exploring some of the Photon’s competitors. This will also reveal something of the motivations behind the design of the Photon.

The Spark Photon is of course not the only IoT device around. In fact the single most used platform for IoT development is the Arduino microcontroller board, although the Raspberry Pi single-board computer is also used extensively in IoT projects.

Arduino

Microcontrollers are essentially low-powered computers on a chip. They have input/output (I/O) pins to which you can attach electronics so that the microcontroller can - well - control things. The Arduino is a simple-to-use and low-cost, ready-made board that allows you to make use of a microcontroller in your projects.

The Arduino has become the platform of choice for makers and hackers looking for a microcontroller to use, and the most common Arduino model is the Arduino Uno.

The popularity of Arduino is due to many factors, including:

§ Low cost (around USD 25 for an Arduino Uno)

§ Open source hardware design - there are no secrets to its design and built-in software

§ Easy to use Integrated Development Environment with which to program the Arduino

§ Plug in shields that plug onto the top of the Arduino and add features such as displays and motor drivers

There is of course one factor that makes an Arduino Uno by itself useless as an IoT device, and that is that it has no network connection, either wired or wireless. This means that you either need to use one of the specialized Arduino models that include an Ethernet network port (such as the Arduino Ethernet) or add a WiFi or Ethernet shield to the Arduino that then gives it the network connection it needs to communicate over the Internet. This adds considerably to the size and cost of your project.

Another possibility is to use the Arduino Yun. This device is the same size as an Arduino Uno but it has a built-in WiFi module. On the face of it, this provides similar hardware capabilities to the Spark Photon, but at a much higher price of around USD 75.

All of these Arduino-based solutions suffer from one major disadvantage as an IoT platform, and that is software. They provide the base capabilities to communicate with the Internet but do not offer any software framework to make it easy to create IoT projects without a lot of tricky network programming.

Later, you will see how the Spark Photon borrows many of the concepts of Arduino, including its programming language, but then provides a software framework with which to build your IoT projects, all at much lower cost than Arduino can compete with.

Raspberry Pi and BeagleBone

The Raspberry Pi and BeagleBone Black are both single-board computers, about the size of a credit card, that run the Linux operating system. They have USB ports and HDMI video output, so you can set them up with a keyboard, mouse and monitor and use them just like a regular computer.

The Raspberry Pi is shown on the left of Figure 1-3 and the BeagleBone Black on the right. Both boards can use low-cost USB WiFi adaptors and have I/O pins to control electronics an interface with sensors making them quite suitable for IoT projects.

Although both boards are quite low cost, the Raspberry Pi from USD 25 and the BeagleBone from USD 55, they are quite large (compared to a Photon) and generally contain a lot more than you need for a simple IoT project.

Intel Edison

Intel has developed a small Linux-based board called the Edison. The Edison is designed to be embedded into IoT projects and is perhaps the most direct competition to the Spark Photon.

The Edison is small but is considerably more expensive than the Photon. It has a delicate 70-contact connector that requires a separate breakout board if you want to start attaching external electronics to it. There are several such prototyping boards available, the most popular of which is an Arduino-compatible board.

Although receiving a lot of interest in the Maker community, this is probably a board that will lend itself best to high-end or professional use, not least because the device is a lot harder to get started with.

A Tour of the Spark Photon

The Photon has two buttons. The Reset button resets the Photon, restarting the processor. The Setup button (“Mode” button on the Core) is used in conjunction with the Reset button to allow more severe resetting of the Photon, including a full factory reset.

At the top of the board, you will find the micro-USB port. Although the main point of this is to provide power to the Photon, it can also be used for USB programming of the board (see Chapter 9) and for USB communication with a computer.

Moving around the board clockwise, there is a section of pins labelled “Power and Reset” on Figure 1-4. The top-most pin (3.3V) is the 3.3V power used by the Photon, as all the logic operates at 3.3V rather than the 5V that you might be used to as an Arduino user. The RST pin allows you to achieve the same effect as pressing the reset button, put electrically, by connecting the pin to ground. You are fairly unlikely ever to want to use this pin.

The VBAT pin allows a small backup battery (or supercapacitor) to be attached to the Photon, to power it while it is in deep sleep mode.

The pins labelled D0 to D7 are general purpose GPIO pins that can act as either digital inputs or outputs (see chapter 4). Some of these pins can also act as analog outputs (pins D0 to D4) using a technique called PWM (Pulse Width Modulation). There is a blue LED next to pin D7 that is connected to D7. You can turn this on and off from your programs, or using the Tinker app on your smartphone.

The Photon has a built-in chip antenna that will work fine in most WiFi situations, but the Photon also has a tiny antenna socket to which an external antenna can be attached. This is useful for extending the WiFi range of the device by adding a more sensitive or directional antenna.

The pins A0 to A5 are primarily analog inputs which can measure a voltage between 0 and 3.3V. These will typically be used for sensors. For example, in Chapter 6 you will use an light sensor with one of these pins. The analog pins can also be used as digital inputs or outputs just like the pins D0 to D7 and, like the digital pins, some of the analog pins (A4, A5) can also be used as PWM analog outputs.

The DAC pin is a special analog output pin. This is a true analog output that can be set to any voltage between 0 and 3.3V with 12 bits of resolution.

The WKP pin is used to wake up the Photon after it has put itself into a deep sleep mode. Writing software that allows the Photon to sleep some of the time makes it practical to run the Photon from batteries where power consumption needs to be minimized.

The TX and RX (Transmit and Receive) are used for serial communication. Above these pins is a second GND pin and the VIN pin. You can supply between 3.6V and 6V to the VIN pin to power the board as an alternative to using the USB port.

The Spark Core vs. Spark Photon

The Photon is not the first IoT board to be produced by Spark. That honor belongs to the Spark Core. Both boards are shown side by side in Figure 1-5, and as you can see the boards look very similar to each other.

Figure 1-5. Figure 1-5. The Spark Core (left) and Photon (right)

The main differences are that the Photon uses a different WiFi chip (Broadcom rather than Texas Instruments). It also uses a faster processor with more RAM.

The pins are mostly the same between the two boards, however there are a few important changes to be aware of. First, the Core does not have DAC and WKP pins; instead it has two extra analog inputs, A6 and A7.

The examples in this book will have all been tested on both the Photon and the Core, so if you have one of the older Cores you should be just fine.

Programming

Spark has gone to great efforts to make programming the Photon and Core as easy as possible, both by providing a very simple-to-use development environment and also by choosing the Arduino C programming language as the basis for the Photon’s programming language.

Since the Photon is an Internet device, it makes perfect sense to program the device over the Internet, so most of the time, you will write the code for your Photon in your web browser and then push it out to your Photon, which will be looking out for updates. An offline programming environment is also available for more advanced users (see Chapter 9).

For those thinking of using the Photon for commercial applications, the ability to update over-the-air is incredibly powerful, as it allows remote updates of the Photon’s software from anywhere in the world.

This may start a few alarm bells ringing. After all if the Photon was controlling your home’s front door or heating, you would not want someone updating your cores and telling them to unlock your doors and turn the heating up to maximum. Fortunately, there are several security mechanisms that are used to secure the Photon. This includes the use of secure socket communication as well as authentication keys that ensure that only you can update your cores.

The Web IDE allows you to write code that you can then send down to one of your Photons over the internet.

Summary

By now, you must be itching to get started with the Spark Photon, so in Chapter 2, you will learn how to get up and running with your Photon and install your first program onto it.