Use web browser to monitor and control car

In this tutorial, we will monitor the video coming from Robot car camera and control the car movement.
In order to complete the task, we need install two web server program in our Pi.
1)webiopi : this is a web server to exchange data with remote browser and send control signal to GPIO pins.
2)mjpg-streamerj: this is a web server which will transfer the video from camera to stream file in server and show it to remote browser.

Software Installation tutorial

Step 1: connect your Raspberry Pi 3 to your local wifi hot spot
In the upright corner of your GUI, find the the wifi icon and connect it to your local wifi, after connection is down, hover the mouse above wifi icon(left to the speaker icon), you will see somthing like “Wlan0 configured 192.168.0.24/24” ,192.168.0.24 is the LAN IP address of your Pi, please write down this IP address for later use. You can use ifconfig command to find this IP address in  terminal window.

Step 2)Change system configuration with following terminal command:

sudo raspi-config

i)Enable SSH by following options:
Interface Options->SSH->Yes->OK

i)Enable I2C by following options:
Interface Options->I2C->Yes->OK

iii)if you use CSI camera, please Enable Camera
Interface Options->Camera->Yes->OK

iv)If you are using US 101 keyboard, you need change keyboard layout
Localisation Options->Change Keyboard Layout->Generic 101-key->
Other->English(US)->The default for the keyboard Layout->
No compose key->OK
After above setting is done,click YES and Finish to complete configuration finally change.use sudo reboot command to restart Pi.

Step 3)Download a putty to use ssh terminal. If you have already download putty or you are using Mac computer, you can skip this step.

Putty is a software to access remote linux shell window with SSH.  You can go to http://www.putty.org and download latest putty version. Using putty is very straight forward, you just need open putty and write the IP from step 1) into the Host name field click open. Pi will ask you user name and password, input default user name pi and default password raspberry, you can login to your pi.
If you can not access Pi terminal with putty, check if you have enabled SSH in Step 2).

Step 4) Download software
Use putty to access Pi terminal, if you are using Mac computer, please use ssh command to access Pi terminal. Then run following commands:

cd  ~

sudo apt-get update && sudo apt-get upgrade

sudo  apt-get  install  rpi.gpio  -y

cd  osoyoo-robot

mkdir  osoyoo-cam-robot

cd   osoyoo-robot/osoyoo-cam-robot

wget  http://osoyoo.com/driver/WebIOPi-0.7.1.tar.gz

wget  http://osoyoo.com/driver/mjpg-streamer.tar.gz

wget  http://osoyoo.com/driver/robot.tar.gz

tar  -xzvf  WebIOPi-0.7.1.tar.gz

tar  -xzvf   mjpg-streamer.tar.gz

tar  -xzvf  robot.tar.gz

step2.安装webiopi

webiopi是一个可控制树莓派GPIO的web框架,该框架面向物联网IoT开发,该项目托管于google,并可在sourceforge上下载到源代码。webiopi支持REST Server,CoAP server,并提供python库和javascript库。

A.下载补丁

cd  ~/osoyoo-robot/osoyoo-cam-robot/WebIOPi-0.7.1/

wget   http://osoyoo.com/driver/webiopi-pi2bplus.patch

B.运行补丁

patch    -p1   -i  webiopi-pi2bplus.patch

C.编译安装webiopi

sudo  ./setup.sh

D.验证是否安装成功

webiopi  -h

如果安装成功会有如下所示输出;如果不成功请重复上面步骤

 
 
  1. sudo webiopi [-h] [-c config] [-l log] [-s script] [-d] [port]
  2. Options:
  3. -h, --help Display this help
  4. -c, --config file Load config from file
  5. -l, --log file Log to file
  6. -s, --script file Load script from file
  7. -d, --debug Enable DEBUG
  8. Arguments:
  9. port Port to bind the HTTP Server

E.运行webiopi

sudo  webiopi  -d  -c  /etc/webiopi/config

然后在电脑端或者手机端打开网页浏览器,在地址栏内输入树莓派ip地址,端口号8000。例如:http://192.168.1.16:8000(根据实际情况填入)。在登录网站时,需要输入账号和密码,默认的账号和密码分别是:webiopi和raspberry。如果进入WebIOPi Main Menu页面,说明环境配置成功了:

终端按下 Ctrl + Z 可以结束进程。

step3.安装mjpg-streamer

MJPG-streamer是一个优秀的开源project,它可以通过HTTP的方式访问linux上面的兼容摄像头,从而做到远程视频传输的效果。在本项目中,我们用MJPG-streamer传输小车摄像头图像,根据图像控制小车运动。

如果是使用CSI接口的摄像头,需要进行如下步骤

A.打开摄像头接口

sudo  raspi-config

选择Enable Camera。

B.在/etc/modules文件中添加一行bcm2835-v4l2,否则/dev下没有设备节点

sudo   nano  /etc/modules

加上:

bcm2835-v4l2(v4l2中的l是小写字母l,不是数字1)

保存退出,并重启Pi

如果使用USB接口摄像头可以跳过上面两步

安装依赖库

cd ~

sudo apt-get install libv4l-dev libjpeg8-dev -y

sudo apt-get install subversion -y

编译安装mjpg-streamer

cd ~/osoyoo-robot/osoyoo-cam-robot/mjpg-streamer/plugins/input_uvc

在Pi要编译通过需要修改配置  

sudo  nano  input_uvc.c (若安装了vim可以输入: sudo  vim   input_uvc.c)

找到

 int width=640, height=480, fps=5, format=V4L2_PIX_FMT_MJPEG

将V4L2_PIX_FMT_MJPEG 改成 V4L2_PIX_FMT_YUYV,保存退出

cd  ~/osoyoo-robot/osoyoo-cam-robot/mjpg-streamer

make  all 

插上摄像头测试mjpg-streamer,命令行输入: ls   /dev/video*  查找设备文件,如果摄像头工作正常,会输出设备文件,如图所示。

(这里我接了一个CSI和USB摄像头,所以有两个设备文件)如果没有输出设备文件,请确认摄像头是否是好的,以及上面的配置是否正确。

开启mjpg-streamer

sudo ./start.sh

然后在浏览器地址栏内输入树莓派ip地址,端口号8899,注意这里的端口号是8899,和之前的端口号(8000)是不同的。页面会显示关于M-JPEG streamer的信息。点击Stream会实时显示摄像头拍摄的内容。

按下 Ctrl + C 可以结束进程。

获取raspberry pi IP地址

ifconfig  wlan0

根据树莓派的实际ip,修改这个html标签

cd  ~/osoyoo-robot/osoyoo-cam-robot/robot

sudo nano index.html (若安装了vim可以输入: sudo  vim  index.html)

找到

src="http://192.168.0.118:8899/?action=stream"

其中,src后面的地址填入树莓派的正确ip地址(来至ifconfig命令),端口号指定为8899(8899是视频流的端口)。

修改webiopi默认设置

sudo  nano  /etc/webiopi/config

A.修改webiopi运行时调用的脚本路径

[SCRIPTS]
# Load custom scripts syntax :
# name = sourcefile
#each sourcefile may have setup, loop and destroy functions and macros
myscript = /home/pi/osoyoo-robot/osoyoo-cam-robot/robot/script.py
#myscript = /home/pi/WebIOPi-0.7.1/examples/scripts/macros/script.py

B.修改服务器访问webiopi时候调用HTML文件路径

# Use doc-root to change default HTML and resource files location
#doc-root = /home/pi/WebIOPi-0.7.1/examples/servo-control
doc-root = /home/pi/osoyoo-robot/osoyoo-cam-robot/robot

C.添加PCA9685地址

[DEVICES]
# Device configuration syntax:
# name = device [args...]
# name   : used in the URL mapping
#device : device name
#args   : (optional) see device driver doc
#If enabled, devices configured here are mapped on REST API /device/name
#Devices are also accessible in custom scripts using deviceInstance(name)
#See device driver doc for methods and URI scheme available

# Raspberry native UART on GPIO, uncomment to enable
# Don't forget to remove console on ttyAMA0 in /boot/cmdline.txt
# And also disable getty on ttyAMA0 in /etc/inittab
#serial0 = Serial device:ttyAMA0 baudrate:9600

# USB serial adapters
#usb0 = Serial device:ttyUSB0 baudrate:9600
#usb1 = Serial device:ttyACM0 baudrate:9600

#temp0 = TMP102
#temp1 = TMP102 slave:0x49
#temp2 = DS18B20
#temp3 = DS18B20 slave:28-0000049bc218

#bmp = BMP085

#gpio0 = PCF8574
#gpio1 = PCF8574 slave:0x21

#light0 = TSL2561T
#light1 = TSL2561T slave:0b0101001

#gpio0 = MCP23017
#gpio1 = MCP23017 slave:0x21
#gpio2 = MCP23017 slave:0x22

pwm0 = PCA9685 slave:0x40
#pwm1 = PCA9685 slave:0x41

#adc0 = MCP3008
#adc1 = MCP3008 chip:1 vref:5
#dac1 = MCP4922 chip:1

step4.安装VNC

sudo apt-get install tightvncserver -y

运行VNC服务器

vncserver :1

第一次运行VNC,需要用户自己设置VNC服务器密码,密码是8个字符,可以设置成raspberr;密码设置完成后还会提示

You will also be asked if you want to create a separate “read-only” password 输入n回车就行

在Windows或Mac上运行VNC Viewer 输入raspberry pi IP地址通过上面设置的密码远程登录Pi

开启IIC接口

sudo  raspi-config

选择Interfacing Options–>I2C–>Yes–>Ok–>Finish

打开一个终端

cd ~/osoyoo-robot/osoyoo-cam-robot/mjpg-streamer

sudo ./start.sh

再打开一个终端

sudo webiopi -d -c /etc/webiopi/config

如果报错请确认两点:

A.上面配置是否正确

B.PCA9685是否供上电了,如没有电,因为软件读取不到IIC设备,会报错。

然后在浏览器地址栏内输入树莓派ip地址,端口号8000(注意这里的端口号不是8899)。

网页上会显示摄像头捕获的画面,点击图标可以控制小车转向。

鼠标点击按钮就可以控制小车了。

Leave a Reply

WordPress spam blocked by CleanTalk.
Bombax Theme designed by itx
"