# Node-red notes 

### Update to new versions

	sudo npm install -g --unsafe-perm node-red
	sudo systemctl restart nodered

### Log

	sudo journalctl -f -u nodered

Per generare messaggi nel log da node-red:

	node.log("Something happened");
	node.warn("Something happened you should know about");
	node.error("Oh no, something bad happened");

### Visibilità delle variabili (scope)

	// initialise the counter to 0 if it doesn't exist already
	var count = context.get('count')||0;
	count += 1;
	// store the value back
	context.set('count',count);
	// make it part of the outgoing msg object
	msg.count = count;

Scope types:

* __context__ Static data at node level
* __flow__ Static data at flow level
* __global__ Static data at global level

Ref: <https://nodered.org/docs/writing-functions>

### Change the node status

	node.status({fill:"red",shape:"ring",text:"disconnected"});
	node.status({fill:"green",shape:"dot",text:"connected"});
	node.status({text:"Just text status"});
	node.status({});   // to clear the status

### Trasmissione msg.payload in javascript all'interno di un nodo Dashboard Template

	<script>
	    var theScope = scope;
	
	    $("#splash_layer").on("click", function(){
	       theScope.send({payload: 'click'});
	    });
	</script>

### Ricezionee msg.payload in javascript all'interno di un nodo Dashboard Template

	<script>
		(function(scope) {
		    scope.$watch('msg.payload', function(data) {
		        $("#message_line").html(data);
		        setTimeout(function(){ 
		            $("#message_line").html("timeout");        
		        }, 3000);
		    });
		})(scope);
	</script>

## Installing

### Roadrunner
	curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
	sudo apt-get install -y nodejs
	sudo npm install -g --unsafe-perm node-red

* <https://nodered.org/docs/getting-started/installation>
* <https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions>

### Rasbian Buster Lite 2019-07-10

Type at command line prompt:

	bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

Enable the [systemd service](/systemd) by typing:

	sudo systemctl enable nodered.service

Then start it:

	sudo systemctl start nodered.service

## Get the access to the NodeRED IDE:

* <http://raspberrypi.local:1880>

## Enabling NodeRED projects

Install __git__ by typing:

	sudo apt-get update
	sudo apt-get install git

Enable the project feature editing the file __setting.js__:

	sudo nano .node-red/settings.js

Add these lines inside the __module.exports__ session:

	module.exports = {
    
	    editorTheme: {
	       projects: {
	           enabled: true
	       }
	    },

	...

Restart NodeRED:

	sudo systemctl restart nodered.service
	
Check for any errors at NodeRED startup:

	sudo journalctl -f -u nodered -o cat

* <https://nodered.org/docs/user-guide/projects/>

## Security

Install __node-red-admin__

	sudo npm install -g node-red-admin

Create a password hash by typing:

	node-red-admin hash-pw
	
Add these lines in __.node-red/settings.js__ inside the __module.exports__ session:

	module.exports = {
		adminAuth: {
		    type: "credentials",
		    users: [{
		        username: "admin",
		        password: "$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.",
		        permissions: "*"
		    }]
		},

Restart NodeRED:

	sudo systemctl restart nodered.service
	
Check for any errors at NodeRED startup:

	sudo journalctl -f -u node-red -o cat

* <https://nodered.org/docs/security>
* <https://nodered.org/docs/node-red-admin>


## Integrazione Node-red con Alexia

Installare il nodo:

* [node-red-contrib-alexa-home-skill](https://flows.nodered.org/node/node-red-contrib-alexa-home-skill)
 
Quindi crearsi un account nel sito <https://alexa-node-red.bm.hardill.me.uk/docs>


## Links

* Documentazione ufficiale <https://nodered.org/>
* Flows <https://flows.nodered.org>
 

@include='bio_sergio_tanzilli'

