But shortly after in Particle Console I see a ‘spark/flash/status’ event with data ‘failed’. I have tried both full and relative names for the binary file. I am using version 6.0.3 of particle-api-js.
To run this you will need an auth.js file with your credentials, like:
I ran into the same problem when trying to flash a Photon via JavaScript from a web-app. Turns out you need to make an HTTP GET request for the file and specify the responsetype of the request. I got it to work with the following code:
// Create a new XMLHttpRequest.
// Just giving a url to the binary file does not work when flashing firmware. We first have to GET the file
var xhr = new XMLHttpRequest()
// Set the event handler for when the request is completed
xhr.onload = function(e) {
// Create a new file from the response
var file = new File([xhr.response], 'lights.bin')
// Flash the binary firmware to the specified device
var flash = lights.app.particle.flashDevice({deviceId: device.id, files: { file1: file} })
flash.then (
function(data) {
console.log('Device flashing started successfully', data)
}, function(err) {
console.log('An error occured while flashing the device, trying again', err)
}
)
}
// Get the file
xhr.open("GET", url)
// Specify the filetype, important!
xhr.responseType = "arraybuffer"
// Make the request
xhr.send()
When using this with Node.js, there’s no need to get the file, since it should be in the specified directory. For web apps you do need to GET the file first though :o
I was able to reproduce the failure to flash behavior with 6.0.3, but it works correctly with 5.3.1 for me.
npm install particle-api-js@5.3.1
Also: Unrelated to your problem, but you don’t have to particle.login in every time you use the Particle API JS. You can just save the accessToken string in your configuration file and pass it directly into any function. This eliminates the need to save the username and password; you can just save the access token.