Using the Arduino Ethernet shield, Part 1 of 2

hi welcome back so far in this course our Arduino has been capable of communicating only with this host computer and the communication was very limited upload the sketch from the host computer to the Arduino and send text messages from it at the window to the computer via the USB serial interface we would like to do much more with communications and in this lecture I’ll show you how to get you are doing a gadget to speak to the Internet there are several ways to make this work in this lecture we will look at the ever net shield which represents the easiest and very capable still method there are also Wi-Fi which is technically very similar to Evan it but Wireless will live Wi-Fi aside for another time Evan it is the most common local area network technology around you probably have an ethernet device in your home connecting your computers printers storage servers etc together and to the internet via router the easiest way to add ethernet capability to your arduino is via the ethernet shield when the shield was added to the adrenals product lineup it opened up a world of possibilities your project could now be connected to the Internet and be part of the Internet of Things an Internet where devices are represented as virtual objects with which people or other objects can interact for example your internet connected fridge could report its temperature and alert you in case of malfunction or your garden watering system could lock the soil humidity levels to an online logging service like Nimbus I’m gonna have a another lecture where I’m going to show you how to do logging on embeds later on I personally find the Internet of Things a fascinating concept and people come up with all sorts of applications to show what is possible to do in this universe I have a link with examples of what people do on the Internet of Things in the lecture notes in the first demo I’ll show you how to connect your Ethernet shield and how to make your Arduino as part of your local area network specifically I’ll show you how to connect to the network automatically by acquiring an IP address via DHCP or how to acquire an IP address manually for each connection your Arduino will report the connection attributes in a console you need the Arduino Uno and another you know add an edge shield okay I’m going to show you now how to connect your having a shield onto your Arduino and then configure it so that it becomes part of your land sever a quick look at the shield itself first so what we have here is a shield with attaches directly onto the adrenal and onto its headers and of course we’ve got the rj45 jack here and then there isn’t it Ethernet controller once you plug it into headers of your other inner owner I think to notice is that communication between the evidence shield and the other you know is happening via four pins so you have pins digital pins 10 11 12 and 13 that are utilized by the evidence shield and it talks to the Adreno using the spi bus which is a serial bus about which I’m going to have a dedicated lecture later on it’s quite a few thing to talk about how the SPI bus but the thing to remember for now is that because these four pins 10 to 13 are engaged for Ethernet communications you can only use the rest but that’s I think that’s quite sufficient for most users at least and I think they remember is that of course this is are quite large device if you want to or go power the pyrotechnic stage and have a project deployed in some package you may want to use something smaller for your evident needs so what you can do in that case is to get one of these it’s essentially the same thing in a much smaller piece of babe it only has the absolutely minimum to get your other net connection and connectivity going it’s got the same shape or variation of the same chip same speed you can use it with the same

I dunno lobby that I’m going to show you in a minute so um that’s what I do if I want to deploy our project another nice thing about this having a chill feel that it’s got an SD card slot so you can you can plug in your mini SD cards in this lot and on these cards you can have files like image files for example that you want to make available to a web server that is running on the Arduino or you can use it for logging purposes so if you’ve got some sensors attached to your project and you want to every few seconds a few minutes take measurements store them onto the SD card and then every couple of hours perhaps you are doing I can use it’s at any connection to send the the data out to a looking server and there are speaking of logging service I have a lecture later on I think it’s going to be lecture 37 at this point in which I’m going to show you how to use an external web-based or cloud-based logging service for your project so back to this and once you have it plugged in there’s nothing else to do are you good to go plug in your Ethernet cable going to hear your router perhaps by a switch and plug the other you know into your computer now for the first sketch I’m going to use one of the built-in examples I’m going to go to file examples are the net and bring up the DHCP chat server I’ll go through the sketch really quickly before I uploaded to the air dinner because it’s actually a couple of things that I need to change here before I can deploy it so the first thing to notice is that we need to include the SBI library so the serial interface at the other net controller on the young the shield is using so I’m going to include that first and then I’m going to include the Evan it the library now the Ethernet library is really well documented so you go to the adding a website go through reference libraries scroll down and you’ll see the other net library here it’s kind of something quite substantial so it gives you a bit of background documentation for example it lets you know which pins are engaged so that you avoid using them for other devices we are playing with the other owner so we’re going to be on pins 11 12 and 13 and it also makes a mention that he number 10 is used as SS which means our slave select pin because the SPI is a bus serial interface that message can have multiple devices connected on the same pins communication panes which are 11 12 and 13 and in order for your sketch and they are doing it to be able to switch one connected slave device to the other it uses select pin so for the for the Ethernet shield that is 10 and also remember that the southerners shield also has a an SD card adapter on on the PCB so if you want to do any reading or writing from the SD card then you need to also use digital pin 4 as the SS pin they the slaves and leg pain for the SD card again I’ve got another lecture to show you how to use the SD card coming up another thing to note is just going through this libraries that you’ve got a number of classes in here of course you’ve got the ethernet class which we are going to be using the the beginner function of in the sketch that we’re going to play with in a minute there’s also our server class documented here it allows you to build a server which sits there listening for connections from clients and it’s also the client class here that we are going to use who want Iran to work with things such as telnet client that we’ll be doing in this example or in the next lecture I’m gonna show you how to build a web server which

were responding to a web client like area web browser so within modeling those types of clients using the client clasp that comes with this internet library ok that’s all for now now back to our example sketch the first thing to see is that there’s a bit of configuration that needs to be done first we want to make this device to become part of a local area network and to do that we need to tell the device or to the sketch what is going to be a hardware address so this is the the Ethernet protocol requires that any host has a hardware MAC address specified usually depending on the internet adapter this addresses are hardwired onto the controller itself but with the Arduino typically you’ll find a sticker either on the PCB device or the books the packaging in which the the other nature comes in and you’ll have a number like this so if you do have such a sticker or perhaps a leaflet inside the packaging just copy whatever values you have on that sticker in this array of bytes I fortunately lost mine a long time ago so because I only have the one I do know at this very moment running in my network it’s highly unlikely there’s gonna be any other device with the same MAC address so I’m pretty confident that this is got to work I’m going to leave it so I could just make a little change if I want to be super safe about this and just make a little change here and just differentiate the MAC address that I’m going to assign to I advise the second thing is the IP address so we will want to connect to the internet all we want to be able to access our Ethernet and device via some kind of Internet service like in our case is going to be telling it to do that again we need to have an IP address is unique in our network we in this example we will be using a service called DHCP which automatically assigns an IP address and provides the appropriate settings to a device that is asking for for such information from the DHCP server but in case a DHCP server is not available in your network what you do is that you just provide our fallback IP address so in my case because I know that my network is at this subnet 191 6 8 1 1 1 I’m just going to change the the third number which was 1 in the default sketch to 1 1 1 to match my own land and the the sutnar that I’m that I have configured my own land in your case it could be just one but you’re gonna have to check that you know an easier way to check that if you are on the Mac is to have a look at your preferences your system preferences so you bring up system preferences and then you check network and you can see that in my case the IP address of my computer it’s this this first part there’s three digits make up the land address so the subnet is 191 6 8 1 1 1 and the last digit is the host address obviously IP address so the address of my computer another thing to notice is this number one 11.1 my case is the router and it’s also the gateway address which again I’m going to use in my ethnic configuration for my Arduino so on Macs and sorry on Windows and Linux there’s a slightly different process for getting those that information about your subnet and your gateway but I’m going to have nodes in the PDF file that comes with this lecture to help you out with this if you are having trouble just ask me as well so I’m going to use dot 177 comma 177 as my fallback IP address for this Arduino

I need to also fix the Gateway so the Gateway is our special computer or device that the Idina will be using if it needs to talk to a service or a web server on the Internet so I’m going to switch that to one one one comma one since I know that that is the gateway plus router in my network and finally I am on a Class C Network not Class B SVT for example here so I’m going to change this 0 in the third digit into a 255 if you’re not sure what a Class B Class A Class C Network is don’t worry too much about it if you are running these examples in your home network then 99.99 percent for cases you will need to have a subnet mask to match the one in this example 255 comma 255 call my t5 5 comma 0 let’s call the Class C address which is very common for home networks okay that’s all the configuration that that I need to do so a couple of other things to notice here before I execute this example here we instantiate the Edit server so we’re setting up a server that will be listening to port 23 support 23 is a default port for telnet services if you’re running a web server you change that to port 80 or if you’re running a SSH server believe that would have been poor 22 or something like that from from memory now so we set that up here now this server is going to be sitting the background listening for connections I’ve got a boolean got a message true or false the two values that they can take that we’re going to be using to let it part of the script know whether there is a new message on your character that has been transmitted to our server from a connected client you’ll see how that works in a minute then setup is pretty typical we start up a sill interface so that you can get some would into the IDE console then what happens what the the next important thing that happens is that we are trying to now start the evidence service on the Arduino and we use the begin function and we pass the MAC address that was specified earlier now this is going to go and try and contact a DHCP server on our network and the DHCP server is going to come back saying here is some credentials I see here some information that you can use to connect them there’s the address and subnet mask and a gateway these are basics that you need but if it doesn’t I think if a DHCP server cannot be connected to or the DHCP server does not return any useful information then it’s going to return a zero value so if that happens we’re just going to go back to our fallback values and started in a service anyway with whatever information we’ll provide it earlier in the configuration segment so we’re going to use a default IP address gateway and subnet and that’s that way at least we we can get started it’s unlikely that in your network you can have this problem because the these days most routers but default run at DHCP server so one way or another the Evan service is going to begin and it’s going to print out the IP address that it was assigned to it using this block so the local IP function returns an array of bytes and that’s how the IP address and the full bias of the IP address are encoded in that value that local IP function returns so we’re going to go through these four bytes in this array and print them out and this is something we haven’t seen before it which is a normal print function that we have seen earlier but this time we are taking a bite as input we are converting it to a decimal and then we are printing out the values so that we can see a decimal instead of by it coming out to the monitor and finally we start this

server and we get into the loop function the loop function would constantly check to see if a if there is a client that is trying to connect I’m gonna show you how a client will connect to this script to this sketch in a minute but if there is a client that is trying to connect then we are going to capture that into the client object which is of adding a client class so if this is the first time that a client connects to us then got message has already been defined as false so if there’s a first time that a client is connected then we’ll let the user know by printing out a message we also send the same message to the clients telnet console to greet them and we change the boolean got a message to true now tell it is a serial type of communication so it will be sending us messages of the words one character at a time so we’ll be grabbing one character at a time and putting it into a local car variable we’re just going to bounce that out to the clients a telnet console and to the serial interface the idea console so basically whatever they are sending to the server they will be getting back to the console okay so let’s upload the sketch and see it in action okay bring up the monitor so he can see that although we specified and iPad rested they won 11.1 77 the local DHCP server actually assigned or leased in IP address that is one 11.6 four so I’m gonna put it aside and bring up the console let’s try to connect it so telnet 192.168.1 one 1.64 hello client I have connected so anything a type now comes back twice your type – let it be once this is a one that I typed and the second occurrence of the character is a woman that was bounced back by the server right one comes back twice it only appears once in the console on the IDE and so on its own does so that just proves that I have connection between my computer to the at the winner via the network this time so the connection is not direct in the next day mod number two we’re going to take this sketch further so we’re going to make a modification that allows us to send commands to the telnet server running on the Arduino and those commands will resort to things like eye LEDs lighting on and off or taking measurements from sensor on the Arduino and returning that information back to us just a bit more a realistic example of the use of connection like this you