Publish test code only prints empty lines

When I flash publishme.ino from the sample library and watch the stream from a terminal using
curl https://api.particle.io/v1/devices/events?access_token=redacted
I see “:ok” and then no text but carriage returns are advancing the cursor. There is nothing in the console window either.
How do I troubleshoot this?

// publishme.ino -- Spark Publishing Example 
unsigned long lastTime = 0UL;
char publishString[40];

void setup() {
}

void loop() {
    unsigned long now = millis();
    //Every 15 seconds publish uptime
    if (now-lastTime>15000UL) {
        lastTime = now;
        // now is in milliseconds
        unsigned nowSec = now/1000UL;
        unsigned sec = nowSec%60;
        unsigned min = (nowSec%3600)/60;
        unsigned hours = (nowSec%86400)/3600;
        sprintf(publishString,"%u:%u:%u",hours,min,sec);
        Spark.publish("Uptime",publishString);
    }
}

What system version are you using?

Firmware 0.7.0

For me your code does produce the expected output just fine

Have you checked the output on console.particle.io/logs?

However, I’d slightly change it this way

        snprintf(publishString, sizeof(publishString), "%02u:%02u:%02u", hours, min, sec);
        Particle.publish("Uptime", publishString, PRIVATE);
  • snprintf() is safer since it won’t inadvertently overflow your buffer.
  • "%02u" will give you the commonly used time format with leading zeros: image
  • Spark is outdated and now called Particle
  • PRIVATE: you should not publish to the PUBLIC event stream without good reason and with 0.8.0 this parameter will be compulsory
1 Like

Great suggestions. The code now works.
I see regular Uptime entries on the console and in a terminal with the curl command.

Thanks for your help.

2 Likes