Objective:
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.

After completing this project, you can visit the Raspberry Pi IP address and see the online status of the  5 Tracking sensors installed in Lesson 2     in your browser window.

Software Installation:
Step 1) Download installation shell script file by typing following command:

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

After running above commands, you will install python Flask library and then create a directory called picar-lesson3 and two sub-directories under picar-lesson3 which are static and templates, the structure is as follows:

/pi-webserver
  /static
  /templates

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.

Step 2) we need to edit python code file and change the IP address in the file.
First, type the command “hostname -I” to get your Raspberry Pi’s IP, after running the command, you will see following result:

[email protected]:~ $ hostname -I

10.0.0.89 2601:647:4b00:e80::7ef6 2601:647:4b00:e80:c018:4806:2c46:9030

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

Now type following commands:

cd pi-webserver
nano pi-webserver.py

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__)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
pi_ip_address='10.0.0.89' # 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) 

		
@app.route("/")
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)

Please replace 10.0.0.89  with your Raspberry Pi IP address

Step 3) Run the code by typing following command:

sudo python pi-webserver.py

Your python flask web server will start. Now in your PC or cell phone which is the same wifi network of your Raspberry Pi, visit http://your_raspberry_pi_ip_address (in my case http://10.0.0.89) , you will see your 5 tracking sensors status as following picture:

In above picture, my 2nd sensor in the left detected black track and other sensor detected white ground, so the sensor values  shows as:

0 1 0 0 0

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