Skip to content

Instantly share code, notes, and snippets.

@vielmetti
Last active November 9, 2019 20:27
Show Gist options
  • Save vielmetti/9f5892da1f30ac6a0a1e to your computer and use it in GitHub Desktop.
Save vielmetti/9f5892da1f30ac6a0a1e to your computer and use it in GitHub Desktop.
/*jshint devel: true, node: true*/
/**
* Start an embedded Node-Red instance
*/
var http = require('http'),
express = require("express"),
path = require("path"),
_ = require("lodash"),
RED = require("node-red"),
fs = require("fs"),
lame = require("lame"),
Speaker = require("speaker")
;
// Config vars - NB: LAN details are LOCAL. If accessing from the WAN (Internet), use the WAN details instead
var mqttSrv = '192.168.1.167', // The LAN address for the MQTT server
mqttWsPort = '9001', // The LAN port for the websocket interface to the MQTT server
nrSrv = '192.168.1.167', // The LAN address for this Node-Red server
nrPort = '8000' // The LAN port for this systems web interface
;
var wanMqttSrv = '192.168.1.167', // The WAN address for the MQTT server
wanMqttWsPort = '9001', // The WAN port for the websocket interface to the MQTT server
wanNrSrv = '192.168.1.167', // The WAN address for this Node-Red server
wanNrPort = '8000' // The WAN port for this systems web interface
;
// Create an Express app
var app = express();
// Add a simple route for static content served from 'public'
app.use("/", express.static("public"));
// Add static route for bower components
app.use('/bower_components', express.static( path.join(__dirname, '/bower_components') ) );
// Create a server
var httpServer = http.createServer(app);
// Create the settings object - see default settings.js file for other options
var nrSettings = {
httpAdminRoot: "/red", // Access the admin web i/f from http://<nrSrv>/red
httpNodeRoot: "/", // Access other NR served pages from http://<nrSrv>/
userDir: path.join(__dirname, '.nodered'), // default: $HOME/.node-red
nodesDir: path.join(__dirname, 'nodes'), // adds extra locn, defaults are userDir/nodes & node-red/nodes
verbose: true, // For better debugging
debugMaxLength: 1000, // max length of debug output
paletteCategories: [
'subflows', 'input', 'output', 'function', 'storage', 'advanced', 'formats', 'Raspberry Pi', 'social', 'analysis'
],
functionGlobalContext: { // enables global context
// os:require('os'),
// arduino:require('duino') // directly control Arduino's over serial, https://www.npmjs.com/package/duino
// -- Pass config variables into NR for reference -- //
'mqttSrv' : mqttSrv,
'mqttWsPort' : mqttWsPort,
'nrSrv' : nrSrv,
'nrPort' : nrPort,
'wanMqttSrv' : wanMqttSrv,
'wanMqttWsPort' : wanMqttWsPort,
'wanNrSrv' : wanNrSrv,
'wanNrPort' : wanNrPort,
// Pass in the path library for cross-platform file system specs
'path' : path,
'fs' : fs,
'_' : _,
'lame' : lame,
'Speaker' : Speaker
}
//httpStatic: '/home/pi/node/nr/static', // only for standalone NR
};
// Initialise the runtime with a server and settings
RED.init(httpServer, nrSettings);
// Serve the editor UI from /red
app.use(nrSettings.httpAdminRoot, RED.httpAdmin);
// Serve the http nodes UI from /
app.use(nrSettings.httpNodeRoot, RED.httpNode);
httpServer.listen(8000);
// Start the runtime
RED.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment