The documentation is here. You’ll need to create the IFTTT service and access token. Skip over the section on publishing and event and the next item is calling a function.
If you don’t have any parameters to your function call you still need to put an empty JSON body in the Body field. I’m not positive this is the problem, but it might be. Just put left and right curly brackets in the Body field of the applet:
Could you please run this minimal html code withchrome and you
deviceID, accessToken and function name and then go inspect → console and give us back console logs.
I believe that it’s an issue with your access token but this is just guessing.
Minimal code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/particle-api-js@8/dist/particle.min.js"></script>
<style>
</style>
</head>
<body>
<script>
var accessToken = "your access token here"; //not display for security
var deviceID = "your dev. id here" //not display for security
var particle = new Particle();
function test(cmd){
var fnPr = particle.callFunction({ deviceId:deviceID, name:'your_function_name', argument: cmd, auth: accessToken});
fnPr.then(
function(data) {
console.log('Function called succesfully:', data);
}, function(err) {
console.log('An error occurred:', err);
});
}
test('');
setInterval(function() {
test('');
}, 5000);
</script>
</body>
</html>
I used postman to validate device id and token, both seem accurate.
I attempted to call a function, which resulted in an error. “error”: “Function lock not found”
Although: here it is correct in my code:
SYSTEM_THREAD(ENABLED);
void setup()
{
// Pin Configuration
pinMode(D2, OUTPUT); // Output to optoisolator, controls REMOTE START output
pinMode(D3, OUTPUT); // Output to optoisolator, controls UNLOCK output
pinMode(D4, OUTPUT); // Output to optoisolator, controls LOCK output
digitalWrite(D2,LOW);
digitalWrite(D3,LOW);
digitalWrite(D4,LOW);
// We'll declare our Particle.function that controls the start, lock, and unlock outputs
Particle.function("control",control); // When the IFTTT service asks for the function "control", the electron will employ the function control() from this app.
}
void loop()
{
// For this project, we don't have anything happening in the void loop
}
int control(String command) //
{
if (command=="lock")
{
digitalWrite(D4,HIGH); // Write pin D4 high to activate optoisolator, effectively sending a ground pulse to the car's LOCK wire
delay (250); // Wait 1/4 second
digitalWrite(D4,LOW); // Turn off output to the optoisolator
}
if (command=="unlock")
{
digitalWrite(D3,HIGH); // Write pin D3 high to activate optoisolator, effectively sending a ground pulse to the car's UNLOCK wire
delay (160); // Wait 1/4 second
digitalWrite(D3,LOW); // Turn off output to optoisolator
}
if (command=="start")
{
digitalWrite(D2,HIGH); // Write pin D2 high to activate optoisolator, effectively sending a ground pulse to the REMOTE START input wire
delay (250); // Wait 1/4 second
digitalWrite(D2,LOW); // Turn off output to optoisolator
}
}
I guess your Boron will be crashing with an SOS panic (unless you are on a really old device OS version) since you haven’t got a return statement in your function handler.
The function signature promises to return an int so your code has to do so.
I’d use three values for the three different commands and one in case none of the expected commands was executed.
int control(String command) {
if (command=="lock") {
digitalWrite(D4,HIGH); // Write pin D4 high to activate optoisolator, effectively sending a ground pulse to the car's LOCK wire
delay (250); // Wait 1/4 second
digitalWrite(D4,LOW); // Turn off output to the optoisolator
return 1;
}
if (command=="unlock") {
digitalWrite(D3,HIGH); // Write pin D3 high to activate optoisolator, effectively sending a ground pulse to the car's UNLOCK wire
delay (160); // Wait 1/4 second
digitalWrite(D3,LOW); // Turn off output to optoisolator
return 2;
}
if (command=="start") {
digitalWrite(D2,HIGH); // Write pin D2 high to activate optoisolator, effectively sending a ground pulse to the REMOTE START input wire
delay (250); // Wait 1/4 second
digitalWrite(D2,LOW); // Turn off output to optoisolator
return 3;
}
return -1;
}
One more important thing
Your control function should return some integer
if you not getting any compiler complains it’s mean that device OS is pretty old
If you decide to update your device OS you can get in trouble
SYSTEM_THREAD(ENABLED);
void setup()
{
// Pin Configuration
pinMode(D2, OUTPUT); // Output to optoisolator, controls REMOTE START output
pinMode(D3, OUTPUT); // Output to optoisolator, controls UNLOCK output
pinMode(D4, OUTPUT); // Output to optoisolator, controls LOCK output
digitalWrite(D2,LOW);
digitalWrite(D3,LOW);
digitalWrite(D4,LOW);
// We'll declare our Particle.function that controls the start, lock, and unlock outputs
Particle.function("control",control); // When the IFTTT service asks for the function "control", the electron will employ the function control() from this app.
}
void loop()
{
// For this project, we don't have anything happening in the void loop
}
int control(String command) // {
if (command=="lock") {
digitalWrite(D4,HIGH); // Write pin D4 high to activate optoisolator, effectively sending a ground pulse to the car's LOCK wire
delay (250); // Wait 1/4 second
digitalWrite(D4,LOW); // Turn off output to the optoisolator
return 1;
}
if (command=="unlock") {
digitalWrite(D3,HIGH); // Write pin D3 high to activate optoisolator, effectively sending a ground pulse to the car's UNLOCK wire
delay (250); // Wait 1/4 second
digitalWrite(D3,LOW); // Turn off output to optoisolator
return 2;
}
if (command=="start") {
digitalWrite(D2,HIGH); // Write pin D2 high to activate optoisolator, effectively sending a ground pulse to the REMOTE START input wire
delay (250); // Wait 1/4 second
digitalWrite(D2,LOW); // Turn off output to optoisolator
return 3;
}
return -1;
}