I’ve just started trying to use Particle.function to send data from a website to a Particle. While the data is being sent and the action is working as expected my JavaScript (jQuery) $.ajax() request is coming back with a Bad Request error.
The only thing I can think of is that this is because my site is on localhost but thought I would run it by the community to see if anyone else has had a similar problem.
JavaScript AJAX request
// Fire off the request to /form.php
request = $.ajax({
url: "https://api.particle.io/v1/devices/" + $("#deviceid").val() + "/settimer",
type: "POST",
data: {args: $("#minutes").val() , access_token: $("#accesstoken").val() }
});
I’m still having this problem but it seems to be a timeout and not a bad request. Do I need to be doing something to tell the API that my request was successful and send that back to the JavaScript in the browser?
Firmware code:
void setup() {
Particle.function("settimer", getMinutes); //listen for the number of minutes
//additional setup stuff
}
//this gets the input from our POST request and sets the number of minutes and runs the countdown
int getMinutes( String command ) {
int minutes = command.toInt(); //minutes from our function
float milliseconds = minutes * 60000; //number of milliseconds
float wait = milliseconds / 85; // total millisconds divided by 85 (we have 85 "steps" between 170 and 255 in the wheel function)
startCountdown(wait); //run the countdown function
}
To elaborate on that, you’d need a return X at the end, and preferably keep the function as short as possible. Set a ‘flag’ which you pick up on in your loop. Then handle all the waiting/actions there. That way your function can respond immediately, and won’t block the rest.
I was able to get the Particle.function() to return correctly by using the following code
bool checkFlag = FALSE; // global to check if getMinutes have been set
int timerMilliseconds;
void setup() {
Particle.function("settimer", getMinutes); //listen for the number of minutes
}
void loop() {
if( checkFlag ) {
startCountdown( timerMilliseconds );//run the countdown function
checkFlag = FALSE;
}
}
//this gets the input from our POST request and sets the number of minutes and runs the countdown
int getMinutes( String command ) {
int minutes = command.toInt(); //minutes from our function
float milliseconds = minutes * 60000; //number of milliseconds
float wait = milliseconds / 85; // total millisconds
timerMilliseconds = wait;
checkFlag = TRUE;
return timerMilliseconds;
}
It seems a bit silly to create timerMilliseconds but I was having trouble converting from float to int I also use wait in another function so couldn’t use the name globally. Any suggestions on how to handle this or is this an okay method of going about this?