• An Arduino Shield, is basically a printed circuit board. An extra board, that you connect to your Arduino to extend its functionality in some way.


  • The Arduino if you look at it all by itself without the shield the hardware is pretty limited, its got a microcontroller, its got pins, holes that you can connect to the microcontroller, its got an LED, there's a USB port that's used to communicate with the computer.


  • But besides that it doesn't have much in the way of interface. It is necessary in a more complicated system to extend it to do certain things that you can't just do with the microcontroller alone. So a shield makes that easy for you, a shield is basically two things really.


  • Its hardware, which is basically the same size of the Arduino and fits right on top of the Arduino. Its sold as pre-wired, pre-manufactured board, and you can buy it manufactured. There are also shields that are not manufactured, where you can just buy the design, it's open source, you can buy the design and you can manufacture it yourself.


  • But generally, you' buy it pre-manufactured, so there's the hardware side of the shield, but there's also the software side, the library that's associated with the Shield. To make it easy to use these Shields - a library with a set of functions that do all of the interesting things that the Shield can do.


  • Benefits of Shields.


    1. No wiring is needed. The circuit on that shield is pre-wired.


    2. The wiring that connects the shield, all the shield pins, to the actual Arduino pins, you don't have to worry about that either, because that's done for you automatically by stacking the shield on top of the Arduino. If you look at the shield, it's got pins underneath it, which fit into the holes of the Arduino. So, you don't have to do any kind of wiring. You just take board, stack it right on top of the Arduino and all the wiring is done for you.


    3. You don't have to design the hardware side of the circuit at all. Simple to use because the library functions take care of the complicated details of how to use the device.


  • Most shields only use a small subset of the pins, even though, all the pins are wired, so you stack and you connect all the pins, most of the pins are not used to communicate between the Arduino and the Shield. And this is important because, If you have multiple shields, say we are using two shields. If they're using the same pins for two different purposes, then the communication will fail. So say, one shield, your using digital input one, for an input, the other shield is using digital pin one for an output, then you got a problem.


  • So, when you're using multiple shields together you need to be aware of what pins are being used for what shield, and how their being used.


  • Now, a common way for shields to communicate with the Arduino is through I-2-C. we can use the wire library to access that. That sharing is fine, right?, if you've got three shields and they all communicate through the Through I2C. They can share exactly the same pins, the SDA and SDL pins, and there's no conflict


  • As long as shield has a different address, and different slave address, no conflict. You can stack as many as you want. As we just learned about when we were talking about I2C.


  • But other times they use, the two different shields can use the same pin for different purposes and there can be a conflict. So, shield headers may not be soldered in.


  • How do we connect to the Internet? - an ethernet shield is generally giving you a wired connection to the Internet.


  • So, it allows Internet connections through a wired interface.


  • The shield includes that Ethernet jack, a port, an RJ45 port.


  • They all use the ethernet library that's provided by Arduino.


  • Internet addresses are an essential parameters in the functions provided by the arduino ethernet library.


  • A MAC address - a MAC address is a unique address which is "hardwired" into each network adapter. It's six bytes long. I


  • Theoretically, it's supposed to be hardwired, but in reality with these ethernet shields you can change that.


  • Example: A laptop that has an ethernet jack and it got a wifi adapter. It would have two MAC address. The ethernet would have one MAC address The wifi would have another MAC address, and those would be fixed in there.


  • But with ethernet shields, most of the time we can actually change the MAC address. Or sometimes they come with a MAC address on them on a sticker, but we can usually change them. But they are unique, so It is assumed by internet protocols that the MAC addresses are different. So no two devices should have the same MAC address.


  • So when you're making up a MAC address, this isn't realistically a problem for us. Because when we're doing this, we're gonna be doing this in our local network. So maybe we'll be talking just amongst the machines in the room or something like that. So as long as there isn't any interference in MAC address between the machines in the room, which is highly unlikely, we won't have a problem.


  • But generally, if you're talking on the big, wide Internet, every MAC address has to be different from every other MAC address. And companies do this getting their own prefix, Mac address, prefix.


  • Everyone that comes from a particular company has the same first several bytes, and then the rest are determined by the company, and they ensure that their different.


  • But the different companies have different sets of most significant bytes so they can never interfere with each other.


  • There's also an IP address - an IP address is four bytes long and it's used in Internet addressing so when you want to send to a machine, you send to an IP address. The IP address can change, however, unlike the MAC address. We are using IP version 4 address in this page.


  • IPv4 address - basically four bytes long, each byte remember is Eight bits. It represents numbers 0 to 255. They're four numbers separated by dots. Each number is a number from 0 to 255.


  • Port address - TCP protocol defines a set of ports. Example - If you have a particular IP address then in addition to its IP address, it has several ports that it can use for communication. So the port number, generally, is mapped to a particular application.


  • So if you have got a web browser running on it. That web browser communicates on port 80 because that is the standard. It communicates with the web server on port 80.


  • The web browser when it wants send a message to web server, it sends it on port 80. And the server is listening to port 80.


  • When you wanna send something through the internet to a machine you got to specify the IP address. But, you also gotta specify the port number. To indicate basically which application protocol you're using. and that's two bits long which means it's 16K. There's 16K possible ports.


  • For instance, it is known port 80 is for internet, for http, communication.


  • DNS or Domain Name Service - it's a service that maps the domain name to an IP address. So domain name is basically a mnemonic name that humans can understand and memorize. And this maps to an IP address, which you need to actually address a machine.


  • For instance, www.uci.edu. maps to is 128.195.188.232. and that translation has to happen every time.


  • If I'm in my browser let's say I type in www.uci.edu, it has to get translated into that IP address before I can address that server and the server that's running on that machine. So, that's what DNS is for, Domain Name Service is for. Basically it's a look-up process where you type in, ucl.edu and your local machine goes to another machine, a Domain Name Server, and asks for the mapping


  • If it knows it sends it back, if it doesn't it forwards it to another domain name server and so on. So, that's DNS, domain name service. And because of this It is common that you have a domain name server like a default domain name server that you go to when you need to resolve a name.


  • So it's often the case that you configure one of these ethernet shields with the IP address of the DNS server so that it can lookup names.


  • Ethernet Library - The library that's used to control an Ethernet controller or, an Ethernet shield.


  • you need to set a #include - to include header files, .h files. For instance, Ethernet.h, EthernetClient.h, EthernetServer.h more.


  •  
    #include <SPI.h>
    #include <Ethernet.h>
    
    
  • To initialize the Ethernet interface to initialize the shield. So these are the first things that you're gonna call when you wanna use the shield.


  • Ethernet.begin, - there are five possible arguments for Ethernet.begin. Only the first one is required and the rest are optional. We'll go through all them and we won't actually use all them.


    1. So first argument that is required is the MAC address. As the MAC address of your Ethernet shield


    2. IP Address, that's the IP address of your shield, optional.


    3. DNS, so that's the IP Address of the domain name server that you wanna connect to by default and you don't have to give that.


    4. A gateway is the address of a router, which knows how to route packets to other networks.


    5. So if you're going outside of our local network then you might want to specify a default gateway. A default router is when it wants to send packets to an outside network


    6. Subnet mask - Subnet mask tells us which other IP addresses are in the local subnet.


  • Dynamic Host Connection Protocol - in order to communicate on the internet, you need an IP address. Every node on the internet needs an IP address. Now note that in our Ethernet type again, we did not have to specify and IP address.


  • It can get one dynamically. DHCP, Dynamic Host Connection Protocol is a protocol that a allows a node, a new node on the network to request a new IP address. Now this is generally enabled on routers by default.


  • Now DHCP is invoked if you call Ethernet.begin ad it has no IP address argument.


  • Another way to do this, instead of allowing DHCP, which gives dynamic IP addresses is that you can also use static IP addresses. Static IP addresses are fixed IP addresses. So that means every time you power your machine and connect to the network, it gets exactly the same IP address.


  • Now a static IP address, that's more difficult to manage. Basically, that IP address needs to be known across the world. It has to be known to any machine that wants to connect to your machine, it needs to know that static IP address.


  • It's useful for servers, because like say, www.uci.edu, there's a web server right there, which I can go to. And in order for my machine to connect to that server to look at the school's web page, I need to be able to look up its IP address. So it needs to be static


  • So servers often have static addresses, but dynamic addresses are very common for non servers.


  • Typically, the Internet is used in the client-server relationship


  • So client-server relationship, what this means is that the server sits and waits for connections


  • Like a web server, it sits there, and it provides a web page.


  • Then a client does the requesting. So a client connects to the server and request some service. Like request a web page


  • Our Ethernet shield can act as a client or a server. So right now we are going to talk about using the Ethernet shield as a client.


  • When you do that, first thing you do is you have to create a client object


  •  
    #include <Ethernet.h>
    #include <SPI.h>
    
    
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    byte ip[] = { 10, 0, 0, 177 };
    byte server[] = { 64, 233, 187, 99 }; // Google
    
    EthernetClient client;
    
    void setup()
    {
      Ethernet.begin(mac, ip);
      Serial.begin(9600);
    
      delay(1000);
    
      Serial.println("connecting...");
    
      if (client.connect(server, 80)) {
        Serial.println("connected");
        client.println("GET /search?q=arduino HTTP/1.0");
        client.println();
      } else {
        Serial.println("connection failed");
      }
    }
    
    void loop()
    {
      if (client.available()) {
        char c = client.read();
        Serial.print(c);
      }
    
      if (!client.connected()) {
        Serial.println();
        Serial.println("disconnecting.");
        client.stop();
       }
    }
    
    
    
    
  • EthernetClient is the name of this class. You say EthernetClient client, and that creates a client object.


  • To connect to a server to get some service from there are two ways of doing it, basically you're calling this function called client.connect. Now client.connect, it takes two arguments. Its two arguments are IP address and a port.


  • So you gotta give the IP address and the port number as its two arguments and then it knows what machine to talk to. Or you can pass the two arguments of the domain name and the port.


  • client.connectis gonna return either a zero or a one. True or false. It returns a one if a connection is successfully made with a server. It returns a zero if it's not.


  • And client.stop that stops the connection. It ends the connection.


  • Now once you've made your connection, your client has made a connection to the server, you want to send data to the server and receive data from the server.


  • client.print or client.println that sends data. print just sends the data and println sends the data and sends a carriage return at the end.


  • Now, and println is the character term. Data is a string, so if you wanna send strings you use print or println. Or an array of bytes which is also a string.


  • Now if you just wanna send raw data, it's not a string at all, then you call client.write and you just pass the value, whatever it is.


  • client.read is used to do the opposite. If want to read data. If you're trying to read data from the server. It calls client.read. No arguments and that just reads the next available byte.


  • Also, you can call client.available. Client.available lets you know if there is data waiting. So if client.available returns one, that means there is data waiting so you can call client.read.


  • If client.available returns to zero, then there's no point in calling client.read because it will fail.


  • Ethernet shield can act as a server.


  • Remember that the goal of a server is to sit listen for connection from clients, for requests from clients. So it's sits at some IP address, listens to a port, and when the request comes in on that port, it services the request, i.e., it receives data, or whatever the request is, and then sends data satisfying the request.


  • First thing you want to do is create a server object by an explicitly call to EthernetServer. A function called EthernetServer which is its constructor. And you pass an argument called that is the port. So this creates a server. So, server is now equal to a server object that is listening on whatever port it is that you specified


  • So if you're making a web server you would listen on port 80 and so forth. To start listening, we have to create a client object. Now this sounds a little counter intuitive.


  • But you need to create a client object because that client object is gonna represent the client that you're talking too, that you're communicating with. So you say EthernetClient client = server.available().


  • So you're calling this function server.available() and that returns a client if a client is waiting


  •  
    #include <Ethernet.h>
    #include <SPI.h>
    
    // Enter a MAC address and IP address for your controller below.
    // The IP address will be dependent on your local network:
    byte mac[] = { 
      0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    IPAddress ip(192,168,1,177);
    
    // Initialize the Ethernet server library
    // with the IP address and port you want to use 
    // (port 80 is default for HTTP):
    EthernetServer server(80);
    
    void setup() {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
       while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
      }
    
    
      // start the Ethernet connection and the server:
      Ethernet.begin(mac, ip);
      server.begin();
      Serial.print("server is at ");
      Serial.println(Ethernet.localIP());
    }
    
    
    void loop() {
      // listen for incoming clients
      EthernetClient client = server.available();
      if (client) {
        Serial.println("new client");
        // an http request ends with a blank line
        boolean currentLineIsBlank = true;
        while (client.connected()) {
          if (client.available()) {
            char c = client.read();
            Serial.write(c);
            // if you've gotten to the end of the line (received a newline
            // character) and the line is blank, the http request has ended,
            // so you can send a reply
            if (c == '\n' && currentLineIsBlank) {
              // send a standard http response header
              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: text/html");
              client.println("Connection: close");
              client.println();
              client.println("<!DOCTYPE HTML>");
              client.println("<html>");
              // add a meta refresh tag, so the browser pulls again every 5 seconds:
              client.println("<meta http-equiv=\"refresh\" content=\"5\">");
              // output the value of each analog input pin
              for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
                int sensorReading = analogRead(analogChannel);
                client.print("analog input ");
                client.print(analogChannel);
                client.print(" is ");
                client.print(sensorReading);
                client.println("<br />");       
              }
              client.println("</html>");
              break;
            }
            if (c == '\n') {
              // you're starting a new line
              currentLineIsBlank = true;
            } 
            else if (c != '\r') {
              // you've gotten a character on the current line
              currentLineIsBlank = false;
            }
          }
        }
        // give the web browser time to receive the data
        delay(1);
        // close the connection:
        client.stop();
        Serial.println("client disonnected");
      }
    }
    
    
  • If a client is waiting to talk to the server then server.available() will return that client


  • The server data available will return false if there's no client available.


  • And then client.stop can be called from the server to cancel the communication with the clients, to end it.


  • Now in order to send data to the client and receive data from the client you just call the same functions we called before, client.print and client.write.


  • Client.print will send the data, client.write will send bytes and individual raw data. And you can do client.read to read data.


  • So you can send and receive data, communicate with the client using client.print or client.read


  • WiFi shield - allows you to connect to the internet, but it does it through a wireless interface, rather than a wired interface like an ethernet shield would do.


  • Sending data on one shield versus sending on another is a very similar operation


  • The IEEE standard used is 802.11


  • So, the library is similar to the Ethernet library, not exactly the same.


  • With the Arduino WiFi Shield, this library allows an Arduino board to connect to the internet. It can serve as either a server accepting incoming connections or a client making outgoing ones.


  • The library supports WEP and WPA2 Personal encryption, but not WPA2 Enterprise. Also note, if the SSID is not broadcast, the shield cannot connect.


  • Arduino communicates with the WiFi shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS.


  • On the Mega, the hardware SS pin, 53, is not used but it must be kept as an output or the SPI interface won't work. Digital pin 7 is used as a handshake pin between the Wifi shield and the Arduino, and should not be used.


  • The WiFi library is very similar to the Ethernet library, and many of the function calls are the same.


  •  
    #include <SPI.h>
    #include <WiFi.h>
    
    char ssid[] = "yourNetwork";     // the name of your network
    int status = WL_IDLE_STATUS;     // the Wifi radio's status
    
    void setup() {
      //Initialize serial and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
      }
    
      // check for the presence of the shield:
      if (WiFi.status() == WL_NO_SHIELD) {
        Serial.println("WiFi shield not present");
        // don't continue:
        while (true);
      }
    
      String fv = WiFi.firmwareVersion();
      if (fv != "1.1.0") {
        Serial.println("Please upgrade the firmware");
      }
    
      // attempt to connect to Wifi network:
      while (status != WL_CONNECTED) {
        Serial.print("Attempting to connect to open SSID: ");
        Serial.println(ssid);
        status = WiFi.begin(ssid);
    
        // 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();
    }
    
    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);
    
      // print your subnet mask:
      IPAddress subnet = WiFi.subnetMask();
      Serial.print("NetMask: ");
      Serial.println(subnet);
    
      // print your gateway address:
      IPAddress gateway = WiFi.gatewayIP();
      Serial.print("Gateway: ");
      Serial.println(gateway);
    }
    
    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);
    }