Intro to Hardware Hacking
Eric Bezzam
DISCLAIMERS!!
When we say hardware hacking…
Breaking into the servers of a government / multinational company.
Putting a bug/virus in your arch-enemy’s machine.
Modifying electronics into a form that was not originally intended.
Creating a hardware solution with various parts.
We have food but…
Co-founder of LauzHack (2016).
Follow me on Medium (@bezzam) and Twitter (@EricBezzam).
CS PhD student @ EPFL.
Prev. tech evangelist at Snips / Sonos.
Welcome! My name is Eric Bezzam.
1
2
3
Some things I do with hardware (lensless camera)
Full tutorial on Medium: https://go.epfl.ch/lenslesspicam
RPi HQ sensor
RPi
Arduino
Outline
Before we start…
Terminology
Knowing the terms helps to find in the manual and/or Google!
Command line interface
When you see something on the Terminal like:
Do you want to continue? [Y/n]
You are expected to give an answer. Pressing “Enter” will do the default (capital letter).
Otherwise enter your option and then press “Enter”.
Arduino vs. Raspberry Pi
Great summary: https://www.electronicshub.org/raspberry-pi-vs-arduino/
Microcontroller (uC).
No need for operating system.
Good for repetitive and/or deterministic tasks (e.g. opening doors, switching lights).
Typically use Arduino IDE to program (C/C++).
No built-in wireless connectivity.
Can plug and unplug freely.
Single board computer (SBC).
Requires operating system (Raspberry Pi OS).
Good for performing multiple and/or complicated tasks (e.g. sending an email based on sensor reading).
Can develop applications in many languages, as long as RPi can compile.
Hardware for Wi-Fi and Bluetooth on board.
Properly shutdown after use / before powering down.
Follow along: go.epfl.ch/hardware_slides
Flashing the board
Full tutorial on Medium: https://medium.com/@bezzam/setting-up-a-raspberry-pi-without-a-monitor-headless-9a3c2337f329
What do you need?
2) Choose OS
3) Plug in SD card and choose the storage device
4) Set advanced settings (WiFi and SSH)
“Gear” button or “Ctrl + Shift + X” shortcut.
Enable SSH and configure WiFi
With multiple Raspberry Pi’s set unique hostname.
5) Write!
Safely eject SD card from file manager.
(Advanced) setting WiFi through wpa_supplicant.conf
To add multiple WiFi networks or to add a WPA enterprise WiFi (e.g. university WiFi) you will have to edit the wpa_supplicant.conf file:
Switching between multiple networks from the Pi (link). Note that your SSH connection will break.
Connecting without a monitor (headless)
Step 1: Insert the microSD card and power up RPi
Approach #1: Secure shell (SSH)
From Terminal window (replace with your hostname):
$ ssh pi@raspberrypi.local
Enter password if you set one.
See this article for how to avoid entering password each time.
Log off with “Ctrl + D” or shutdown (from Pi!!) with:
$ sudo shutdown -h now # -r to reboot
Approach #2 : Virtual network computing (VNC)
Steps:
$ sudo raspi-config
From Raspberry Pi:
Select “Interface Options”, then “VNC”. Select “Yes” to enable the VNC server.
2) Determine IP address of Raspberry Pi
pi@raspberrypi:~ $ ifconfig
eth0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
...
...
...
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
...
...
...
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.XXX.XXX netmask 255.255.255.0 broadcast 192.168.1.255
...
...
...
3) Download VNC viewer on your computer
Download link: https://www.realvnc.com/en/connect/download/viewer/
4) Enter hostname / IP address and connect
Congrats!
All set up!
Which accessories / components?
Depends on application.
Adafruit great place with clear tutorials: https://www.adafruit.com
Example: smart speaker with visual interface.
Microphone(s)
Speaker
Screen
Camera
Typical steps
sudo apt-get -y update
sudo apt-get -y upgrade
sudo shutdown -r now
Camera
Full tutorial (V2 camera): https://projects.raspberrypi.org/en/projects/getting-started-with-picamera
Full tutorial (HQ camera): https://medium.com/@bezzam/headless-raspberry-pi-hq-camera-setup-and-focusing-258ac5cca5ea
2) Power up, enable the camera, and reboot
$ sudo raspi-config
From Raspberry Pi:
Select “Interface Options”, then “Camera”. “Legacy Camera” for Buster OS.
3) Test via command line
Raspberry Pi commands: raspistill, raspivid
(From Raspberry Pi) take a picture and save to file:
$ raspistill -o image.jpg
(From local computer) copy file from Raspberry Pi.
scp pi@raspberrypi.local:image.jpg ~/Desktop/image.jpg
4) Control with Python
Using picamera library (or picamerax for latest developments).
Create a file called simple_capture.py with vim or nano:
sudo apt-get install vim # if not installed
vim simple_capture.py
This will open the editor.
Mini vim tutorial
Full cheatsheet: https://vim.rtorr.com/
Outside of Edit mode (should see the command in bottom line):
4) Control with Python (continued)
Copy (Ctrl + Shift + V) following code into simple_capture.py:
Run script:
$ python simple_capture.py
from time import sleep
from picamera import PiCamera
camera = PiCamera()
camera.resolution = (1024, 768)
camera.start_preview()
# Camera warm-up time
sleep(2)
camera.capture('foo.jpg')
GPIO components
GPIO: General purpose input/output
Power down Raspberry Pi when connecting components!
First component: MEMS microphone
Adafruit documentation: https://learn.adafruit.com/adafruit-i2s-mems-microphone-breakout
2) Power up Raspberry Pi and install software
# 1) Update Pi software (then reboot)
sudo apt-get -y update
sudo apt-get -y upgrade
# 2) Install dependencies
sudo apt install python3-pip
cd ~
sudo pip3 install --upgrade adafruit-python-shell
# 3) Install driver software (then reboot)
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/i2smic.py
sudo python3 i2smic.py
For the I2S mic (link):
3) Check installation
Linux typically relies on ALSA (Advanced Linux Sound Architecture) for audio I/O.
Useful commands: arecord, aplay, alsamixer
List devices to check for proper setup:
pi@raspberrypi:~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: sndrpii2scard [snd_rpi_i2s_card], device 0: simple-card_codec_link snd-soc-dummy-dai-0 [simple-card_codec_link snd-soc-dummy-dai-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
4) Test!
Record 5 seconds to WAV file using detected card number:
arecord -D plughw:1 -c 1 -r 16000 -f S32_LE -t wav -d 5 file.wav
Playback (connect headphones to jack):
aplay file.wav
Or copy to computer.
scp pi@raspberrypi.local:file.wav ~/Desktop/file.wav
Second component: OLED screen
Adafruit overview: https://learn.adafruit.com/adafruit-1-5-color-oled-breakout-board/overview
2) Power up Raspberry Pi and install software
# 1) Update Pi software (then reboot)
sudo apt-get -y update
sudo apt-get -y upgrade
# 2) Install dependencies
sudo apt install python3-pip
sudo apt-get install fonts-dejavu
sudo apt-get install python3-pil
sudo pip3 install --upgrade setuptools
cd ~
sudo pip3 install --upgrade adafruit-python-shell
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo python3 raspi-blinka.py
# 3) Install driver software
sudo pip3 install adafruit-circuitpython-rgb-display
3) Check installation
pi@raspberrypi:~ $ python3 blinkatest.py
Hello blinka!
Digital IO ok!
I2C ok!
SPI ok!
done!
4) Test!
There are a few test scripts provided here.
We’ll try second one for drawing shapes and text (link).
Be sure to uncomment the line for the corresponding display (1.27" SSD1351 in our case).
I’ve also set BORDER=10 and FONTSIZE=14.
Create Python script on Raspberry Pi and run:
python3 draw_shapes_and_text.py
Arduino
Install IDE
Launch IDE and plug in Arduino
Arduino properly detected
Setup code (runs once at startup)
Code that loops after setup.
Not detecting?
Troubleshooting tips: https://chipwired.com/arduino-not-recognized-troubleshoot/
“Tools” and check/set “Board and “Port”.
STILL STUCK?!
Examples
File > Examples > 01. Basics > Blink. → Blink the on-board LED.
Flash board / run code
“Verify” to check that code compiles.
“Upload” to upload code to the board, aka flash.
Blinking LED.
Connecting components?
Similar to Raspberry Pi!
To sum up
Takeaways
When in doubt? RTFM and Google.
For more LauzHack events (and pizza)
Sign-up to our mailing list: go.epfl.ch/lauzhackmailing
Follow us on Twitter (@LauzHack).
Join committee!