When I click the button, I can see the device object logged to my console but nothing else happens… My core doesn’t get flashed. ‘firmware.ino’ is sitting in the root of my directory but I’ve also tried uploading it to a server. The device object was created using spark.getDevice. Has anyone had success using the flash command?
Thanks, I appreciate your help! I tried an absolute path which gets me a little bit further along in that I’m now seeing an error:
Uncaught TypeError: r.form is not a function
I’m also getting a CORS error but I think that may be because of the error above… I tend to see CORS errors when something else is actually going wrong. I haven’t had issues with sending/receiveing POST and GET requests:
Error: CORS request rejected: https://api.spark.io/v1/devices/XXXXXXXX?access_token=XXXXXXXXX
So, it turns out that I was using the client side CDN version of ParticleJS. It appears that certain commands just can’t be run in this way (like flash, signal, stopSignal… etc.). I did, however, install the Spark Node package and have had success running the commands with Node.
Since the code runs on the client, it’s not enough to just stick the file in the same directory, you have to actually download it. I’m not sure what the best way to do that is, but I managed to get flashing working over here, check the source for the code.
let me know if that works for you.
It seems you can only flash from the server side NodeJS version of ParticleJS. I’ve since created a jQuery script that can flash from a front-end. I have a working example here:
This may very well be similar to #Moors7 example. There’s a few different things going on in this script, including login, device selection…etc. But the piece you’re most interested in is as follows:
var flashDevice = function(deviceId, accessToken) {
$.ajax({
method: "GET",
// url can be relative or absolute to your hosted script - this won't work in a local environment
url: "YOUR_URL_TO_FIRMWARE_HERE",
success: function(file) {
// create file
var f = new File([file], "firmware.ino");
// create form-data object and add file to it
var form = new FormData();
form.append("file", f);
// put firmware onto device
$.ajax({
async: true,
crossDomain: true,
url: "https://api.particle.io/v1/devices/" + deviceId + "?access_token=" + accessToken,
method: "PUT",
headers: {},
processData: false,
contentType: false,
mimeType: "multipart/form-data",
data: form
});
}
});
};
To use it, you’ll need to include jQuery, the above script and call the function, like this:
Nope, my example uses the library It's just that you first have to get the file before you can upload it, much like you do in your own example. When loading the webpage, the file isn't simply included with the other files. You need to explicitly GET it. After that, you can use the functions just fine.