Arduino接口-week4-Lesson1

来源:互联网 发布:mac 打开icloud 编辑:程序博客网 时间:2024/04/19 19:10

This series of articles are the study notes of "Interfacing with the Arduino", by Prof. Harris, Department of Computer Science, University of California, Irvine. This article is the notes of week 4, lessen 1.

Week 4


1. Lesson 1


1.1 Lecture 1.1 - Arduino Shields


1.1.1 Arduino Shields

(1) Shield

A printed circuit board (PCB) that adds functionality to your Arduino. 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.
So, 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 on there. And then, 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.

(2) Hardware

A circuit is pre-wired and sold on a printed circuit board. 
So a shield makes that easy for you, a shield is basically two things really. Its hardware, which is that little board, printed circuit board, 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 is open source, you can buy the design and you can manufacture it yourself. But generally, you're going to buy it pre-manufactured.

(3) Software

A software library is provided to interact with the hardware.
so there's the hardware side of the shield, but there's also the software side, the library. The library that's associated with the Shield. So to make it easy, we talked about libraries. To make it easy to use these Shields, that you're provided with some kind of a library with a set of functions that do all of the interesting things that the Shield can do.


1.1.2 Benefits of Shields

(1) No wiring needed

-    Circuitis pre-wired

Why do you use a shield? No wiring is needed. There's a picture of a shield. That's an Ethernet Shield right there, which actually we'll use that in a future lecture. The circuit on that shield is pre-wired. So, if you look at that shield, it's got several components on there, chips and things like this, all wired together.
That wiring you, as a user, you did not have to do. That wiring is given to you. It's all pre-wired, so you don't have to worry about the details of the wiring which can be hard.
  • Connections to Arduino are fixed by stacking
Also, 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.
So, 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. So, that's a great thing. You don't have to design the hardware side of the circuit at all.
  • Simple to use 
  • Library takes care of complicated details
Simple to use because the library functions take care of the complicated details of how to use the device. Like in this one this is an Ethernet controller. And there's a chip in there that's says, wiznet Ethernet controller chip if I remember the name. And that's kind of a complicated chip to use.
And that's kind of a complicated chip to use. I've used that chip separately, and there's a data sheet, a thick data sheet, and you have to understand it. With these library functions, you don't have to understand any of that. You just call them and you've got an Ethernet connection.


1.1.3 Connected Pins

  • Pins on the bottom of the shield connect to the pins of the Arduino
Pins, so the picture is actually a shield without the pins attached. So the pins on the bottom,  the Arduino got a set of holes, rows of holes on the top or bottom, or left to right depending on your orientation that with pins. So, these Shield have pins underneath them, that go right in so they line up with the holes on top of the Arduino. You can take the shield, and stack it right in there, and the wiring is automatically done.
  • Most shields only use a small subset of the pins
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 I'm using two shields, I'm stacking two. 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, ditto pin one for an input, the other shield is using digital pin one for an output, then you got a problem. 
  • Need to know which pins are used when using multiple shields
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 I2C. Which we learned about using the wire library right, we can use the wire library to access that. That sharing is fine.
If you've got three shields and them all communicate through the Trough 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.
  • Shield headers may need to be soldered
shield headers may not be soldered in. So sometimes when you a shield, it's not fully manufactured. Like in the one we see in this picture right here, you got the shield, but it doesn't have its header pins soldered in. So, you'll see on one of the sides it has holes. It does not have actual pins underneath. Just next to the shield are the headers, what are called the headers. Those are the pins that you're going to solder in.



1.2 Lecture 1.2 - Ethernet Shield

So we're finally getting into that Internet part, of Internet of things. How do we connect to the Internet? So, an Ethernetshield is generally giving you a wired connection to the Internet. Which may or may not be what you want, but right now, we'll start with that. So, it allows Internet connections through a wired interface. So you can look at it you can see the picture there.

 

1.2.1 Ethernet Shields

(1) Allow internet connections through a wired interface

So, it allows Internet connections through a wired interface. So you can look at it you can see the picture there. This is a typical Ethernet shield.

(2) Shield includes an Ethernet jack (RJ45) for a network cable

And the shield includes that Ethernet jack, a port, an RJ45 port. That's the plug. You can see the big plug there where you plug an Ethernet cable into. So a network cable you can plug into that RJ45 port. 

(3) several shields available

There are several shields available. So this is one Ethernet shield, but there are a lot of different Ethernet shields available. 

(4) Common library is used

But they use a common library. They all use the Ethernet library that's provided by Arduino.


1.2.2 Internet addresses

So, a few things we've got to get straight before we can really talk about the A Ethernet shield because there's some set-up activities you have to do. So, we just got to go over what Internet addresses are. Because some of these parameters have to be set-up when you start using the shield. So, or they don't have to be, but they can be set-up. So, we need to review what these are.

(1) Mac address: unique address “hardwired” into each network adapter. 6 bytes long.

A MAC address is a unique address which is "hardwired" into each network adapter. It's six bytes long. It's a six byte long number. Now, I put hardwired in quotes because, theoretically, it's supposed to be hardwired, but in reality with these Ethernet shields you can change that. But if you were to buy a component off the shelf, buy any component.

Let's take a laptop. Say I buy a laptop and it has wifi and wired. It 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. You can't change those.But with our Ethernet adapters, our 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 among 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 by, you know, every company that it makes Ethernet adapters and network adapters. They get their own prefix Mac address. Everyone that comes from a particular company has the same first several bytes, and then the rest are determined by the company, and they insure that their different. They're distinct. But the different companies have different sets of most significant bytes so they can never interfere with each other.
So there's a MAC address and we're going to have to set the MAC address on our Ethernet shield.

(2) IP address: address used for addressing by internet protocols. 4 bytes long

There's also an IP address. Now 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.

Now the IP address can change, however, unlike the MAC address. The MAC address is not meant to change. It's supposed to be stuck with a hardware device. For it's life. The IP address can change.

Now actually we can see an IPV 4 address.We're only going to talk about IPV 4. IPV 4 is the earlier version of IP addresses, which is mostly currently still in use. It'sbasically four bytes long, each byte remember is Eight bits.It represents numbers 0 to 255. So you can seethis representation up here, this dotted decimal notation, which is very commonto see IP addresses in. They're four numbers separated by dots. Each number isa number from 0 to 255. So that's what a typical IP address looks like.

(3) Port: number identifying the application protocolbeing used 2 bytes long

Now, there's also a port. TCP protocol defines a set of ports. So, say you've got a particular IP address, like my laptop, okay, and it has its IP address. Right now, it is running and it hassome IP address. In addition to its IP address, it has several ports that it can use for communication.

So the portnumber, generally, is mapped to a particular application. So right now,my laptop is over there an it's got a web browser running on it. That webbrowser communicates on port 80 because that is the standard. If you have a web browser, web server they communicate on port 80. That's just the standard. It'slistening to port 80. The web browser when it wants send a message to web server, it sends it on port 80. Communicates with port 80 on the server machine. And the server is listening to port 80. Now if something comes in on another port number the server will not hear it. Will not pay attention to it.It's listening to its one port.

So, when you want to send something through the internet to a machine you are going to specify the IP address. But,you are also going to specify the port number. To indicate basically which application protocol you'reusing. And that's two bits longwhich means it's 16K. There's 16K possible ports. A lotof these are pre-known. For instance, it is known port 80 is for internet, for http, communication.


1.2.3 Domian Name Service

(1) Service that maps domain names to IP addresses

Now DNS or Domain Name Service. What this is, it's a service that maps the domain name to an IP address. 

(2) Names are much easier to memorize than IP addresses

Domain name is basically English or not English, but a mnemonic name that humans can understand and memorize.

(3) www.uci.edu=128.195.188.232

Now, so for instance, www.uci.edu. That, I can remember pretty easily. That is the domain name,
but the actual IP address that maps to is 128.195.188.232. So that translation has to happen every time. So 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 Domain Name Service is for.

(4) DNS server store the mapping

Basically it's a look-up process where you type in, ucl.edu and your machine, local machine goes to another machine, a Domain Name Server, a different machine, and asks it, hey do you know the mapping? Do you know for this name, do you know what it's IP address is. If it knows it sends it back, if it doesn't it forwards it to another domain name server and so on.

(5) Internet servers/ clients may need to be configured with a DNS server

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 controllers or, in our case Ethernet shield, you configure it with a DNS server with the IP address of the DNS server, so when it's looking up a name it says, oh I'm gonna go to whatever's in my default list. Now if you don't have a default list, your network provider has a default also, but you can specify your own if you want too.


1.3 Lecture 1.3 - Ethernet Library


1.3.1 Ethernet #include

So now, we're gonna start talking about the Ethernet library. The library that's used to control an
Ethernet controller, an Ethernet shield. So we'll start with #include.

(1) There are many .h files to include

- Ethernet.h
- EthernetClinen.h
- EthernetServer.h
So at the top, like with all these libraries, you need to set a #include to include a bunch of header files, .h files. There are a lot. So for instance, Ethernet.h, EthernetClient.h, EthernetServer.h and there are several more.

(2) Sketch -> Import -> Library -> Ethernet

The easy way to do this in your sketch is just to go to the ITE, go to Sketch > import library > Ethernet and it'll just stick in all the #includes that you need for the Ethernet library.


1.3.2 Initializing the Ethernet Interface

There’s several functions provided to initialize the Ethernet interface to initialize the shield. So these are the first things that you're going to call when you want to use the shield. 

(1) Invoke the Ethernet.begin() function

So Ethernet.begin, that's really the function, which is common name for the first function in any library, but Ethernet.being, you call that function.

(2) 5 possible arguments, only the first is required

Now 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. Well, the first one we have to give the rest we probably won't, but let's go through all of them.

(3) The 1st argument: Mac Address (required)

So, first argument that is required is the MAC address. As the MAC address of your Ethernet shield, which you can make up, by the way. And most fields you can just create which ever one you want to create and you can use that. Or if it has a sticker on there that indicates its MAC Address, you can us that too.

(4) The 2nd argument: IP address

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

(5) The 3rd argument: DNS

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

(6) The 4th argument: Gateway

A gateway is the address of a router, which knows how to route packets to other networks. So if you're going outside of our local network, which we won't in this class. But if you are, then you might want to specify a default gateway. A default router to go to when it wants to send packets to an outside network, it can go ask this gateway router for the path to say, how do I get this there? Now if you don't give a gateway, it will find a gateway. But sometimes a particular service will have it's own gateway to speed thing sup, but you don't have to do that. Also the subnet mask. 

(7) The 5th argument:Subnet mask

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

So this is sort of a flexible idea, but a subnet you can think of as your local network. And generally, what happens with the IP addresses. The prefix, so it's a 4-byte number, 4 numbers separated bydecimals. And the higher bytes, the most significant bytes are often held constant for all the machines in a local subnet.So in a local subnet maybe it's 128, 198, 200 are the first 3 numbers and then the last number is a 1 from my machine, a 2 for the next machine, a 3 for the next machine and so on, but they share. In that case, the local subnet shares the high 3 bytes. So thesubnet mask tells you which bits, specifically are shared by the local subnet.You don't have to specify this. If you don't, it'll default to 255.255.255.0.So what that means is remember 255 in binary is all 1s. So all 1s, all 1s, all1s and then all 0s. So that's saying that the local subnet is just the last byte.

So, all the nodes in a local subnet share the same high 3 bytes. That's what it says and that'll be the default if you don't set it, but you don't have to.

1.3.3 Dynamic Host Connection Protocol

Dynamic Host Connection Protocol. So this is pretty important or DHCP.

(1) Every node on the internet needs an IP address

So in order to communicate on the internet, you need an IP address. Every node on the internet needs an IP address. 

(2) DHCP allows the IP address to be assigned dynamically

Now note that in our Ethernet type again, we did not have to specify and IP address. So how is it gonna communicate on the Internet without an IP address? Well, it has to get one. It can get one dynamically. So 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.

(2) DHCP allows the IP address to be assigned dynamically

Now note that in our Ethernet type again, we did not have to specify and IP address. So how is it gonna communicate on theInternet without an IP address? Well, it has to get one. It can get onedynamically. So 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.

So right here, right now my laptop is sitting over there and I open it up. And when I opened it up,it connected to the local network at school and it uses DHCP and the local network is setup for DHCP, that's turned on. And my machine basically in order to communicate, it's first thing it does is it asks through using this DHCP protocol, it asks for an IP address. So, it gets it's IP address dynamically from the network and then the network finds some available IP address and tells my machine. So here's your IP address and that's what we're assuming is being used that's very common.

So let's just assume that's whats being used and we don't have to specify an IP address, we can just assume DHCP is being used to give us an IP address.

(3) DHCP is invoked if Ethernet.begin() has no IP address argument

Now DHCP is invoked if you call Ethernet.begin and it has no IP address argument. So if you just give it, if you call with a MAC address argument and nothing else, no IP Address doesn't know anything, then it automatically invokes the DHCP. Making a request, you don't have to deal with these details, but your machine will. Your Ethernet shield will make a request to the local network to ask for an IP address. Well, after you call up again.

(4) Most routers are configured for DHCP

Most routers are configured for DHCP. Now another way to do this, instead of allowing DHCP, which gives dynamic IP addresses is that you can also use static IP addresses.

(5) Static IP addressesare typical for servers

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. It's typical 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, my laptop 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, it needs to sit in the same place and be in a table somewhere. So when I request, it's address I can get it. So servers often have static addresses, but dynamic addresses are very common for non servers.

0 0
原创粉丝点击