Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Debugging Code for the Particle Core/Photon

DZone's Guide to

Debugging Code for the Particle Core/Photon

This tutorial shows how to create a debugging tool for your Particle device, since their Web IDE does not provide a console.

· IoT Zone
Free Resource

Cisco IoT makes digital transformation a reality in factories, transportation, and utilities. Learn how to start integrating with Cisco DevNet.

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 thisdebug 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

Cisco is a software company. Surprised? Don’t be. Join DevNet to explore APIs, tools, and techniques that developers are using to add collaboration, IoT, security, network priority, and more!

Topics:
iot ,hardware ,debugging

Published at DZone with permission of Dustin Collins, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}