Hop on over to the Particle.io store and order any of their cool devices.
While developing this plugin and the demo app I used a Photon Kit and it was a joy to work with.
Thanks, Brandon Satrom for sending one over!
tns plugin add nativescript-particle
If you want to just play with your Particle device without writing code yet, follow these steps to install the demo app I've created with NativeScript Core:
git clone https://github.com/EddyVerbruggen/nativescript-particle
cd nativescript-particle/src
npm i
npm run demo.ios # or demo.android
Tip: If you get tired entering your login credentials every time you log in, set the
PARTICLE_USERNAME
andPARTICLE_PASSWORD
properties to reflect your own.
Want to see the demo in action? Check out this short video πΊ.
All examples below assume you have these imports and instantiated the Particle
class:
import { Particle, TNSParticleDevice } from "nativescript-particle";
const particle = new Particle();
Communication between your app and a device is HTTP (REST) based, so the first step is authenticating yourself with the Particle Cloud:
particle.login(
{
username: "my-particle-username@mydomain.com",
password: "my-particle-password"
})
.then(() => console.log("Login successful"))
.catch(error => console.log(`Login error: ${error}`));
Once done interacting with your device(s) it's best to log out as this will do a little cleanup in the plugin and underlying SDK.
There's no reason not to because it couldn't be easier:
particle.logout();
Make sure you've claimed a device in your Particle account, then do this to list them in your app:
particle.listDevices()
.then((devices: Array<TNSParticleDevice>) => {
if (devices.length === 0) {
console.log("No devices have been claimed in this account.");
} else {
console.log("Devices fetched.. now do something neat with 'em.");
}
})
.catch(error => console.log(`Error fetching devices: ${error}`));
The returned list of TNSParticleDevice
objects has these properties and functions:
Property | Type | Description |
---|---|---|
id | string |
The unique ID of this device. |
name | string |
The given name of this device. |
status | string |
The current status of the device, usually normal . |
type | TNSParticleDeviceType |
One of Unknown , Core , Photon , P1 , Electron , RaspberryPi , DigistumpOak , RedBearDuo , Bluz . |
functions | Array<string> |
The list of functions currently available on the device. You can invoke these with callFunction (see below). |
variables | Array< TNSParticleDeviceVariable > |
The list of variables currently available on the device. You can get their values with getVariable (see below). |
You can invoke any of the functions
you discovered on the device.
As an example let's assume you've flashed this code tutorial to your device,
so there's a led
function which takes 1 argument: the value must be either "on"
, or "off"
:
const myDevice: TNSParticleDevice = null; // you got this from 'listDevices'
myDevice.callFunction("led", "on")
.then(result => console.log(`Result: ${result}`))
.catch(error => console.log(`Error in callFunction: ${error}`));
What if you have a function which takes multiple arguments? Let's assume you're using the tinker app and want to set "D7"
to "HIGH"
via the "digitalWrite"
function:
myDevice.callFunction("digitalWrite", "D7", "HIGH")
.then(result => console.log(`Result: ${result}`))
.catch(error => console.log(`Error in callFunction: ${error}`));
Getting a variable is quite similar to callFunction
.
Let's say you have a variable named "analogvalue"
, then this will give you the current state of that variable:
const myDevice: TNSParticleDevice = null; // you got this from 'listDevices'
myDevice.getVariable("analogvalue")
.then(result => console.log(`Result: ${result}`))
.catch(error => console.log(`Error in getVariable: ${error}`));
markoImake for adding a few very cool features.
Happy IoT'ing! πΉπ€πͺπ²π‘πΈπβπ¦ππ