Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Upgrade to Support Latest Node #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ node_modules

# Optional REPL history
.node_repl_history

package-lock.json
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:7.7.4-alpine
FROM node:18-alpine
ADD . /code
WORKDIR /code
RUN npm install
Expand Down
45 changes: 39 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@

The virtual sensor application is a handy utility to generate sensor data in JSON format and publish it over MQTT or MQTT-TLS. This is practical when you don't have physical sensors available or if you'd like to simulate a variety of data for testing purposes.

During the rest of these labs, you may continue to use your physical sensors, or if you like you can use this virtual sensor program. The labs in the rest of this workshop are compatible with both approaches.
During the rest of these labs, you may continue to use your physical sensors, or if you like you can use this virtual sensor program. The labs in the rest of this workshop are compatible with both approaches.

## Download and Install
## Download and Install

The source code can be found on Github in the SSG-DRD-IOT organization. You may need to install **git** first.

### Mosquitto

Make sure to install mosquitto.

If you are on Mac,

```bash
brew install mosquitto
brew services start mosquitto
```

If you are on Ubuntu:

```bash
sudo apt install -y mosquitto
sudo systemctl start mosquitto
```

### Virtual Sensor

```bash
sudo apt-get install git
```
Expand All @@ -20,9 +41,10 @@ git clone https://github.com/SSG-DRD-IOT/virtual-sensor
cd virtual-sensor
```

mosquitto
Here is the help text of the virtual sensor program.
```

```bash
Generates sensor data traffic over MQTT and MQTT-TLS.

Data is always sent to a topic like *sensors/__name of sensor__/data* where the name is set by the **-n** option.
Expand Down Expand Up @@ -60,17 +82,21 @@ Analog Sensor Options
```

## Setup

First you must install the dependencies

```shell
npm install
```

Now you can continue with the examples

## Examples

### Default parameters

If you don't specify any parameters an analog sensor named *"temperature"* will a minimum of 17 and maximum value of 30 will be created. The MQTT traffic will be sent to a MQTT broker on the *sensors/temperature/data* topic on the localhost machine and data will be echoed to the console every 2 seconds.

```bash
$ node virtual-sensor.js
Connected to MQTT server at mqtt://localhost:1883/
Expand All @@ -82,7 +108,7 @@ Publishing sensors data on sensors/temperature/data

### Subscribing

If you want to see the data going into your MQTT topic you can use mosquitto to subscribe to that topic and see the data that is coming in.
If you want to see the data going into your MQTT topic you can use mosquitto to subscribe to that topic and see the data that is coming in.

```
mosquitto_sub -h localhost -p 1883 -t "sensors/temperature/data"
Expand All @@ -95,39 +121,46 @@ mosquitto_sub -h localhost -p 1883 -t "#"

```



### Analog and Digital Data Sources

Create a Analog data source named "light-sensor" with a minimum output value of 300 and a maximum of 500

```bash
node virtual-sensor.js --name "light-sensor" --min=300 --max=500
```

Create a digital GPIO data source named "relay"

```bash
node virtual-sensor.js --name "relay" --digital
```

### Hostname and Port Options

Create a digital GPIO data source named "relay"

```bash
node virtual-sensor.js --name "relay" --digital
```

### Encryption Options

Enable MQTT-TLS. This changes the port to 8883 and assumes the certificates and key files to be stored in the /etc/mosquitto directory.

```bash
node virtual-sensor.js --tls
```

### Delay and Timeout options

Put a 5 second delay between sensor readings.

```bash
node virtual-sensor.js --delay 5000
```

Stop virtual sensor after 10 seconds.

```bash
node virtual-sensor.js --timeout 10000
```
15 changes: 6 additions & 9 deletions optionDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

// Command line argument definitions
const optionDefinitions = [{
export const optionDefinitions = [{
name: 'help',
description: 'Display this usage guide.',
alias: 'h',
Expand Down Expand Up @@ -140,7 +140,7 @@ const optionDefinitions = [{
]


const sections = [{
export const sections = [{
header: 'A Virtual Sensor Application',
content: 'Generates sensor data traffic over MQTT and MQTT-TLS'
},
Expand Down Expand Up @@ -171,10 +171,7 @@ const sections = [{
}
];


var ex = {
optionDefinitions: optionDefinitions,
sections: sections
}

module.exports = ex;
export default {
optionDefinitions: optionDefinitions,
sections: sections
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node virtual-sensor.js --tls"
},
"type": "module",
"keywords": [
"sensors"
],
Expand Down
34 changes: 17 additions & 17 deletions virtual-sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@
'use strict';

// A library to colorize console output
var chalk = require('chalk');
import chalk from 'chalk'

// Require MQTT and setup the connection to the broker
var mqtt = require('mqtt');
import mqtt from 'mqtt'

// Libary to parse command line arguments
const commandLineArgs = require('command-line-args')
const getUsage = require('command-line-usage');
import commandLineArgs from 'command-line-args'
import getUsage from 'command-line-usage';

// Command line argument definitions
const cmdLineArgsDefinitions = require('./optionDefinitions')
import cmdLineArgsDefinitions from './optionDefinitions.js'
const optionDefinitions = cmdLineArgsDefinitions.optionDefinitions
const sections = cmdLineArgsDefinitions.sections

// NodeJS Library to interact with a filesystem
import fs from 'fs';

// Parse the command line arguments
const options = commandLineArgs(optionDefinitions);

// Print the help text
if (options.main.help) {
info(getUsage(sections))
Expand All @@ -66,24 +70,20 @@ if (options.sensor.digital != true) {
// Set the default port number
var port = 1883;
if (options.encryption.tls != undefined) {
port = 8883;
port = 8883;
}
if (options.connection.port != undefined){
port = options.connection.port;
if (options.connection.port != undefined) {
port = options.connection.port;
}


if (options.encryption.tls != undefined) {
// NodeJS Library to interact with a filesystem
var fs = require('fs');

var KEY = fs.readFileSync(options.encryption.key);
var CERT = fs.readFileSync(options.encryption.cert);
var TRUSTED_CA_LIST = [fs.readFileSync(options.encryption.ca)];

var HOST = 'localhost';
if (options.connection.hostname != undefined) {
var HOST = options.connection.hostname;
var HOST = options.connection.hostname;
}

// Setup MQTT-TLS options
Expand Down Expand Up @@ -119,14 +119,14 @@ var topic = "sensors/" + options.sensor.name + "/data";

// On the client connect event run a function
// to log the event to the console
client.on('connect', function() {
client.on('connect', function () {
info("Connected to MQTT server at " + "mqtt://" + options.connection.hostname + ":" + port + "/");
info("Publishing sensors data on " + chalk.bold.white(topic));
/*
Runs a function to emit a temperature sensor value
every *delay* milliseconds
*/
setInterval(function() {
setInterval(function () {
// Get a random integer value between a min and a max
var value = randomIntBetween(min, max);

Expand Down Expand Up @@ -159,14 +159,14 @@ client.on('connect', function() {

if (options.main.timeout != undefined) {
info("Timeout set for " + options.main.timeout + "ms")
setInterval(function() {
setInterval(function () {
process.exit();
}, options.main.timeout)
}
});

// MQTT error function - Client unable to connect
client.on('error', function() {
client.on('error', function () {
info("Unable to connect to MQTT server");
process.exit();
});
Expand Down