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



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 )
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
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') //