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 using the following command:
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/sensor folder.
  2. 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": {
"rpio": "^0.9.16",
"ws": "^2.3.1"
}
}
  1. Create the /home/pi/sensor/index.js file with the following content, replacing REMOTE-SERVER-ADDRESS.com with a real value:
var WebSocket = require('ws');
var rpio = require('rpio');

var ws;
var receiver = 'ws://REMOTE-SERVER-ADDRESS.com:8080';
rpio.open(11, rpio.INPUT);

var establishConnection = function () {
ws = new WebSocket(receiver);
ws.on('close', establishConnection);
ws.on('error', establishConnection);
};
establishConnection();

var sendStatus = function () {
var status = rpio.read(11) === 0;
console.log('light status: ' + status);

var data = JSON.stringify({
device: 'raspberry',
timestamp: Date.now(),
light: status
});

try { ws.send(data); }
catch (e) { console.log('failed to send data to ' + receiver); }

setTimeout(sendStatus, 1000);
};
sendStatus();
  1. Create the /home/pi/sensor/Dockerfile file with the following content:
FROM hypriot/rpi-node:boron-onbuild