Industrial Internet Application Development
上QQ阅读APP看书,第一时间看更新

Preparing an SD card

To prepare an SD card, follow the sequence of actions as described:

  1. Download the latest Raspbian LITE image (available at https://www.raspberrypi.org/downloads/raspbian/).
  2. Connect your SD card to a computer and use Etcher (https://etcher.io/) to flash the Raspbian .img file to the SD card.
  3. Enable SSH:
cd /Volumes/boot
touch ssh
  1. To enable Wi-Fi, create wpa_supplicant.conf with the following content:
network={
ssid="YOUR_SSID"
psk="YOUR_WIFI_PASSWORD"
}
To create a file in a Linux console, you can use the GNU nano editor. It is pre-installed in most Linux distributives. All you need is to run the nano FILE_NAME command and follow the displayed instructions.
  1. Create the /home/pi/hub folder.
  2. Create the /home/pi/hub/package.json file with the following content:
{
"name": "hub",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"modbus": "0.0.16",
"request": "^2.81.0"
}
}
  1. Create the /home/pi/hub/index.js file with the following content, replacing REMOTE-SERVER-ADDRESS.com and REMOTE-SENSOR-ADDRESS with real values:
var request = require('request');

var log = console.log;
//var mb = require('modbus').create(true); // enable debug output
var mb = require('modbus').create();

var sensor = 'REMOTE-SENSOR-ADDRESS';
var receiver = 'http://REMOTE-SERVER-ADDRESS.com:8080';

mb.onError(function (msg) {
log('ERROR', msg);
});

// create master device
var ctx = mb.createMaster({

// connection type and params
con: mb.createConTcp(sensor, 1502),
//con: mb.createConRtu(1, '/dev/ttyS1', 9600),

// callback functions
onConnect: function () {
log('onConnect');
log(ctx.getReg(2));
ctx.setBit(1, false);

//send to receiver
var data = {
device: 'sensor1',
timestamp: Date.now(),
reg2: ctx.getReg(2)
};
request.post({url: receiver, form: data}, function (err) {
if (err) console.log('Failed to send to ' + receiver);
});

ctx.destroy();
},
onDestroy: function () {
log('onDestroy');
}
});
  1. Create a /home/pi/hub/Dockerfile file with the following content:
FROM hypriot/rpi-node:boron-onbuild
RUN apt-get update && apt-get install -y libmodbus5
  1. Create the /home/pi/sensor folder.
  1. Create the /home/pi/sensor/package.json file with the following content:
{
"name": "sensor",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"modbus": "0.0.16"
}
}
  1. Create the /home/pi/sensor/index.js file with the following content, replacing REMOTE-HUB-ADDRESS.com with a real value:
var log = console.log;
var mb = require('modbus').create();

mb.onError(function (msg) {
log('ERROR', msg);
});

// create device memory map
var data = mb.createData({ countReg: 5, countBit: 2 });
data.setReg(2, 321);
data.setBit(1, true);
data.dumpData(); // show memory map

// create slave device
var ctx = mb.createSlave({

// connection type and params
con: mb.createConTcp('REMOTE-HUB-ADDRESS.com', 1502),
//con: mb.createConRtu(1, '/dev/ttyS0', 9600),

// data map
data: data,

// callback functions
onQuery: function () {
log('onQuery');
//ctx.dumpData();
log(ctx.getBits(0, 2));
},
onDestroy: function () {
log('onDestroy');
}
});

// destroy device
//setTimeout(function () {
//
ctx.destroy();
//}, 5000);
  1. Create the /home/pi/sensor/Dockerfile file with the following content:
FROM hypriot/rpi-node:boron-onbuild
RUN apt-get update && apt-get install -y libmodbus5