doc - SEAS - University of Pennsylvania

advertisement
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 preset 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
5. 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
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.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
#include <SPI.h>
#include <Ethernet.h>
/**********************************************************
* CETS has assigned static IPs to a bunch of mac addresses
* If you DHCP using those mac addresses it will get the
* static IP assigned to it. Otherwise if you assign your
* own mac add and IP address to a device then it will have to
* go through the security page of Airpenn net.
* These mac and static address are tested to work within the
* engineering building.
***********************************************************/
// Fill in the analog pin connected to the temperature sensor
int tempPin = A0;
// Enter a MAC address and IP address for your controller below.
byte mac[] = {
0x**, 0x**, 0x**, 0x**, 0x**, 0x**};
EthernetServer server(80);
// (port 80 is default for HTTP):
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);// Set up serial monitor with PC
while(!Serial){
; //wait for serial port to connect. Needed for Leonardo
}
// start the Ethernet connection and the server:
Ethernet.begin(mac);// Set up the Ethernet Shield
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
2
34.
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 closed after
completion of the response
54.
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
55.
56.
57.
58.
client.println();
client.println("<!DOCTYPE HTML>"); //html tag to define the document type
client.println("<html>"); //html tag indicating start of html document
float sensorReading = analogRead(tempPin); //read temperatuyre from the senso
r
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
sensorReading=(((sensorReading*5000)/1024)-500)/10;
int degreeF = ((sensorReading*9)/5)+32; //convert the value of temperature fr
om celcius to farenhite
client.print("["); //characters that help in parsing the document
client.print(degreeF);
client.print("]"); //characters that help in parsing the document
client.println("<br />"); //html tag for next line
client.println("</html>"); //html tag indicating end of document
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 disconnected");
}
}
5. Make sure you can see the temperature reading on the Serial monitor.
Part II – Building the Client
3
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.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
/**********************************************************
* CETS has assigned static IPs to a bunch of mac addresses
* If you DHCP using those mac addresses it will get the
* static IP assigned to it. Otherwise if you assign your
* own mac add and IP address to a device then it will have to
* go through the security page of Airpenn net.
* These mac and static address are tested to work within the
* engineering building.
***********************************************************/
byte mac[] = {
0x2A, 0x00, 0x**, 0x**, 0x**, 0x**};
byte server[] = {
192, 168, **, **}; //this is your partners Ip address
//To find out IP address got to examples -> Ethernet -> DhcpAddressPrinter and upload
the code with your MAC address
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/ HTTP/1.0";
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
EthernetClient client;
// Pin for the buzzer.
const int buzzerPin = 9;
// Sets the threshold temperature in Fahrenheit for the buzzer
int alarmThreshold = 85;
void setup() {
Serial.begin(9600);
while(!Serial) {
;// wait for serial port to connect. Needed for Leonardo only
4
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
}
//start the Ethernet connection:
Ethernet.begin(mac);
}
void loop() {
String pageValue = connectAndRead(); //connect to the server and read the output
// Serial.println(pageValue);
int temperature = pageValue.toInt();
Serial.println(temperature);
// enter your logic here for the buzzer
delay(5000); //wait 5 seconds before connecting again
}
String connectAndRead() {
//connect to the server
// Serial.println("Connecting...");
if(client.connect(server, 80)) {
//
Serial.println("Connected");
client.print("GET ");
client.println(location);
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}
else {
Serial.println("Connection failed");
}
}
String readPage() {
stringPos = 0;
memset ( &inString, 0, 32); //clear inString memory
while(true) {
if(client.available()) {
char c = client.read();
//
Serial.println(c);
if (c == '[') {
startRead = true;
}
else if(startRead) {
if (c != ']') {
inString[stringPos] = c;
stringPos ++;
}
else {
startRead = false;
client.stop();
client.flush();
//
Serial.println("disconnecting");
return inString;
}
}
}
}
}
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)
5
Download