The purpose of this project is to provide simple directions on how to establish communication between NodeJS application and Particle Photon. We are not going use Publish/Subscribe events, only NodeJS.
PreparationIn this demo, we are not using any sensors or actuators. You need Particle Photon, account on particle.io and NodeJS installed on PC or BeagleBone or Raspberry... For NodeJS you need install 'particle-api-js'
npm install particle-api-js --save
SoftwareIn this demo we have two application. One application is running on Particle Photon and other on PC as NodeJS script. Particle Photon firmware provide two very simple features. We could call them "cloud variable" and "cloud function". Cloud variable mean that you have ability to read value of some variable declared in Photon application from remote destination using Particle cloud service.
- Photon code for Cloud variable
int int_value;
Particle.variable("int_val", &int_value, INT);
- NodeJS code for using Photon variable
particle.getVariable({ deviceId: d_id, name: 'int_val', auth: l_token }).then(
function (data) {
console.log('Device variable retrieved successfully:', data.body.result);
}, function (err) {
console.log('An error occurred while getting attrs:', err);
});
Above code shows how to write both sides (Photon & NodeJS). With Particle.variable(), variable int_value is mapped to int_val term for cloud access. To request value from NodeJS we are using particle.getVariable(), and you can see second parameter is 'int_val'. When NodeJS execute this statement, it send request to Particle Cloud and ask for int_val value from Photon with deviceId.
Cloud function mean that you have ability to initiate executing Photon function from remote destination and receive result if needed.
- Photon code for Cloud function
Particle.function("int_fun",int_function);
int int_function(String command) {
if (command=="wifi_rssi") {
return WiFi.RSSI();
}
if (command=="blink") {
led_state = !led_state;
digitalWrite(led,led_state);
return led_state;
}
}
- NodeJS code for Cloud function
var fnPr = particle.callFunction({ deviceId: d_id, name: 'int_fun', argument: 'wifi_rssi', auth: l_token });
fnPr.then(
function (data) {
console.log('Function called succesfully WiFi_RSSI: ', data.body.return_value);
}, function (err) {
console.log('An error occurred:', err);
});
var fnPr2 = particle.callFunction({ deviceId: d_id, name: 'int_fun', argument: 'blink', auth: l_token });
fnPr2.then(
function (data) {
console.log('Function called succesfully LED: ', data.body.return_value);
}, function (err) {
console.log('An error occurred:', err);
});
On the Particle side we have two statements. First, Particle.function() map internal int_function to "int_fun" term. When we need call function, we are going to use "int_fun" as name of the function. We have parameter for int_function(String command). With that parameter we can send additional instruction, what we want to Photon do. In this example we prepare two commands : wifi_rssi and blink. When "wifi_rssi" command is sent, Photon return Wireless signal strength. When "blink" command is sent, Photon change status of LED connected to pin D7. On the NodeJS side we have particle.callFunction() method. Two main parameters are "name" and "argument". Value of "name" parameter is name of our Cloud function (in this case 'int_fun') and with "argument" we send data as command.
When Photon finish with executing internal function, with return statement it send result back to NodeJS application.
ConclusionParticle Photon and Particle cloud service are simple but with strong power. I know, you could ask why I didn't use Events? Publish and subscribe? My intention is show solution without storing data in Particle event hub. In that case, you need write complex stream receiver on NodeJS side. Internet of Things is coming, and our devices should talk ...
Full code for Photon and NodeJS are attached as file in this article.
Comments