Skip to content

Setup Development Environment

Meliurwen edited this page May 10, 2018 · 27 revisions

In this tutorial you'll be guided step by step in the building and setup of CoderBot, from ground up to the fully working evironment, directly from source.

It is composed in four "macro-steps": Preparation of the Machine, Preparation of the Environment, Installation of the Core and Configuration. The first macro-step is composed in two mutually exclusive options: Preparation of the Virtual Machine (qemu) and Preparation of the Physical Machine (Raspberry Pi).

Note: The VM option is for testing and development purposes only.

Table of Contents

Preparation of the Machine

Preparation of the Virtual Machine (qemu)

You can release the mouse lock with L-CTRL + L-ALT or CTRL + ALT + G

Requirements

Packages:

  • qemu-system-arm (>= 2.11)

Recommended distros:

  • Ubuntu 18.04 or higher
  • Debian 10 Buster or higher

Install qemu

On Debian and Debian-based distros:

sudo apt install qemu-system-arm

Install the Virtual Machine

Create the directories that will host our virtual machine's files:

mkdir ~/rasp-qemu && mkdir ~/rasp-qemu/qemu-rpi-kernel

Move into qemu-rpi-kernel folder and download the patched kernel and its .dtb:

cd ~/rasp-qemu/qemu-rpi-kernel
wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.9.59-stretch
wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb

Move to the parent folder, download and unzip Raspbian's image:

cd ..
wget https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-03-14/2018-03-13-raspbian-stretch-lite.zip
unzip 2018-03-13-raspbian-stretch-lite.zip && rm 2018-03-13-raspbian-stretch-lite.zip

Create an executable file and give execution permission:

echo "qemu-system-arm -kernel ./qemu-rpi-kernel/kernel-qemu-4.9.59-stretch \
                   -append 'root=/dev/sda2 panic=1 rootfstype=ext4 rw' \
                   -hda 2018-03-13-raspbian-stretch-lite.img \
                   -cpu arm1176 \
                   -m 256 \
                   -machine versatilepb \
                   -no-reboot \
                   -serial stdio \
                   -dtb ./qemu-rpi-kernel/versatile-pb.dtb \
                   -net nic -net user,hostfwd=tcp::2222-:22" > coderbot.sh && chmod +x coderbot.sh

Execute the .sh file, et voilà!

./coderbot.sh

Enable SSH

In order to enable SSH permanently from the first boot, we'll have to create an empty file named "ssh" (without extension) into the boot partition.

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:

512 bytes of sector size * 8192 start sector = 4194304 bytes of offset

Now that we have the offset we can mount our partition in a temporary folder:

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

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

sudo touch tmp/ssh

Now we umount the partition and delete the temporary folder:

sudo umount tmp
rm -d tmp

Enable SSH Key-Based Authentication

Create the keys in keys folder:

mkdir keys && ssh-keygen -t rsa -b 2048 -q -f keys/rpi_key -P ""

Pass the public key to the Raspberry trough SSH:

ssh-copy-id pi@localhost -i keys/rpi_key.pub -p 2222

Now we should be able to login:

ssh pi@localhost -p 2222 -i ~/rasp-qemu/keys/rpi_key

Preparation of the Physical Machine (Raspberry Pi)

sudo dd bs=4M if=/dev/mmcblk0 of=2018-04-18-raspbian-stretch-lite.img
nano /media/username/boot/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=IT

network={
    ssid="networkName"
    psk="networkPassword"
    key_mgmt=WPA-PSK
}
touch /media/username/boot/ssh
sudo nano /media/username/rootfs/etc/dhcpcd.conf
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
arp -a
ssh pi@ipAddress -p raspberry

Update Raspbian:

sudo apt update && sudo apt upgrade

Update the Firmware:

sudo apt-get install rpi-update && sudo rpi-update

Enable audio and camera

If /boot/config.txt does not exists:

touch /media/mdog/boot/config.txt

else:

nano /media/mdog/boot/config.txt

Add or, if present, modify these parameters to this:

# 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

References:

Check if 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 folowing 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 [email protected]:test.jpg ~

On Windows:

pscp.exe username@remoteHost:test.jpg d:\

References:

⚠️ Manca nei repo di stretch il pacchetto gpac

PIGPIO (GPIO daemon)

sudo apt-get install pigpio python-pigpio python3-pigpio

References:

To automate running the daemon at boot time, run:

sudo systemctl enable pigpiod

References:

reboot

Check if pigpiod is running:

sudo service pigpiod status

Audio

Open:

sudo nano /etc/apt/sources.list

Decomment this line:

deb-src http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi

Update:

sudo apt-get update

References:

Execute:

amixer cset numid=3 1

Edit file asound.conf

sudo nano /etc/asound.conf

add the following:

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

Install pyaudio and espeak (text to speech):

sudo apt-get install python-pyaudio espeak

Reboot and test with this command:

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

Scratch for GPIO

Download and execute the script:

sudo wget http://goo.gl/dANpKr -O isgh.sh && sudo bash isgh.sh

Install Flask

Install picamera, PIL, opencv:

sudo apt-get install python-picamera python-pil python-opencv

Install pip:

sudo apt-get install python-pip

Install Flask:

sudo pip install Flask

Preparation of the Environment

Stuff...

Clone this wiki locally