Ads 468x60px

Selasa, 29 Mei 2012

Intro to the WIZ820io Module – by Ben Roberts


Components

Qty.        Item
1          Arduino
1          WIZ820io
1          Ethernet Patch Cable (if internet connection is wireless)
1          Arduino 1.0 IDE
1          Hobby Servo
1           LM355 Temperature Sensor
1           300-1k Ω Resistor

Introduction

Hardware

The WIZ820io is the main component on the Arduino Ethernet Shield, and consists of two main components: An RJ45 Ethernet connector and a W5200 chip. The W5200 supports TCP/IP (Transmission Control Protocol/Internet Protocol), which is a standard communication protocol of the internet. Basically, information sent over the internet is separated into packets of information that is sent and received per TCP. Internet Protocol (IP) handles the destination of the transmitted information, typically given as a four-byte address.

SPI

External communication (In our case, the external device is Arduino) with the 820io is done using an interface protocol that was originally developed by Motorola, called SPI (Serial Peripheral Interface). SPI allows for communication between a Master device (the Arduino), and one, or multiple, Slave devices (the 820io). As seen in Figure 1, every device on the SPI bus shares a clock signal, provided by the master. The MOSI line (Master Out Slave In) transmits data from the master to the slaves, and the MISO line (Master In Slave Out) transmits from the slave devices to the master. The master has designated Slave Select (SS1, SS2) pins that select which slave to communicate with. Slave Select is active low, so to initiate communication, the master pulls the slave’s SS line low, provides a clock signal, and the master and slave can send and receive data. In contrast to another popular interfacing protocol, I2C, which has only two lines; clock and data, SPI can send and receive data between the master and slave (MOSI and MISO) simultaneously. This functionality makes SPI ideal for streams of data, such as those being sent over the internet. SPI can facilitate speeds in the tens of MHz, and if you only have a few slaves, it is generally easier to implement than I2C. Arduino’s Ethernet Library functions handle most of the SPI communication with the 820io, but there are many other devices and sensors that use SPI.
SPI Bus
Figure 1:SPI Bus. Master supplies a clock signal to slaves. MOSI and MISO allow for simultaneous communication between master and slaves.

Procedure

1. In order to use the 820io with Arduino’s Ethernet Library you’ll have download and replace a couple files in the Ethernet Library. Go to: WIZnet and download the Library for Arduino zip file. This file should contain two files: w5100.cpp and w5100.h. Place these files in the Arduino directory under Libraries>Ethernet>Utility.
2. Connect the WIZ820io to the Arduino following the schematic in Figure 2.

WIZ820io pinout
Wiring Diagram: Arduino to 820io
Figure 2: Wiring Diagram: Arduino to 820io. Pin connections defined by the Arduino SPI Library. No connection on pins nINT and PWDN.

Internet Configuration

Since the 820io needs a physical internet connection to access the internet, you will need to do one of two things: 1.) Connect to the internet wirelessly from your PC and share your computer’s connection through an Ethernet cable between your PC and the 820io or, 2.) Connect the 820io directly to the internet with an Ethernet cable. If you’re using a hardwired connection, you can skip the next section: ‘PC Connected to Wireless Network’.
PC Connected to Wireless Network
If you are accessing the internet wirelessly through a network that doesn’t allow file sharing etc. (ie. The SJSUcampus network), you will probably not be able to communicate with the 820io because of restrictions placed on the network, so, either find a hardwire connection or a new wireless network (ie. ME106 or your home network).
To share your wireless connection with the Ethernet port on your PC, first load the example BareMinimum into Arduino, make sure the 820io is wired as shown in Figure 2, and have an Ethernet patch cable connecting the 820io and your PC. Next, go to the Control PanelàNetworkàChange Adapter Settings. Then select your wireless network from the list of networks and Change Settings of this Connection. Under the Sharing tab you should be able to check the box, Allow other Network Users to Connect through this Computer’s Connection. Then, from the pull-down select the network that you want to share your wireless with, probably Local Area Connection, then OK. Now, back at the Network Connections window your wireless network should say Shared underneath it.

IP Address

The most vital piece of information when communicating with a device via the internet is the IP address. An IP address is a 32-bit number used to identify a particular host (the 820io) on a particular TCP/IP network. For example, if we’re sending information to the address, 192.168.137.5, the network that the host is located on is given by the first three bytes: 192.168.137, and the host address is 5.
3. If you’re using the Arduino 1.0 IDE, upload the example DhcpAddressPrinter from the Ethernet library. Open the serial monitor, and after a few seconds the IP address of the 820io should be displayed. This will be the IP address for the 820io on the network you are currently connected to. If you’re using a different version of the IDE, see Appendix C for details on identifying an IP for your network.

 Client/Server and ChatServer Example

The 820io/Arduino and your PC interact through a Client/Server relationship. Client/server works on the basis of sharing resources from server to client, where the client initiates a request from the server and the server processes the request and returns results. An example of client/server is an email account; when you check your email your PC acts as a client requesting access to your account from a server where your emails are stored. The server grants the request and returns your emails. The 820io/Arduino and your PC share the same relationship, where the Arduino is a server and your PC or web browser is the client.
To talk to Arduino in the ChatServer example you will need a Telnet client software to communicate with Arduino’s IP. You can find this software on the ME106 website, or you can search socktest download and download it for free.

4. Open the ChatServer example from the Ethernet Library and insert the IP address you found for your device in step 2 with commas separating each byte, instead of periods. If you scroll down a few lines you’ll see the comment: telnet defaults to port 23. And FYI, the default for HTTP is port 80. You can view and edit the default ports in Windows under Control Panel>Internet Options>Connections>LAN Settings>Proxy Server. After inserting the IP in the IDE, upload the program. Open the telnet software, insert the device IP in the field and make sure the port is set the same as it was in the program. Open the IDE serial monitor and in the telnet software go to Actions>Connect to Server. In the software window, you should see a message that you are connected to the Arduino’s IP and port. If you type in the telnet software window, the server should acknowledge your request with a greeting.

Figure 3: IP, MAC, Gateway, Subnet, Port. This block of addresses is in all Ethernet sketches. Generally, the only address that is specific to the 820io, and needs to be changed, is IP. If you have an Ethernet Sheild, there will generally be a specific MAC address on the board. If you plan to access Arduino remotely, you may need to change Gateway and Subnet. For more info on gateway and subnet: http://support.microsoft.com/kb/164015.

Servo Modified ChatServer

This example is meant to show how input data from the client are handled with the client.Read() function. In this example, the input data is read to a character string and converted to an integer that is passed to the Servo.Write function.
5.  Connect a servo to pin 5, paste the code from Appendix A and insert your device’s IP into the program. Connect to the telnet software as above. If you send a 3-digit angle to Arduino’s IP (ie. 025,055,135), the servo will go to that angle.
If you look in Loop(), the client.read() call actually reads one byte/ASCII character from the client each time through the loop. This means that the 3-digit angle you provided is actually stored as three separate bytes of data, therefore, the data needs to be manipulated before it can read by Servo.Write. Back in program, after client.read(), each character is added to the string, readString. The following if statement converts the string to an integer to pass into the MoveServo function. This is done by first putting each string element in a character array with the .toCharArray command. Then the atoi operator converts the array to an integer to pass to the function. In this example, and in general, if Arduino is getting random characters from a client that need to be processed as continuous data, the characters will probably have to be manipulated in some way before they can used.

HTTP Sensor Read

This example processes a web browser request to read an analog sensor output on pin A0 and prints the value back to the browser. The example uses an LM335 temperature sensor, but any analog sensor will work. The LM335 has a time constant of over 80 seconds, so, it will be slow to respond to temperature changes.

Figure 4: LM335 Schematic and Connection.

6. Connect the LM335 as shown in Figure 4. After inserting your device’s IP into the code in Appendix B, upload it to Arduino. Open a web browser (ie. Chrome, Mozilla etc…). In the web address field enter http://yourIPaddress/?A. The page should display the analog value of the sensor, similar to that in Figure 5.

Figure 5: Web Browser Input and Sensor Output.
Looking at the program, the client communication loop is basically the same as the previous ChatServer example, except we have the program looking for a specific character, a “?”, before it starts reading characters to be processed by the program. In this case, the character ‘A’ initiates the analogRead of the sensor. If you insert a Serial.println(c) after the client.read() line and type your IP in the browser again, this time without a ‘?’, you will see that the Http: request returns a lot of information that is not particularly useful. This is why we used a ‘?’ to tell Arduino where to begin looking for the useful data. The last piece of information the program looks for is a blank line (‘\n’), which ends the client communication.

Appendix A: Servo Modified ChatServer.
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
void MoveServo(int string, int old);
Servo myservo;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(                      );
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
String readString = String(); //string for fetching data from address
// telnet defaults to port 23
EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
int n;                      //new char to int
int count = 0;
int Oldn = 0;               //old char to int value
char carray[4];            //char array to store string, 3 characters, 1 NULL
void setup() {
myservo.attach(5);
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
// open the serial port
Serial.begin(9600);
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!gotAMessage) {
Serial.println(“We have a new client”);
client.println(“Hello, client!”);
gotAMessage = true;
}
// read the bytes incoming from the client:
char c = client.read();
//store characters to string
readString += c;
count++;
//Serial.println(count);
if (count == 5)
{
//Put String into character array
readString.toCharArray(carray,sizeof(carray));
//convert character array to integer with atoi
n = atoi(carray);
Serial.println(n);
//Reset count and String
count = 0;
readString = String();
MoveServo(n,Oldn);
Oldn = n;
}
}
}
void MoveServo(int string, int old)
{
if (string >= old)
{
for(int i=old; i<string;i++)
{
myservo.write(i);
delay(15);
}
}
else
{
for(int i=old; i>=string; i–)
{
myservo.write(i);
delay(15);
}
}
}
Appendix B: HTTP Sensor Read.
#include <Ethernet.h>
#include <SPI.h>
boolean reading = false;
#define sensPin A0
byte ip[] = {              };   // Add IP address
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server = EthernetServer(80); //HTTP default port 80
int sensVal;
void setup(){
//Pins 10,11,12 & 13 are used by 820io
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
}
void loop(){
// listen for incoming clients, and process qequest.
checkForClient();
}
void checkForClient(){
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if(reading && c == ‘ ‘) reading = false;
if(c == ‘?’) reading = true; //found the ?, begin reading the info
if(reading){
Serial.print(c);
if( c==’A')
{
sensVal = analogRead(sensPin);
sensPrint(sensVal, client);
}
}
if (c == ‘\n’ && currentLineIsBlank)  break;
if (c == ‘\n’) currentLineIsBlank = true;
else if (c != ‘\r’) currentLineIsBlank = false;
}
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
}
}
void sensPrint(int val, EthernetClient client)
{
//Client needed for HTML output purposes.
client.print(“Sensor Value is: “);
client.println(val);
}

Appendix C: IP Address for IDE’s Earlier than Arduino 1.0.
The Ethernet Library in versions of the Arduino IDE before 1.0 don’t support the DhcpAddressPrinter example. To find suitable network ID’s that will work depends on whether you are sharing your wireless connection with the 820io, or providing the 820io a hardwired internet connection. If you are using a hardwired connection, plug this connection into the LAN jack on your PC. If you’re sharing your wireless: connect a patch cable from your PC to the 820io and have Arduino powered through USB. Next, go to Start MenuàSearch and type ‘cmd’. This should open the command prompt. In the prompt, type ‘ipconfig’ and find the block of information referring to your Ethernet/Local Area Connection. As described earlier, the first three bytes of this IP refer to your network address and are vital to communication through the network. The last byte is the specific host address, which you can arbitrarily choose. So, choose a number between 0-255 for the last byte, and in the command prompt type ‘ping’ followed by a space, followed by the 3-byte network address and your chosen host address. If this request is ‘Timed Out’, then you can use the address that you just pinged as your IP. If you get a reply from the ping, then you need to try another host address.

Figure 6: Command Prompt Display of LAN IP. Command prompt display after typing ‘ipconfig’

 Figure 7: Ping. “Timed Out” request shows that there is no device currently using the address. A reply indicates that there is already a device using the address.

Tidak ada komentar:

Posting Komentar