Good evening everyone. I’m working on a project using particle photon. The project consists of turning on and off various devices. I noticed that using the code below as a simulation to connect two devices I get success. However, for one thing it’s not very good. The problem is that when sending a command to turn on one led and then send another command to turn on the second led, the second only turns on after sending another command through the node, that is, to turn on the second led you need two clicks or two commands via node.
I was doing it through HTML using buttons for the clicks when I noticed what happened, that’s when I did it directly through node believing that the fault was in the HTML code and it didn’t matter. Can anyone give a hint if there is any flaw in my code or if it has something to do with the particle API.
I tried it through Jquery using the POST method and it works the same.This is very important to me, as I cannot pass on to my customers an application that requires two clicks to turn on one device and one click to turn on another. Thanks.
int ledPin1 = D1;
int ledPin2 = D2;
int statusPin;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Spark.function("IoT", ledSwitch);
}
void loop() {
}
int ledSwitch(String command) {
if (command.equalsIgnoreCase("D1")) {
statusPin = !statusPin; // toggle the value
digitalWrite(ledPin1, statusPin);
return 1;
}
if (command.equalsIgnoreCase("D2")) {
statusPin = !statusPin; // toggle the value
digitalWrite(ledPin2, statusPin);
return 1;
}
return -1;
}
Commands below extracted from particle documents.
var fnPr = particle.callFunction({ deviceId: 'xxxxxx', name: 'IoT', argument: 'D1', auth: token });
fnPr.then(
function(data) {
console.log('Function called succesfully:', data);
}, function(err) {
console.log('An error occurred:', err);
});
var fnPr = particle.callFunction({ deviceId: 'xxxxx', name: 'IoT', argument: 'D2', auth: token });
fnPr.then(
function(data) {
console.log('Function called succesfully:', data);
}, function(err) {
console.log('An error occurred:', err);
});