My home automation system. Creating an operating system image

  • Tutorial
Introduction The

installation of the system on the Raspberry Pi 2.3

I will describe the easiest, in my opinion, way for beginners.

All action will occur in Ubuntu 18.04.

For help on the commands, use the “man” command + the desired command.

man ls

Or the necessary command with the key --help

ls --help


1. Installing the Raspbian Stretch Lite operating system on a microSD card.

MicroSD card should be class 10 and with a capacity of 8 gigabytes.
Insert the SD card into the computer and enter the command:

lsblk



In the example, the map is defined as mmcblk0, then I will use this name.

Then, to clear the card of previously recorded information (then we will make a disk image), you need to erase it with the command:

dd if=/dev/zero of=/dev/mmcblk0

This command requires superuser rights, for this you need to add the keyword “sudo” at the beginning . Hereafter, I give commands without sudo . When entering commands, use autocompletion (Tab key) this is necessary to control the correct input of commands and file paths. While the command is running, you can download a fresh distribution of the operating system.

Download from here: raspberrypi.org
We need Raspbian Stretch Lite. Unpack the downloaded zip, we need a file with the img extension - this is the disk image that we will copy to the card. So, if we have a disk image and the dd command is completed, then move on.

Copy the image to the card:

dd if=/path/to/raspbian.img of=/dev/mmcblk0 bs=4M status=progress

after completion, enter the command:

sync

We take out the card and insert it back.

Create an empty file on the boot disk of our map:

touch /media/$USER/boot/ssh

This is necessary so that an ssh server is launched on Raspberry.

The card must be safely removed, inserted into the Raspberry, connected to a wired network and energized. If there is no 2 apmera power supply, then you can power it from a computer with USB3. At the first boot, the system will increase the size of the disks to the size of the card, it will take some time.

In order to find out the IP address of our RPi, you can use the arp-scan utility. If it is not installed, set:

apt install arp-scan

We find out the name of our network card:

ip a

or

netstat -i



I have a wifi card named wlp1s0.

Run arp-scan:

arp-scan -I wlp1s0 -l



I need an RPi with IP 192.168.0.114, the address is assigned by DHCP and can be anything.

We go to RPi by ssh:

ssh pi@192.168.0.114

you need to enter yes at the first authentication, then enter the password for pi - raspberry .

If we managed to log into Raspberry, enter the command:

init 0

This command turns off RPi.

You need to insert the card into the computer again. Now we need the “gparted” program, if it is not installed, set:

apt install gparted

We start gparted, you need to change the disk size mmcblk0p2, leave at the end an unallocated area of ​​300-500 megabytes or 2-3% of the size of the card, this is necessary for our image to fit on cards of different manufacturers, and they can vary in size. The result should be something like this:



After completing work with gparted, you can make a backup copy of the card, so that in case of problems in further experiments, you have a ready-made image.

dd if=/dev/mmcblk0 | bzip2 > raspbian.bz2

now we can always restore our disk with the command:

bzcat raspbian.bz2 | dd bs=4M of=/dev/mmcblk0

As a result, we have a microSD card with a ready-to-use operating system and its backup copy.

2. Configuring Raspbian Stretch Lite.

Go to RPi on yyr ssh. To configure the static addresses of the wired network and wifi, add the following settings to the end of the /etc/dhcpcd.conf file:

interface eth0
static ip_address = 192.168.0.10 / 24
static routers = 192.168.0.1
static domain_name_servers = 8.8.8.8, 8.8.4.4
interface wlan0
static ip_address = 192.168.0.10 / 24
static routers = 192.168.0.1
static domain_name_servers = 8.8.8.8, 8.8.4.4


With these settings, RPi will have the same IP on a wired or wifi network.

The wifi configuration file - /etc/wpa_supplicant/wpa_supplicant.conf
add the following settings to it:

network = {
ssid = "NAME"
psk = "PASS"
}

where NAME and PASS username and password from the wifi network. If it is not known in advance where to use our RPi, then the name and password can be entered later.

Add tasks to the cron program

crontab -e

At the first start, you will need to select a text editor, nano - will be the most familiar.



In order for the schedules to work, add the following settings:

PATH = / usr / sbin: / usr / bin: / sbin: / bin
SHELL = / bin / bash


And the tasks themselves, we will record the time of each download and the duration of continuous work, this will be useful information further.

reboot sleep 60 && date >> my-reboot.txt
@weekly uptime >> my-uptime.txt


To exit, use the key combination Ctrl + x, Y, Enter To

set the time zone, use the command:

dpkg-reconfigure tzdata

To increase the resource of the SD card, you can stop logging and remove the swapfile:

(I think this should reduce the likelihood of the SD card failing or data corruption during sudden RPi shutdowns during operation):

apt update
apt purge dphys-swapfile
apt autoremove --purge
apt install busybox-syslogd
dpkg --purge rsyslog

Installation of programs:

apt install mosquitto mosquitto-clients sqlite3 libsqlite3-dev mc htop arp-scan vim supervisor python-pip python3-pip python-virtualenv python-dev

pip install psutil paho-mqtt
pip3 install psutil paho-mqtt 

For homekit2mqtt you need to install nodejs:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
apt-get install -y nodejs

Checking the installed version

node -v
nodejs -v

Install node-red:

npm install -g --unsafe-perm node-red

We launch:

node-red



In the address bar of the browser, type the IP address of RPi 192.168.0.10:1880. Finish



node-red, press Ctrl + c, go to the .node-red directory

cd .node-red

And install packages for node-red, they may be needed for testing, configuration and communication of various devices.

npm i node-red-dashboard node-red-contrib-sonos node-red-contrib-knx node-red-node-serialport node-red-contrib-globalcache node-red-contrib-counter 

We return to the home directory

cd

Install homekit2mqtt:

apt install libavahi-compat-libdnssd-dev
npm install -g homekit2mqtt --unsafe-perm

You can try running homekit with the command

homekit2mqtt



It will start with the default settings and you can add a new device in the Home program on your iPhone or iPad . We will leave the current

user password pi .

Now that everything is installed and verified, you need to backup our SD card in the same way as before. Now everything is ready to install the automation system.

Also popular now: