Raspberry Pi robot with iOS, Android app and web UI.
-
Raspberry Pi v3 Model B
-
Camera: 8MP Raspberry Pi Camera v2
-
Micro SD Card
8G or above.
-
USB Sound Card
I'm using this.
-
Microphone
3.5mm jack, I'm using this.
-
Speaker
Mini speaker with 3.5mm jack.
-
Robot Car Kit: ZK-4WD
-
Motor driver: L298n
-
5v Power Supply
Powers the Raspberry Pi.
-
Battery Power Supply
Two 18650 (3.7v) batteries with battery case. Powers the motor driver.
-
DC-DC Converter: LM2596S
Converts 7.4v to 5v to power the servo.
-
Servo: SG90 9g
-
Servo Mount
I'm using this (keyword: 2 Axis Servo Gimbal FPV Camera Platform), but with only 1 servo because there're only two unique channels of hardware PWM output and I need to reserve one for IR.
-
IR Transmitter
Initially I used the IR module for Arduino: KY-005, but the range was short.
Then I found this (keyword: 1/3W High Power IR Transmitter Module For Arduino) that can get several meters.
-
IR Receiver: KY-022 (IR Receiver for Arduino)
-
Bread Board: SYB-170
-
DuPont Line
20 or 30cm male to female.
-
Resistors
1/4W 1kΩ & 2kΩ.
The "9v" battery is actually two 3.7v 18650 batteries.
The "9v" battery is actually two 3.7v 18650 batteries.
-
(Optional) Format SD card
-
Install SD Card Formatter.
-
Open SD Card Formatter and choose Overwrite Format.
-
Click Format.
-
-
Install Raspbian from image (Official doc)
-
Download .zip file for RASPBIAN STRETCH WITH DESKTOP.
-
Unzip the file to extract the image, ie: 2017-07-05-raspbian-jessie.img.
-
Open a terminal and run
diskutil list
. You will see a list of all your disks. -
Connect the SD card reader with the SD card inside.
-
Run
diskutil list
again, identify the disk (not the partition) of your SD card, e.g. disk3, not disk3s1. -
Unmount your SD card by using the disk identifier:
sudo diskutil unmountDisk /dev/disk[n]
(replacing [n] with the number of the disk, ie: /dev/disk3). -
Copy the image to your SD card:
sudo dd bs=1m if=[path-to-the-image-file] of=/dev/rdisk[n] conv=sync
(replacing [path-to-the-image-file] ie: ~/Downloads/2017-07-05-raspbian-jessie.img, and replacing [n] with the number of the disk, ie: /dev/rdisk3).
-
2) SSH to Raspberry Pi (Official doc)
-
Enable SSH:
For Raspbian released after the end of Nov 2016, SSH is disabled by default. It can be enabled by placing a file named ssh, without any extension, onto the boot partition of the SD card.
-
Eject your SD card and insert it into the Raspberry Pi.
-
Connect the Raspberry Pi to your home router with a ethernet cable, then power it using a micro USB charger.
-
Determine the IP address of the Raspberry Pi. If you do not have access to the router, open a terminal and run
sudo nmap -sP -PI -PT 192.168.1.0/24
to scan all the devices in your home network. -
Open a terminal, run
ssh pi@[IP-of-Raspberry-Pi]
(replacing [IP-of-Raspberry-Pi] with actual IP address, ie: 192.168.1.16), enter default password raspberry. -
(Optional) Change password:
passwd pi
.
3) (Optional) Configure Graphical Remote Desktop (Official doc)
- If VNC connect is not yet installed on your Raspberry Pi, run
sudo apt-get update
sudo apt-get install realvnc-vnc-server realvnc-vnc-viewer
-
To enable VNC Server, run
sudo raspi-config
, navigate to Interfacing Options, scroll down and select VNC > Yes. -
Open VNC Viewer, enter your Raspberry Pi's IP address to connect.
4) Connect to Wifi (Official doc)
- Run
cat /etc/network/interfaces
. Make sure you have:
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
- Run
sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
. Go to the bottom of the file and add the following:
network={
ssid="{Name-of-the-Wifi}"
psk="{Password-of-the-Wifi}"
}
- Run
sudo service networking restart
to take effect. Now you can unplug the ethernet cable and use wifi only.
If which python3
returns nothing, run sudo apt-get install python3
.
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt install nodejs
node -v
Install the nginx package sudo apt-get install nginx
and start the server sudo /etc/init.d/nginx start
.
Install the library for ALSA applications development files: sudo apt-get install libasound2-dev
. If you got 404 Not Found, run sudo apt-get upgrade --fix-missing
.
avconv is needed for converting recorded videos to .mp4 format: sudo apt-get install libav-tools
mpg123 is used for playing .mp3 format music: sudo apt-get install mpg123
cd ~
git clone https://github.com/shaqian/PiBot.git
Do not install the wheels when testing.
-
Refer to the circuit diagram and connect the wires.
-
Install rpio, run
npm install rpio
. -
Start node, run
node
. -
Initialize output:
var rpio = require('rpio');
rpio.open(29, rpio.OUTPUT, rpio.LOW);
rpio.open(31, rpio.OUTPUT, rpio.LOW);
rpio.open(38, rpio.OUTPUT, rpio.LOW);
rpio.open(40, rpio.OUTPUT, rpio.LOW);
- Go forward:
rpio.write(29, rpio.LOW);
rpio.write(31, rpio.HIGH);
rpio.write(38, rpio.LOW);
rpio.write(40, rpio.HIGH);
- Go backward:
rpio.write(29, rpio.HIGH);
rpio.write(31, rpio.LOW);
rpio.write(38, rpio.HIGH);
rpio.write(40, rpio.LOW);
- Stop:
rpio.write(29, rpio.LOW);
rpio.write(31, rpio.LOW);
rpio.write(38, rpio.LOW);
rpio.write(40, rpio.LOW);
-
Refer to the circuit diagram and connect the wires.
-
Run:
cd ~/PiBot/PiBotServer/bin
chmod +x direct.py
- The position is controlled by duty cycle, normally ranges from 2.5 to 11.5. Go to middle position:
./direct.py 7
- Full left:
./direct.py 2.5
- Full right:
./direct.py 11.5
-
Refer to the circuit diagram and connect the wires.
-
Run:
cd ~/PiBot/PiBotServer/bin/temp_hum
chmod +x getTemp.py
chmod +x getHum.py
- Get temperature:
./getTemp.py
- Get humidity:
./getHum.py
I use IR Receiver to read IR signal from remote controls of home appliances, then emulate the remote control by sending the same signal from Raspberry P.
I borrowed the code from this repo but changed the ports as well as the value of ST_BASE which was 0x20003000 for older Raspberry Pi but should be 0x3F003000 for Raspberry Pi 3 (BCM2837).
-
Refer to the circuit diagram and connect the wires.
-
Run
gpio -v
to check if wiringPi is already installed. If not, install Wiring Pi. -
Run
cd ~/PiBot/PiBotServer/bin
. Compile the code and generate the binary:
gcc ir_decode.c -lwiringPi -o decode.out
chmod +x decode.out
-
Run
sudo ./decode.out
. It would print [0] every one second when no IR signal is received. -
Point the remote control to the IR receiver and press the button. The decoded IR signal is a series of hex numbers, ie: 0xb2,0x4d,0x1f,0xe0,0x98,0x67,[48].
-
Repeat the same for "ON" button and "OFF" button, and note down the IR signals.
Disconnect the IR Receiver as we are done with IR decoding.
-
Refer to the circuit diagram and connect the wires.
-
Run the following to duplicate the ir_encode code.
cd ~/PiBot/PiBotServer/bin
cp ir_encode.c on.c
cp ir_encode.c off.c
- Run
vi on.c
, and change the hex string in line 166 to what you have decoded, ie:
char data[6] = {0xb2,0x4d,0x1f,0xe0,0xd8,0x27};
- Run
vi off.c
, and change the hex string in line 166 to what you have decoded, ie:
char data[6] = {0xb2,0x4d,0x7b,0x84,0xe0,0x1f};
- Compile the code and generate the binary:
gcc on.c -lwiringPi -o on.out
chmod +x on.out
gcc off.c -lwiringPi -o off.out
chmod +x off.out
-
Send "ON" signal:
sudo ./on.out
. -
Send "OFF" signal:
sudo ./off.out
.
Raspberry Pi has a built-in 3.5mm jack for audio output but no input. I use a USB sound card for both speaker output and microphone input.
-
Plug the USB sound card in. Connect the microphone and speaker to the sound card.
-
Run
arecord -l
andaplay -l
and identify card number of your USB Audio Device, e.g. card 1. -
Record a 5 seconds test audio:
arecord -Dplughw:[n] -fcd -d5 -c1 -twav -r16000 test.wav
(replacing [n] with the number of USB Audio Device, e.g. Dplughw:1). -
Play the test audio:
aplay -Dplughw:[n] test.wav
(replacing [n] with the number of USB Audio Device, e.g. Dplughw:1). -
To set the USB sound card to be the default audio device, run
sudo vi /lib/modprobe.d/aliases.conf
, insert the following and save:
options snd_usb_audio index=0
options snd_bcm2835 index=1
options snd slots=snd_usb_audio,snd_bcm2835
-
Reboot:
sudo reboot
. -
Run
cat /proc/asound/modules
and you should have:
0 snd_usb_audio
1 snd_bcm2835
- Play the .wav again without specifying device:
aplay test.wav
.
I use picam to generate HTTP Live Streaming (HLS).
-
Connect Raspberry Pi Camera Board to the Raspberry Pi.
-
Run
sudo raspi-config
, navigate to Interfacing Options and select Camera > Yes. -
Install dependencies for picam:
sudo apt-get update
sudo apt-get install libharfbuzz0b libfontconfig1
- Create directories and symbolic links:
cd ~;mkdir picam;cd picam
cat > make_dirs.sh <<'EOF'
#!/bin/bash
DEST_DIR=~/picam
SHM_DIR=/run/shm
mkdir -p $SHM_DIR/rec
mkdir -p $SHM_DIR/hooks
mkdir -p $SHM_DIR/state
mkdir -p $DEST_DIR/archive
ln -sfn $DEST_DIR/archive $SHM_DIR/rec/archive
ln -sfn $SHM_DIR/rec $DEST_DIR/rec
ln -sfn $SHM_DIR/hooks $DEST_DIR/hooks
ln -sfn $SHM_DIR/state $DEST_DIR/state
EOF
chmod +x make_dirs.sh
./make_dirs.sh
- Install picam library:
wget https://github.com/iizukanao/picam/releases/download/v1.4.6/picam-1.4.6-binary-jessie.tar.xz
tar xvf picam-1.4.6-binary-jessie.tar.xz
cp picam-1.4.6-binary-jessie/picam ~/picam/
- Start HTTP Live Streaming:
cd ~/picam
./picam -o /run/shm/hls
- Configure Nginx server. Run
sudo vi /etc/nginx/sites-available/default
and add the following inside server { ... } bloc:
location /hls/ {
root /run/shm;
}
- Restart Nginx:
sudo /etc/init.d/nginx restart
- Test playback:
The HLS will be available at http://[IP-of-Raspberry-Pi]/hls/index.m3u8.
-
With QuickTime on a Mac:
Open QuickTime player, go to File > Open Location, enter http://[IP-of-Raspberry-Pi]/hls/index.m3u8.
-
Or playback in a web browser:
i. Download the test file and copy it to Nginx root.
sudo cp ~/PiBot/PiBotServer/public/hls_test.html /var/www/html/hls_test.html
ii. From the Raspberry Pi or any PC that in the same network, navigate to http://[IP-of-Raspberry-Pi]/hls_test.html in a web browser.
Enable both HTTP on port 80 and HTTPS on port 443. HTTPS is required to get recording working in Chrome when requests are not originated from local host. See: https://goo.gl/rStTGz
-
Run
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/snippets/nginx-selfsigned.key -out /etc/nginx/snippets/nginx-selfsigned.crt
to create the certificates used for HTTPS in nginx. -
Replace the content in /etc/nginx/sites-enabled/default with ~/Cognitive-Bot/ngnix/default.
-
Run
sudo nginx -t
to test the configurations. If successful, restart nginx serversudo /etc/init.d/nginx restart
.
-
Run
cd ~/PiBot/PiBotServer;npm install
to install all dependencies. -
Start HLS video streaming:
cd ~/picam
./make_dirs.sh
./picam -o /run/shm/hls --vflip --hflip
- To start the application, run:
cd ~/PiBot/PiBotServer
sudo npm start
- Navigate to
http[s]://[IP-of-Raspberry-Pi]
in a web browser (replacing [IP-of-Raspberry-Pi] with the actual IP address, ie: 192.168.1.16).
-
Build and run in simulator:
cd PiBot/PiBotApp npm install npm run ios
-
Run on device:
Refer to Running your app on iOS devices in React Native official guide.
-
Build and run in simulator:
cd PiBot/PiBotApp npm install npm run android
-
Run on device:
Refer to Running your app on Android devices in React Native official guide.
Or use the PiBot.apk in this repo.