Trouble flashing device with Javascript SDK flash command

I’m trying to flash my device using the Javascript SDK and jQuery. The code looks like this:

$('#btn-flash-device').click(function(e) {

    console.log(device);

    device.flash('firmware.ino', function(err, data) {
        if (err) {
            console.log('error');
        } else {
            console.log('worked');
        }
    });

    e.preventDefault();
});

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?

Check this out: https://github.com/spark/sparkjs/blob/master/examples/node/flash-core.js

You might need the absolute path for the .ino file

1 Like

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

try https://api.particle.io/v1/devices/XXXXXXXX?access_token=XXXXXXXXX

Not sure if you should be passing the access token in the url though…

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.

1 Like

Hi Danof,

I am getting the same issues you had (r.form is not a function etc…), could you elaborate on how you fixed the problem.

I am trying to make a webpage to flash the firmware update.

Regards,

Jason.

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.

1 Like

Hi Jason,

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:

http://embed.plnkr.co/7WdS8h/

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:

flashDevice('YOUR_DEVICE_ID', 'YOUR_ACCESS_TOKEN');

My plunker example has functions for retrieving device id and access token as well.

I hope this helps.

1 Like

Nope, my example uses the library :wink: 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.

Hi Guys,

Thanks so much for your quick replies, I’ll let you know how I go.

Cheers,

Jason

1 Like

All working, thanks a lot guys, you saved me many hours of chin scratching.

1 Like