# sketch per board ethernet relè

**URL:** <https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424>\
**Category:** Generale\
**Created:** [August 2, 2013, 2:25pm UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424 "2013-08-02T14:25:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 2, 2013, 2:25pm UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/1 "2013-08-02T14:25:36Z")

</div>

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!! 😃 😃  
Qualcuno può darmi una mano?  
Grazie

```cpp
/*
  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** _

---

<div class="post-metadata">

**Author:** ![ratto](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ratto/32/214060_2.png) [@ratto](https://forum.arduino.cc/u/ratto)\
**Post date:** [August 2, 2013, 2:39pm UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/2 "2013-08-02T14:39:32Z")

</div>

cosa vuol dire che non funziona ? cos'è che non funziona ? 😉

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 2, 2013, 2:43pm UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/3 "2013-08-02T14:43:35Z")

</div>

Non riesco ad entrare dalla pagina browser!

---

<div class="post-metadata">

**Author:** ![PaoloP](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paolop/32/214739_2.png) [@PaoloP](https://forum.arduino.cc/u/PaoloP)\
**Post date:** [August 2, 2013, 3:34pm UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/4 "2013-08-02T15:34:39Z")

</div>

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](http://forum.arduino.cc/index.php?topic=149082.0))

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 2, 2013, 3:47pm UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/5 "2013-08-02T15:47:29Z")

</div>

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](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!! 😃 😃

---

<div class="post-metadata">

**Author:** ![PaoloP](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paolop/32/214739_2.png) [@PaoloP](https://forum.arduino.cc/u/PaoloP)\
**Post date:** [August 2, 2013, 5:31pm UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/6 "2013-08-02T17:31:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 3, 2013, 10:52am UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/7 "2013-08-03T10:52:26Z")

</div>

```cpp
//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!! 😃 😃  
Aiuto!!

---

<div class="post-metadata">

**Author:** ![nid69ita](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nid69ita/32/901258_2.png) [@nid69ita](https://forum.arduino.cc/u/nid69ita)\
**Post date:** [August 3, 2013, 11:16am UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/8 "2013-08-03T11:16:34Z")

</div>

@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 ]😃

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

2.inizializzare

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

```

non basta l'ultimo, devi farlo per tutti

```cpp
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

---

<div class="post-metadata">

**Author:** ![nid69ita](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nid69ita/32/901258_2.png) [@nid69ita](https://forum.arduino.cc/u/nid69ita)\
**Post date:** [August 3, 2013, 11:24am UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/9 "2013-08-03T11:24:57Z")

</div>

Questa sinceramente non la capisco:

```cpp
    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ò:

```cpp
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'\">");
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 3, 2013, 12:04pm UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/10 "2013-08-03T12:04:02Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![nid69ita](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nid69ita/32/901258_2.png) [@nid69ita](https://forum.arduino.cc/u/nid69ita)\
**Post date:** [August 3, 2013, 1:14pm UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/11 "2013-08-03T13:14:46Z")

</div>

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ì:

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

```

a così:

```arduino
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](http://playground.arduino.cc/Main/Printf)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 3, 2013, 1:55pm UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/12 "2013-08-03T13:55:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 7, 2021, 11:33am UTC](https://forum.arduino.cc/t/sketch-per-board-ethernet-rele/176424/13 "2021-05-07T11:33:59Z")

</div>

Closed on May 7, 2021, 11:33am UTC


