Debugging code for the Particle Core/Photon

I’ve been spoiled as a software engineer. When I write Python, Ruby or Javascript I have a REPL to try different ideas out. I can set breakpoints in PyCharm, RubyMine or the Chrome Devtools to inspect my stack at any point. The feedback cycle is short. I can TDD, even with infrastructure code. Developing code for hardware is not so straightforward. In this post I’ll share my current workflow for writing code for devices.

Hardware is messy; that’s part of the fun of it. In my free time I tinker with hardware: microcontrollers, sensors and robots. Writing code for Arduino and Particle devices can be frustrating. It can be difficult to tell why something isn’t working. Your code is perfect, but you have a broken connection somewhere or that new sensor you’re trying out was damaged in shipping. Or your code has a bug, but it’s hard to tell in the tangle of interrupts what your path of execution is.

Particle Web IDE

When you write code for a Particle device, you do it in their Web IDE. This is great, you can easily compile your code, flash your device, and add different community libraries as needed. The IDE doesn’t include a console though, so you can’t just values to console for inspection. You can, however, publish events from your device to the Particle cloud. I write some version of this debug function in my applications.

// Log message to cloud, message is a printf-formatted string
void debug(String message, int value) {
    char msg [50];
    sprintf(msg, message.c_str(), value);
    Spark.publish("DEBUG", msg);
}


// Toggle LEDs on and off, given a string like "7,0" or "4,1"
int ledToggle(String command) {
    int ledPin = -1;
    char * params = new char[command.length() + 1];

    strcpy(params, command.c_str());
    char * param1 = strtok(params, ",");
    char * param2 = strtok(NULL, ",");

    if (param1 != NULL && param2 != NULL) {
        ledPin = atoi(param1);
        debug("ledPin = %d", ledPin);
        // Check for valid digital pin
        if (ledPin >= 0 && ledPin < 8) { 
            int mode = (bool(atoi(param2)) ? HIGH : LOW)
            debug("mode = %d", mode);
            digitalWrite(ledPin, mode);
            return 0;
        }
    }
    return -1;
}

Now I can open up the Logs section in the Particle Dashboard and run commands with the Particle CLI to debug my code.

$ particle call core01 led "0,1"
$ particle call core01 led "1,1"

Particle logs

I’m not a C++ guru, so I’ve been using IdeaOne’s online C++14 compiler and interpreter to test small bits of code out. This tool has really sped up my workflow. Instead of compiling/flashing/running/logging on every change to my code, I usually mock up a function in IdeaOne and then bring it into the Particle Web IDE when I’ve got it working.

IoT development is moving fast, so I’m hopeful that we’ll see even more improvements to developer workflows for devices as more companies and open-source contributors jump in. I’m particularly interested in new and novel ways to optimize lean software delivery. Happy hacking!

My home office

 
29
Kudos
 
29
Kudos

Now read this

Lessons learned running a DevOps meetup

I’ve been organizing the Boston DevOps meetup for almost two years now. When the organizer spot opened up I jumped at the chance. My experience DevOps-ing along in smaller organizations gave me a pretty narrow view of the movement and I... Continue →