I. Objective
II. Parts and Devices
III. Hardware Installation
IV. Software Installation

Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES ここでご購入を!

There are many ways to control Raspberry Pi Robot Car over Internet.  In this project, we introduce a popular method, which is to install a Web Server on Raspberry Pi. In other words, we can turn our Pi board into a website and use browse or Cell phone APP to exchange data with Raspberry Pi.

No. Picture Device Qty. Accessories Link
1 Tracking sensor module 1 M2.5 Plastic Screw x 2
M2.5 Plastic Nut x2
M2.5 Plastic Pillar x 2
Click here to buy
2 7pin 25 cm Female to Female Cable 1 Click here to buy
3 Philips screwdriver 1 Click here to buy

Step 1: Install the smart car basic framework as per Lesson 2. If you have completed lesson 3, you can just keep it

Step 1) Make sure you have installed rpi.gpio and adafruit-pca9685 library, and enable I2C in lesson 1, and power on the robot car.

Step 2) Download installation shell script file by typing following command:

wget http://osoyoo.com/driver/picar/pi-webserver.sh

Step 3) type the following command:

bash pi-webserver.sh

After running above commands, you will install python Flask library and automatically create a directory called pi-webserver and two subdirectories under pi-webserver which are static and templates, the structure is as follows:

The pi-webserver directory will store our python code file, static directory will store CSS file and templates folder will store HTML files. If you don’t know what is HTML, CSS file, it doesn’t matter.

If you don’t know the IP address of your Pi, please type the command “hostname -I” to get your Raspberry Pi’s IP, after running the command, you will see following result:

In the above result, is the IP address of my Raspberry Pi.

Step 4) We need to edit python code file and change the IP address in the file. Please type the following commands to enter the pi-webserver folder:

cd pi-webserver

Step 5)
Please type the following command to edit the file pi-webserver.py

nano pi-webserver.py

Step 6) Please replace “”  with your Raspberry Pi IP address, and then click “Ctrl” and “x” then click “Y” to save the file and then click “enter” to exit the file.

You will open pi-webserver.py in nano editor and see the python code as follows:

import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)
pi_ip_address='' # replace this ip value with your Raspberry Pi IP address, you can use ifconfig command to see the IP

#define actuators GPIOs
sensor1= 25 # No.1 sensor from right
sensor2= 9 # No.2 sensor from right
sensor3= 11 # middle sensor
sensor4= 8 # No.2 sensor from left
sensor5= 7 #No.1 sensor from left
#initialize GPIO status variables
Sts1 = 0
Sts2 = 0
Sts3 = 0
Sts4 = 0
Sts5 = 0

# Define sensor pins as output
GPIO.setup(sensor1, GPIO.IN)   
GPIO.setup(sensor2, GPIO.IN) 
GPIO.setup(sensor3, GPIO.IN) 
GPIO.setup(sensor4, GPIO.IN)   
GPIO.setup(sensor5, GPIO.IN) 

def index():
	# Read Sensors Status
	Sts1 = GPIO.input(sensor1)
	Sts2 = GPIO.input(sensor2)
	Sts3 = GPIO.input(sensor3)
	Sts4 = GPIO.input(sensor4)
	Sts5 = GPIO.input(sensor5)

	templateData = {
      'title' : 'IR tracking sensor Status!',
      'sensor_status_1'  : Sts1,
      'sensor_status_2'  : Sts2,
      'sensor_status_3'  : Sts3,
      'sensor_status_4'  : Sts4,
      'sensor_status_5'  : Sts5
	return render_template('index.html', **templateData)

if __name__ == "__main__":
   app.run(host=pi_ip_address, port=80, debug=True)


Step 7) Run the code by typing the following command:

sudo python pi-webserver.py

Step 8) Your python flask web server will start. Now in your PC or cell phone which is the same Wi-Fi network of your Raspberry Pi, open browser and visit http://your_raspberry_pi_ip_address (in my case, you will see your 5 tracking sensors status in your browser:

The sensor status will be updated every 5 seconds, so if you put the black line under other sensors, the sensor value in the above web page will change after 5 seconds.