I know I can get a Particle.variable JSON string using a command in my browser like
https://api.spark.io/v1/devices/1111111111111/passDouble?access_token=2222222222222222
where passDouble is a defined variable. The very useful output is in the form
{"cmd":"VarReturn","name":"passDouble","result":481,"coreInfo":{"last_app":"","last_heard":"2018-02-18T06:22:07.927Z","connected":true,"last_handshake_at":"2018-02-18T06:17:46.909Z","deviceID":"31002c000d47353136383631","product_id":6}}
which I can parse to display the result of “481” on a web page.
Can we do something similar with particle.publish or does it always produce a server send event? (like a websocket it keeps sending multiple JSON sets of data which is much harder to parse)
My working example code is use Device ID for 1111… and access token for 2222222…
Every 3 seconds my webpage shows the new random number put into the variable “myDouble” and sent as the particle.variable “passDouble”
Following is my web page using async and await to fetch the url
<script>
async function myGet() {
myData = await fetch('https://api.spark.io/v1/devices/1111111111111/passDouble?access_token=2222222222222222')
myBetter = await myData.json()
document.getElementById('myA5').innerHTML = myBetter.result + '<br>'
setTimeout('myGet()', 3000)
}
</script>
<input type=button value=new4 onclick="{
myGet()
}">
<div id="myA5">...</div>
And the simple .ino is just generating a random number to present to the web page.
double myDouble = 0;
void setup()
{
// variable name max length is 12 characters long
Particle.variable("passDouble", &myDouble, DOUBLE);
}
void loop() {
delay(1000);
myDouble = random(111, 999);
}
The reason I am curious is I really like published events and I was wondering about mesh devices and keeping track of 40 devices would be very easy with an organized web page.