Skip to content

Setup Development Environment

Roberto edited this page Feb 2, 2020 · 27 revisions

In this tutorial you'll be guided step by step in the building and setup of CoderBot, from a raspbian image to the working environment.

The VM option has subpar performances and it's discouraged.

Table of Contents

Download and preparation for first boot

Download and unzip Raspbian

Move to your home folder, download and unzip Raspbian's image:

cd ~
wget -O raspbian-stretch-lite.zip http://director.downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-04-19/2018-03-13-raspbian-stretch-lite.zip
unzip raspbian-stretch-lite.zip && rm raspbian-stretch-lite.zip

Preparation of the first boot

Introduction

In order to enable SSH, audio, raspicam permanently and automatically connect to your wifi network from the first boot, we'll have to create an empty file named "ssh" (without extension), create two config files into the boot partition and modify one config file into the rootfs partition.

Mount partitions

Now we'll have to mount the boot partition. The problem is that the .img files are raw images of a whole disk. That means they start with a bootloader and a partition table. We have to find out the offset of the partition in bytes and mount it with the offset option of mount.

fdisk -l 2018-03-13-raspbian-stretch-lite.img

This will show us the sector size of the disk and the start sector of the boot partition (the one with FAT filesystem).

Disk 2018-03-13-raspbian-stretch-lite.img: 1,7 GiB, 1858076672 bytes, 3629056 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xa8fe70f4

Device                                Boot Start     End Sectors  Size Id Type
2018-03-13-raspbian-stretch-lite.img1       8192   93802   85611 41,8M  c W95 FAT32 (LBA)
2018-03-13-raspbian-stretch-lite.img2      98304 3629055 3530752  1,7G 83 Linux

We can use these data to calculate the offset in bytes with this formula:

sector size in bytes * start sector offset = offset in bytes

In my case:

For boot partition:
512 bytes of sector size * 8192 start sector = 4194304 bytes of offset

For rootfs partition:
512 bytes of sector size * 98304 start sector = 50331648 bytes of offset

Now that we have the offset we can mount our partitions in temporary folders:

mkdir boot && sudo mount -o loop,offset=4194304 2018-03-13-raspbian-stretch-lite.img boot
mkdir rootfs && sudo mount -o loop,offset=50331648 2018-03-13-raspbian-stretch-lite.img rootfs

Enable SSH

We create an empty file named "ssh" inside the boot partition:

sudo touch boot/ssh

Enable and configure Wifi

We create the wifi configuration file that will be copy-pasted in /var/run/wpa_supplicant:

sudo nano boot/wpa_supplicant.conf

Adding the following text, specifying the SSID and the password of your wifi network:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=IT

network={
    ssid="networkName"
    psk="networkPassword"
    key_mgmt=WPA-PSK
}

To make sure that our Raspberry will automatically connect to our network at boot, we will modify a file in the "rootfs" partition:

sudo nano rootfs/etc/dhcpcd.conf

Adding these lines:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Enable audio and camera

In order to enable both audio and camera, we'll have to create (or modify) a file named config.txt into the boot partition:

nano boot/config.txt

Add or (if already present) modify these parameters:

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

# Gives 128MB of memory to the GPU
gpu_mem=128

# Enable camera
start_x=1

# Disable camera LED (optional)
#disable_camera_led=1

Umount partitions

Now we umount the partitions and delete the temporary folders:

sudo umount boot
sudo umount rootfs
rm -d boot
rm -d rootfs

Now the Raspbian's img file is ready to be flashed on an SD card or booted on a virtual machine like qemu.

Preparation of the Machine

Preparation of the Virtual Machine (qemu)

Click here for the tutorial (incomplete at the moment)

Preparation of the Physical Machine (Raspberry Pi)

Flash the SD card

Check which devices are currently connected to your machine:

lsblk

For example, this is the output of my machine:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 232,9G  0 disk 
├─sda1   8:1    0   150G  0 part /
└─sda2   8:2    0  82,9G  0 part 

Insert the SD card into your machine and run the previous command again:

lsblk

This is the new output on my machine; you can see that a new device appeared, this is the SD card (in my case is mmcblk0):

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 232,9G  0 disk 
├─sda1        8:1    0   150G  0 part /
└─sda2        8:2    0  82,9G  0 part 
mmcblk0     179:0    0   7,4G  0 disk 
├─mmcblk0p1 179:1    0  41,5M  0 part 
└─mmcblk0p2 179:2    0   7,4G  0 part

Now we can flash the img file on our SD card:

Be careful to copy-paste this command, be sure to write your device. In my case is mmcblk0, but your can be different!

sudo dd bs=4M if=2018-04-18-raspbian-stretch-lite.img of=/dev/mmcblk0 conv=fsync

Once finished, unplug the SD card and insert it into the Raspberry Pi.

Preparation of the Environment

Enstablish a connection

Power on your Raspberry Pi, after one or two minutes it should be connected to your network.
We know that every Raspberry Pi's MAC address starts with b8:27:eb. knowing this information find its IP address in our network is trivial, and we can use arp or arp-scanner tools:

Between arp and arp-scanner the latter one is recommended because it performs an "active scan" (it simply sends ARP packets to the Ethernet broadcast address which is ff:ff:ff:ff:ff:ff) and waits hosts response. The arp's tool instead displays the kernel's IPv4 network neighbour cache.

arp -a | grep b8:27:eb
sudo arp-scan --localnet | grep b8:27:eb

Found our Raspberry Pi's IP address we can now establish an ssh connection with it:

ssh pi@ipAddress -p raspberry

Update Raspbian and the Firmware

Update Raspbian (it can take from few seconds to several minutes):

sudo apt update && sudo apt upgrade

Reboot:

sudo reboot

Check if the camera works

With the raspy point the camera to some object and take a picture with the following command:

raspistill -v -o test.jpg

Check if the file test.jpg has been created typing the following command:

ls

To be 100% sure that the camera works we have to see the picture we've just shot. We can't do that directly from the terminal, so another option is to download and open it to our main system. On *nix systems:

scp pi@ipAddress:test.jpg ~

⚠️ in Debian Stretch's repo the gpac package is not present

I2C

When you first boot the raspberry, run raspi-config and enable i2c.

TODO: Check raspi-config source and allow enabling i2c headlessly.

sudo echo "dtparam=i2c_arm=on" > /boot/config.txt
sudo sed /etc/modprobe.d/raspi-blacklist.conf -i -e "s/^\(blacklist[[:space:]]*spi[-_]bcm2708\)/#\1/"
sudo modprobe spi-bcm2708

PIGPIO (GPIO daemon)

sudo apt-get install pigpio

To automate the daemon to start at boot time, run:

sudo systemctl enable pigpiod

Restart the system:

sudo reboot

Once the system is restarted we want to check if our changes has been applied, to do so, we look if pigpiod is running:

sudo service pigpiod status

Audio

Execute:

amixer cset numid=3 1

Edit file asound.conf

sudo nano /etc/asound.conf

Add the following lines:

pcm.!default {
    type hw
    card 0
}
ctl.!default {
    type hw
    card 0
}

Create file /etc/modprobe.d/alsa-base.conf

sudo nano /etc/modprobe.d/alsa-base.conf

With the following content:

options snd_usb_audio index=0
options snd_bcm2835 index=1
options snd slots=snd_usb_audio,snd_bcm2835
apt install espeak

Restart the system:

sudo reboot

And enter this command to test if audio and the text-to-speech works:

espeak -ven+f3 -k5 -s150 "I've just picked up a fault in the AE35 unit"

You can now proceed to Deploy the Python Application.

References:

Enable Audio and Camera
Check if Camera works
PIGPIO
Audio