# Cloud Event To Photon/Electron?

**URL:** <https://community.particle.io/t/cloud-event-to-photon-electron/24287>\
**Category:** General\
**Created:** [July 10, 2016, 9:04pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287 "2016-07-10T21:04:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdugat](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jdugat/32/11171_2.png) [@jdugat](https://community.particle.io/u/jdugat)\
**Post date:** [July 10, 2016, 9:04pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/1 "2016-07-10T21:04:47Z")

</div>

Is there a way to set a PIN to HIGH or send a message to a Photon/Electron ?

I’m trying to place an Electron on a jet ski, however, would only want to have the electron publish the GPS coordinates when I ask it to. The logical place to do this is with a web page containing the access token and device ID. I know web hooks can act on data published with the Particle.publish() function, however, I’m wondering if there is reverse of that from the cloud to the device?

Any help would be appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [July 10, 2016, 9:15pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/2 "2016-07-10T21:15:40Z")

</div>

So, this?  
[https://docs.particle.io/reference/firmware/electron/#particle-function-](https://docs.particle.io/reference/firmware/electron/#particle-function-)

---

<div class="post-metadata">

**Author:** ![jdugat](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jdugat/32/11171_2.png) [@jdugat](https://community.particle.io/u/jdugat)\
**Post date:** [July 10, 2016, 9:27pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/3 "2016-07-10T21:27:49Z")

</div>

Follow-up Question:

From the example, exposing a function with Particle.function is a way to send a integer value to the photon/electron from Web POST.

Does this chew up data between the cloud and the photon or electron, or is it event/interrupt driven?

Example from the documentation:  
[https://api.particle.io/v1/devices/0123456789abcdef/brew](https://api.particle.io/v1/devices/0123456789abcdef/brew) \ -d access\_token=123412341234 \ -d “args=coffee”

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [July 10, 2016, 9:39pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/4 "2016-07-10T21:39:39Z")

</div>

Any and all remote internet based actions, be it from the device to the cloud or otherwise will consume data. How else would you communicate with it via the cloud? The alternative is to send it a text, using a non-Particle SIM, but that’s more expensive, so I wouldn’t recommend that.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [July 11, 2016, 12:55am UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/5 "2016-07-11T00:55:35Z")

</div>

It does “chew up” data but it should be a fairly small amount of data and you’d only be sending it whenever you want GPS coordinates.

---

<div class="post-metadata">

**Author:** ![jdugat](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jdugat/32/11171_2.png) [@jdugat](https://community.particle.io/u/jdugat)\
**Post date:** [July 11, 2016, 2:12am UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/6 "2016-07-11T02:12:41Z")

</div>

So, the amount of data it would send would be less than sending GPS long/lat once every hour?

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [July 11, 2016, 2:53am UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/7 "2016-07-11T02:53:16Z")

</div>

That would depend. If you are only requesting lat/long on demand then it would depend on how often you are requesting GPS data. What is your use case?

---

<div class="post-metadata">

**Author:** ![jdugat](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jdugat/32/11171_2.png) [@jdugat](https://community.particle.io/u/jdugat)\
**Post date:** [July 11, 2016, 10:22am UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/8 "2016-07-11T10:22:28Z")

</div>

The GPS data would only be demanded if lost/stolen, someone didn’t come back to the boat/meeting place on time or to test to make sure everything is working. it would send/publish data in the following format GMT Time, Date, Long, Lat. (about 45 characters).

07:00.00,11072016,+29 38.1207’,-095 46.3992’

---

<div class="post-metadata">

**Author:** ![jdugat](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jdugat/32/11171_2.png) [@jdugat](https://community.particle.io/u/jdugat)\
**Post date:** [July 11, 2016, 1:43pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/9 "2016-07-11T13:43:28Z")

</div>

I have the following code, however, when I try to trigger the “find” Particle.function(); using the following URL

(device ID and access token have been shorten). I’ve verified my access token to be correct, so I’m thinking my URL isn’t correct.

[https://api.spark.io/v1/devices/4f004/find?access\_token=1fa33?args=doh](https://api.spark.io/v1/devices/4f004/find?access_token=1fa33?args=doh)"

I get back the following:

{  
“error”: “invalid\_token”,  
“error\_description”: “The access token provided is invalid.”  
}

```
int val = LOW;

void setup() 
{
  pinMode(D1,OUTPUT);
  digitalWrite(D1,LOW);
  Particle.function("find", pubGPS);
}

void loop()
{
   val = digitalRead(D1);
   if (val == HIGH)
     {
        Particle.publish("Location","+28 38.5417,-81 56.3664");
         delay(1000);
         digitalWrite(D1,LOW);
     }
}
    
int pubGPS(String command)
    {
      if(command == "doh")
      {
        digitalWrite(D1, HIGH);
        return 1;
      }
      else return -1;
    }

```

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/2/26406834c8d6ce4ae24b5c85502bbbf96b8fa313.png)

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [July 11, 2016, 1:59pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/10 "2016-07-11T13:59:48Z")

</div>

For a function call you’d need a POST request, rather than the GET request your browser is making. If you want to just test for functionality, you could try this page to test the functions easily: [http://jordymoors.nl/interface](http://jordymoors.nl/interface)  
If you want to learn how to do it yourself, go with this:  
[Tutorial: Spark Variable and Function on One Web Page](http://community.particle.io/t/tutorial-spark-variable-and-function-on-one-web-page/4181)

Or using the JS library perhaps this one:

> **[Web Page Sample Code using Particle API JS](http://community.particle.io/t/web-page-sample-code-using-particle-api-js/21590)**
>
> I hadn’t used the Particle API JS from a web page yet, so I made a little circuit and little sample web page to test it out. It worked well for me and I thought others might find it useful for tips and tricks. For example, it shows how to do...

---

<div class="post-metadata">

**Author:** ![jdugat](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jdugat/32/11171_2.png) [@jdugat](https://community.particle.io/u/jdugat)\
**Post date:** [July 11, 2016, 8:58pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/11 "2016-07-11T20:58:57Z")

</div>

Thanks for the tips. I’ve now decided to move away from the Particle.publish() and Particle.function() approaches. I found Particle.variable() works much better and less overhead for me. The only problem I have now is how to parse what is coming back from the URL. The HTML I have below, won’t do a post (pressing the get! button) returns a 401 unauthorized error in the console. This appears due to the fact that the access\_token is not making its way to the GET request. I’m fairly certain, I’m missing something and I hope you can point me in the right direction. I’m fairly certain the access\_token being null is my problem. I’m just not sure where to add it. Any help would be greatly appreciated.

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/4/4b1bbdbc583de016572ba8b90dd077dbeafc9a0b.png)

[https://api.particle.io/v1/devices/4f004/lonlat?access\_token=1fa3](https://api.particle.io/v1/devices/4f004/lonlat?access_token=1fa3) (shorten to protect the deviceID and tokenID)

The URL below returns

{  
“cmd”: “VarReturn”,  
“name”: “lonlat”,  
“result”: “+29 38.1126’,-095 46.3988’”,  
“coreInfo”: {  
“last\_app”: “”,  
“last\_heard”: “2016-07-11T20:22:41.957Z”,  
“connected”: true,  
“last\_handshake\_at”: “2016-07-11T17:49:28.164Z”,  
“deviceID”: “4f004”,  
“product\_id”: 10  
}  
}

```
<html>
   <head>
     <title>GPS Dashboard</title>
   </head>
   <body>
     <td>
       <div class="input-group input-group-sm">
   	      <span class="input-group-btn">
   	        <input type="text" class="form-control" placeholder="Click Get!" readonly="" id="lonlat4f004">
            <button class="btn btn-default" type="button" onclick="update('4f004', 'lonlat')">Get!</button>
          </span>
        </div>
     </td>
   </body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/sparkjs/1.0.0/spark.min.js" type="text/javascript" charset="utf-8"></script>
<script>
	var deviceId = "4f004";
	var access_token = "1fa3";
	
	function execute(deviceId, func) {
		argument = document.getElementById(func + 'input'); 
		spark.callFunction(deviceId,func,argument.value,null);
		argument.value = "";
	};
	
	function update(deviceId, variable) {
		spark.getVariable(deviceId,variable,function(err, data){
			console.log(data);
			variableValue = document.getElementById(variable + deviceId);
			variableValue.value = data.result;
		});
	};
</script>   
</html>
```

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [July 11, 2016, 9:15pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/12 "2016-07-11T21:15:07Z")

</div>

Hmm, it seems as though you’re still using the old spark JS library which has since been superseded by the Particle API library. If at all possible, do consider switching. If that’s not possible, then you can check these old docs. You’re missing a login in there (you can use an accesstoken or regular credentials for that). [https://github.com/spark/sparkjs/blob/master/examples/node/get-variable.js](https://github.com/spark/sparkjs/blob/master/examples/node/get-variable.js)

---

<div class="post-metadata">

**Author:** ![jdugat](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jdugat/32/11171_2.png) [@jdugat](https://community.particle.io/u/jdugat)\
**Post date:** [July 12, 2016, 2:29pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/13 "2016-07-12T14:29:23Z")

</div>

I used your code from [http://jordymoors.nl/interface/](http://jordymoors.nl/interface/) page, so I’m fairly certain it works. When I login to your page, I get what is shown in the screenshot below.

The URL works, it gives me the data back that I need, but I suspect to do it properly, I will need to execute it from within an HTML document and then capture what comes back for it work like your code below does. If I need to change the JS lib, I have no problem doing that, I was just going with what worked on your page. Also do I have to login if I use both the deviceID and Access\_Token?

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/a/a3afa483850467b2a7dd1dea7b74c841dbaff171.png)

[https://api.particle.io/v1/devices/4f004/lonlat?access\_token=1fa33](https://api.particle.io/v1/devices/4f004/lonlat?access_token=1fa33) (shorten to preserve the actual deviceID and  
Access Token)

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/7/7dcb2d43ec4476957167112348330a7580b69f79.png)

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [July 12, 2016, 2:48pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/14 "2016-07-12T14:48:58Z")

</div>

> [@jdugat](#):
>
> When I login to your page, I get what is shown in the screenshot below.

That's the important clue.  
The login procedure requests an access token which will then be used for the next page.  
So in order to have your own page do the same, you need to go through the login process just the same.

---

<div class="post-metadata">

**Author:** ![jdugat](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jdugat/32/11171_2.png) [@jdugat](https://community.particle.io/u/jdugat)\
**Post date:** [July 12, 2016, 4:29pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/15 "2016-07-12T16:29:54Z")

</div>

If I use this URL (shorten to hid the full deviceID and access\_token) and I’m not logged in, I get the results that follows the URL listed below. If I use both do I need to login?

[https://api.particle.io/v1/devices/4f0043/lonlat?access\_token=1fa334](https://api.particle.io/v1/devices/4f0043/lonlat?access_token=1fa334)

Results:

{  
“cmd”: “VarReturn”,  
“name”: “lonlat”,  
“result”: “+29 38.1213’,-095 46.4118’”,  
“coreInfo”: {  
“last\_app”: “”,  
“last\_heard”: “2016-07-12T13:55:28.958Z”,  
“connected”: true,  
“last\_handshake\_at”: “2016-07-11T17:49:28.164Z”,  
“deviceID”: “4f004”,  
“product\_id”: 10  
}  
}

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [July 12, 2016, 5:01pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/16 "2016-07-12T17:01:49Z")

</div>

You need to log in in order to acquire the access token rather than hard coding it.

---

<div class="post-metadata">

**Author:** ![jdugat](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jdugat/32/11171_2.png) [@jdugat](https://community.particle.io/u/jdugat)\
**Post date:** [July 12, 2016, 6:09pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/17 "2016-07-12T18:09:05Z")

</div>

Understood, but if I know what it is, can’t I just pass it and process the results?

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [July 12, 2016, 8:35pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/18 "2016-07-12T20:35:34Z")

</div>

It kinda depends on which library you’re using. The old JS library (the code above, and on my interface) requires you to log in with either credentials _or_ an accesstoken. It’s a requirement for the library to somehow internally store the accesstoken for further use. The new library however allows you to log in using your credentials only after which it gets an accesstoken that it stores/uses in the ‘user code’. Thus, if you know the accesstoken, you can skip the logging in step and just use the accesstoken as-is.

So, if you’re not going to use either library, then just use that fixed URL and handle the data any way you’d like. If you do intent on using a JS library, there are two options:

1. use the old library -\> log in, using the accesstoken or regular credentials.
2. use the new library -\> use an accesstoken directly, or acquire one by logging in with regular credentials.

---

<div class="post-metadata">

**Author:** ![jdugat](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jdugat/32/11171_2.png) [@jdugat](https://community.particle.io/u/jdugat)\
**Post date:** [July 12, 2016, 8:39pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/19 "2016-07-12T20:39:52Z")

</div>

By using the new lib, will I be able to parse the returned values?

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [July 12, 2016, 8:40pm UTC](https://community.particle.io/t/cloud-event-to-photon-electron/24287/20 "2016-07-12T20:40:51Z")

</div>

Would be kinda useless if you couldn’t, right…?  
[https://docs.particle.io/reference/javascript/#getvariable](https://docs.particle.io/reference/javascript/#getvariable)

[Next page](https://community.particle.io/t/cloud-event-to-photon-electron/24287.md?page=2)
