ese205/labs/Networking-Arduino.docx · Web viewNetworking using Arduino Introduction: In this lab,...

14
University of Pennsylvania Department of Electrical and Systems Engineering Networking using Arduino Introduction : In this lab, you will learn how to use Arduino’s network shield to communicate between two Arduinos over the internet using UDP (User Datagram protocol). UDP is a simple, lightweight standard for sending messages referred to as datagrams over the internet. You can also use the network shield to host a simple website on Arduino, write to a website, read texts/feeds from websites using TCP (transfer control protocol, typically used to deliver web content to your computer) but these techniques are out of scope of the lab. You are welcome to try if you are interested and the TAs will be glad to help you. Using UDP allows the Arduino to send simple messages like the status of a sensor or some analog values through the school’s router network and back to another receiver unit. This receiver does not necessarily have to be another Arduino. UDP can be used to send messages via wired Ethernet or wireless Wi-Fi to smart phones, tablets, computers, and other internet-enabled devices. Peer to Peer Communication over internet using UDP protocol

Transcript of ese205/labs/Networking-Arduino.docx · Web viewNetworking using Arduino Introduction: In this lab,...

University of Pennsylvania Department of Electrical and Systems Engineering

Networking using Arduino

Introduction:

In this lab, you will learn how to use Arduino’s network shield to communicate between two Arduinos over the internet using UDP (User Datagram protocol). UDP is a simple, lightweight standard for sending messages referred to as datagrams over the internet. You can also use the network shield to host a simple website on Arduino, write to a website, read texts/feeds from websites using TCP (transfer control protocol, typically used to deliver web content to your computer) but these techniques are out of scope of the lab. You are welcome to try if you are interested and the TAs will be glad to help you. Using UDP allows the Arduino to send simple messages like the status of a sensor or some analog values through the school’s router network and back to another receiver unit. This receiver does not necessarily have to be another Arduino. UDP can be used to send messages via wired Ethernet or wireless Wi-Fi to smart phones, tablets, computers, and other internet-enabled devices.

Peer to Peer Communication over internet using UDP protocol

Figure 1 - Transmitter and Receiver Modules

Prelab:

1. Internet routers form the backbone of the data transmission over the internet. What does an internet router do?

2. Data messages sent over the internet can travel through dozens of points before reaching the destination. Even something simple like web browsing is actually immensely complex. To see this in action, open a command-prompt (Windows) or Terminal (Mac, Linux) and perform a trace route by typing “tracert www.???.com” on Windows or “traceroute www.???.com” on Mac or Linux. Replace the ??? with a website domain name of choice and do not type the “ ”. Press enter to see exactly how a message will travel from your computer to reach the web site you entered, including which routers it passes through. Some websites to try are www.upenn.edu, www.google.com, www.arduino.cc. Let’s have a competition. Try at least three websites that you know, and write down the number of hops it takes to reach the destination web site. The number of hops is the number of routers (lines in the results list) your message travels through to get to the final destination. The student who can find a website that takes the most number of hops to reach wins the claw.

3. For this lab, you will be using the LM34 temperature sensor. Look up the temperature sensor online. How does the analog output of the sensor correspond to the temperature in degrees Fahrenheit? You will need to know this conversion for the lab.

Goal:

- To sound a buzzer and display temperature on a remote Arduino controlled by a temperature sensor connected on a home Arduino via UDP using network shields.

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. One team may build the temperature sender Arduino unit in Part 1, while the other team may build the receiver in Part 2. You will combine your units to show the final network communication in action.

Parts Required

1. 2 Arduino Boards2. 2 USB Cables3. 2 Ethernet Cables4. 2 Network Shields (pair up with another group)5. 2 Breadboard Shields6. 1 LM34 Temperature Sensor7. 1 LCD screen8. 1 Buzzer9. Lots of Wires

Part I – Building the sender

In this part, one team will build the sender unit and display the temperature data on the PC screen using serial monitor to confirm operation. The Ethernet cable does not need to be connected for this section.

1. Place the network shield over an Arduino board. On top of that, place the breadboard shield. The fit is tight, so squeeze down to make sure all the pins make contact.

2. Wire the temperature sensor as shown to an Analog input of the Arduino. Pay attention to the orientation of the sensor as shown below. If you connect it backwards, you WILL damage the sensor.

3.

Figure 2 – Wiring of the LM34 Fahrenheit Temperature sensor.

Figure 3 – The completed temperature sending unit.

In the next section below, you will fill out the code to get the unit operational. But first, you should understand how a UDP datagram (message) travels to get to the other Arduino. Each Arduino will be identified with a MAC address and IP address which your TA’s will assign. These unique addresses point exactly to your Arduino units and tell the routers where to relay messages.

As your message travels down the Ethernet cable from the sender unit, it will contain the unique destination MAC and IP addresses of the receiver unit to which we want to send the message. The message packet will travel to the school’s central router located on campus, where it will then be relayed back into this room to the group with the receiver unit.

4. Copy the text below for the sender unit. You will need to make some changes first to set up the sensor and sender unit. Refer to your MAC and IP assignments given by the TA’s to set up these sections, or ask a TA if you don’t have them. Follow the guidelines, filling in lines and values in the starred sections.

#include <SPI.h>#include <Ethernet.h>#include <Udp.h>

/**********************************************************CETS has assigned static IPs to a bunch of mac addressesIf you DHCP using those mac addresses it will get the static IP assigned to it. Otherwise if you assign yourown mac add and IP address to a device then it will have togo through the security page of Airpenn net.These mac and static address are tested to work within the engineering building.***********************************************************/

// Replace "**" in the MAC and IP array with assigned digitsbyte mac[] = {0x2A, 0x00, 0x**, 0x**, 0x**, 0x**};byte ip[] = {158, 130, **, **};byte remoteIp[4] = {158, 130, **, **}; // Receiver unit’s IP address

// -------- Do not change the section below -----------------char UDPMessageBuffer[80];const unsigned int localPort = 9631;unsigned int remotePort = 1369;// ----------------------------------------------------------

// Fill in the analog pin connected to the temperature sensorint analogPin = **;

void setup(){ Ethernet.begin(mac, ip); // Set up the Ethernet Shield Udp.begin(localPort); // Open a socket for this port Serial.begin(9600); // Set up serial monitor with PC}

void loop(){ /**************************************************** Print out the values from the temperature sensor onto

the serial monitor in degrees Fahrenheit. Math required to convert the value to temperature in F. You should have figured this out for your pre-lab. ****************************************************/

float val = analogRead(analogPin);

int degreeF = val ** **; // Replace *s with some math(+, -, *, /)

Serial.println(degreeF); // Show value on serial monitor

// Send the temperature to the receiver Arduino itoa(degreeF, UDPMessageBuffer, 10); Udp.sendPacket(UDPMessageBuffer, remoteIp, remotePort); strcpy(UDPMessageBuffer, ""); // Clear the message // Delay for 200ms so temperature is updated once every 200ms delay(200);}

5. Compile and upload the code to the Arduino. Confirm the temperature readout by opening Serial Monitor in the Arduino IDE. If the numbers are off, check your math in the program!

6. Keep in mind the sensor is not always spot on, especially if it was damaged by a previous user. Make sure to complete the starred section on sending the data packet over UDP before moving on.

Part II – Building the Receiver

Here a group will build the receiver and confirm that it works as expected. The screen wiring can be tricky so pay attention and be careful. Again, the Ethernet cable does not need to be connected.

1. Place another network shield over a second Arduino board. Place the breadboard shield on top of that and press it down tight to ensure pins are making contact.

2. Obtain some 90 degree male headers from the bins. These will be needed because the power pins on the breadboard shield will be covered under the screen. You can also just use some jumper wires which have flexible ends and are not coated in rigid black plastic. Place these header pins or jumper wires into the power pins (5V, GND). See below. This will allow the pin to be accessible under the screen.

Figure 4: The power pins extended out by using a 90 degree male header to fit the screen above.

3. Place the screen overtop on the breadboard shield. Follow the wiring diagram below to wire the screen. If you have problems later in the lab, there is no way for us to easily know whether it’s a wiring fault or Arduino fault. Please double check connections before moving on.

Figure 5: Wiring diagram to connect the screen to the Arduino on the breadboard shield.

4. Next connect the alarm buzzer to the Arduino digital pin 9. Make sure the “+” side connects to the Arduino and the other side to ground (GND). Your board should look like below with the buzzer and screen attached.

Figure 6: The completed receiver unit with a screen and buzzer.

5. Copy the text below to make the receiver unit. Read through the code and fill in any starred sections with your pin numbers. Remember to match IP and MAC addresses and use only the ones assigned to your group.

#include <SPI.h>#include <Ethernet.h>#include <Udp.h>#include <LiquidCrystal.h>

/**********************************************************CETS has assigned static IPs to a bunch of mac addressesIf you DHCP using those mac addresses it will get the static IP assigned to it. Otherwise if you assign yourown mac add and IP address to a device then it will have togo through the security page of Airpenn net.These mac and static address are tested to work within the engineering building.***********************************************************/

// Replace "**" in the mac and ip array with assigned digitsbyte mac[] = { 0x2A, 0x00, 0x**, 0x**, 0x**, 0x** };byte ip[] = {158, 130, **, **};

// -------- Do not change the section below -----------------const unsigned int localPort = 1369; // Process ID portchar recvdBuffer[UDP_TX_PACKET_MAX_SIZE+1]; // Buffer for incoming data

byte remoteIp[4]; // Holds source IP address from incoming dataunsigned int remotePort; // Holds source port # from incoming dataLiquidCrystal lcd(8, 6, 7, 5, 3, 4); // Set up LCD screen on open pins

// ------------------------------------------------------------

// Pin for the buzzer. Pin 9 is the easiest available pin.const int buzzerPin = 9;

// Sets the threshold temperature in Fahrenheit for the buzzerint alarmThreshold = 85;

void setup(){ lcd.begin(16, 2); // Set up LCD, only done once in beginning Ethernet.begin(mac,ip); // Set up the Ethernet Shield Udp.begin(localPort); // Open a socket for this port Serial.begin(9600); // Set up serial monitor with PC pinMode(buzzerPin, OUTPUT); // Set buzzer pin to output

/********************************************************** The following code will print a test on the LCD. It will print out "Hi" on the first line, move to the second line, and then print "there" so the screen reads "Hi there." See how it is done below to use for displaying temperature later. ***********************************************************/ lcd.clear(); // Always clear the lcd before writing a frame lcd.print("Hi"); // Print "Hi" for testing lcd.setCursor(0, 1); // Move cursor to beginning of next line lcd.print("there"); // Print "there" for testing // Delay to see text on screen before it's cleared and overwritten delay(500);}

void loop(){ int recvdSize = Udp.available(); if(recvdSize) { Udp.readPacket(recvdBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);

recvdBuffer[recvdSize] = '\0'; recvdSize -= 8; // Gets rid of the message header Serial.println(recvdBuffer); // Prints received data to serial /******************************************************* Print out the temperature and alarmThreshold on the lcd screen. Hint: Refer to setup() above to see LCD writing commands. The temperature value is stored in the variable

called temperature. *******************************************************/ int temperature = atoi(recvdBuffer);

/******************************************************* Activate buzzer alarm when temperature threshold is exceeded using if else statements. *******************************************************/

}}

6. Compile and upload the code to Arduino. Confirm that the screen works by checking that it says “Hi there” for half a second before continuing. Check your serial monitor to see if your Arduino is receiving the correct temperature reading from the sender Arduino.

Figure 7: Side view of the completed receiver unit.

Part III – Networked Connection

1. Connect the Ethernet cables to jacks along the upper wall. Make sure the jack is not covered with blue tape. Reset your Arduinos by removing the USB cable and reinserting.

2. Watch the magic unfold! If your addresses were typed in correctly, the Arduinos should be communicating. You may have to tweak your LCD screen lines to get proper display or tweak the code for setting the alarm threshold. Pinch the sensor to set off the alarm. Show a TA when done!

More Tasks

1. Switch up the teams and go long distance. Pair your Arduino with that of another group from the other side of the lab.

2. Can more than one sender unit send temperature to the same receiver? Try it by organizing with another group. Have one receiver show one temperature on the upper line of the screen and the other temperature on the lower line. Show a TA when you’re done.

Extra Credit

Add up/down buttons to adjust the alarm threshold level. You will need to come up with a strategy to “de-bounce” your buttons. Mechanical buttons have metal contacts inside that will actually bounce open and closed many times when the button is only pressed down once. The Arduino is fast enough to detect these bounces, so your program will need to prevent itself from reacting more than once from a button press. Hint: A delay() is usually part of the solution.

Last updated by Parth Chopra, 12-3-2013