Hardware Reference
In-Depth Information
the peripheral accordingly. After this has happened, a break command is used
to exit the code from the connected client loop, and it starts listening for a new
client connection to do the whole process over again.
Putting It Together: Web Server Sketch
Given all the requirements listed in the previous sections, you can now construct
a server program for the Arduino. These programs tend to be fairly nontrivial
because they require the use of several state variables that track the interac-
tion between the client and server. The sketch in ListingĀ 14-2 works great for
accomplishing the tasks of controlling an RGB LED and speaker. If you want
to add additional functionality with more GET variables, it should be fairly
straightforward to do so. The areas where you can insert this extra functional-
ity are called out in the code comments.
Listing 14-2: Web Server Codeā€”control_led_speaker.ino
//Arduino Web Server
//Some code adapted under MIT License from
//http://bildr.org/2011/06/arduino-ethernet-pin-control/
#include <Ethernet.h>
#include <SPI.h>
const int BLUE =5;
const int GREEN =6;
const int RED =7;
const int SPEAKER =3;
//For controlling LEDS and the speaker
//If you want to control additional things, add variables to
//control them here.
int freq = 0;
int pin;
//Set to your MAC address!
//It should be on your sticker. If you can't find it,
//make one up, or use this one.
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x4A, 0xE0 };
//Start the server on port 80
EthernetServer server = EthernetServer(80); //port 80
boolean receiving = false; //To keep track of whether we are
//getting data.
void setup()
{
Serial.begin(9600);
Search WWH ::




Custom Search