Using Spark.publish() with Simple JSON Data


Hi @Ali

It is not completely clear to me what you are trying to do here, but I take it that you want to store the credentials locally in the browser to avoid typing them in over and over. Some your particle API calls do not make sense to me, like your POST request.

If I were starting this today, I would recommend using ParticleJS, the Javascript API that is officially supported:


That’s is right. I am trying to store them locally. I will look into particleJS and see if that makes it any easier for me.


I’ve used that library on this page. There’s also local storage included, should you be interested.


Thank you!


I keep getting this error when trying to install spark.js.
I have CLI installed and its working fine.

The link for your example page opens fine on my phone. But does not show me the button when i open it using Chrome on my Desktop.


I just installed the latest Node.js on a clean system and get the same errors. Disregarding that, I do find the Library in there.
You should be able to find them in the directory_you_were_in\node_modules\spark path. Depending on what you want to do, you might not even need them like that. You can use the CDN to include the minified library into your webpage, if that’s what you’re making.

I also installed the latest Chrome version in which I do see the button. Not sure what’s going on there. Works in Edge, Chrome, Opera, (mobile) Safari at first glance. Haven’t tried others as of yet. The code is on GitHub as well, Should you find that easier.


Yea i will use CDN. The button doesnt show in Chrome or Firefox on another computer. It shows on my iphone. its connected to the same network. What im trying to make is a webapp that can use one login page and carry over DeviceID and Token over multiple pages. Pages would have Post events and Publish events.


Hi @Ali

When I have problems with Chrome (in particular) and Firefox, I always try incognito mode. Chrome in particular tries to cache lots of junk other browsers don’t and it can cause problems. These days when I want to run the web IDE, I always just start it in incognito mode to avoid problems, for instance.


Its not just Chrome. The browsers don’t seem to be downloading spark.min.js Couldn’t get anything to work. I just pointed the source to read locally to get it working for now


Hi again,

@andreh posted an amazing WEBAPP written in Angular Here. I modified the files to work for my project. He has however been missing on the forum and i desperately need some help with getting my publish events working. I am a novice at everything programming. His app.js file has a function for events built into it.

 /** Register a handler which will be called for each incoming event from any owned device. */
        registerMineEvents: function(handler) {
            this.registerDeviceEvents('mine', handler);

        publishEvent: function(event) {
            return $ + '/devices/events', event);

and he uses ng-repeat to show all the events in a list format.

 <div ng-show="events[] && events[].length > 0">
                            <table class="table">
                                        <th class="hidden-xs">Time</th>
                                    <tr ng-repeat="event in events[]">
                                        <td class="hidden-xs">{{event.published_at}}</td>

What if i want to show specific events by themselves instead of them creating sequential duplicates and update those events whenever a new value is pushed as per the programming in Photon. Kind of like the tutorial you did.

Any help will be greatly appreciated as i am quite stuck.


Hi @Ali

I am not an angular.js user so maybe @Moors7 or someone else could help more.

Here is how you register for the events and do the initial JSON parsing:

eventSource.addEventListener('EventNameHere', function(e) {
            var rawData = JSON.parse(;
            var parsedData = JSON.parse(;
        }, false);

Where I put ... you need to add code to find your table (I would name it and use getElementById) and then add data to it (I would use the insertRow method of the table).


Thank you for your reply. I will continue try using the example.

@Moors7 can you help out?


I figured out how his works. Angular has a filter option that can be used to call up specific events.


<tr ng-repeat="event in events[] | filter: 'yourEventName'  ">



Nice and usefull tuto.

I can’t manage to read those data with Particle.subscribe() on another particle.

Data is received, but no way to translate the data (char) in different integer to pass data from one device to another.

Any idea ?


Hi @sylvaing

In Javascript, lots of functions take strings and automatically convert them to integers. See this page for details:

In this case it seems like the Javascript like this is what you want:

  var myInt = parseInt( parsedData.myJSONField );


thx for your answer, but i want to receive and compute those data in my photon, ie not in javascript. I’m strugling with sscan() finction without any results for the moment.
like that:



If you provide the actual input string and the exact code you use, we might be able to help.

It’s sscanf() you want, and you should have same amount of receiving variables as you have place holders in the format string and you’d need to check the return value of sscanf() to know how many of your variables could be filled successfully.


yes sorry, i did use sscanf() without any good result.

Here is my code:

int led = D0;
int boardLed = D7;
int lightsensor = A0;
int soundsensor = A1;
int movesensor = A2;
int pushsensor = A3;
int power = A5;
int sensorValue = 0;
int needUpdate = 1;
int lightValue = 0;
int soundValue = 0;
int moveValue = 0;
int pushValue = 0;
int buddyLightValue = 0;
int buddySoundValue = 0;
int buddyMoveValue = 0;
int buddyPushValue = 0;

char message[40];
char receivedMessage[40];

char myName[12] = "lehio_3";
char buddyName[12] = "lehio_3";

void setup() {
    pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
    pinMode(boardLed,OUTPUT); // Our on-board LED is output as well
    pinMode(power,OUTPUT); // Our on-board LED is output as well
    Particle.subscribe(buddyName, myHandler);
    analogWrite(power,4095); // max 4095

void loop() {

    delay(5000); // 1000 = 1s - So scan every 5 sec
    // light data analysis
    sensorValue = analogRead(lightsensor)/16; // from 0-255
    if (sensorValue != lightValue) {
        needUpdate = 1;
        lightValue = sensorValue;
// Here other data analysisi... (deleted)

    // send datas only if required
    if (needUpdate == 1) {
        needUpdate = 0;

void myHandler(const char *event, const char *data)
    Particle.publish("debug1",data); // Here I have the requires data          
    Particle.publish("debug2",message); // HERE IT DON'T WORK...


Hi @sylvaing

Here some things that are wrong in your code:

  • Are you really using JSON structure in your publish? A JSON is a structure text representation that uses field names in double-quotes, followed by a colon, followed by the data. Your sscanf parameter string does not follow this format at all.

  • As @ScruffR pointed out above, you have four %u’s in your string but are passing five integer addresses.

  • The first integer address is repeated. I cannot imagine that this gives good, repeatable results.


After reformatting your code it seems as if there is an error with your curly braces in the last function too.

You are using “%u” but have signed int as receiving variables. Is this intentional?

analogWrite() on a non DAC pin like your power (A5) supports max 255 and not 4095 as you use in this


What’s the point of the first printf() in this?

    if (needUpdate == 1) {
        needUpdate = 0;

What exactly “don’t work”?`

    Particle.publish("debug2",message); // HERE IT DON'T WORK...

I can’t see this in your post.
What does the string you actually receive look like?
What do you see published as debug1 and debug2?
We can guess what it should look like from reading the code and we could flash your code to our own devices and try, but since you already got that data, just post it.
Also with a description what you’d expect it to look like.