sketch per board ethernet relè

Ciao!!
Sto sperimentando questo nuovo mondo e devo fare parecchia strada.
Ciò che volevo fare è un sistema simil domotico per camera mia: aver la possibilità di dare corrente a ciò che voglio tramite http.
lo sketch che ho trovato fa a caso mio....ma non funziona!! :smiley: :smiley:
Qualcuno può darmi una mano?
Grazie

/*
  Network Relay System


*/

#include "etherShield.h"
#include "ETHER_28J60.h"

int i, rsize, relay[] = {8, 7, 6, 5, 3, 2, 1, 0};

static uint8_t mac[6] = {0x10, 0x10, 0x10, 0x02, 0x00, 0x00};
static uint8_t ip[4] = {192, 168, 1, 90};
static uint16_t port = 80;
unsigned long runtime, starttime;

ETHER_28J60 ether;

void setup() {
  pinMode(4, OUTPUT);    // 
  digitalWrite(4, HIGH); // Same as above.

//  Serial.begin(57600);
  ether.setup(mac, ip, port);
  rsize = sizeof(relay) / 2;
  for (i = 0; i < rsize; i = i + 1) {
    pinMode(relay[i], OUTPUT);
  }
  runtime = 0;
  starttime = millis();
}

void loop() {
  int c;
  char * param;
  char * params;

  if (params = ether.serviceRequest()) {
    if (strstr(params, "?status")) {
      for (i = 0; i < rsize; i = i + 1) {
        ether.print(digitalRead(relay[i]));
      }
    } else {
      if((strlen(params) > 3) && (!(strstr(params, "favicon.ico")))) {
        runtime = 0;
        param = strtok(params, "?&");
        while (param != NULL) {
          if (strstr(param, "p=")) {
            i = atoi(param + 2) - 1;
          } else if (strstr(param, "c=")) {
            c = atoi(param + 2);
            if (c == 1) {
              for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); }
              digitalWrite(relay[i], HIGH);
            } else {
              for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); }
            }
          } else if (strstr(param, "t=")) {
            runtime = (unsigned long)(atoi(param + 2)) * 1000;
            if (runtime < 15000) { runtime = 15000; }
            starttime = millis();
          }
          param = strtok(NULL, "& ");
        }
        if (runtime == 0) {
          runtime = 60000;  // 60 sec. default run time (takes 10 seconds for valve to open).
          starttime = millis();
        }
      }

      ether.print("<tt>Network Relay System\n
\n");
      for (i = 0; i < rsize; i = i + 1) {
        ether.print("
Port #");
        ether.print(i+1);
        ether.print(": <a href='?p=");
        ether.print(i+1);
        if (digitalRead(relay[i]) == 1) {
          ether.print("&c=0'>Off</a> in ");
          ether.print((runtime - (millis() - starttime))/1000);
          ether.print(" seconds.\n");
        }
        else {
          ether.print("&c=1'>On</a>\n");
        }
      }
      ether.print("<p><a href='/'>Refresh</a>,<a href='/?status'>Status</a>\n");
    }
    ether.respond();
  }

  if ((unsigned long)(millis() - starttime) > runtime) {
    for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); }
  }
}

edit by mod: per favore includere il codice usando gli appositi tag

cosa vuol dire che non funziona ? cos'è che non funziona ? :wink:

Non riesco ad entrare dalla pagina browser!

Per prima cosa dovresti rieditare il tuo post precedente inserendo il codice con tra i tag CODE. Per vedere come fare consulta il regolamento al punto 7 ([REGOLAMENTO] Come usare questa sezione del forum - Italiano - Arduino Forum)

Poi immagino che tu abbia una Arduino e una Shield Ethernet con Enc28J60, giusto?
Hai verificato la corrispondenza della scheda con la libreria? Se hai una Eth shield ufficiale quello sketch difficilmente funzionerà perchè usa un chip diverso.

Ah ok!!
Quindi con questa shield
http://www.cooking-hacks.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/a/r/arduino_ethernet_3_600_1.png
lo sketch non gira!!
se è così butto via tutto!! :smiley: :smiley:

Lo devi riadattare.
Devi sostituire la libreria con la libreria Ethernet che è integrata nell'IDE.

//Aggiungo le librerie necessarie
#include <SPI.h>
#include <Ethernet.h>
#include <WebServer.h> //libreria webduino

//definiamo due array: il primo per definire l'indirizzo mac, il secondo per definire l'indirizzo IP.
//Per quanto riguarda l'indirizzo IP selezionate un indirizzo valido per la vostra rete locale.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {192, 168, 1, 177};

//definiamo la porta di ascolto
WebServer webserver("", 80);
int ledPins[] = {2,3,4,5,6,7,8,9};

boolean pin1, pin2, pin3, pin4, pin5, pin6, pin7, pin0 = false;


//avviamo il server e settiamo tutte le variabili relative al pin2,pin3,pin4 in stato di ON e in stato di OFF
void Start(WebServer &server, WebServer::ConnectionType type,
           char *url_param, bool param_complete)
{
  server.httpSuccess();
  if (type != WebServer::HEAD)
  {
    String s = "";
     if (param_complete == true)
    {
      s = url_param;

      if ( s == "pin1=ON")
      {
        pin1 = true;
        digitalWrite(ledPins[1], HIGH);
      }
      else if ( s == "pin1=OFF")
      {
        pin1 = false;
        digitalWrite(ledPins[1], LOW);
      }
       //altro
        if ( s == "pin2=ON")
      {
        pin2 = true;
        digitalWrite(ledPins[2], HIGH);
      }
      else if ( s == "pin2=OFF")
      {
        pin2 = false;
        digitalWrite(ledPins[2], LOW);
      }
      //altro
        if ( s == "pin3=ON")
      {
        pin3 = true;
        digitalWrite(ledPins[3], HIGH);
      }
      else if ( s == "pin3=OFF")
      {
        pin3 = false;
        digitalWrite(ledPins[3], LOW);
      }
      if ( s == "pin4=ON")
      {
        pin4 = true;
        digitalWrite(ledPins[4], HIGH);
      }
      else if ( s == "pin4=OFF")
      {
        pin4 = false;
        digitalWrite(ledPins[4], LOW);
      }
       //altro
        if ( s == "pin5=ON")
      {
        pin5 = true;
        digitalWrite(ledPins[5], HIGH);
      }
      else if ( s == "pin5=OFF")
      {
        pin5 = false;
        digitalWrite(ledPins[5], LOW);
      }
     
    }

    //Generazione della pagina HTML
    P(htmlHead) =
    "<html>"
    "<head>"
    "<title>Web server gestione Rele</title>"
    "</head>"
    "<body>";

    server.printP(htmlHead);
     server.print("
<p align=\"center\"><img src=\"http://linuxmx.it/images/logo215px.png\" width=\"150\"></p><p align=\"center\"><table border=\"1\" width=\"500\" cellpadding=\"1\"cellspacing=\"1\" bordercolor=\"#0000FF\">

");


    if(pin1 == true)
      server.print("<tr><td style=\"color: red;background: #99CC33;\" valign=\"middle\" >PIN 1 ON</td><td>");
    else
      server.print("<tr><td style=\"color: black;\" valign=\"middle\" >PIN 1 OFF</td><td>");
 
    if(pin1 == false)
      server.print("<input type=\"button\" value=\"Accendi\" onclick=\"location.href='index.htm?pin1=ON'\">");
    else
      server.print("<input type=\"button\" value=\"Spegni\" onclick=\"location.href='index.htm?pin1=OFF'\">");
    //ALTRA TABELLA
    if(pin2 == true)
      server.print("<tr><td style=\"color: red;background: #99CC33;\" valign=\"middle\" >PIN 2 ON</td><td>");
    else
      server.print("<tr><td style=\"color: black;\" valign=\"middle\" >PIN 2 OFF</td><td>");
 
    if(pin2 == false)
      server.print("<input type=\"button\" value=\"Accendi\" onclick=\"location.href='index.htm?pin2=ON'\">");
    else
      server.print("<input type=\"button\" value=\"Spegni\" onclick=\"location.href='index.htm?pin2=OFF'\">");
    //ALTRA TABELLA
    if(pin3 == true)
      server.print("<tr><td style=\"color: red;background: #99CC33;\" valign=\"middle\" >PIN 3 ON</td><td>");
    else
      server.print("<tr><td style=\"color: black;\" valign=\"middle\" >PIN 3 OFF</td><td>");
 
    if(pin3 == false)
      server.print("<input type=\"button\" value=\"Accendi\" onclick=\"location.href='index.htm?pin3=ON'\">");
    else
      server.print("<input type=\"button\" value=\"Spegni\" onclick=\"location.href='index.htm?pin3=OFF'\">");
      //ALTRA TABELLA
    if(pin4 == true)
      server.print("<tr><td style=\"color: red;background: #99CC33;\" valign=\"middle\" >PIN 4 ON</td><td>");
    else
      server.print("<tr><td style=\"color: black;\" valign=\"middle\" >PIN 4 OFF</td><td>");
 
    if(pin4 == false)
      server.print("<input type=\"button\" value=\"Accendi\" onclick=\"location.href='index.htm?pin4=ON'\">");
    else
      server.print("<input type=\"button\" value=\"Spegni\" onclick=\"location.href='index.htm?pin4=OFF'\">");
    //ALTRA TABELLA
    if(pin5 == true)
      server.print("<tr><td style=\"color: red;background: #99CC33;\" valign=\"middle\" >PIN 5 ON</td><td>");
    else
      server.print("<tr><td style=\"color: black;\" valign=\"middle\" >PIN 5 OFF</td><td>");
 
    if(pin5 == false)
      server.print("<input type=\"button\" value=\"Accendi\" onclick=\"location.href='index.htm?pin5=ON'\">");
    else
      server.print("<input type=\"button\" value=\"Spegni\" onclick=\"location.href='index.htm?pin5=OFF'\">");
     
      
    server.print("</td></tr>");

    server.print("</td></tr>");

    server.print("</table></p></body></html>");

  }
}





void setup()
{
  //inizializzo l'ethernet shield con il mac e il address
  Ethernet.begin(mac, ip);
  webserver.setDefaultCommand(&Start);
  webserver.addCommand("index.htm", &Start);

  //avvia il server web
  webserver.begin();
  delay(100);
  
  
    
  for(int i = 0; i < 8; i++){        
      pinMode(ledPins[i],OUTPUT); 
  }                                  
 
 
  pinMode(ledPins[0],OUTPUT);
  pinMode(ledPins[1],OUTPUT);
  pinMode(ledPins[2],OUTPUT);
  pinMode(ledPins[3],OUTPUT);
  pinMode(ledPins[4],OUTPUT);
  pinMode(ledPins[5],OUTPUT);
  pinMode(ledPins[6],OUTPUT);
  pinMode(ledPins[7],OUTPUT);
  (end of commented code)*/
}



void loop()
{
  webserver.processConnection();
}

Sto riscrivendo lo sketch ma ora sono fermo ad unn epic foul!! :smiley: :smiley:
Aiuto!!

@ale, forse non hai capito, scrivere aiuto non serve a nulla.
Aiuto su cosa? Cosa non ti funziona? Mica abbiamo la palla di vetro e vediamo cosa succede a casa tua ]:smiley:

  1. cosa vuol dire questo? (end of commented code)*/
    di sicuro il compilatore darà problemi visto che manca l'apertura dei commenti

2.inizializzare

boolean pin1, pin2, pin3, pin4, pin5, pin6, pin7, pin0 = false;

non basta l'ultimo, devi farlo per tutti

boolean pin1=false, pin2=false, pin3=false, pin4=false, pin5=false, pin6=false, pin7=false, pin0 = false;

comunque inutile, le booleane globali sono inizializzate a false

Questa sinceramente non la capisco:

    if(pin1 == true)
      server.print("<tr><td style=\"color: red;background: #99CC33;\" valign=\"middle\" >PIN 1 ON</td><td>");
    else
      server.print("<tr><td style=\"color: black;\" valign=\"middle\" >PIN 1 OFF</td><td>");
    if(pin1 == false)
      server.print("<input type=\"button\" value=\"Accendi\" onclick=\"location.href='index.htm?pin1=ON'\">");
    else
      server.print("<input type=\"button\" value=\"Spegni\" onclick=\"location.href='index.htm?pin1=OFF'\">");

Se pin1 è vero nell'else è falso, perciò:

if(pin1 == true)
{     server.print("<tr><td style=\"color: red;background: #99CC33;\" valign=\"middle\" >PIN 1 ON</td><td>");
      server.print("<input type=\"button\" value=\"Spegni\" onclick=\"location.href='index.htm?pin1=OFF'\">");
}else
{     server.print("<tr><td style=\"color: black;\" valign=\"middle\" >PIN 1 OFF</td><td>");
      server.print("<input type=\"button\" value=\"Accendi\" onclick=\"location.href='index.htm?pin1=ON'\">");
}

Bene
ho apportato le modifiche!!
il problema è che se uso solo i primi 2 pin tutto va bene!
Poi quando inserisco il code per il terzo la pagina browser si blocca!!

Può essere un problema memoria RAM. Arduino uno ha solo 2K di ram per i dati.
Prova a usare F() per tutte le print del codice html:
da così:

server.print("<tr><td style=\"color: red;background: #99CC33;\" valign=\"middle\" >PIN 1 ON</td><td>");

a così:

server.print(F("<tr><td style=\"color: red;background: #99CC33;\" valign=\"middle\" >PIN 1 ON</td><td>"));

Con la F() questi testi occuperanno memoria assieme allo sketch (nei 32K di flash) non occupando la ram.
Vedi questo, a metà pagina "Using Flash Memory for string storage":
http://playground.arduino.cc/Main/Printf

Grande!!
Non ne ero a conoscenza!!
Grazie mille,e grazie al tuo intuito!!
altro che palla di vetro!!