I have been working on an Android app and want to ensure that the Electron is running the correct program. To do this I created a cloud variable:
Particle.variable("Version-v6e", "");
Since I am using the variable name (not the value), I can check the version a lot faster since the Particle Cloud retains a list of registered variables and does not need to talk to the device to get the name (rather than value) of the variable for each request. (I am using this API call: https://api.particle.io/[device_name/id]?access_token=[access_token]")
The problem I am running into is when the Electron is in safe mode. In this case, the Electron remains connected (so the last_head statistic is continuously updated to the Cloud) & the variable “Version-v6e” remains on Particle’s Cloud variable list.
I basically have no way of telling the difference between safe-mode and regular operation without contacting the device each time. I want the device to contact the cloud and remove the variable “Version-v6e” from the variable list before it enters safe mode.
Is there a way to remove the variable from the Particle Cloud before entering safe mode?
I initially thought I could do something along the lines of:
I’m considering a work-around, but am looking for an easier way.
My idea is to create a retained boolean variable, and then reset the Electron. When the device restarts, check the retained boolean in setup(): if true enter safe mode & make boolean false, else register cloud variable “Version-v6e”.
I really don’t want to make safe-mode a retained variable since, I don’t want to unplug/flash the Electron every time it is not supposed to be in safe mode. Any suggestions?
That’s an interesting technique, but you’re going to run into a few problems because that’s not how it’s intended to be used.
The last heard from date is not continuously updated. It’s the time the session was established. If you’re continuously online (not using sleep modes), this could be as much as 7 days ago.
It’s not possible to detect you’re currently in safe mode from user firmware, because it’s not run. That’s the point of safe mode, to prevent user firmware from running.
You have a very small window in which to set variables and functions after boot. Within a few seconds of the start of setup(), typically. After that, any changes to variables will be ignored.
You cannot remove a variable or function. You can only not register them on the next session, which requires disconnecting from the cloud, reset, or sleep.
From my understanding of the last heard values, one can get two different last heard values from the cloud.
If one request a list of all of the devices tied to the account from the Cloud, then I believe last heard is the value of when the session was established
If you request an individual device, then the last heard values is the time of the last connection from the cell tower. If the device is online then it will have connected within the last 23 minutes.
I was hoping I could remove the variable name from the Cloud list before I entered safe mode. This way if I miss the event “spark/status/safe-mode” I will still be able to see that the program is not running…
So after reading your last bullet, I inserted “Particle.disconnect();” before entering safe mode and it removed all of the variable names from the Cloud! This is perfect! Thank you for your input, in was very helpful.
I think my next step will be looking into how to get the device out of safe mode when I do not want to flash a new file. I’m thinking that it might be possible to flash a very small, incorrect binary file to the device, so that the device will run into an error when installing and exit safe-mode. I hoping that the cloud will not check the file before sending to my Particle device and that the previous firmware will remain on the device.
I was able to get out of safe mode remotely by flashing a binary file with the text “0” to the cloud. Without the “0” the cloud will run into an error. Remember to flash via a pre-compiled binary as mention in Particle’s Cloud API reference materials, so that the cloud will not try to compile the file.
As Rick said, you somewhat "can", but not during one session but only with a new session.
(a word of caution - or semantic nit-picking - about the "disconnecting" and "sleep" part in and off itself - AFAIK you need to reset the device (entering Safe Mode works in a similar fashion) to make it forget the previous list of registered items. A mere disconnect/reconnect or stop mode sleep will not help there as the device still knows what you had registered and will reregister and/or add new items - deep sleep and disconnect with a subsequent session "purge" (e.g. System.enterSafeMode()) will work tho')
So when you
While you cannot set the return value of a function as you'd attempt with Particle.variable("Version-v6e", "") = false;, you can use your technique to start a new session, don't register any new items, connect to the cloud and then enter Safe Mode after the "clean" session was established.