Using particle Java Script to get data from function on Electron

I have read numerous pages, everything seems to dance around my answer and I cant wrap my head around it. I had a working asset tracker project, but the java script was using the spark api.
the tutorial I followed years ago only used device id and access token to retrieve data from a function, then I parsed the data into the useful information. this is the part of my webpage to get the data

     device.onEvent(false, function(data){ 
    if ( == "GPS") { 
   var gpslocation =',');
   var lat = gpslocation[0]; 
   var lat = lat.replace(/[{}]/g, ''); 
   var lat = parseFloat(lat);
   var lon = gpslocation[1];
   var lon = parseFloat(lon);
   var mph = gpslocation[2];
  var batt = gpslocation[3];
  var batt = batt.replace(/[{}]/g, '');			
  var mph = parseFloat(mph);
 var batt = parseFloat(batt);
 var mylocation=new google.maps.LatLng( lat, lon);

and this is where I call the function

I define my acessToken as a variable, it just seems as if there isn’t anyway to use the api without user and password , which id rather not use.

What exactly is your question?

how to use java script to pull data from an electron using the particle api without having to use username and password. the function on the electron is called
Particle.function("gps", gpsPublish);
I was able to use the acess token with the spark api , but the information on particle docs for the particle java script is confusing.

The SDK isn’t really that complicated.
You just need to understand the “common” flow of “drilling” down to the entity you want to access.

  1. you need the account that owns a device
  • either you already have an access token for that account, or
  • you need to acquire it via a login
  1. you need to select the respective device via the access toke you got in step 1
  2. on the selected device tap the desired endpoint

Since you already have the access token and device ID, you’d use an API that takes these two pieces of data to hand you an object representing the desired device - e.g. this one :wink: and then use any method that object exposes
if you only want to call a specific function on a particular device but don’t need to do anything else with that device just use that respective API

If you looked at the docs for particle.login() you’d see that this API retrieves the access token and stores it for future use - if you already have that token, you wouldn’t need to acquire it that way of course :wink:

would the following

particle.callFunction({ deviceId: 'DEVICE_ID', name: 'brew', argument: 'D0:HIGH', auth: token });

have to be a variable like

var funtionCall =particle.callFunction({ deviceId: 'DEVICE_ID', name: 'brew', argument: 'D0:HIGH', auth: token });

what if the argument is not needed? leave blank or 0
and would the variable functionCall be the data returned from the function on the electron ?

Particle.function("gps", gpsPublish);

int gpsPublish(String command) {
    if (t.gpsFix()) {
        lat = (t.readLatDeg());
        lon = (t.readLonDeg());
        speedKnots = t.getSpeed(); 
        speedMph = speedKnots*1.151;
        if(speedMph < 2) { 
        speedMph = 0; }
      char data[256];
       snprintf(data, sizeof(data), " %f, %f, %.2f, %.2f, tinkerToy : %i " , lat, lon, speedMph, fuel.getVCell(), unitId);
      Particle.publish("GPS", data, PRIVATE);

        return 1;
    } else {
      return 0;

is the function ran when calling “gps”.
would the data be stored I the variable as a array ?

before I would use

spark.getDevice(deviceName, function(err, d) {
						device = d;

						device.onEvent(false, function(data){ 
							if ( == "GPS") { 

I keep looking to try and figure out what device = d means , not sure where d came from. I had followed instructions that were on I think havicstudios .com , but all his stuff is gone now. im at the point id be willing to pay someone to help me figure this out and help me to understand so I don’t have so many questions

It seems your actual problem is not the Particle SDK but rather missing the basic concepts of JavaScript.


The second parameter in your spark.getDevice() call is a “callback” function that will be triggered to deal with the results generated by the actual call. This callback function will receive two parameters (err and d) where the first one would be some error “object” but the second would represent the actual device object (reference) you were requesting.
And the active code of that callback is supposed to do something with these parameters - e.g. storing the object reference in a global variable (device) to be used elsewhere in your code.

From here

If you called the lambda parameter devID (instead of d) you’d obviously write device = devID;.

BTW, the last code snippet is not calling a Particle.function() but subscribing to an even.

Nope, this would be a promise. You need to get the reference to that promise in order to treat the asynchronous outcome of the call.
The example in the docs shows how it’s done.

If I didn’t have a lot of experience with JS I’d just pass an empty string as argument :wink:

The example calls a Particle.function("brew", ...) so in your case you’d replace 'brew' with 'gps' - not exactly rocket science.

BTW, you don’t actually want 'DEVICE_ID' in there, this should just inform the discerning reader what is meant to go there.