Maker.io main logo

How To Debug the Photon

2019-09-04 | By Maker.io Staff

Photon

The Particle Photon is a cloud-based microcontroller development IoT system that has some major advantages over other mainstream microcontrollers. However, with no physical programmer, debugging the Photon can be somewhat tricky at times. In this How-To, we will look at methods for debugging the Particle Photon!

Serial Port Debugging

If a PC or laptop is available, then the Particle’s serial port can be used to help with debugging code. Values can be checked by printing their value, specific messages can be inserted into key locations of the code to see if those lines are, in fact, being executed, and values can even be injected with the use of a terminal. The example below shows how different unique messages can be put into the code to confirm that each stage is being executed, while the end of the loop shows values being printed to see their results.

Copy Code
void loop()
{
// Some untested code here
Serial.println("Code has reached point A");
// Some other untested code here
Serial.println("Code has reached point B");
// Some extra untested code here
Serial.println("Code has reached point C");
Serial.print("Variable has the value: ");
Serial.println(variable1);
}

Cloud Debugging

Cloud debugging, a novel feature of the Particle Photon, is very similar to the serial port. Instead of needing a PC with a USB port for forming a serial connection, we instead take advantage of the online console. This console publishes events that occur on the Particle Photon, and the Particle Photon can create its own events with the use of Particle.publish().

This function, as seen before, takes two parameters: the event name and optional data. In some cases, we do not require the use of additional data (as the very act of calling this event informs other devices and the cloud that something has happened), but we can use this additional data parameter to print out variables or other messages. For example, we could print messages including errors messages from HTTP failures or where in the code the Particle is currently at. A simple example of using this as a debugger would be to place the publish() function into key locations that will get called (assuming the code reaches those points).

Copy Code
void loop()
{
// Some untested code here
Particle.publish("DEBUG", "Code has reached point A");
// Some other untested code here
Particle.publish("DEBUG", "Code has reached point B");
// Some extra untested code here
Particle.publish("DEBUG", "Code has reached point C");
// Publish variable
Particle.publish("DEBUG_VAL", variable);
}

However, this code example has one issue; only 1 message may be published per second. This means that this use of code tracking should be used with great caution and with delays between publish() functions, so that limits are not reached. This type of debug method is also untidy and could be further improved with the use of a #define!

Copy Code
#define DEBUG_PRINT(...) { Particle.publish( "DEBUG", String::format(__VA_ARGS__) ); }

This line of code (if inserted at the top of your code) can be used as an easy way to print data to the console as debug information without having to use the Particle.publish() function. The example below shows how it can be used.

Copy Code
#define DEBUG_PRINT(...) { Particle.publish( "DEBUG", String::format(__VA_ARGS__) ); } void setup() { } void loop() { DEBUG_PRINT("HELLO"); delay(1000); }

Viewing the debug information can be done in one of two ways: by viewing all device events or viewing the events for a specific device. To view all events from all devices, you can either navigate to https://console.particle.io/events or click the icon in the left of the window as shown below. From the main page, the center window shows all the events occurring, and any event that matches “DEBUG” is the event triggered by the device under test.

How To Debug the Photon

However, this shows all events from all devices, which may be somewhat difficult to navigate if there are multiple devices publishing data. To view individual device events, navigate to My Devices > click the specific device, and the events window for that device should appear.

How To Debug the Photon

TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum