m2m is a client module for machine-to-machine communication system framework node-m2m.
npm install node-m2mjs
$ npm install m2m
`
$3
For projects requiring raspberry pi peripheral access such as GPIO, I2C, SPI and PWM, you will need to install array-gpio module.
`js
$ npm install array-gpio
`
Using the Browser Interface
$3
Using the browser interface, you can download, edit and upload your application code from your remote clients and devices from anywhere.
To allow the browser to communicate with your application, add the following m2mConfig property to your project's package.json.
`js
"m2mConfig": {
"code": {
"allow": true,
"filename": "device.js"
}
}
`
Set the property allow to true and provide the filename of your application.
From the example above, the filename of the application is device.js. Replace it with the actual filename of your application.
$3
Using the browser interface, you may need to restart your application after a module update, code edit/update, or a remote restart command.
Node-m2m uses nodemon to restart your application.
`js
$ npm install nodemon
`
You can add the following nodemonConfig and scripts properties in your project's npm package.json as auto-restart configuration.
`js
"nodemonConfig": {
"delay":"2000",
"verbose": true,
"restartable": "rs",
"ignore": [".git", "public"],
"ignoreRoot": [".git", "public"],
"execMap": {"js": "node"},
"watch": ["node_modules/m2m/mon"],
"ext": "js,json"
}
"scripts": {
"start": "nodemon device.js"
},
`
From the example above, the filename of the application is device.js. Replace it with the actual filename of your application when adding the scripts property. Then restart your node process using npm start command as shown below.
`js
$ npm start
`
For other custom nodemon configuration, please read the nodemon documentation.
Code Edit and Auto Restart Automatic Configuration
Install nodemon.
`js
$ npm install nodemon
`
To configure your package.json for code editing and auto-restart without manual editing of package.json, start your node process with -config flag.
m2m will attempt to configure your package.json by adding/creating the m2mConfig, nodemonConfig, and scripts properties to your existing project's package.json. If your m2m project does not have an existing package.json, it will create a new one.
Assuming your application filename is device.js, start your node application as shown below.
`js
$ node device -config
`
Stop your node process using ctrl-c. Check and verify your package.json if it was properly configured.
If the configuration is correct, you can now run your node process using npm start command.
`js
$ npm start
`
Your application should restart automatically after a remote code update, an npm module update, a remote restart command from the browser interface.
$3
Unlike the device/server applications, users can create client applications without registering it with the server.
Node-m2m tracks all client applications through a dynamic client id from the browser.
If you have multiple clients, tracking all your clients by its client id is not easy.
You can add a name, location and a description properties to your clients as shown below.
This will make it easy to track all your clients from the browser interface.
`js
const m2m = require('m2m');
const client = new m2m.Client({name:'Main client', location:'Boston, MA', description:'Test client app'});
client.connect(() => {
...
});
``