Setup and configure an rpiZero with nodejs, typescript, yarn, pm2, and a test project
Raspberry Pi uses an SSD card for all storage. You must place your OS on this card. You can install many OS's but Raspbian is recommended. Since the pi zero is smaller, I recommend the headless version which does not have a Desktop windows-like environment, only terminal.
- Copy Raspbian to an SSD card
- (Preferred: Headless)
- https://www.raspberrypi.org/software/
- Open the ssd card on your computer in a terminal
cd yourDriveLetter:
SSH access is required to use your pi without a monitor, through a terminal.
touch ssh
create wpa_supplicant.conf and add the text below, so that on reboot, your pi will automatically connect to wifi.
Note, this only works for the Raspberry Pi Zero W model, with built in wifi.
-
nano wpa_supplicant.conf
-
add the following text to the file and save
country=US ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="YOUR_SSID" scan_ssid=1 psk="YOUR_WIFI_PASSWORD" }
-
Make sure your raspberry pi zero is off
-
Now eject your sd card from your computer, and insert it into your pi zero
-
make sure your raspberry pi is the only one on the network, otherwise it will be named raspberrypi-2 (or whatever #), and you'll have to likely access it by IP address
-
power on your raspberry pi zero
-
wait a minute then try to connect
ssh pi@raspberrypi * the password is raspberry
- you are now in a terminal of your raspberry pi zero!
-
(OPTIONAL) change your password
sudo passwd
If you don't do this, then you'll just be prompted for your password everytime you connect
- on your computer find your key
ls ~/.ssh/*.pub
- if you don't have a key or want a different one generate it
ssh-keygen
- once you have your key, copy it over ssh to your rpi
ssh-copy-id pi@raspberrypi
then authenticate using your password
- you should now be able to access your rpi without a password
ssh pi@raspberrypi
Recommended for source control, tracking changes in your project, and letting you fetch code from remote repositories like github.com
sudo apt install git
SSH access allows you to authenticate using keys instead of passwords.
https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh
- list ssh keys, use an existing public one or create a new one
ls ~./ssh/*.pub
- to generate a key
ssh-keygen -t ed25519 -C "your_user_name@github.com"
- display the key
cat ~./ssh/id_ed25519.pub
- copy the displayed text to the keyboard
- add the key to your github account ssh keys
- test your key, you should get a success message
ssh git@github
From here you can either run the rest of the steps manually, or clone the repo and run the setup file using sudo sh setup.sh
If you want your pi to show you the correct time, you'll have to set the timezone. The time updates automatically with an internet connection.
sudo raspi-config
- go to localization options > timezone
- REMOVE Microsoft Crap
sudo rm /etc/apt/sources.list.d/vscode.list
sudo rm /etc/apt/trusted.gpg.d/microsoft.gpg
- update system package list
sudo apt update
- (OPTIONAL) update all installed packages (You probably don't need this if your image is recent)
sudo apt-get dist-upgrade
Node JS will allow you to run javscript (or typescript) code as an alternative to Python.
for rpi zero, use armv61 binaries from nodejs.org
these binaries are no longer officially supported as of node 12+, so you'll have to install node12, or an unnoficial binary
- download the latest official armv61 binary (node 11.x)
- or download the latest unnoficial binary here:
IN the example below we will use the latest unnoficial version
sudo mkdir ~/downloads
cd ~/downloads
- for ease, set this variable to the build version nodeVer=v15.9.0 sudo wget https://unofficial-builds.nodejs.org/download/release/$nodeVer/node-$nodeVer-linux-armv6l.tar.xz
- extract and run it sudo tar -xvf node-$nodeVer-linux-armv6l.tar.xz
- copy to local sudo cp -R node-$nodeVer-linux-armv6l/* /usr/local/
- verify installation, nodejs and npm should now be installed
node -v
npm -v
Yarn is an alternative to NPM
- npm is bundled with node, use npm to install yarn
- otherwise apt-get will find the cmdtest yarn package which is different
- install yarn globally
sudo npm install --global yarn
- verify installation
yarn -v
This will allow you to automatically start your code on power on, or restart code that exits https://www.npmjs.com/package/pm2
- install pm2 globally (use
npm
if you didn't install yarn)sudo yarn global add pm2
- install typescript for pm2 (#optional if you want pm2 to run typescript files, but not recommended for production)
sudo pm2 install typescript
- verify installation
pm2 -v
If you would like to use typescript instead of plain javascript
sudo yarn global add typescript
sudo yarn global add ts-node
- Start a repl using
ts-node repl
This will let you use the pinout
command
sudo apt install python3-gpiozero
If you would like to run angular applications.
sudo yarn global add @angular/cli
You can clone the rpiZeroSetup repo to see if your rpiZero is setup correctly.
I recommend putting your repos in a folder like 'projects'
mkdir projects
cd projects
- If you setup ssh access to git, you can use the command below
git clone git@github.com:seadonk/rpiZeroSetup.git
- If not, then use this command
git clone https://github.com/seadonk/rpiZeroSetup.git
cd rpiZeroSetup
- Install packages
yarn install
yarn start
or npm start if you didn't install yarn- You should see test output
yarn start-express
- You should see a url that you can access to find "Hello World"
- In your projects directory, make a new folder:
mkdir helloWorld
cd helloWorld
- Create an app.ts file and add some output
nano app.ts
console.log(`${new Date().toLocaleTimeString('short')} - Hello World!`)
- Run your app
ts-node app.ts
- You should see your hello world output.