-
Mark Yoder authored5344c9e5
Internet of Things
You can easily connect BeagleBone Black to the Internet via a wire (:ref:`networking_wired`), wirelessly (:ref:`networking_wireless`), or through the USB to a host and then to the Internet (:ref:`networking_usb`). Either way, it opens up a world of possibilities for the "Internet of Things" (IoT).
Now that you're online, this chapter offers various things to do with your connection.
Accessing Your Host Computer's Files on the Bone
Problem
You want to access a file on a Linux host computer that's attached to the Bone.
Solution
If you are running Linux on a host computer attached to BeagleBone Black, it's not hard to mount the Bone's files on the host or the host's files on the Bone by using sshfs. Suppose that you want to access files on the host from the Bone. First, install sshfs:
bone$ sudo apt install sshfs
Now, mount the files to an empty directory (substitute your username on the host computer for username and the IP address of the host for 192.168.7.1):
bone$ mkdir host
bone$ sshfs username@$192.168.7.1:. host
bone$ cd host
bone$ ls
The ls command will now list the files in your home directory on your host computer. You can edit them as if they were local to the Bone. You can access all the files by substituting :/ for the :. following the IP address.
You can go the other way, too. Suppose that you are on your Linux host computer and want to access files on your Bone. Install sshfs:
host$ sudo apt install sshfs
and then access:
host$ mkdir /mnt/bone
host$ sshfs debian@$192.168.7.2:/ /mnt/bone
host$ cd /mnt/bone
host$ ls
Here, we are accessing the files on the Bone as debian. We’ve mounted the entire file system, starting with /, so you can access any file. Of course, with great power comes great responsibility, so be careful.
The sshfs command gives you easy access from one computer to another. When you are done, you can unmount the files by using the following commands:
host$ umount /mnt/bone
bone$ umount home
Serving Web Pages from the Bone
Problem
You want to use BeagleBone Black as a web server.
Solution
BeagleBone Black already has the nginx web server running.
When you point your browser to 192.168.7.2, you are using the nginx web server. The web pages are served from /var/www/html/. Add the HTML in :ref:`networking_index_html` to a file called /var/www/html/test.html, and then point your browser to 192.168.7.2:/test.html.
:download:`test.html <../code/06iot/test.html>`
You will see the web page shown in :ref:`networking_node_page`.

test.html as served by nginx
Interacting with the Bone via a Web Browser
Problem
BeagleBone Black is interacting with the physical world nicely and you want to display that information on a web browser.
Solution
Flask is a Python web framework built with a small core and easy-to-extend philosophy. :ref:`networking_builtin_server` shows how to use nginx, the web server that's already running. This recipe shows how easy it is to build your own server. This is an adaptation of Python WebServer With Flask and Raspberry Pi.
First, install flask:
bone$ sudo apt update
bone$ sudo apt install python3-flask
All the code in is the Cookbook repo:
bone$ git clone https://git.beagleboard.org/beagleboard/beaglebone-cookbook-code
bone$ cd beaglebone-cookbook-code/06iot/flask
First Flask - hello, world
Our first example is helloWorld.py
:download:`helloWorld.py <../code/06iot/flask/helloWorld.py>`
- The first line loads the Flask module into your Python script.
- The second line creates a Flask object called
app
. - The third line is where the action is, it says to run the index() function when someone accesses the root URL (‘/’) of the server. In this case, send the text “hello, world” to the client’s web browser via return.
- The last line says to “listen” on port 8080, reporting any errors.
Now on your host computer, browse to 192.168.7.2:8080 flask an you should see.

Test page served by our custom flask server
Adding a template
Let’s improve our “hello, world” application, by using an HTML template and a CSS file for styling our page. Note: these have been created for you in the “templates” sub-folder. So, we will create a file named index1.html, that has been saved in /templates.
Here's what's in templates/index1.html:
:download:`index1.html <../code/06iot/flask/templates/index1.html>`
Note: a style sheet (style.css) is also included. This will be populated later.
Observe that anything in double curly braces within the HTML template is interpreted as a variable that would be passed to it from the Python script via the render_template function. Now, let’s create a new Python script. We will name it app1.py:
:download:`app1.py <../code/06iot/flask/app1.py>`
Note that we create a formatted string("timeString") using the date and time from the "now" object, that has the current time stored on it.
Next important thing on the above code, is that we created a dictionary of variables (a set of keys, such as the title that is associated with values, such as HELLO!) to pass into the template. On “return”, we will return the index.html template to the web browser using the variables in the templateData dictionary.
Execute the Python script:
bone$ .\app.py
Open any web browser and browse to 192.168.7.2:8080. You should see:

Test page served by app1.py
Note that the page’s content changes dynamically any time that you refresh it with the actual variable data passed by Python script. In our case, “title” is a fixed value, but “time” change it every second.
Displaying GPIO Status in a Web Browser - reading a button
Problem
You want a web page to display the status of a GPIO pin.
Solution
This solution builds on the Flask-based web server solution in :ref:`networking_nodejs`.
To make this recipe, you will need:
- Breadboard and jumper wires.
- Pushbutton switch.
Wire your pushbutton as shown in :ref:`js_pushbutton_fig`.
Wire a button to P9_11 and have the web page display the value of the button.