Using Spark.publish() with Simple JSON Data


#48

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.
[/quote]

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


#49

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.


#50

@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?


#51

@brianr,

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

Example:

// 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;

        Spark.publish("my_var_1",String(my_var_1));
    }
}

#52

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


#53

I put this up over here:


#54

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: 'https://api.spark.io/v1/devices/xxx/events?access_token=xxx',
            method: "GET"
});

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

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

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

    console.log(JSON.stringify(obj));
};

var onData = function(event) {
            var chunk = event.toString();
            appendToQueue(chunk.split("\n"));
};


requestObj.on('data', onData); 

#55

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!


#56

ups… Berlin, I meant :slight_smile:


#57

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


#58

@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!


#59

HI @bko, just did that.

Here we go:
http://techblog.hybris.com/2014/05/02/consuming-spark-core-sse-events-via-node-js/


#60

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.


#61

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.


#62

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.


#63

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


#64

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 https://api.spark.io/v1/events/Uptime/?access_token=<<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.


#65

I have cyan breathing led.

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


#66

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.


#67

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