FeatureImage15

Chapter 10: The Internet of Things

Chapter 11: The Internet of Things

/Chapter 11: The Internet of Things
Chapter 11: The Internet of Things2019-01-11T16:13:49+00:00

Introduction

This is the chapter web page to support the content in Chapter 10 of the book: Exploring BeagleBone – Tools and Techniques for Building with Embedded Linux. The summary introduction to the chapter is as follows:

In this chapter you are introduced to the concept of the Internet of Things (IoT). Two new sensors are discussed—a simple temperature sensor and a more complex Bluetooth low‐energy sensor. Using the BBB, these sensors are the example things that are connected to the Internet. Several different IoT communications architectures are described: The first architecture configures the BBB to be a web server that uses server‐side scripting to display sensor data. Next, custom C/C++ code is described that can push sensor data to the Internet and to platform as a service (PaaS) offerings, such as ThingSpeak and Xively. Finally, a client/server pair for high‐speed Transmission Control Protocol (TCP) socket communication is described. The latter part of the chapter introduces some techniques for managing distributed BBB sensors, and physical networking topics, including connecting the BBB to the Internet using Wi‐Fi; setting the BBB to have a static IP address; and using Power over Ethernet (PoE) with the BBB. By the end of this chapter you should appreciate the power of the BBB to leverage Linux software in order to build complex network‐attached devices, and you should be able to build your own IoT devices.

Learning Outcomes

After completing this chapter, you should be able to:

  • Interface to the Analog Devices TMP36 temperature sensor and to the Texas Instruments SensorTag using Bluetooth Smart.
  • Install and configure a web server on the BBB and use it to display static HTML content.
  • Enhance the web server to send dynamic web content that uses CGI scripts and PHP scripts to interface to BBB sensors.
  • Write the code for a C/C++ client application that can communicate using either HTTP or HTTPS.
  • Interface to platform as a service (PaaS) offerings, such as ThingSpeak and Xively, using HTTP and custom APIs.
  • Use the Linux cron scheduler to structure workflow on the BBB.
  • Send e‐mail messages directly from the BBB and utilize them as a trigger for web services such as IFTTT.
  • Build a C++ client/server application that can communicate at a high speed and a low overhead between any two TCP devices.
  • Manage remote BBB devices, using monitoring software and watchdog code, to ensure that deployed services are robust.
  • Configure the BBB to use Wi‐Fi adapters and static IP addresses, and wire the BBB to utilize Power over Ethernet (PoE).

Additional Materials

CGI using C++ on the BeagleBone (Ggicc)

In this chapter I describe how you can build web-based CGI applications that can interface with electronics hardware that is attached to the BeagleBone using Bash scripts that call C/C++ programs. The solution works well for very straightforward applications, but this linked discussion investigates more advanced solutions for applications where there are more complex interactions — for example, the use of web forms to pass data between your web browser and the application that is executing on the BeagleBone. In the linked blog discussion I begin by explaining how you can use a C/C++ program, rather than a CGI script, to display a web page. I then investigate the use of the GNU Cgicc library for more structured and complex interactions. All of the source code for this example is in the Exploring BeagleBone GitHub repository in the chp10/cgicc directory.

Products Described in the Chapter

A number of different Wi-Fi adapters are described in the chapter, such as those in the figure below. The widget on the left-hand side links to some of these adapters on Amazon, where you can find other products, even health related like the compression socks women that are good for women that practice sports.  However, not all of these adapters are confirmed to be working. The adapters that were evaluated in the book that are working well are:

  • The TP-LINK 300Mbps Wireless N Mini that uses the RTL8192CU chipset. Two alternatives are presented in this list: the Rosewill Wireless Adapter and the generic 300Mbps Realtek adapter, which both use the same chipset. The two alternatives have not been tested
  • The Edimax EW-7811Un that uses the RTL8188CUS chipset. It is listed along with two alternatives: the Wi-Pi Raspberry Pi adapter, and the Plugable USB 2.0 adapter, which both use the same chipset. The two alternatives have not been tested, but the Wi-Pi is known to work well.
  • The Asus USB-N10 adapter that uses the RTL8188SU chipset.

Digital Media Resources

Here the digital resources referred to in the chapter web page are provided. There are high-resolution versions of some of the important figures and links to videos, resources and websites that are described in the chapter.

Source Code

All of the source code that is described in this book is available in a public GitHub repository: Derek Molloy Exploring BeagleBone repository.

You can clone this repository on a Linux desktop computer or your BeagleBone using the command:

The code for this chapter can be accessed in the chp10 folder of the cloned repository. The formatted code for the client/server example is provided here for your convenience.

Some High-Resolution Figures from this Chapter

Here are high-resolution images of some of the more complex figures in this chapter, which may help you in wiring the circuits. Please note that you can close this pop-up window by pressing the Escape key.

External Resources

Important Documents

External Web Sites

AM335x ARM A8 Technical Reference Manual

The AM335x Technical Reference Manual (TRM)

BeagleBone Black System Reference Manual

The BeagleBone Black System Reference Manual (SRM)

Errata

  • Page 391. There should be a line echo “” after the line echo “Content-type: text/html” in the script that displayed in the center of the page. The code in the GitHub repository is correct.
  • Page 396. The title of code Listing 10-2 should be /exploringBB/chp10/WebBrowser/WebBrowser.c not WebClient/WebClient.c. The correct title is used in the description and elsewhere.

Recommended Books on the Content in this Chapter

       

Feel free to contact us on Social Media, we were recently able to Buy automatic instagram likes for all our content, this way the posts will grow on popularity.

41 Comments

  1. TB January 4, 2015 at 10:23 pm

    Loc 10515: The link for the TMP35/36/37 data sheet is broken. It isn’t a link.

    • Derek January 4, 2015 at 10:48 pm

      It seems to be working here at tiny.cc/ebb1001 I wonder if this is a “.” problem again? Some people are reporting that the period is being added to the URL and messing it up on the Kindle version. I tried creating a parallel set of URLs with periods added, but unfortunately I am not permitted.

  2. TB January 4, 2015 at 10:35 pm

    Loc 10486: Could you please explain this line of code…
    ************************************************
    float cur_voltage = adc_value * (1.80f / 4096.0f);
    ************************************************

    I certainly understand that the BBB has a 12-bit ADC, hence the 4096. However I am not sure where you’re getting the 1.80f from? Does it have something to do with the conversion from Fahrenheit to Celsius? There’s a 1.8 constant there, but I am not quite sure where the one in your code comes from. What am I missing?

    • Derek January 4, 2015 at 10:45 pm

      The 1.80 is the 1.8V upper voltage level of the ADC. This calculation results in the exact voltage level on the ADC input.

      • TB January 4, 2015 at 10:59 pm

        I’m still not sure that makes sense to me, but I do remember you discussing it in one of the earlier chapters–so I’ll go back and look for it. It would be easier if the search feature worked, but it doesn’t yet. So I’ll have to search manually…but I do remember seeing something about converting the raw ADC data, so it might well be easily explained there. But it’s not jumping out at me right now.

        • TB January 4, 2015 at 11:02 pm

          Ah…got it. Location 4466.

          The 1.8 volts is divided into 4096 steps, so each integer “step” (adc_value, here) is 1.8/4096 wide. OK, I do remember it now, lol.

    • TB January 4, 2015 at 10:50 pm

      Also, in regards to the rest of that block of code to get the temperature (in degreesC), it might be eaiser to solve for the equation of the line and use that in that function. It would seem to remove one line of code (and some math) from that function. Since the voltage output is a linear 0.01v / degC, one can simply express the equation as this:
      ************************************************
      y – 0.75 = 0.01(x – 25)
      y = 0.01x – 0.25 + 0.75
      y = 0.01x + 0.50 (where y = voltage, and x is degrees C)

      so: degC = (voltage – 0.50) / 0.01
      ************************************************
      So I am still not quite sure where the “1.80f” comes from, but your function can simply become…

      float getTemperature( int adc_value)
      {
      float cur_voltage = adc_value * ( 1.80f / 4096.0f);
      return ( (cur_voltage – 0.50) / 0.01);
      }
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      Looking at it again now, I guess it doesn’t simplify the math all that much, as you still have one division–and I’ve added a multiplication operation. It would be interesting to profile each block of code and see which executes faster.

      • Derek January 4, 2015 at 11:17 pm

        Yes, I haven’t optimized the code in this case as it is not called very often — however, I would hope that compiling this with -O3 would result in the 1.80f/4096.0f being replaced with the resulting constant.

  3. TB January 4, 2015 at 11:27 pm

    No doubt it would. And you could easily do that yourself of course, with a double. But that’s largely irrelevant, as what you can’t optimize (in this case) is either of the lines of code involved in the replacement of your code with mine. And since I added a multiplication operation, it might be better to simply leave it as the text lists it originally…

  4. TB January 5, 2015 at 12:15 am

    Loc 10585: Simple grammar error. Should be:
    ************************************************
    Integrate sensors and use THEM to intercommunicate between devices.
    ************************************************

    • Derek January 5, 2015 at 12:26 am

      Thanks. The “it” refers to the web server, not to the sensors. I could have phrased the point more clearly!

      • TB January 5, 2015 at 12:50 am

        Ah… I noticed a few more of those tiny.cc/ebbXXXX links not being links–and not all of them were following by a period. But the actual link seems to work correctly, once you type it in. Kindle cloud browser doesn’t allow you to copy and paste, but if you type them into the URL bar they generally seem to point at what you want them to. So I’m not worrying about those anymore, given that you’ve said they seem to work for you.

  5. TB January 5, 2015 at 1:04 am

    Loc 1652: The index.html write will have to be done as root of course, as root is owner and group for /var/www–and there are no write privileges set for others. So unless you chmod it accordingly, the write will have to be as root.

    • TB January 5, 2015 at 1:08 am

      …same as for /usr/lib/cgi-bin, actually.
      Maybe somehow your distro is different than mine? I’m using the latest Debian image from the Beagleboard.org site.
      *******************************************
      $ uname -a
      Linux beaglebone 3.8.13-bone50 #1 SMP Tue May 13 13:24:52 UTC 2014 armv7l GNU/Linux
      $ ls -l | grep cgi
      drwxr-xr-x 2 root root 4096 Feb 1 2014 cgi-bin
      *******************************************

      • Derek January 5, 2015 at 9:48 am

        Thanks for that. Yes, you may need to make /var/www and /usr/lib/cgi-bin writable so that a user can add scripts. I don’t actually recall having done that, but I have used so many images that it is possible.

        • TB January 5, 2015 at 1:36 pm

          Understood. I just listed these comments here in case someone was having problems and needed a hint as to what might be amiss. It was no problem whatsoever to just run chmod, or create the file(s) as root. And since everyone had read and execute privilege, it runs just fine…

  6. TB January 8, 2015 at 1:41 am

    Anyone else try to sign up for an Xively free development account? I did tonight and they are telling me there’s a waiting list for the free accounts, so I was just wondering if anyone knew how long that took. They didn’t give any indication in the email I received.

    • Derek January 8, 2015 at 2:45 am

      That’s new and a pity, as it is a great platform to work with. Please let me know how long it takes. Luckily I have covered both Xively and ThingSpeak in the chapter (and fortunately ThingSpeak is open source). I can have a look at alternatives if there are continued access problems. Derek.

      • TB January 8, 2015 at 9:49 pm

        I got ThingSpeak to work after playing with it a bit. I posted a link in the Embedded Fun group. Pretty slick!

  7. TB January 8, 2015 at 4:36 am

    Loc 11391: In Table 10-3 where you talk about wireless connectivity for the BBB, your table talk about XBee and mentions that is has short range and low throughput. However I wondered if you’ve seen some of the “XBee Pro 900-series” modules? I’ve never used one of those particular units, but this SparkFun link does show what I’ve read about them in the past–that they have impressive range and throughput. I’d like to try one of those things actually, and may do that sooner rather than later–and I was just wondering if you’ve played around with them at all?
    ************************************************
    https://www.sparkfun.com/pages/xbee_guide

    • Derek January 8, 2015 at 12:11 pm

      Maybe it could be more clear, but the table lists ZigBee as the highest performer in terms of range out of all of the entries in this table (at approx. 100m — the others could be as little as 10m). I have used the the XBee Pro modules (really impressive!) — I built an LED flasher that flashed until it lost a signal and I was able to walk a good distance away from my house. The figures listed in the table are “realistic” values for urban environments. I believe that with line-of-sight that much greater range is possible, but I think the table gives a generalized indicative comparison of the technologies. Jeremy Blum’s book has a full discussion on ZigBee with the Arduino — I would have loved to have covered it too with the BBB — maybe it will be an addendum! Derek.

  8. TB January 8, 2015 at 4:54 am

    Loc 11525: Where you talk about configuring the BBB to use a static IP address, your text states…

    ************************************************
    “To allocate a static IP address for the wireless adapter, you can alter the /etc/network/interfaces configuration file to manually specify the address…”
    ************************************************
    Then you give an example entry, where you use “auth eth0.” Shouldn’t that be “auto wlan0,” for instance? Isn’t the “eth0” interface in Linux the first wired Ethernet connection by default? I mean, it’s probably possible to label the wireless interface as eth0, but all the standard convention I’ve seen uses wlanX.

    • TB January 8, 2015 at 4:57 am

      Ah…maybe you referred to the wireless interface, but then just gave the wired interface (eth0) syntax as an example? The comment does mention that it is “The primary network interface,” so maybe you weren’t calling the wireless adapter “eth0” at all…

      • Derek January 8, 2015 at 12:14 pm

        Yes, exactly. I am using a wired adapter eth0 for the static example, rather than wireless. Apologies for the confusion. Derek.

  9. Michael K Johnson January 21, 2015 at 2:35 am

    On page 384, under “A Room Temperature Sensor”, you say “These voltage output levels are within the safe levels for the BBB ADC—the room temperature range is not!”

    That’s really confusing. I hope the BBB ADC works within the normal room temperature range; I wasn’t planning to refrigerate my BBB while using the ADC… ☺

    Are you trying to say that if the TMP36 reported one volt per degree C, it would exceed 1.8V? (Yes, I rather guess it would!)

    Is the main point that this is a rare component that can be powered from 5V and yet provide a safe output without clamping or scaling for a 1.8V-limited ADC?

    • Derek January 21, 2015 at 9:16 pm

      Hi Michael — Thanks! I should have clarified what I meant more clearly… to get a voltage output of ~0.1V the room temperature would have to be -40 degrees Celsius (which is -40 Fahrenheit), and at ~1.8V the room temperature would have to be +125 degrees Celsius (which is 257 Fahrenheit). According to Gerald Coley, the BeagleBone components have a safe range of ~0-50 degrees Celsius. Regardless, that range is beyond my safe operating room temperature range! Derek.

  10. Marty Porter February 24, 2015 at 1:13 am

    Derek, Is there a problem with your comments not posting correctly? I notice that the last comments are from Jan 27. I posted since then and saw that you had answered, but the last couple of days I’ve been checking back to see your answer and it has been gone. Is it possible that your message system has lost the comments since Jan. 27? Thanks.

    • Derek February 24, 2015 at 1:35 am

      Hi Marty, I don’t think so. Some chapters seem to have many more questions than others — Chapter 10 is a quiet one! Derek.

    • Derek February 24, 2015 at 1:37 am

      Hi Marty, I searched and see your question (with my response) — it is on the book main page. Hope it helps, Derek.

  11. Gary Rubin April 9, 2015 at 10:13 pm

    I like the DS18B20 vs the TMP36. Maybe because I found it to be cheaper, but it is more advanced. It does require a bit more programming but I used them with great success with the Raspberry Pi, the PIC32MX795F512L, and the Teensy 3.1. Since I picked the BeagleBone Black as my choice for an embedded Linux prototyping platform, I am actively working to use this sensor. Since I have so many of these sensors, both in raw form and in waterproof probe casings, it makes sense for me to do this. If anyone is interested, I’ll post what I do to get the DS18B20 working with the BBB. And as always, thank you Derek! This looks to be an exciting chapter to work through!!

  12. Rod April 22, 2015 at 7:10 am

    External link typo

    Debian network configuration: tiny.cc/ebb109

    should be : tiny.cc/ebb1009

    Regards

    • Derek April 22, 2015 at 10:17 am

      Thank Rod, I have set that URL to also point to the same reference. Thanks, Derek.

  13. Rod April 22, 2015 at 8:36 am

    While I’m at it..

    p396 Listing10.2 …./WebClient/WebClient.c

    should be
    …/webBrowser/webBrowser.c

    Regards

    • Derek April 22, 2015 at 10:18 am

      Thanks Rod, Adding to the list of errata. Kind regards, Derek.

  14. Carl September 16, 2015 at 9:19 pm

    Derek –

    Your WiFi tutorial in the book was really great and useful. I used it to get the Ralink Technology Corp RT5370 chipset Wireless adapter dongle working. It is the Purple/White one sold many places such as at element14:

    Anyhow, since I ran into a few minor obstacles, I thought I’d post them here to help keep your great book and great website up-to-date. You really can’t imagine how helpful your book and site are to me and our team.:
    Here are the steps I needed to do to get this working well on my system.
    first, I had to add several missing packages to my linux distro: 3.8.13-bone70
    1.) sudo apt-get install usbutils ….. (to use lsusb to see my WiFi Dongle info)
    2.) sudo apt-get install wireless-tools ….. (to use iwlist wlan0 scan to seach for my network info)
    3.) sudo apt-get install wpasupplicant ….. (to use the wpa-conf and other wpa tools)
    4.) sudo apt-get install firmware-Ralink ….. (to install the Ralink RT5370 driver)

    With these done, getting the rest working was simple with your book as a guide.
    My procedures were as follows:
    1.) Download the above
    2.). cd /etc
    3.) wpa_passphrase SELabs myNetworkPassword > /etc/wpa.conf
    4.) Open that file and delete the #myNetworkPassword line to remove this comment that announces to the world my wpa password
    5.) check my networkl settings and confirm that pairwise and group settings are CCMP & TKIP as you had them in the book
    6.) edit my /etc/network/interfaces to add auto wlan0 to have it auto detect at boot, and then exactly as you have it in the book

    I also found ethe following websites helpful for this:
    1.) http://elinux.org/BBBWiFiConfigs
    2.) https://wiki.debian.org/WiFi/HowToUse#wpasupplicant
    3.) http://manual.aptosid.com/en/inet-setup-en.htm

    simple as that…20-minutes effort and I’m up and running without a network cable to tie me down anymore 🙂

    Thanks again for the wonderful book and your great support of it on this site. You have contributed more than you know to the success of myself and many others.

    Best Regards,
    Carl
    Michigan, USA

    • Derek September 18, 2015 at 10:44 pm

      Thanks Carl, Very useful! Derek.

  15. Andy September 24, 2015 at 10:44 pm

    Hi Derek,
    I am still loving this book. Another pair quick questions;

    1) I can get wifi access if I run ‘ifup wlan0’ when I log on. I have even added this command to my .profile file, so I have access automatically when I log in.
    What do I need to do to activate wlan0 when the BBB boots – but I have yet to log on?
    This will allow me to connect using SSH wirelessly.

    2) How do I store multiple router credentials ie my home router and office router?
    currently I have….
    # WiFi Example
    auto wlan0
    iface wlan0 inet dhcp
    wpa-ssid “AndroidAP”
    wpa-psk “cccc”

    Thank in Advance
    Andy

    • Derek September 27, 2015 at 11:15 pm

      Thanks Andy, it should come up automatically if you add the line auto wlan0 in the /etc/network/interfaces file — I’m surprised that it doesn’t! (I see that you have done this in the second question) On the multiple credentials, I have had some luck with adding multiple network entries to my /etc/wpa_supplicant/wpa_supplicant.conf with two ID strings. Then in the /etc/network/interfaces file under wlan0 add entries for both network entries. See the network blocks section in: https://www.freebsd.org/cgi/man.cgi?query=wpa_supplicant.conf. Be warned, it can be a frustrating process! Kind regards, Derek.

      • Andy September 30, 2015 at 12:44 pm

        Hi Derek,

        I did some more testing last night. I am using an android app called ‘fing’ to monitor local IP address allocation on my home network.

        I have added the “ifup wlan0” in my .profile script.

        If I boot the device by applying power over the 5vdc jack, I do not get an IP address appearing for the BBB.

        If I connect over USB via 192.168.7.2 and login ( which causes the “ifup wlan0” to execute via my .profile script). I see an IP address appear.

        Is there some other boot script, which executes prior to login, which I can add the “ifup” command to?

  16. Carl Schell June 5, 2016 at 3:34 am

    Hi Derek,

    I’ve had a problem with WiFi for quite some time that I’ve simply lived with, but found the solution tonight, so I thought I’d share in-case anyone else has the same issue (or in-case I forget in the future!). I have had WiFi Dongle with the Ralink RT5370 chipset working on one dongle for a while…I’ve made many copies of my modified BBB image, and this dongle works on all of the devices I use (~10). Great…BUT…

    When I ordered more WiFi dongles with the same chipset, none worked! I tried everything I could to fix, but nothing helped…Until tonight when I stumbled on an old piece of info on a Google Groups forum from 2013.

    In it, there was the following suggestion by William Hill: “When a WiFi dongle is plugged in udev detects it and assigns it an interface name “wlan0” .. “wlan1” .. “wlan2″ etc in first come first served order. This interface name is linked to the dongle’s MAC address, so when you clone a system the new wifi dongle doesn’t get assigned the same interface name, and networking breaks. So edit the saved state file /etc/udev/rules.d/70-persistent-net.rules and delete the old entry. ”

    I did exactly this and it worked perfectly, and I now can get all my various BBB systems up on Wifi using any of the dongles (provided I follow this step when not working)!

    Thanks again for the great book and great site. I have pre-ordered your Exploring Raspberry Pi book and look forward to its’ release!

    Carl – Michigan, USA

    • Derek June 7, 2016 at 3:35 pm

      Thanks Carl, really useful info. Hopefully you enjoy the new book. There is some overlap in content between Exploring Beaglebone and Exploring Raspberry Pi, particularly in the first few chapters. I have made a comparison page available on the Raspberry Pi website at http://exploringrpi.com/comparison/ so you can see what you are getting. Thanks for the support! Derek.

Comments are closed.

Exploring BeagleBone

This is the companion site for the book “Exploring BeagleBone: Tools and Techniques for Building with Embedded Linux” by Derek Molloy. The site structures and contains all of the digital media that is described in the book. Each chapter in the book links to an individual web page, which can be accessed using the menu on the top right-hand side of this page when you close this menu. For details of the book itself, click here.

Recent Works

Latest from Derek Molloy YouTube Channel

Oops, something went wrong.