Disponível somente no TrabalhosFeitos
  • Páginas : 9 (2100 palavras )
  • Download(s) : 0
  • Publicado : 18 de março de 2012
Ler documento completo
Amostra do texto
Getting hands on Arduino Ethernet Shield
Posted on April 7, 2011 by admin| 52 Comments
Updated! Arduino Ethernet code
Since last Arduino Ethernet code there we several Arduino IDE releases with changes that affected the code listed in this post. Due high interest, we updated it with minor changes that makes it work as expected. The biggest occurred because Wstring.h library isn’t nolonger in use, because String.h library is included in core that brings some difference in several functions used in code.
In code we need to write
readString += c; instead readString.append(c);
if(readString.indexOf(“L=1″) >0) instead if(readString.contains(“L=1″))
Also we need to re-import Ethernet.h library in order to bring along all necessary libraries like Client.h, Server.h, SPI.h,Udp.h.
The other problem occurred when program run was that LED actually never lights up when checkbox is selected. I used Serial.print(c); to track down the problem. And it seems that method GET sends two strings:

Our code was catching and analyzing both strings. We only need to take string where parameter is sent: “GET /?L=1 HTTP/1.1” and skip “GET /favicon.ico HTTP/1.1”. As our example is verysimple we can see ,that second string lacks “?” symbol. So we check the string if there is a ‘?’ symbol, of not, skip whole analyze.
And last fix is applied to LED checkbox. Now it stays checked if LED is ON. Hope you find these changes useful.
Here is a code listing:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 | #include <SPI.h>#include <Client.h>#include <Ethernet.h>#include <Server.h>#include <Udp.h>byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac addressbyte ip[] = { 192, 168, 0, 110 };           // ip in lanbyte gateway[] = { 192, 168, 0, 1};            // internet access via routerbyte subnet[] = { 255, 255, 255, 0 };                   //subnet maskServer server(80);                                      //server portbyte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)int ledPin = 4;  // LED pinchar link[]="http://www.scienceprog.com/"; //link dataString readString = String(30); //string for fetching data fromaddressboolean LEDON = false; //LED status flagvoid setup(){//start Ethernet  Ethernet.begin(mac, ip, gateway, subnet);//Set pin 4 to output  pinMode(ledPin, OUTPUT);//enable serial datada print  Serial.begin(9600);}void loop(){// Create a client connectionClient client = server.available();  if (client) {    while (client.connected()) {   if (client.available()) {    char c = client.read();     //readchar by char HTTP request    if (readString.length() < 100)      {        //store characters to string        readString += c; //replaces readString.append(c);      }        //output chars to serial port        Serial.print(c);        //if HTTP request has ended        if (c == '\n') {          //dirty skip of "GET /favicon.ico HTTP/1.1"          if (readString.indexOf("?")<0)          {            //skip everything          }          else          //lets check if LED should be lighted        if(readString.indexOf("L=1") >0)//replaces if(readString.contains("L=1"))           {             //led has to be turned ON             digitalWrite(ledPin, HIGH);    // set the LED on             LEDON = true;           }else{             //led has to be turned OFF             digitalWrite(ledPin, LOW);   // set the LED OFF             LEDON = false;           }          // now output HTML data starting with standart header          client.println("HTTP/1.1 200 OK");          client.println("Content-Type: text/html");          client.println();          //set background to yellow          client.print("<body style=background-color:yellow>");          //send first...
tracking img