Articles Hierarchy

Articles Home » Processing » RPI_NODE_P5.JS

RPI_NODE_P5.JS

summary:


having a local server to play p5.js should be easy with a Raspberry Pi
using Node and Express server.


update NODE, install npm
make app
p5.js and sketch
Raspbian node server start tools
test about a forum question
node and database
node on RPI4



current update / info:
Raspbian GNU/Linux 10
DEBIAN buster
Linux version 4.19.66-v7+
Raspberry Pi 3 Model B Plus Rev 1.3
inet 192.168.1.204
cpu-temp=52.1'C
date: Sat 2019-10-05 17:44:44

already installed:
pi@RPI:~/projects/p5js $ node -v
v10.15.2



update NODE, install npm


follow my own article here
( in a temp dir what later is deleted, do:
but check on new version first:
)

wget https://nodejs.org/dist/latest/node-v12.11.1-linux-armv7l.tar.xz
tar -xvf node-v12.11.1-linux-armv7l.tar.xz
cd node-v12.11.1-linux-armv7l
sudo cp -R * /usr/local/
sudo reboot

pi@RPI:~/projects/p5js $ node -v
v12.11.1
pi@RPI:~/projects/p5js $ npm -v
6.11.3



make app


old way:
mkdir myapp
cd myapp
npm init
npm install express --save

new way:
sudo npm install -g express-generator
i work in
/home/pi/projects/nodeserver/
here should make a
package.json{
"name": "nodeserver",
"version": "1.0.0",
"description": "RPI node server",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"raspberry",
"pi",
"node server",
],
"author": "KLL",
"license": "ISC",
"dependencies": {
"express-session": "^1.17.1"
}
}


npm install express-session --save

/usr/local/bin/express -> /usr/local/lib/node_modules/express-generator/bin/express-cli.js
+ express-generator@4.16.1
added 10 packages from 13 contributors in 2.778s

express --view=pug myapp
cd myapp
npm install
DEBUG=myapp:* npm start

> myapp@0.0.0 start /home/pi/projects/p5js/myapp
> node ./bin/www
myapp:server Listening on port 3000



p5.js and sketch


lazy as usual, i go my newer sketch ( to be sure to catch a 0.9.0rev ) and download it.
( note: that downloads contain all files flat structure, so later should clean up )
unzip and copy all to
myapp/public/
restart server and WORKS
inside my network from a PC browser ( firefox ) call
http://192.168.1.204:3000/
manually i combine it with a test reading a csv file from assets, but see in console only



Raspbian node server start tools


made also a
nano start#!/bin/sh
# p5js/myapp/start
MYPATH='/home/pi/projects/p5js/myapp'
echo '5s'
env sleep 5s
echo 'start node express 5pjs webserver, stop with [ctrl][c]'
cd $MYPATH
DEBUG=myapp:* npm start


chmod +x start
./start


well, to start that start you would need to go to that directory first...
nano start.desktop[Desktop Entry]
Version=1.0
Name=start
Name[en_GB]=start
Comment='node express p5js server'
Exec=lxterminal -e '/home/pi/projects/p5js/myapp/start'
Icon=kll_favicon.png
Terminal=false
Type=Application
Categories=Utility;KLL;
StartupNotify=true


and that you might copy to your desktop. OR
at
/home/pi/.config
create a /autostart subdirectory and copy that start.desktop in:
/home/pi/.config/autostart/start.desktop
( and reboot )



failed with build p5js editor


test about a forum question


also ref to Processing forum question
that is from the coding train, but not use the p5.js at all,

so just for test i replaced the index.html ( from p5.js ) with this
but the local csv file i tested already,
assets/test.csv
and get

so i renamed that index.html to test.html,
got the original data from the coding train website and put it also under assets/
and call the page with:
http://192.168.1.204:3000/test.html
manually:



test more @Kent's csv script, this works


node and database


try to get under node npm a database running


for the server RPI i try ( via terminal )
sudo apt install mariadb-server ( 150 MB / [Y] )
( expected usual admin user password question BUT nothing ???) try
mysql -u user -p
only errors, well not have user or password configured OR DEFAULTS?
lucky i typed

sudo mysqlpi@RPI4:~ $ sudo mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 42
Server version: 10.3.17-MariaDB-0+deb10u1 Raspbian 10

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> help

General information about MariaDB can be found at
http://mariadb.org

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don't write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'

MariaDB [(none)]> status
--------------
mysql Ver 15.1 Distrib 10.3.17-MariaDB, for debian-linux-gnueabihf (armv7l) using readline 5.2

Connection id: 42
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 10.3.17-MariaDB-0+deb10u1 Raspbian 10
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 13 min 50 sec

Threads: 7 Questions: 63 Slow queries: 0 Opens: 32 Flush tables: 1 Open tables: 26 Queries per second avg: 0.075
--------------
MariaDB [(none)]> help contents
You asked for help about help category: "Contents"
For more information, type 'help ', where is one of the following
categories:
Account Management
Administration
Compound Statements
Data Definition
Data Manipulation
Data Types
Functions
Functions and Modifiers for Use with GROUP BY
Geographic Features
Help Metadata
Language Structure
Plugins
Procedures
Table Maintenance
Transactions
User-Defined Functions
Utility

MariaDB [(none)]> help account management
You asked for help about help category: "Account Management"
For more information, type 'help ', where is one of the following
topics:
CREATE USER
DROP USER
GRANT
RENAME USER
REVOKE
SET PASSWORD

MariaDB [(none)]> quit
Bye
pi@RPI4:~ $



here find some better help
sudo mysql_secure_installation
Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y


$mysql -u root -p
password

CREATE USER 'pi'@'localhost' IDENTIFIED BY 'raspberry';

CREATE DATABASE processing;
USE processing;
status
GRANT ALL ON processing.* TO 'pi'@'localhost';
quit


$mysql -u pi -p
raspberry
use processing;
status


now a next step is about using SQL, best by making and executing sql files?
( bad style, never do the password thing like that!! )
nano dbdo with content:
#!/bin/bash
#
mysql -u pi --password=raspberry -t< "/home/pi/projects/p5jsdb/database/mycommand.sql" > "/home/pi/projects/p5jsdb/database/mycommand.log"

chmod +x dbdo
nano mycommand.sql with content for test:
use processing;
status;

./dbdo
cat mycommand.log
pi@RPI4:~/projects/p5jsdb/database $ cat mycommand.log
--------------
mysql Ver 15.1 Distrib 10.3.17-MariaDB, for debian-linux-gnueabihf (armv7l) using readline 5.2

Connection id: 44
Current database: processing
Current user: pi@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 10.3.17-MariaDB-0+deb10u1 Raspbian 10
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 20 hours 1 min 54 sec

Threads: 7 Questions: 94 Slow queries: 0 Opens: 32 Flush tables: 1 Open tables: 26 Queries per second avg: 0.001
--------------

pi@RPI4:~/projects/p5jsdb/database $


sure you can execute a sql script also "from inside" when login already:
mysql -u your_username -p your_password
use db_name
source /file.sql


make_accounts.sql
USE processing;

SELECT * FROM accounts;

DROP TABLE accounts;

CREATE TABLE accounts (
user_id INT(11) NOT NULL AUTO_INCREMENT,
user_name varchar(50) NOT NULL,
password varchar(255) NOT NULL,
user_score varchar(255),
user_email varchar(100) NOT NULL,
CONSTRAINT user_pk PRIMARY KEY (user_id)
);

ALTER TABLE accounts ENGINE=Myisam;

INSERT INTO accounts (user_name, password, user_score , user_email) VALUES ( 'test', 'test', '{score:0}', 'test@test.com');
INSERT INTO accounts (user_name, password, user_score , user_email) VALUES ( 'game', 'game', '{score:100}', 'game@test.com');

SELECT * FROM accounts;




now try to setup a NODE web server page again, but
when i try above again ( for this db test project )
i noticed lots of warnings... so instead of a
one line terminal command to make express server
needed:
__________________
sudo npm install -g express-generator
express --view=pug p5jsdb
cd p5jsdb
npm install
npm audit fix
sudo npm install -g npm
DEBUG=p5jsdb:* npm start
__________________

now try install maria db for connect to server:

npm install mariadb

and inside app.js use
const mariadb = require('mariadb') //


node on RPI4


i used the slim desktop pack for install RPI4
and when i try
pi@RPI4:~ $ uname -m
armv7l
pi@RPI4:~ $ node -v
bash: node: command not found
pi@RPI4:~ $ sudo apt list nodejs
Listing... Done
nodejs/stable 10.19.0~dfsg1-1+rpi1 armhf
pi@RPI4:~ $

means that possibly because Node-RED not preinstalled also NO Node ready.
i check here
for LTS see
https://nodejs.org/dist/v12.16.3/node-v12.16.3-linux-armv7l.tar.xz
for CURRENT see
https://nodejs.org/dist/v14.3.0/node-v14.3.0-linux-armv7l.tar.xz
and try according this: use now:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install -y nodejs

and now see
pi@RPI4:~ $ node -v
v14.3.0
pi@RPI4:~ $ npm -v
6.14.5


for setup webserver:
sudo npm install -g express-generator
mkdir projects
cd projects
mkdir nodeserver
cd nodeserver
npm init

npm install express-session --save
express --view=pug myapp

creates a other subdir

cd myapp
npm install
DEBUG=myapp:* npm start


nano start#!/bin/sh
# noderserver/myapp/start
MYPATH='/home/pi/projects/nodeserver/myapp'
echo '5s'
env sleep 5s
echo 'start node express webserver, stop with [ctrl][c]'
echo 'start on (remote) browser'
ifconfig | grep -o "inet 192.168......"
echo ' IP:3000'

cd $MYPATH
DEBUG=myapp:* npm start

chmod +x start
also might need:

nano /home/pi/.config/autostart/nodeserver.desktop
/home/pi/.config/autostart/nodeserver.desktop[Desktop Entry]
Version=1.0
Name=start
Name[en_GB]=start
Comment='node express server'
Exec=lxterminal -e '/home/pi/projects/nodeserver/myapp/start'
Icon=kll_favicon.png
Terminal=false
Type=Application
Categories=Utility;KLL;
StartupNotify=true



setup current p5.js project by download from online editor into public subdir


start from PC browser:




i still like my PTZ operation, but when i try it from my old Android tablet
a long (click) tip on the canvas works for the X Y rotation ( Tilde adjust )
but a Zoom like use 2 fingers works for the webpage, but not the processing zoom,
and the alternate keyboard operation for PTZ also not usable from tablet,
so i build in 3 more sliders for PTZ tablet operation...
try at: editor.p5js



above scripts for start / autostart at boot work nice,
but i was looking for something modern / snappy
find: pm2
and give it a try:
You install it:
npm install -g pm2
Start a cluster of, for example, 3 processes:
pm2 start app.js -i 3

but here already run into problems.. looks like it restarts the app every 2 seconds? and the SITE is not reachable

And make pm2 starts them at boot:
pm2 startup
later can check it:
pm2 monit