Book Image

Internet of Things with Arduino Cookbook

By : Marco Schwartz
Book Image

Internet of Things with Arduino Cookbook

By: Marco Schwartz

Overview of this book

Arduino is a powerful and very versatile platform used by millions of people around the world to create DIY electronics projects. It can be connected to a wide variety of sensors and other components, making it the ideal platform to build amazing Internet of Things (IoT) projects on—the next wave in the era of computing. This book takes a recipe-based approach, giving you precise examples on how to build IoT projects of all types using the Arduino platform. You will come across projects from several fields, including the popular robotics and home automation domains. Along with being introduced to several forms of interactions within IoT, including projects that directly interact with well-known web services such as Twitter, Facebook, and Dropbox we will also focus on Machine-to-Machine (M2M) interactions, where Arduino projects interact without any human intervention. You will learn to build a few quick and easy-to-make fun projects that will really expand your horizons in the world of IoT and Arduino. Each chapter ends with a troubleshooting recipe that will help you overcome any problems faced while building these projects. By the end of this book, you will not only know how to build these projects, but also have the skills necessary to build your own IoT projects in the future.
Table of Contents (14 chapters)
Internet of Things with Arduino Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Grabbing the content from a web page


To illustrate how the WiFi101 library is working on the MKR1000 board, we are now going to use it to grab the content of a web page, and display the result inside the Serial monitor.

Getting ready

You do not need any extra steps here, simply make sure that you have the WiFi101 library installed.

How to do it...

Let's now see the sketch for this recipe. As it is really similar to the sketch of the previous recipe, I will only highlight the main pieces of code that were added here:

  1. You first need to define which page we are going to grab. Here, I will just make the board grab the www.example.com page:

    char server[] = "www.example.com";
  2. Then, we need to create an instance of a Wi-Fi client:

    WiFiClient client;
  3. Then, inside the setup() function of the sketch, we connect to the server we defined earlier, and request the Web page:

    // Connect to server
      if (client.connect(server, 80)) {
        Serial.println("connected to server");
        
        // Make a request:
        client.println("GET / HTTP/1.1");
        client.println("Host: www.example.com");
        client.println("Connection: close");
        client.println();
      }
  4. Inside the loop() function of the sketch, we then read the data coming back from the server, and print it inside the Serial port:

    while (client.available()) {
        char c = client.read();
        Serial.write(c);
      }
  5. We then stop the connection with the following piece of code:

    // Stop the connection
      if (!client.connected()) {
        Serial.println();
        Serial.println("disconnecting from server.");
        client.stop();
    
        // do nothing forevermore:
        while (true);
      }
  6. It's now time to try this sketch! First, grab the code from the GitHub repository of this book, and then change your Wi-Fi credentials inside the code. Then, upload the code to the board, and open the Serial monitor. This is what you should see:

If you can see that, it means that the board has successfully grabbed the content of the web page and displayed it inside the Serial monitor.

How it works...

The sketch uses the Wi-Fi client of the WiFi101 library, which is a very powerful object that we will use again in several chapters of this book.

See also

I now recommend checking the next recipe, in which you will actually learn how to use the Wi-Fi client library to send data to a cloud server.