DA ETHERNET SHIELD AL WIFI SHIELD

Buona sera a tutti,
ho fatto un progettino con arduino + ethernet shield praticamente uso l Arduino sia come webserver che webclient per accendere un led dal browser e richiamo una pagina asp che mi aggiorna lo stato sul database.
adesso vorrei fare la stessa cosa però usando il nuovo Wifi shield ufficiale di arduino ho provato a copiare lo stesso codice e cambiando le librerie ect.. ma non mi funziona benissimo cioè mi fa un giro accende il led aggiorna il database e si disconnette e non risponde più alle richieste del browser
posto lo sketch ethernet funzionante da convertire in wifi shield

ethernet:

//zoomkat 7-03-12, combined client and server
//simple button GET with iframe code

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //assign arduino mac address
byte ip[] = {192, 168, 1, 5 }; // ip in lan assigned to arduino
byte gateway[] = {192, 168, 1, 1 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port arduino server will use
EthernetClient client;
//char serverName[] = "web.comporium.net"; // (DNS)  web page server
byte serverName[] = { 192,168,1,200 }; // (IP)  server IP address

String readString; //used by server to capture GET request 

void sendGET(char,String);
void  controllo_led(int,String);


const int  INDICE_PIN = 4; // definisce una costante per il numero di pin  
  
String comandGET[INDICE_PIN] = {"01","02","03","04"};    // COMANDI GET INVIATI DAL BROWSER 
int pinLed[INDICE_PIN] = {2,3,4,5};     // I LED IN COPIA CON I COMANDI GET  



void setup(){
  
for(int indice = 0; indice < 4; indice++)  
  {  
    pinMode(pinLed[indice], OUTPUT);  // dichiara il LED come output
    digitalWrite(pinLed[indice], LOW); // inizializzo tutti i led "SPENTI"
  } 
  Ethernet.begin(mac,ip,gateway,gateway,subnet); 
  server.begin();
  Serial.begin(9600); 
  Serial.println("ARDUINO GESTIONE SCAFFALI"); // what to do to test client
}

void loop(){
  // check for serial input

  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
         Serial.print("readString:");   
          Serial.print(readString); //print to serial monitor for debuging 
            
            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println(F("HTTP/1.1 204 Zoomkat"));
            client.println();
            client.println();  
          }
          else {   
            client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
            client.println(F("Content-Type: text/html"));
            client.println();

            client.println(F("<HTML>"));
            client.println(F("<HEAD>"));
            client.println(F("<TITLE>Arduino GET test page</TITLE>"));
            client.println(F("</HEAD>"));
            client.println(F("<BODY>"));

            client.println(F("<H1>ARDUINO MEGAZZINO</H1>"));

            // DIY buttons
            client.println(F("Pin4"));
            client.println(F("<a href=/?cod=000000&pos=01 target=inlineframe>ON</a>")); 

            client.println(F("Pin5"));
            client.println(F("<a href=/?cod=111111&pos=22 target=inlineframe>ON</a>")); 
           

            client.println(F("Pin6"));
            client.println(F("<a href=/?cod=222222&pos=33 target=inlineframe>ON</a>")); 
            
            client.println(F("<IFRAME name=inlineframe style='display:none'>"));          
            client.println(F("</IFRAME>"));
            client.println(F("</BODY>"));
            client.println(F("</HTML>"));
          }

          delay(1);
          //stopping client
          client.stop();
/*
          ///////////////////// control arduino pin
          if(readString.indexOf('1') >0)//checks for 2
          {
           controllo_led(led_1);    // set pin 5 high
            Serial.println("Led 5 On");
            Serial.println();
          }
          
          if(readString.indexOf('2') >0)//checks for 2
          {
           controllo_led(led_2);    // set pin 5 high
            Serial.println("Led 5 On");
            Serial.println();
          }
          
          if(readString.indexOf('3') >0)//checks for 2
          {
           controllo_led(led_3);    // set pin 5 high
            Serial.println("Led 5 On");
            Serial.println();
          }
          
          
          
                if (pos == "01") 
          {
            controllo_led(led_1,cod);    // set pin 5 high
            Serial.println("Led 5 On");
           }
          
*/        

         //GET /?cod=000000&pos=01 HTTP/1.1
         
            String cod=readString.substring(10,16);
            String pos=readString.substring(21,23);
             Serial.println(cod);
             Serial.println(pos);

         for(int indice = 0; indice < INDICE_PIN; indice++)  
          {  
            String stato=comandGET[indice];
            if  (pos == stato)   
              { 
              int stato = digitalRead(pinLed[indice]);
              if ( stato == LOW) 
              { 
                for(int indice = 0; indice < 4; indice++)  
                  {  
                    digitalWrite(pinLed[indice], LOW); // spengo tutti i led 
                  } 
                digitalWrite(pinLed[indice], HIGH); 
                
              }
              else if ( stato == HIGH ) 
              { 
                digitalWrite(pinLed[indice], LOW);        
                 int stato = digitalRead(pinLed[indice]);
                  if ( stato == LOW) 
                    { 
                      String stato="1";
                      sendGET(cod,stato);
                    } 
              }  
              }  
          }  
          //clearing string for next read
          readString="";
        }
      }
    }
  }
} 
//*************************************************CONTROLLO DEI LED ON/OFF***************************************
/*void  controllo_led(int pin,String cod)
{
  
    int stato = digitalRead(pin);
    
    if ( stato == LOW) 
    { 
        digitalWrite(pin, HIGH);
        int stato = digitalRead(pin);
        if ( stato == HIGH) 
          { 
            String stato="1";
            sendGET(cod,stato);
          } 
          else
          { 
            String stato="0";
            sendGET(cod,stato);
          }
          
    }
    else if ( stato == HIGH ) 
    { 
      digitalWrite(pin, LOW);
      int stato = digitalRead(pin);
        if ( stato == LOW) 
          { 
            String stato="1";
            sendGET(cod,stato);
          }
        else
          { 
            String stato="0";
            sendGET(cod,stato);
          }
          
    }
}
*/
//////////////////////////
void sendGET(String cod, String stato) //client function to send and receive GET data from external server.
{
  
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    
   /* String cod="000007";
    String stato="1";*/
    String invio_conferma="GET /Arduino2.asp?cod=" + cod + "&" + "stato=" + stato + " HTTP/1.0"; // GET /Arduino2.asp?cod=000008&stato=1 HTTP/1.0
    
    Serial.println(invio_conferma);
    Serial.println();
    
    client.println(invio_conferma);
    client.println();
    
  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}

spero che riuscite ad aiutarmi grazie mille e buona serata a tutti

Ciao,
e quello convertito? ]:slight_smile:

Cmq qui trovi un esempio di codice scritto in modo che funzioni sia con Eth che con Wifi:

@pitusso

grazie di avermi risposto comunque volevo copiare anche lo sketch WIFI solo che mi dava un errore praticamente ho superato il limite di 9500 caratteri disponibili per il post adesso lo carico nuovamente.
per quanto riguarda l'esempio che mi hai suggerito sembra ci sia solo il WEBclient per inviare dati (letture dei sensori) al server a me serverebbe anche il Webserver per ricevere comandi dal browser per accendere un led praticamente quest'è il mio progetto dovrei accendere dei led ed inviare la conferma con il codice del led (123456) ed il suo stato quando si spegne (1:vuol dire l operazione andata a buon fine)
//GET /Arduino2.asp?cod=000008&stato=1 HTTP/1.0

alla mia pagina in .asp cosi memorizzo lo stato dei leds sul database

con la ethernet shield funziona perfettamente senza problemi invece con lo scudo wifi di arduino fa un giro solo e si blocca li

#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>

char ssid[] = "wedo-D-Link";      //  your network SSID (name) 
char pass[] = "wedosport-dlink";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(84);
WiFiClient client;
byte serverName[] = { 192,168,1,200 }; // (IP)  server IP address

String readString; //used by server to capture GET request 

void sendGET(char,String);
void  controllo_led(int,String);


const int  INDICE_PIN = 4; // definisce una costante per il numero di pin  
  
String comandGET[INDICE_PIN] = {"01","02","03","04"};    // COMANDI GET INVIATI DAL BROWSER 
int pinLed[INDICE_PIN] = {8,9,10,11};     // I LED IN COPIA CON I COMANDI GET  


void setup(){
  Serial.begin(9600); 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
for(int indice = 0; indice < 4; indice++)  
  {  
    pinMode(pinLed[indice], OUTPUT);  // dichiara il LED come output
    digitalWrite(pinLed[indice], LOW); // inizializzo tutti i led "SPENTI"
  } 
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    while(true);        // don't continue
  } 
  Serial.println("ARDUINO GESTIONE SCAFFALI"); // what to do to test client
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  } 
    Serial.println("Connesso alla rete :");
   server.begin();                           // start the web server on port 80
   printWifiStatus();                 // you're connected now, so print out the status
  }

void loop(){
  // check for serial input

  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.print("readString:");   
          Serial.print(readString); //print to serial monitor for debuging 

  /*           
            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println(F("HTTP/1.1 204 Zoomkat"));
            client.println();
            client.println();  
          }
          else {   
            client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
            client.println(F("Content-Type: text/html"));
            client.println();

            client.println(F("<HTML>"));
            client.println(F("<HEAD>"));
            client.println(F("<TITLE>Arduino GET test page</TITLE>"));
            client.println(F("</HEAD>"));
            client.println(F("<BODY>"));

            client.println(F("<H1>ARDUINO MEGAZZINO</H1>"));

            // DIY buttons
            client.println(F("Pin4"));
            client.println(F("<a href=/?cod=000000&pos=01 target=inlineframe>ON</a>")); 

            client.println(F("Pin5"));
            client.println(F("<a href=/?cod=111111&pos=22 target=inlineframe>ON</a>")); 
           

            client.println(F("Pin6"));
            client.println(F("<a href=/?cod=222222&pos=33 target=inlineframe>ON</a>")); 
            
            client.println(F("<IFRAME name=inlineframe style='display:none'>"));          
            client.println(F("</IFRAME>"));
            client.println(F("</BODY>"));
            client.println(F("</HTML>"));
          }
*/
          delay(1);
          //stopping client
          client.stop();        

         //GET /?cod=000000&pos=01 HTTP/1.1
           String get=readString.substring(0,3);
           if (get=="GET"){ 
            String cod=readString.substring(10,16); // prendo il codice del LED della stringa inviata dal browser
            String pos=readString.substring(21,23); // prendo il la posizione del LED da accendere
             Serial.println(cod);
             Serial.println(pos);
             Serial.println(get);
             for(int indice = 0; indice < INDICE_PIN; indice++)  
              {  
                String stato=comandGET[indice];
                if  (pos == stato)   
                  { 
                  int stato = digitalRead(pinLed[indice]);
                  if ( stato == LOW) 
                  { 
                    for(int indice = 0; indice < 4; indice++)  
                      {  
                        digitalWrite(pinLed[indice], LOW); // spengo tutti i led 
                      } 
                    digitalWrite(pinLed[indice], HIGH); 
                    
                  }
                  else if ( stato == HIGH ) 
                  { 
                    digitalWrite(pinLed[indice], LOW);        
                     int stato = digitalRead(pinLed[indice]);
                      if ( stato == LOW) 
                        { 
                          String stato="1";
                          sendGET(cod,stato);
                        } 
                  }  
                  }  
              }
            }
          //clearing string for next read
          readString="";
        }
      }
    }
  }
} 
//*************************************************CONTROLLO DEI LED ON/OFF***************************************
/*void  controllo_led(int pin,String cod)
{
  
    int stato = digitalRead(pin);
    
    if ( stato == LOW) 
    { 
        digitalWrite(pin, HIGH);
        int stato = digitalRead(pin);
        if ( stato == HIGH) 
          { 
            String stato="1";
            sendGET(cod,stato);
          } 
          else
          { 
            String stato="0";
            sendGET(cod,stato);
          }
          
    }
    else if ( stato == HIGH ) 
    { 
      digitalWrite(pin, LOW);
      int stato = digitalRead(pin);
        if ( stato == LOW) 
          { 
            String stato="1";
            sendGET(cod,stato);
          }
        else
          { 
            String stato="0";
            sendGET(cod,stato);
          }
          
    }
}
*/
//////////////////////////

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}


void sendGET(String cod, String stato) //client function to send and receive GET data from external server.
{
  
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    
   /* String cod="000007";
    String stato="1";*/
    String invio_conferma="GET /Arduino2.asp?cod=" + cod + "&" + "stato=" + stato + " HTTP/1.0"; // GET /Arduino2.asp?cod=000008&stato=1 HTTP/1.0
    
    Serial.println(invio_conferma);
    Serial.println();
    
    client.println(invio_conferma);
    client.println();
  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}