Using Spark.publish() with Simple JSON Data


Hi @mwdll

The TTL or time to live value is set to 60 seconds in all the published responses. To the best of my knowledge this is a “reserved-for-future-expansion” thing and is not used right now.

The stream should be continuously broadcasting (including keep-alives every 9 seconds) but it does die from time to time. There are three variables, the core, the cloud and the browser and generally speaking it is the browser that fails for me, but it is sometimes hard to know. You can use curl to log events to a file on the command line on a PC too.

You can remove the connect button and just always connect to the stream when the html page loads. That would give you the best chance of restarting, but it makes debugging harder.

If you are worried about missed events, you should consider Spark.variables instead. With Spark.publish like in this tutorial, you get push-type behavior, but with Spark.variables it would pull-type behavior. Both are good but they just have different uses.

I am note sure what you mean by:[quote=“mwdll, post:47, topic:3469”]
It also doesn’t seem to be published, so I can’t leverage it in some downstream system.

So if you are having a specific problem, let me know and I will try to help.


Thanks @bko. That was exactly what I needed to know, that I’m not missing some great capability of related to TTL. I was hoping there was some short-term storage of events that TTL was defining… maybe that is for the future. I want to capture events on a server, but I don’t want to listen constantly. Will have to wait for webhooks and keep using a REST call to my server using TCPClient.


@bko, I’ve played with both of these tutorials with a great deal of success! They’re excellent write ups and the firmware and html is easy to follow!!!

I’ve searched at great length to find some example html just to show variables. That would seem an easier task than displaying an event, something that updates a web page as often as specified, but I’ve come up short trying to modify these spark.publish examples to show variables instead of events. Frankly, I think it’s just a matter of retrieving, parsing, and displaying the json, but again, comin’ up short… Any chance you could shed some light on how to do such a simple task?



You can use the Spark.publish() to “publish” the variables you need.


// publishjson.ino -- Spark Publishing Example 
unsigned long lastTime = 0UL;

int my_var_1 = 0;

void setup() {

void loop() {
    unsigned long now = millis();
    //Every 15 seconds publish uptime
    if (now-lastTime>15000UL) {
        lastTime = now;



I will write something up tonight and start a new thread on reading Spark.variables with an HTML web page.


I put this up over here:


Hi @bko, I am just at the #thingscon in Munich, met @zach and solved my problem to consume these events with a plain node.js program, e.g. from the server. I ripped the code off the spark CLI into a small standalone program and just post this here for others in case they need it. Below code uses the node request library, ripped out of the spark cli :slight_smile:

var request = require('request');
var extend = require('xtend');

var requestObj = request({
            uri: '',
            method: "GET"

var chunks = [];
var appendToQueue = function(arr) {
    for(var i=0;i<arr.length;i++) {
        var line = (arr[i] || "").trim();
        if (line == "") {
        if (line.indexOf("data:") == 0) {
            chunks = [];

var processItem = function(arr) {
    var obj = {};
    for(var i=0;i<arr.length;i++) {
        var line = arr[i];

        if (line.indexOf("event:") == 0) {
   = line.replace("event:", "").trim();
        else if (line.indexOf("data:") == 0) {
            line = line.replace("data:", "");
            obj = extend(obj, JSON.parse(line));


var onData = function(event) {
            var chunk = event.toString();

requestObj.on('data', onData); 


Hi @hansamann

Thanks for that! Node.js is not my thing so it will really help folks. Maybe you even want to write up your own tutorial using it–that would be great!

I wish I was there at Thinkscon with you and @zach ! Munich is a great city and I really enjoyed Wursthaus in der Au and of course, the beer. Have fun!


ups… Berlin, I meant :slight_smile:


:smiley: Well, you won’t find my favorite Bavarian pub there, but it is still a nice place. And there’s beer.


@bko, that new writeup was exactly what I was after! A simple writeup for displaying variables AND published streams gets people off on the right foot! I tried pulling apart spark helper, but as wonderful as that tool is, the code behind it was just too complex to try to simplify. Thanks so much for all that you do!


HI @bko, just did that.

Here we go:


Hi @captnkrunch

I put a new mini-tutorial up over here that builds a table of published core data dynamically.

I hope this give you some ideas for your database record idea.


The basic situation is a sensor farm periodically reporting events to a server but the sensor’s future activity is modified by the contents of a database. Thus, I need to transmit and record the event but i need to send data back to the reporting sensor. I agree that these tutorials should be self-contained so just echoing the sensor value back would be a great start. Thanks again.


Hi @captnkrunch

So I did another introductory tutorial here using both a Spark function and a variable. This one controls a servo,

The web database part of this is tricky since without a dedicated server, which I can’t really use for a tutorial, you are limited in what you can show.


IM getting waiting for data … connect and then no more.
My spark is online.


Hi @kiwibird1

Ok, let’s check a few things:

So you loaded the code above into your core and it is breathing cyan on the LED, right?

You remembered to edit the HTML file and put in your access_token and device_id’s, right?

Have you tried looking for you core’s event using curl? The curl program allows you to do HTTP requests like a web browser:

curl<<access token>>

That way you can tell if it is the core side or the web side that is not working.

You want to keep your access token secret, but your device id shows when you publish a public event, so we can see it. Right now, only three cores are publishing the Uptime event and only is doing it in JSON format:

So if you core is 53ff…1587, then it is publishing a JSON. Otherwise, I would start looking at the core side.


I have cyan breathing led.

ps how do I get email notifications on a topic? I just get the blue dot with tracking


Hi @kiwibird1

With curl or with the HTML, you don’t need the << >> angle braces around the numbers. You want to put just the numbers there.

Also you cannot use your device name, you must use the hex number device id in the HTML file for the deviceID variable.

To get email from the forum, click on your avatar picture in the upper right corner and select Profile. Now click on the Preferences button with the little gear and scroll down to the email section. There are several options for you to choose there.


The forum support is excellent thank you.
Im confused. Is my access token ok, and what is my device id. the 4752…