7
University of Pennsylvania Department of Electrical and Systems Engineering ESE 111 Networking using Arduino Introduction : In this lab, you will learn how to use Arduino’s Ethernet shield to communicate between two Arduinos over the internet using TCP. TCP (Transmission Control Protocol) and UDP (User datagram Protocol) are two widely used protocols to send bits of data – known as packets over the internet. Both TCP and UDP are built on top of IP (Internet Protocol) and are used for different applications . Every device on the network has a unique physical address called its MAC address. Every device on the network communicating over IP has a logical address called the IP address. For example the server hosting Google’s website also has a IP (http://74.125.224.72/ ). If your computer is connected to the internet, you can find out its IP by searching for your IP address on google. We will set up one Arduino as a server which can serve any client be it a web browser or another Arduino client. Communication over internet using Server and Client model Goal: - Alert the client by turning on a buzzer if the temperature on the server exceeds a pre-set level. For this lab, you will need to pair up with another group so that you will have 2 network shields to use between the groups. Parts Required 1. 2 Arduino Boards 2. 2 Network Shields (pair up with another group) 3. 2 Breadboard Shields 4. 1 LM34 Temperature Sensor 1

 · Web viewOpen a new instance of Arduino software, and connect a new Arduino with Ethernet shield on it to a different serial port than the server Arduino. Build a buzzer circuit

  • Upload
    lamdat

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Page 1:  · Web viewOpen a new instance of Arduino software, and connect a new Arduino with Ethernet shield on it to a different serial port than the server Arduino. Build a buzzer circuit

University of PennsylvaniaDepartment of Electrical and Systems Engineering

ESE 111

Networking using Arduino

Introduction:

In this lab, you will learn how to use Arduino’s Ethernet shield to communicate between two Arduinos over the internet using TCP. TCP (Transmission Control Protocol) and UDP (User datagram Protocol) are two widely used protocols to send bits of data – known as packets over the internet. Both TCP and UDP are built on top of IP (Internet Protocol) and are used for different applications. Every device on the network has a unique physical address called its MAC address. Every device on the network communicating over IP has a logical address called the IP address. For example the server hosting Google’s website also has a IP (http://74.125.224.72/). If your computer is connected to the internet, you can find out its IP by searching for your IP address on google. We will set up one Arduino as a server which can serve any client be it a web browser or another Arduino client.

Communication over internet using Server and Client model

Goal:

- Alert the client by turning on a buzzer if the temperature on the server exceeds a pre-set level.

For this lab, you will need to pair up with another group so that you will have 2 network shields to use between the groups.

Parts Required

1. 2 Arduino Boards2. 2 Network Shields (pair up with another group)3. 2 Breadboard Shields4. 1 LM34 Temperature Sensor5. 1 Buzzer

Part I – Building the Server

1. Insert the network shield on the Arduino and insert a bread board shield on top of that.

2. Connect the temperature sensor to 5V and Ground to the respective pins and measure the voltage on the Multimeter. Make sure the temperature changes causes the change in voltage output.

1

Page 2:  · Web viewOpen a new instance of Arduino software, and connect a new Arduino with Ethernet shield on it to a different serial port than the server Arduino. Build a buzzer circuit

3. Connect the temperature sensor – LM34 (a nice tutorial on how to use lm34) as shown in the figure to Analog input 0 of the Arduino. Make sure you don’t connect the sensor backward otherwise you WILL damage the sensor.

Figure 1 – Server setup with temperature sensor.

4. Compile and upload the code below to set up a server. Make sure you fill in you Mac and IP address correctly.

1. #include <SPI.h>  2. #include <Ethernet.h>  3. /********************************************************** 4.  * CETS has assigned static IPs to a bunch of mac addresses 5.  * If you DHCP using those mac addresses it will get the  6.  * static IP assigned to it. Otherwise if you assign your 7.  * own mac add and IP address to a device then it will have to 8.  * go through the security page of Airpenn net. 9.  * These mac and static address are tested to work within the  10.  * engineering building. 11.  ***********************************************************/  12.   13. // Fill in the analog pin connected to the temperature sensor  14. int tempPin = A0;  15.   16. // Enter a MAC address and IP address for your controller below.  17. byte mac[] = {  18.   0x**, 0x**, 0x**, 0x**, 0x**, 0x**};  19.   20. EthernetServer server(80);  // (port 80 is default for HTTP):  21.   22. void setup() {  23.   // Open serial communications and wait for port to open:  24.   Serial.begin(9600);// Set up serial monitor with PC  25.   while(!Serial){  26.     ; //wait for serial port to connect. Needed for Leonardo  27.   }  28.   // start the Ethernet connection and the server:  29.   Ethernet.begin(mac);// Set up the Ethernet Shield  30.   server.begin();  31.   Serial.print("server is at ");  32.   Serial.println(Ethernet.localIP());  33. }  34.   

2

Page 3:  · Web viewOpen a new instance of Arduino software, and connect a new Arduino with Ethernet shield on it to a different serial port than the server Arduino. Build a buzzer circuit

35. void loop() {  36.   // listen for incoming clients  37.   EthernetClient client = server.available();  38.   if (client) {  39.     Serial.println("new client");  40.     // an http request ends with a blank line  41.     boolean currentLineIsBlank = true;  42.     while (client.connected()) {  43.       if (client.available()) {  44.         char c = client.read();  45.         Serial.write(c);  46.         // if you've gotten to the end of the line (received a newline  47.         // character) and the line is blank, the http request has ended,  48.         // so you can send a reply  49.         if (c == '\n' && currentLineIsBlank) {  50.           // send a standard http response header  51.           client.println("HTTP/1.1 200 OK");  52.           client.println("Content-Type: text/html");  53.           client.println("Connection: close");  // the connection will be close

d after completion of the response  54.           client.println("Refresh: 5");  // refresh the page automatically ever

y 5 sec  55.           client.println();  56.           client.println("<!DOCTYPE HTML>");  //html tag to define the document 

type  57.           client.println("<html>"); //html tag indicating start of html documen

t  58.           float sensorReading = analogRead(tempPin); //read temperatuyre from t

he sensor  59.           sensorReading=(((sensorReading*5000)/1024)-500)/10;  60.           int degreeF = ((sensorReading*9)/5)+32; //convert the value of temper

ature from celcius to farenhite  61.           client.print("["); //characters that help in parsing the document  62.           client.print(degreeF);   63.           client.print("]"); //characters that help in parsing the document  64.           client.println("<br />"); //html tag for next line        65.           client.println("</html>"); //html tag indicating end of document  66.           break;  67.         }  68.         if (c == '\n') {  69.           // you're starting a new line  70.           currentLineIsBlank = true;  71.         }  72.         else if (c != '\r') {  73.           // you've gotten a character on the current line  74.           currentLineIsBlank = false;  75.         }  76.       }  77.     }  78.     // give the web browser time to receive the data  79.     delay(1);  80.     // close the connection:  81.     client.stop();  82.     Serial.println("client disconnected");  83.   }  84. }  

5. Make sure you can see the temperature reading on the Serial monitor.

3

Page 4:  · Web viewOpen a new instance of Arduino software, and connect a new Arduino with Ethernet shield on it to a different serial port than the server Arduino. Build a buzzer circuit

Part II – Building the Client

1. Open a new instance of Arduino software, and connect a new Arduino with Ethernet shield on it to a different serial port than the server Arduino.

2. Build a buzzer circuit on a breadboard shield as shown in the figure 2.

Figure 2 – Client setup with a buzzer

3. Compile and upload the following code to set up the client. Read through the code and fill in any starred sections

1. #include <SPI.h>  2. #include <Ethernet.h>  3. #include <LiquidCrystal.h>  4. // Enter a MAC address for your controller below.  5. // Newer Ethernet shields have a MAC address printed on a sticker on the shield 

 6. /********************************************************** 7.  * CETS has assigned static IPs to a bunch of mac addresses 8.  * If you DHCP using those mac addresses it will get the  9.  * static IP assigned to it. Otherwise if you assign your 10.  * own mac add and IP address to a device then it will have to 11.  * go through the security page of Airpenn net. 12.  * These mac and static address are tested to work within the  13.  * engineering building. 14.  ***********************************************************/  15. byte mac[] = {  16.   0x2A, 0x00, 0x**, 0x**, 0x**, 0x**};  17. byte server[] = {  18.   192, 168, **, **}; //this is your partners Ip address19. //To find out IP address got to examples -> Ethernet -> DhcpAddressPrinter and

upload the code with your MAC address 20. //The location to go to on the server  21. //make sure to keep HTTP/1.0 at the end, this is telling it what type of file i

t is  22. String location = "/ HTTP/1.0";  23. char inString[32]; // string for incoming serial data  24. int stringPos = 0; // string index counter  25. boolean startRead = false; // is reading?  26. EthernetClient client;  27.   28. // Pin for the buzzer.  29. const int buzzerPin = 9;  30. // Sets the threshold temperature in Fahrenheit for the buzzer  31. int alarmThreshold = 85;  32. void setup() {  

4

Page 5:  · Web viewOpen a new instance of Arduino software, and connect a new Arduino with Ethernet shield on it to a different serial port than the server Arduino. Build a buzzer circuit

33.   Serial.begin(9600);  34.   while(!Serial) {  35.     ;// wait for serial port to connect. Needed for Leonardo only  36.   }  37.   //start the Ethernet connection:  38.   Ethernet.begin(mac);  39. }  40. void loop() {  41.   String pageValue = connectAndRead(); //connect to the server and read the out

put  42.   //  Serial.println(pageValue);  43.   int temperature = pageValue.toInt();  44.   Serial.println(temperature);  45.   // enter your logic here for the buzzer   46.   delay(5000); //wait 5 seconds before connecting again  47. }  48. String connectAndRead() {  49.   //connect to the server  50.   //  Serial.println("Connecting...");  51.   if(client.connect(server, 80)) {  52.     //    Serial.println("Connected");  53.     client.print("GET ");  54.     client.println(location);  55.     client.println();  56.     //Connected - Read the page  57.     return readPage(); //go and read the output  58.   }  59.   else {  60.     Serial.println("Connection failed");  61.   }  62. }  63. String readPage() {  64.   stringPos = 0;  65.   memset ( &inString, 0, 32); //clear inString memory  66.   67.   while(true) {  68.     if(client.available()) {  69.       char c = client.read();  70.       //      Serial.println(c);  71.       if (c == '[') {  72.         startRead = true;  73.       }  74.       else if(startRead) {  75.         if (c != ']') {  76.           inString[stringPos] = c;  77.           stringPos ++;      78.         }  79.         else {  80.           startRead = false;  81.           client.stop();  82.           client.flush();  83.           //          Serial.println("disconnecting");  84.           return inString;  85.         }  86.       }  87.     }  88.   }  89. } 

5

Page 6:  · Web viewOpen a new instance of Arduino software, and connect a new Arduino with Ethernet shield on it to a different serial port than the server Arduino. Build a buzzer circuit

4. Make sure you can see the temperature reading on the serial port monitor. Set and tweak the threshold to make the buzzer go off when a certain temperature is measured. (hint: the temperature will start increasing if you hold the pinch the sensor – body heat)

6