Using Spark.publish() with Simple JSON Data

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

1 Like

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.

1 Like

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…

Hi @kiwibird1

The access token is the longer number (40 hex digits) that you can get from the Build web IDE by clicking on the gear at the bottom left of the page. This number represents “you” to Spark cloud. You should not share your access token with anybody or publish it here in the forum since someone could take over your core(s).

The device ID is a shorter number (24 hex digits) that you can get from the Build web IDE by clicking on the target icon (second from the bottom) on the left. You should then see the name you have given your core(s) and “>” which you should click on to get actual core device ID number.

1 Like

I got my device id from command prompt, and it was wrong. I am now working. :slight_smile:
In the big picture, is there something like tinker for web browsers? If not how to start?
the docs say
"Deploying a Spark web appComing soon! We’ll give you instructions for how to deploy a web app on Heroku that can talk with a Spark Core."

1 Like

Hi @kiwibird1

You can look through my tutorials here in the forum. I have written a bunch like this JSON one to show how to do things with your using an HTML page in a browser. Recently I wrote another HTML file for the Controlling Two LEDs of the 'net example. That is here and is very similar to Tinker. I have been meaning to finish an HTML/Javascript Tinker controller but I haven’t finished it yet.

For Heroku, you have a lot more options since you are running a server, of course. I know that the Spark team really likes Heroku too.

To get email notifications from a single topic whenever something is posted, go to the bottom of the page, and click the button with the blue dot which says “tracking”. You should get some alternative options, with explanation of what they do. I believe “watching” notifies you of every new post in that topic.

1 Like

HI @Photonic

The line above should match your published event so where you have ‘cm’ you should instead have ‘distance’, the name of the event that you are publishing.

You should be able to change any of the strings but if it doesn’t seem to work, try reloading the page. If that doesn’t help, then open up the Javascript debug console in your browser (I like Chrome for this) and see if you are getting an error.


Spot on. That was one of the errors. Solved 1 piece.
Also a good experience that sometimes the intended characters are mistyped. E.g the quotation marks were in a strange italics format.
The JS debug was a great help as well.

Now I’ll need to find out why am I receiving 0 value for distance.

1 Like

Hello! Hope this thread is still active. i have a question, as I am running into issues posting JSON to my webhook. It seems that when I do post the brackets are wrapped in double quotes, causing errors. For example, I will post some serial data with several variables and I get"{CONTENTS}", more specifically, this is what happens:

"Serial_data": "{&quot;.5-1&quot;: &quot;5&quot;, &quot;1.1-1.5&quot;: &quot;0&quot;, &quot;1.6-2.0&quot;: &quot;0&quot;, &quot;2.1-2.5&quot;: &quot;0&quot;, &quot;2.5-2.9&quot;: &quot;0&quot;}"

Is there a way to avoid this? Here is the code that is posting the string:

    if(millis() - keenTimer > 30000){
    sprintf(keenString, "{\".5-1\": \"%u\", \"1.1-1.5\": \"%u\", \"1.6-2.0\": \"%u\", \"2.1-2.5\": \"%u\", \"2.5-2.9\": \"%u\"}", half, one, oneHalf, two, twoPlus);
    Spark.publish("keen", keenString, PRIVATE);

Here is the webhook I am trying to fire off:

"event": "keen",
"url": "MY_URL",
"requestType": "POST",
"json": {
    "Serial_data" : "{{SPARK_EVENT_VALUE}}",
    "published_at": "{{SPARK_PUBLISHED_AT}}",
    "coreid": "{{SPARK_CORE_ID}}",
"mydevices": true

Hi @BarryAllen

The only thing I can see is that your fields names in the JSON aren’t alpha characters but numeric instead. I don’t know what the rules are, but you could try changing “.5-1” to something like “A” and test again.

Maybe @Dave can comment on the web hook part and &quot; you are seeing there. I am not sure how that is being recoded.

Totally a stab in the dark, but try escaping your escapes:

This way, the (single) backslashes should survive the sprintf() call so that they can work as intended when posted to the webhook. I think.