Arduino Interface
Arduino Interface
Ethernet Library
The Ethernet library works with the Arduino Ethernet shield and other related
devices. The Ethernet library allows us to connect the Arduino board to the
Internet.
The SPI bus acts as an intermediate between the board and the shield.
The associated library is:
1. #include <Ethernet.h>
2. #include <SPI.h>
For example, TelnetClient, WebServer, WebClientRepeating, WebClient,
ChatServer, DnsWebClient, UdpNtpClient, UdpSendReceiveString, etc.
This library is designed to work with the Arduino Ethernet Shield, Arduino
Ethernet Shield 2, Leonardo Ethernet, and any other W5100/W5200/W5500-
based devices. The library allows an Arduino board to connect to the Internet. The
board can serve as either a server accepting incoming connections or a client
making outgoing ones. The library supports up to eight concurrent connections.
#include <SPI.h>
#include <Ethernet.h>
// network configuration. gateway and subnet are optional.
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };
// telnet defaults to port 23
EthernetServer server = EthernetServer(23);
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
}
void loop()
{
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client == true) {
// read bytes from the incoming client and write them back
// to any clients connected to the server:
server.write(client.read());
}
}
Simple Ethernet Client Example
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google
EthernetClient client;
void setup(){
Ethernet.begin(mac, ip);
Serial.begin(9600);
client.connect(server, 80);
delay(1000);
Serial.println("connecting...");
if (client.connected()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}}
void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c); }
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
WiFi Library
The WiFi library permits Arduino to establish a connection with the internet. It
can either be a server to receive the incoming connections or a client to perform
outgoing connections.
The personal encryptions supported by the WiFi library are WPA2 and WEP
except for WPA2 Enterprise. Arduino uses the SPI bus to communicate with the
WiFi shield.
The library is declared as:
1. #include <WiFi.h>
The examples include WiFiWebClient, WiFiWebServer, etc.
Connecting the WiFly Shield to the Arduino is easy. Solder on the headers, and
attach the shield to the Arduino board. You can use male headers or stackable
headers, depending on how you intend to use your WiFly. For detailed
instructions on how to connect headers to the WiFly shield, please see
the Arduino Shields tutorial.
Next, connect the Arduino to the computer through the USB cable. Some of the
LEDs on the WiFly shield should now be blinking. You should see the yellow LED
blinking quickly and the green LED blinking slowly. This indicates that the WiFly is
open for new connections.
WiFly Shield Library
To aid in the use of the WiFly shield, several authors have contributed to making a
WiFly library. It has gone through several iterations, and, although it is not
perfect, it allows the end user to easily get the WiFly up and running.
Interaction with the WiFly Shield will be done via the Arduino Serial monitor or a
terminal program of your choice. In order to do this, we’ll need an example sketch
called SpiUartTerminal.