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

Configuring your Arduino board for the IoT


In this recipe, we are going to finally learn how to use the on-board Wi-Fi chip that is on the MKR1000 board, and connect the board to your local Wi-Fi. This is a very important recipe, are we are going to use this in nearly every recipe of this book to build IoT projects.

Getting ready

Before we can use the Wi-Fi chip that is on the board, we need an Arduino library to be able to control it. The library for this chip is called the WiFi101 library and you can find it inside the Arduino library manager.

To access the library manager, simply go to Sketch | Include Library | Manage Libraries inside the Arduino IDE. Then, type wifi101 to find the library:

To install the library from there, simply click on the Install button just next to the library version.

How to do it...

Let's now connect the board to your Wi-Fi network. The sketch is quite long here, so I have split it into three parts.

This is the main part of the sketch, which will actually connect your chip to your local Wi-Fi network:

// Libraries
#include <SPI.h>
#include <WiFi101.h>

// Credentials
char ssid[] = "wifi-name";     //  your network SSID (name)
char pass[] = "wifi-pass";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

void setup() {
  
  // Serial 
  Serial.begin(115200);

  // Attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // Wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  printCurrentNet();
  printWifiData();

}

void loop() {

  // Check the network connection once every 10 seconds:
  delay(10000);
  printCurrentNet();
}

What you actually need to change here are the following lines:

char ssid[] = "wifi-name";     //  your network SSID (name)
char pass[] = "wifi-pass";  // your network password

You need to change those lines to put your own Wi-Fi network's name and password. This is something you will have to do in all the sketches for the rest of this book, as we are always going to connect the Arduino board to the local Wi-Fi network.

Then, the following function will print data about your IP address:

void printWifiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip);

  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  Serial.print(mac[5], HEX);
  Serial.print(":");
  Serial.print(mac[4], HEX);
  Serial.print(":");
  Serial.print(mac[3], HEX);
  Serial.print(":");
  Serial.print(mac[2], HEX);
  Serial.print(":");
  Serial.print(mac[1], HEX);
  Serial.print(":");
  Serial.println(mac[0], HEX);

}

And this function will print data about the current Wi-Fi network to which your Arduino board is connected:

void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);
  Serial.print("BSSID: ");
  Serial.print(bssid[5], HEX);
  Serial.print(":");
  Serial.print(bssid[4], HEX);
  Serial.print(":");
  Serial.print(bssid[3], HEX);
  Serial.print(":");
  Serial.print(bssid[2], HEX);
  Serial.print(":");
  Serial.print(bssid[1], HEX);
  Serial.print(":");
  Serial.println(bssid[0], HEX);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

  // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption, HEX);
  Serial.println();
}

It's now time to finally test this sketch and connect your board to the Internet! You can get the whole sketch from the GitHub repository of the following book:

https://github.com/marcoschwartz/iot-arduino-cookbook

Now, make sure to change your Wi-Fi name and password inside the sketch, and then upload the sketch to the board. Immediately open the Serial monitor. This is what you should see:

If you can see something similar, congratulations, your board is now connected to your Wi-Fi network and to the Internet (assuming your Wi-Fi router is connected to the Internet).

How it works...

The WiFi101 library makes it really easy to use the on-board Wi-Fi chip of the MKR1000 board, and to easily connect the board to the Internet. This is a very useful function that we are going to use in the whole book.

See also

I now recommend checking the two remaining recipes in this chapter, to learn how to actually use the Internet connection of the board to interact with web services.