====== Software Setup ====== - Open the terminal app\\ \\ Click the magnifying glass in the top right corner of the screen and type "terminal" to find the terminal app - Type the following command and press enter to connect to the Raspberry Pi terminal via SSH\\ \\ Replace the text "pibot##" with the label on the PiBot The password to enter when prompted is "DogsAndCatsAreNice2." without the quotes. The '.' at the end is important!ssh pi@pibot##.local - Perform the following steps to enable the CSI interface on the Raspberry Pi board - Type the following command and press enter to run the Raspberry Pi configuration utility\\ \\ sudo raspi-config - Use the up and down arrow keys to select the "Interfacing Options" menu item and then press the enter key\\ \\ {{ :pibot:lesson4:softwaremac:s1.png |Main Menu}} - Use the up and down arrow keys to select the "Camera" menu item and press the enter key\\ \\ {{ :pibot:lesson4:softwaremac:s2.png |Interface Menu}} - Use the left and right arrow keys to select the "Yes" option and press the enter key\\ \\ {{ :pibot:lesson4:softwaremac:s3.png |Camera Enable}} - Select the "Ok" option and press the enter key\\ \\ {{ :pibot:lesson4:softwaremac:s4.png |Camera Confirmed}} - Use the left and right arrow keys to select the "Finish" option and press the enter key\\ \\ {{ :pibot:lesson4:softwaremac:s5.png |Exit}} - Use the left and right arrow keys to select the "Yes" option and press the enter key\\ \\ {{ :pibot:lesson4:softwaremac:s6.png |Reboot}} - Open the Visual Studio Code from the Applications directory on the flash drive - Click on the small green square in the bottom left corner of the screen\\ \\ {{ :pibot:lesson4:softwaremac:s7.png |VSCode Main Screen}} - Select the "Connect Current Window to Host..." option\\ \\ {{ :pibot:lesson4:softwaremac:s8.png |VSCode Remote-SSH}} - Type "pi@pibot##.local" and press enter\\ \\ Replace the text "pibot##" with the label on the pibot The password to enter when prompted is "DogsAndCatsAreNice2." without the quotes. The '.' at the end is important! It may take a couple of minutes while it installs some files on the PiBot {{ :pibot:lesson4:softwaremac:s9.png |VSCode Remote Connection}} - Click the "Open Folder" button\\ \\ {{ :pibot:lesson4:softwaremac:s10.png |VSCode Open Folder}} - Select the "pi" folder and click the "OK" button\\ \\ {{ :pibot:lesson4:softwaremac:s11.png |VSCode Browse}} - Open a terminal in Visual Studio Code from the Terminal->New Terminal menu - Type the following commands and press enter after each line to download the MJPEG Streamer code for this lesson\\ \\ wget http://osoyoo.com/driver/picar/webcam.sh wget http://osoyoo.com/driver/picar/camstart.sh bash webcam.sh - Type the following command and press enter to start the MJPEG Streamer server for this lesson\\ \\ bash camstart.sh - Open the Safari App, type http://pibot##.local:8899 in the address bar, press enter, and click on the Stream link at the left to test the MJPEG Streamer software\\ \\ Replace the text "pibot##" with the label on the pibot {{ :pibot:lesson4:softwaremac:s12.png |MJPEG streamer Test}} - Open another terminal in Visual Studio Code from the Terminal->New Terminal menu - Type the following commands and press enter after each line to download the Python and HTML code for this lesson\\ \\ Answer "yes" or "y" when asked to install extra packages or continue wget http://osoyoo.com/driver/picar/osoyoowebcar.sh bash osoyoowebcar.sh - Close the current folder in Visual Studio Code from the File->Close Folder menu - Click the double pages icon on the left and click the "Open Folder" button\\ \\ {{ :pibot:lesson4:softwaremac:s13.png |VSCode Open Folder}} - Select the "osoyoowebcar" folder and click the "OK" button\\ \\ {{ :pibot:lesson4:softwaremac:s14.png |VSCode Browse}} - Select the webcar.py file in the list\\ \\ {{ :pibot:lesson4:softwaremac:s15.png |VSCode webcar.py}} - Replace the **192.168.0.32** on line 15 with **0.0.0.0** - Save the change by using the File->Save menu - Click on the templates directory and select the index.html file\\ \\ {{ :pibot:lesson4:softwaremac:s16.png |VSCode index.html}} - Replace the **192.168.0.32** on line 34 with **pibot##.local**\\ \\ Replace the text "pibot##" with the label on the pibot - Save the change by using the File->Save menu - Open a terminal in Visual Studio Code from the Terminal->New Terminal menu - Type the following command and press enter to run the Python code for this lesson\\ \\ sudo python3 webcar.py - Open the Safari App, type http://pibot##.local in the address bar, and press enter\\ \\ Replace the text "pibot##" with the label on the pibot You may need to adjust the mounting position of the camera holder to point straight forward {{ :pibot:lesson4:softwaremac:s17.png |Webcar app}} ====== Software Explanation ====== The webcar.py file uses a Python library called Flask that handles the details of serving a webpage to your web browser. Using libraries like Flask is a powerful way to get advanced capabilities in your software without having to write a lot of code from scratch. By using the Flask library there are only a few lines of code that are required to make the PiBot drive around from your web browser. - In the webcar.py file find the last line of code. What do you think this line does?\\ \\ The app.run function is what calls the Flask library and tells it to send pages to your web browser. Port 80 is the standard port used on the Internet when you type %%http://%% at the beginning of the web address - In the webcar.py file, find where the action function is defined. What do you think this function does?\\ \\ If you don't remember what a variable is, review the description from [[:pibot:lesson1:softwaremac#software_explanation|Lesson 1]] The '@app.route' that appears above the action function is called a function decorator. It tells Python that this function has special meaning to the Flask library. The @app.route decorator is used by Flask to run a function when a certain URL is accessed by your web browser. Every page displayed by your web browser can be accessed by a URL. Most URLs start with %%http://%% or %%https://%% and then the website name like google.com or yahoo.com. After the website, often there is a path that tells the web server exactly which web page you want to see. Flask uses that path to determine what function to run based on the @app.route decorator. - How do the '' and '' values in the decorator relate to the parameters of the action function?\\ \\ The '' and '' values in the call to the @app.route decorator are passed to the action function as the 'action' and 'cmd' parameters. - What are some valid paths that will cause the the action function to do something? Find what the code does when that path is requested by your web browser.\\ \\ Each path ends up calling another function in the code as determined by the 'if' statements. Find where those functions are defined in the code to see what happens when each path is requested. - Add code to the action function so that when the path '/print/hello' is requested the text "Hello from PiBot" is printed out. - Save your changes by using the File->Save menu - Type control + C in the terminal window and then run the following command in the Visual Studio Code terminal to get your new code\\ \\ sudo python3 webcar.py - In the Safari App type http://pibot##.local/print/hello in the address bar and press enter. What happens in the terminal window?\\ \\ Replace the text "pibot##" with the label on the pibot - In the webcar.py file find the call to the render_template function. What is the name of the file that it will send?\\ \\ The render_function template takes the file name that is passed as its parameter and sends it to the web browser that is accessing it. All of the files must be in the 'templates' directory for Flask to be able to send them. - Open the 'index.html' file from the templates directory - In the index.html file find where it requests one of the paths that you saw in the webcar.py file. What function is it part of?\\ \\ The "GET" command is a standard HTTP command that web browsers use to request a URL from the web server. Some other commands are "POST", "PUT", and "CONNECT". - Find the '