BrowserBot

Introduction

The “BrowserBot” is a web browser controlled robot using the BrickPi. With this project, we demonstrate how to control a BrickPi robot directly from the browser of any computer, tablet, or phone.

Using the BrowserBot you can build your own web-controlled robot very easily with just a few lines of code. The BrowserBot is a good place to start if you want to build a project using BrickPi to send and receive information directly to a web page.


Building the BrowserBot

BrowserBot uses the same body the Simplebot. Here are some links for building instructions:


Code

The code for BrowserBot is available on our BrickPi Python Github Repository under the “Project Examples” folder.

To pull the example code, just clone the BrickPi Python Repository:

If you don’t have Git installed, download and install it from here.


Overview

There are two files in the Project_ExamplesbrowserBot folder in the repository:

Browser_Client_Code.html: This runs on the client computer, cell phone, or tablet browser, the one from which you want to control the bot.

RPi_Server_Code.py: This code runs on the Raspberry Pi Server and controls the robot.


Prepare Your Pi

This code needs “Tornado” for Python installed on your Raspberry Pi. Tornado will allow us to use Web Sockets with our Raspberry Pi. Web Sockets will allow us to communicate back and forth between the Raspberry Pi and the client.

To install Tornado on Raspberry Pi, first install PIP on the RaspberryPi:

sudo apt-get install python-pip

And then install Tornado for Python using PIP:

pip install tornado

To run this example, you won’t need to know too much about Tornado or Websockets. But if you are new to Web Sockets and want to know more about how they work, here’s a good place to learn a little more.


Using the Robot

The Raspberry Pi and the client (your browser, phone, tablet, or computer) must be connected to the same WiFi network. So begin by making sure everything is on the same wifi network.

Once you’re connected and have Tornado installed on the RaspberryPi, run RPi_Server_Code.py in terminal on the Raspberry Pi:

python RPi_Server_Code.py

After the server is running on the Raspberry Pi, download Browser_Client_Code.html from the BrickPi Github Repository to the device you want to control the BrowserBot from.

Run the client code Browser_Client_Code.html in the web browser of any device such as a laptop or a mobile phone as long as they are on the same network as the Raspberry Pi.

Once the HTML page is loaded, enter the BrickPi’s IP Address into the text box and press “clickme”. There will be a short lag as the browser connects to the Raspberry Pi, and then you should be able to control the BrowserBot with the buttons on your web page.


Errors and Troubleshooting

If you get the following error:

socket.error: [Errno 98] Address already in use

Run netstat on the terminal and check which process is currently using the port 9093:
sudo netstat -ap |grep :9093

Note down the PID (Process IDentifier) of the process running it and kill that process:
kill pid

If it does not work use:
kill -9 pid

If the error does not go away, try changing the port number ‘9093’ both in the client and server code.

Tip: Don’t use Ctrl+Z to stop the program, use Ctrl+C.

If you use Ctrl+Z, it will not close the socket and you won’t be able to run the python Server on the Raspberry Pi the next time.



Last modified: Monday, 30 November 2020, 6:14 PM