IoT — Setup and first steps with the Raspberry Pi Zero
Author
KalleDate Published
The first step of using a Raspberry Pi is the preparation of the operating system, the Pi uses an SD card as primary hard drive so this step can be done with every computer equipped with a SD card reader. For this course I will use the Raspian OS which is the official system by the Raspberry Pi foundation.
After downloading and extracting the OS image you have to create a bootable SD card with this file. To do so I recommend Etcher which is an easy to use tool to flash OS images to portable drives. For a more specific setup guide please follow the official readme.
Before starting the Raspberry Pi with the freshly flashed SD card there are two more steps necessary. To work with the device it should connect to the WiFi and ssh must be enabled for remote control.
To enable ssh just create an empty file named ssh
without any extension in the root folder of the SD card.
To connect the Raspberry Pi to a wireless network you create a file named wpa_supplicant.conf
, also in the root folder of the SD card. In this file you can list networks the device should connect to.
Now the Raspberry Pi is ready to go.
Finding the Raspberry on the network
After connecting the small computer to a power source it will boot and automatically connect to one of the listed wireless networks if available. So it should be connected but at the moment the IP address is unknown. Finding out under which address the Raspberry Pi can be reached is unfortunately not very comfortable. There is one easy way, but it's not supported on all networks. A Raspberry Pi has a default hostname of raspberrypi
and (if supported) you can connect to it with ssh
.
If the hostname resolution is not supported you have to find the IP address, one way is checking the router and looking in the list of connected devices. The last option is to use a network scanner like nmap(available on linux and MacOS, installer available vor Windows). To list all devices connected to the network with the open port 22 (ssh port) use the command nmap -p22 192.168.178.0/24
, change 192.168.178.0
to represent the ip address range in use.
After finding the IP address you can connect via ssh
with the user pi
and the password raspberry
ssh pi@192.168.178.28
When you are connected one of the first things you do should be a password change. The command for this is passwd
.
Installing Node.js
The Raspberry Pi Zero uses an ARMv6 processor and because of this it's not possible to install Node via the officially documented apt-get
command. Instead you have to download the binary published from the Node.js distribution site. For this course I recommend the latest version which is at the moment 11.1.0 so the correct download link for the Pi Zero is . To download the binary on the device connect via ssh and run the command
curl -o node-v11.1.0-linux-armv6l.tar.gz
After downloading extract the contents of the tar-file
tar -xzf node-v11.1.0-linux-armv6l.tar.gz
The last step is to copy the binaries to the usr/local path so they are found as executables.
sudo cp -r node-v11.1.0-linux-armv6l/* /usr/local/
Now you should be able to run the command node -v
which should print v11.1.0
.
Because npm (the node package manager) is using git for some dependencies, and because it is in general very useful install git as the last step
sudo apt-get install git
Say hello to the LED with Node.js
Now that the Pi is ready it is time for the hello world of IoT. It's called blinky and as name suggests it let's an LED blink. At first create a new folder for the "blinky" project, in this folder create a package.json
and an index.js
file.
Working with node relies heavily on the usage of packages and for blinky there are two very useful packages:
- onoff - a package to get easy access to the GPIO pins
- sleep-promise - a package which enables easy async sleep calls
Because it makes no sense to run this program on your normal computer don't install these dependencies with npm but only list them in the package.json
:
The "onoff" package gives easy access to the GPIO pins via the exported Gpio
class. To control a pin create a new Gpio object with the pin number and the direction:
To see which pin has which number take a look on pinout.xyz/, this site has a great interactive overview.
The "sleep-promise" package provides a simple sleep function which takes a number of milliseconds to wait and returns a promise which gets resolved after the given time. Promises can be awaited in async
functions.
A program switching the output of pin 23 to high and after 1 second to low again would look like this:
The last step left is getting the program on the Pi and start it. First create a new folder on the raspberry pi:
mkdir blinky
And then use scp
on your local computer to copy the index.js file and the package.json to the Raspberry Pi:
Or use a GUI based tool like cyberduck to manage files.
When the blinky project is on the Pi you need to install the packages to do so, run npm install
in the "blinky" folder on the Raspberry Pi. After the installation there should be a "node_modules" folder and you should be able to run the program with the command npm start
.
Tasks
- Prepare a SD card with raspian so that the Raspberry Pi connects to your WiFi
- connect to the Pi via ssh and install node
- connect an LED and let it blink with JavaScript
- extend the program to blink X times
- alter the blinky program to use a random rhythm
IoT — Reading and sending sensor data
Learn how to read and send sensor data using a Raspberry Pi and a DHT11 sensor. This guide covers setting up the sensor, installing necessary libraries, and creating a Node.js application to collect and transmit temperature and humidity data to a server.
Mailing List
If you want to receive updates on new posts, leave your email below.