K O logo
IoT

IoT — Setup and first steps with the Raspberry Pi Zero

Author

Kalle

Date Published

This AI-generated image shows a neatly organized workspace featuring a Raspberry Pi Zero, surrounded by electronic components, tools, a laptop, a smartphone, and a cup of coffee, highlighting DIY tech projects.

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.

1country=DE
2ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
3update_config=1
4# Network 1
5network={
6 ssid="NameOfMyWiFi"
7 psk="passwordOfMyWiFi"
8 key_mgmt=WPA-PSK
9}
10
11# Network 2
12network={
13 ssid="SSID2"
14 psk="password2"
15 key_mgmt=WPA-PSK
16}

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:

  1. onoff - a package to get easy access to the GPIO pins
  2. 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:

1{
2 "name": "blinky",
3 "version": "1.0.0",
4 "main": "index.js",
5 "scripts": {
6 "start": "node index.js"
7 },
8 "dependencies": {
9 "onoff": "^3.2.2",
10 "sleep-promise": "^8.0.1"
11 }
12}

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:

1const Gpio = require("onoff").Gpio;
2
3const led = new Gpio(23, "out");
4
5led.writeSync(1); // set the output of the pin to high

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.

1const sleep = require("sleep-promise");
2
3async function awaitSleep() {
4 console.log("before sleep"); // gets printed without delay
5 await sleep(5000);
6 console.log("after sleep"); // gets printed with a delay of 5 seconds
7}
8
9awaitSleep();

A program switching the output of pin 23 to high and after 1 second to low again would look like this:

1const Gpio = require("onoff").Gpio;
2const sleep = require("sleep-promise");
3
4const led = new Gpio(23, "out");
5
6async function blink() {
7 console.log("led on");
8 led.writeSync(1);
9 await sleep(1000);
10 console.log("led off");
11 led.writeSync(0);
12}
13
14blink();

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:

1scp ./package.json pi@192.168.178.28:~/blinky/package.json
2scp ./index.js pi@192.168.178.28:~/blinky/index.js

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
 This AI-generated image depicts a Raspberry Pi project setup with sensors, LEDs, and wires connected to a breadboard. A laptop displays code, and holographic icons represent various sensor readings like temperature and humidity.

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.