Which tool should I use to debug my particle project and how to debug it

Hi,there
I am a newer for the particle.I have Written my code and compile it successfully.But when I flash it into my Particle Core.The Core doesn’t run as expected.I wonder Which tool I should use to debug my Particle Project and how to use it .
I am here waiting for your sincere help.thanks.

Oh, cheers. Debugging embedded systems like this that don’t have line-by-line tools can be tedious.
It’s best in cases like this to use the USB serial port as a debug terminal, inserting Serial.println() statements.

In setup(), add the following line:

  Serial.begin(9600);       // for debug.  Has to be first in the code, or the USB-RS232 driver doesn't start

This will open a USB serial port at 9600 baud; you are welcome to other speeds if necessary.

In sections of your program that aren’t functioning properly, you can use something like this:
Serial.print("MyVar: "); Serial.println(MyVar, DEC);

You can also make your code wait for you to press a key in the serial terminal with the following code:
while (!Serial.available()) Spark.process();

Keep in mind with all these debugging methods, microprocessors run many millions of instructions per second. It’ll probably zip through your code before you can realize what happened, or spill out debug messages by the hundreds.

Note that the USB serial port is a little persnickety in Windows. Not only is it easy to lock the port up (requiring you to unplug the Core, and plug it back in, as well as close and re-open the Serial Terminal in Particle Dev), if you debug certain number patterns, Windows is likely to confuse your Core for a serial mouse. There are solutions for that on this forum; personally, I had to do a lot more than a simple registry edit with Windows 7 x64 to get it to stop installing serial mouse drivers.
That being said, it’s probably the best way to debug your programs on the Particle platforms. In Particle Dev, the serial monitor is the lowest button on the left side (it looks like the USB symbol). Click it, and select the serial port from the list. NOTE: You have to wait for your code to start running for the Core to initialize its serial port. To prevent locking the port up, close the Serial monitor window before resetting/programming/unplugging your Core.

Hope this helps.

1 Like

If you’re building locally, you can use the Particle Programmer Shield. This allows you to step-debug through code in an IDE.

1 Like

This is probably the most helpful thing I’ve found in Community. The resources in Particle never really laid this out so well on how to insert debug into the code.

I would like to add the steps for getting a terminal application to show the output after it is in the code.

1. Flash binary code via CLI over USB to Electron
(see Particle_Build (WEB) → USB Flash for Electron)
(be sure you’ve logged in on terminal, and installed the particle CLI software)

Code Example:
void loop() {

digitalWrite(led1, HIGH);         //Turn on module D7 blue led

delay (1000); //Delay to avoid problems
Count = Count+1;
while(!Serial.available()) Particle.process();
Serial.println(Count); // Prints out the device ID over Serial
}

2. Let Electron boot up on the new firmware to flashing Cyan

3. Open Terminal Window
a. particle login
b. particle serial list → prints list address of particle USB connections
c. particle serial monitor → data starts to show on screen

Option: “Particle Dev” IDE – terminal window Serial Monitor

  1. Open Particle Dev
  2. Open Terminal window (click on USB symbol on left side tool bar).
  3. The Terminal window open in Particle Dev (Atom)
  4. Select the device from the terminal pull down window “/dev/cu.usbmodemFA131” and then click Connect.
  5. Type “particle serial monitor” (w/o quotes) in the command line at bottom of terminal window.
  6. Data will start showing on screen (example is a simple counter every second)
1 Like

I’m not convinced that particle serial monitor is a good tool in connection with

while(!Serial.available()) Particle.process();

AFAIK particle serial monitor is noly good for watching the output but not to send anything to the device in order to break out of the while() - unless recent version added that feature to CLI

And 5. should not be required after 4.