Snippets

Peter Scargill Raspbian Jessie and Node Red script for original RPI

Created by Peter Scargill last modified
#!/bin/bash
source ./hackitt_includes.sh;
# This script is intended for the original Raspberry Pi - NOT the P2
# You need the include file as well, see Bitbucket
# Assuming access as PI user 
# This script SHOULD take a November 2015 JESSIE installation (from the RPI site - NOT NOOB) 
# through to a full Node-Red setup
# including Mosquitto with web sockets, SQLITE ( xxx.xxx.xxx.xxx/phpliteadmin), 
# UI (xxx.xxx.xxx.xxx:1880/ui) and Webmin(xxx.xxx.xxx:10000)
#
# http://tech.scargill.net - much of this was thanks to help from others!
# 
# If you want security you need to add this to the settings.js file in /home/pi/.node-red
#
# Suggested improvements welcome - I'm learning!!
#
#    functionGlobalContext: {
#        // os:require('os'),
#        // bonescript:require('bonescript'),
#        // jfive:require("johnny-five"),
#		moment:require('moment'),
#		fs:require('fs')
#    },

#    adminAuth: {
#    type: "credentials",
#    users: [{
#       username: "admin",
#        password: "your encrypted password see node red site",
#        permissions: "*"
#    }]
#},
#
#  httpNodeAuth: {user:"user", pass:"your encrypted password see node red site"},


task_start "Update" "Updating repositories then any programs"
if [ $skip -eq 0 ]  
then
cd /home/pi
sudo apt-get -qq -o=Dpkg::Use-Pty=0 --yes --force-yes update
sudo apt-get -qq -o=Dpkg::Use-Pty=0 --yes --force-yes upgrade
task_end
fi

task_start "Prerequisites" "Enabling PING and SAMBA (to access the hostname externally - and STUFF"
if [ $skip -eq 0 ]  
then
# fix for RPI treating PING as a root function - by Dave
sudo setcap cap_net_raw=ep /bin/ping
sudo setcap cap_net_raw=ep /bin/ping6
# this one ensures the unit shows up on the network by hostname... works a treat
sudo apt-get install samba samba-common-bin


# Prerequisite suggested by Julian
sudo apt-get install build-essential git python-serial scons libboost-filesystem-dev libboost-program-options-dev libboost-system-dev libboost-thread-dev libboost-all-dev libsqlite3-dev subversion libcurl4-openssl-dev libusb-dev cmake 
task_end
fi

task_start "Mosquitto" "Loading Mosquitto and setting up user"
if [ $skip -eq 0 ]  
then
# installation of Mosquitto/w/Websockets
cd /home/pi
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get update
cd /home/pi
sudo apt-get install mosquitto

# Setup kill anonymous, emable websockets then set username and password for accessing Mosquitto
# As we are PI - create temporary file with changes - then add to Mosquitto.conf
echo '======================================= UPdating mosquitto.conf'
cat <<EOT > /tmp/tmpnoderedfile
listener 9001
protocol websockets
listener 1883
allow_anonymous false
password_file /etc/mosquitto/passwords
EOT
sudo bash -c "cat /tmp/tmpnoderedfile >> /etc/mosquitto/mosquitto.conf"
rm /tmp/tmpnoderedfile

# Create or append to mosquitto passwords then add user
sudo bash -c "echo '' >> /etc/mosquitto/passwords"
user_input mquser "Enter desired user (admin for example) for Mosquitto"
user_input mqpass "Enter desired password for Mosquitto" "hide"
sudo mosquitto_passwd  -b /etc/mosquitto/passwords $mquser $mqpass
task_end
fi

task_start "Node-Red" "Loading Node-Red"
if [ $skip -eq 0 ]  
then
# Dave provided most of this - installation of Node-Red and my selection of nodes
sudo apt-get remove -y nodered nodejs nodejs-legacy npm
sudo apt-get autoremove -y

#curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
#sudo apt-get install -y build-essential python-dev python-rpi.gpio nodejs
# use below for old pi - above for new pi
wget http://node-arm.herokuapp.com/node_archive_armhf.deb
sudo dpkg -i node_archive_armhf.deb
sudo apt-get install build-essential python-dev python-rpi.gpio

ln -fs /usr/bin/local/node /usr/bin/node
sudo npm cache clean
sudo npm install -g --unsafe-perm node-red
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start -O /usr/bin/node-red-start
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop
sudo ln -fs /usr/local/bin/node /usr/bin/node
sudo chmod +x /usr/bin/node-red-st*
sudo systemctl daemon-reload
mkdir .node-red
cd .node-red
sudo npm install -g node-red-admin
npm install moment
npm install node-red-contrib-grove
npm install node-red-contrib-bigtimer
npm install node-red-contrib-esplogin
npm install node-red-node-pushbullet
npm install node-red-contrib-freeboard
npm install node-red-node-openweathermap
npm install node-red-node-google
npm install node-red-node-sqlite
npm install node-red-contrib-ui
npm install node-red-node-emoncms
npm install node-red-node-geofence
npm install node-red-contrib-ivona
npm install node-red-contrib-moment
npm install node-red-contrib-particle
npm install node-red-contrib-graphs
npm install node-red-node-ledborg
npm install node-red-node-ping
npm install node-red-node-random
npm install node-red-node-smooth
npm install node-red-contrib-npm
npm install raspi-io
npm install node-red-contrib-gpio
npm install node-red-contrib-admin
task_end
fi

task_start "Install Webmin" "Installing Webmin - expect interaction"
if [ $skip -eq 0 ]  
then
cd /home/pi
mkdir webmin
cd webmin
wget --no-verbose http://prdownloads.sourceforge.net/webadmin/webmin-1.780.tar.gz
sudo gunzip -q webmin-1.780.tar.gz
sudo tar xf webmin-1.780.tar
cd webmin-1.780
sudo ./setup.sh /usr/local/Webmin
cd /home/pi/webmin
sudo rm *.tar
task_end
fi

task_start "Apache Installation with PHP and SQLITE and PHPLITEADMIN" "Installing Apache and PHP and SQLITE and PHPLITEADMIN..."
if [ $skip -eq 0 ]  
then
cd /home/pi
sudo groupadd -f -g33 www-data
sudo apt-get -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install apache2 php5 libapache2-mod-php5


cd /home/pi
sudo apt-get install sqlite3
sudo apt-get install php5-sqlite
mkdir dbs
sqlite3 /home/pi/dbs/iot.db << EOF
CREATE TABLE IF NOT EXISTS \`pinDescription\` (
  \`pinID\` INTEGER PRIMARY KEY NOT NULL,
  \`pinNumber\` varchar(2) NOT NULL,
  \`pinDescription\` varchar(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS \`pinDirection\` (
  \`pinID\` INTEGER PRIMARY KEY NOT NULL,
  \`pinNumber\` varchar(2) NOT NULL,
  \`pinDirection\` varchar(3) NOT NULL
);
CREATE TABLE IF NOT EXISTS \`pinStatus\` (
  \`pinID\` INTEGER PRIMARY KEY NOT NULL,
  \`pinNumber\` varchar(2)  NOT NULL,
  \`pinStatus\` varchar(1) NOT NULL
);
CREATE TABLE IF NOT EXISTS \`users\` (
  \`userID\` INTEGER PRIMARY KEY NOT NULL,
  \`username\` varchar(28) NOT NULL,
  \`password\` varchar(64) NOT NULL,
  \`salt\` varchar(8) NOT NULL
);
CREATE TABLE IF NOT EXISTS \`device_list\` (
  \`device_name\` varchar(80) NOT NULL DEFAULT '',
  \`device_description\` varchar(80) DEFAULT NULL,
  \`device_attribute\` varchar(80) DEFAULT NULL,
  \`logins\` int(11) DEFAULT NULL,
  \`creation_date\` datetime DEFAULT NULL,
  \`last_update\` datetime DEFAULT NULL,
  PRIMARY KEY (\`device_name\`)
);
CREATE TABLE IF NOT EXISTS \`readings\` (
  \`location\` varchar(20) NOT NULL,
  \`value\` int(11) NOT NULL,
  \`logged\` timestamp NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS \`pins\` (
  \`gpio0\` int(11) NOT NULL DEFAULT '0',
  \`gpio1\` int(11) NOT NULL DEFAULT '0',
  \`gpio2\` int(11) NOT NULL DEFAULT '0',
  \`gpio3\` int(11) NOT NULL DEFAULT '0'
);
INSERT INTO PINS VALUES(0,0,0,0);
CREATE TABLE IF NOT EXISTS \`temperature_record\` (
  \`device_name\` varchar(64) NOT NULL,
  \`rec_num\` INTEGER PRIMARY KEY,
  \`temperature\` float NOT NULL,
  \`date_time\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
.exit
EOF

chmod 777 /home/pi/dbs
chmod 666 /home/pi/dbs/iot.db
cd /home/pi


sudo apt-get install unzip
cd /var/www/html
sudo mkdir phpliteadmin
cd phpliteadmin
sudo wget --no-verbose https://bitbucket.org/phpliteadmin/public/downloads/phpLiteAdmin_v1-9-6.zip
sudo unzip phpLiteAdmin_v1-9-6.zip
sudo mv phpliteadmin.php index.php
sudo mv phpliteadmin.config.sample.php phpliteadmin.config.php
sudo rm *.zip
sudo mkdir themes
cd themes
sudo wget --no-verbose https://bitbucket.org/phpliteadmin/public/downloads/phpliteadmin_themes_2013-12-26.zip
sudo unzip phpliteadmin_themes_2013-12-26.zip
sudo rm *.zip
user_input litepasswd "Enter desired password for PHPLiteAdmin" "hide"
sudo sed -i -e 's#\$directory = \x27.\x27;#\$directory = \x27/home/pi/dbs/\x27;#g' /var/www/html/phpliteadmin/phpliteadmin.config.php
sudo sed -i -e "s#\$password = \x27admin\x27;#\$password = \x27$litepasswd\x27;#g" /var/www/html/phpliteadmin/phpliteadmin.config.php
sudo sed -i -e "s#\$subdirectories = false;#\$subdirectories = true;#g" /var/www/html/phpliteadmin/phpliteadmin.config.php
cd /home/pi

task_end
fi

task_start "MP3 Installation" "Installing mg123..."
if [ $skip -eq 0 ]  
then
sudo apt-get install mpg123
task_end
fi

#task_start "Internet Time Updater for Webmin" "Ntpdate installing..."
#if [ $skip -eq 0 ]  
#then
#sudo apt-get -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install ntpdate
#task_end
#fi


#task_start "Email SMTP Installation" "Installing mail utils and SMTP..."
#if [ $skip -eq 0 ]  
#then
#cd /home/pi
#sudo apt-get  -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install mailutils ssmtp
#task_end
#fi

task_start "Install SCREEN" "Installing SCREEN"
if [ $skip -eq 0 ]  
then
cd /home/pi
sudo apt-get install screen
task_end
fi


task_start "Remove WolfRam(0.4GIG), Sonic Pi(90Mb), Minecraft and Scratch" "Removing programs..."
if [ $skip -eq 0 ]  
then
cd /home/pi
sudo apt-get purge -y wolfram-engine
sudo apt-get autoremove -y scratch
sudo apt-get autoremove -y sonic-pi
sudo apt-get autoremove -y minecraft-pi

task_end
fi

echo "After reboot - you must manually edit /home/pi/.node-red/settings.js to UNCOMMENT the line"
echo "which starts  os:require('os') and remove the comma if here are no other lines in that section"
echo " "

echo "Run node-red-start and you will see the line needed to start node-red at boot"
echo "This works for RPI2 - may not for RPI - can start RPI manually with 'node-red'"
echo " "
echo "All done. Rebooting."
sudo reboot

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.