Reader small image

You're reading from  Internet of Things with Arduino Cookbook

Product typeBook
Published inSep 2016
PublisherPackt
ISBN-139781785286582
Edition1st Edition
Tools
Concepts
Right arrow
Author (1)
Marco Schwartz
Marco Schwartz
author image
Marco Schwartz

Marco Schwartz is an electrical engineer, entrepreneur, and blogger. He has a master's degree in electrical engineering and computer science from Supélec, France, and a master's degree in micro engineering from the Ecole Polytechnique Fédérale de Lausanne (EPFL), Switzerland. He has more than five years' experience working in the domain of electrical engineering. Marco's interests center around electronics, home automation, the Arduino and Raspberry Pi platforms, open source hardware projects, and 3D printing. He has several websites about the Arduino, including the Open Home Automation website, which is dedicated to building home automation systems using open source hardware. Marco has written another book on home automation and the Arduino, called Home Automation With Arduino: Automate Your Home Using Open-source Hardware. He has also written a book on how to build Internet of Things projects with the Arduino, called Internet of Things with the Arduino Yun, by Packt Publishing.
Read more about Marco Schwartz

Right arrow

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.

Previous PageNext Page
You have been reading a chapter from
Internet of Things with Arduino Cookbook
Published in: Sep 2016Publisher: PacktISBN-13: 9781785286582
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Marco Schwartz

Marco Schwartz is an electrical engineer, entrepreneur, and blogger. He has a master's degree in electrical engineering and computer science from Supélec, France, and a master's degree in micro engineering from the Ecole Polytechnique Fédérale de Lausanne (EPFL), Switzerland. He has more than five years' experience working in the domain of electrical engineering. Marco's interests center around electronics, home automation, the Arduino and Raspberry Pi platforms, open source hardware projects, and 3D printing. He has several websites about the Arduino, including the Open Home Automation website, which is dedicated to building home automation systems using open source hardware. Marco has written another book on home automation and the Arduino, called Home Automation With Arduino: Automate Your Home Using Open-source Hardware. He has also written a book on how to build Internet of Things projects with the Arduino, called Internet of Things with the Arduino Yun, by Packt Publishing.
Read more about Marco Schwartz