Tutorial: Getting Started with Spark.publish()


#4

@Bradders Spark.publish allows you push data out from the core as opposed to Spark.variable which you’d have to poll for. So for example, instead of polling your core every 30 seconds to see if a motion sensor has been triggered, you can instead use Spark.publish to trigger the event the moment the motion sensor goes off.


#5

@bko This is awesome! Thanks for putting this together!


#6

bko, thanks this was a great help.


#7

bko, Good work… Working very fine…


#8

I’m stuck. Clicked on “Connect”, got “Waiting for data”. Using Chrome. Javascript console says “Opened!”. I’m at 5 minutes or more, but still “Waiting for data.” Spark breathing cyan. Any suggestions? Problem solved, just reflashed Spark Core and webpage responded immediately.

Well done tutorial. Thank you!


#9

I am glad you got it working! There is a follow-on tutorial doing a similar thing but using a JSON from the core to web, to show how to return multiple values that are easily parsed on the web page.


#10

@bko Can you post Spark Publish example with PHP proxy, it is not working for me. Help Appreciated!


#11

Are you using @wgbartley PHP proxy from here?

You then have to change your URLs to be more like in this example:

$.get('/proxy.php?'+CORE_ID+'/variable_name', function(response) {
        console.log(response);
});

#12

Hi @bko, what argument would one use to pass in a string rather than an int or a double? looks like I can’t pass in a string or char through %u, %f, or %e. Thought I would ask you before I try every letter of the alphabet!
Thank you!


#13

Hi @jkkim93

If the char array string already completely formed, you can just publish it. If you want to use sprintf() to add some formatting, the %s is how to say that.


#14

@bko , if I pass the array in without sprintf(), how would I call to print it on the html side? Right now I’m parsing json, but it is coming out as null (json.data)- wondering if you could do a short explanation of how to read in arrays to print on the html side. thanks!


#15

In this example, the parsedData.data should be the string that you are sending. If you are getting a null, something is wrong on one end or the other.


#16

Awesome tutorial @bko Thank you very much for taking the time to put it together. I’ve been pulling my hair out trying to get this to work.


#17

Thank you very much for this tutorial.
I think, this is what I need for the development I’m doing.

However: I have copied and pasted you sample INO and did the same with the HTML, replacing the Device_ID and AccessToken.
However: When I click the ‘connect’ button, nothing happens.
I see ‘Errored’, in the Ctrl-Shft-J info.

For sure I checked both, and found everything in order, as far as I’m concerned…

What could be wrong?
I have no clue where to look.


#18

Hi @RensD

Can I ask what browser you are using? The event stuff does not work Internet Explorer. Try Chrome for best results.

The device_id and access_token hex strings need to be in double-quotes.

You can also use curl to see your event on the command line. That way you can make sure the core is sending events.


#19

Thanks for the reply.

(I’m using Chrome)
By sending the http-request manually, I got the full error message:
{ “code”: 400, “error”: “invalid_grant”, “error_description”: “The access token provided has expired.” }

So, after renewing the token, all worls fine. :slight_smile:


#20

Excellent, thanks so much!


#21

So I am having the same problem that @MiltSchreiber was having.

The webpage will open and when I click on Connect it says “Opened!” however it just breathes cyan and says waiting for data.

I have checked the accessToken and I have checked the deviceID and I have reflashed, saved, and reloaded all of the code.

I am using Google Chrome and coding in the Spark IDE and flashing it from there.

I just can’t seem to find the issue that is causing this…

Any suggestions? I know I am not giving much information on this but I am not sure what I need to provide. I can get whatever you may need to help troubleshoot this.

Thank you for your help


#22

Hi @busterdavidson

Let’s try to figure out which part is not working, the core or the web.

There are currently three users publishing the “Uptime” event to the public event stream that this tutorial uses:

Do you see your core’s device id in the table? If so that means you are publishing, just not receiving. If not, we can look at the code on the core. You would likely be one of the first two, since the third entry is for the next tutorial using JSONs.

In Chrome you can open the Javascript console by pressing Ctrl-Shift-J on Windows. Reload the web page and click connect again. See if any messages come up on the console like “Opened!” or “Errored!”. If you get the opened message, click on the Network tab at the top of the debug console to see the streams in use. You should see a stream with your access token and the size should be increasing periodically.

Let me know and we can go through the steps to debug it.


#23

No mine was not listed but at the time I also was not running that piece of code. I can re flash my core and attempt again.

I appreciate your help!