Sketch per relè da 8 con enc28j60 problema ip

Ciao a tutti sono alle prime armi con Arduino, sto cercando di mettere su un mini progettino di domotica.
Sto usando arduino uno rev3 , la ethernet ENC28J60 e una scheda relè da 8 posti. Ho trovato questo sketch ma se lo modifico per 3 relè, mi perde la connessione e non si pinga più l'ip. Mi dareste qualche consiglio.
grazie
Maurizio

originale:

#include <EtherCard.h>

#define LED1PIN  2 //definisce il pin del LED 1
#define LED2PIN  3 //definisce il pin del LED 2

//settaggio dei valori statici
static byte mymac[] = {0x00,0x19,0xCB,0xF4,0x03,0x01};
static byte myip[] = {192,168,1,61};
static byte gwip[] = {192,168,1,1};
static byte netmask[] = {255,255,255,0};
static byte dnsip[] = {8,8,8,8};
byte Ethernet::buffer[700];

boolean led1Status;
boolean led2Status;

void setup () {
 
  Serial.begin(9600);
  Serial.println("2 WebRele' Ip Statico");
 
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Accesso fallito all'Ethernet Shield");
 else
   Serial.println("Ethernet Shield initializzato");
 
  if (!ether.staticSetup(myip, gwip, dnsip, netmask ))
    Serial.println("Impossibile assegnare l'indirizzo");
  else
    Serial.println("Indirizo statico configurato");
    
  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.netmask);
  ether.printIp("Gateway:\t", ether.gwip);
  ether.printIp("DNS:\t\t", ether.dnsip);
   Serial.println();
  
  pinMode(LED1PIN, OUTPUT);
  pinMode(LED2PIN, OUTPUT);
  
  digitalWrite(LED1PIN, LOW);
  digitalWrite(LED2PIN, LOW);
  
  led1Status = false;
  led2Status = false;  
}
  
void loop() {
 
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  
  if(pos) {
    
      if(strstr((char *)Ethernet::buffer + pos, "GET /?RELAY1") != 0) {
        led1Status = !led1Status;
        digitalWrite(LED1PIN, led1Status);
        Serial.println("Ricevuto comando per Rele' 1");  
      }
      
      if(strstr((char *)Ethernet::buffer + pos, "GET /?RELAY2") != 0) {
        led2Status = !led2Status;
        digitalWrite(LED2PIN, led2Status);
        Serial.println("Ricevuto comando per Rele' 2");        
      }
      
      BufferFiller bfill = ether.tcpOffset();
      bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
                        "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
                        "<html><head><meta name='viewport' content='width=200px'/></head><body>"
                        "<div style='position:absolute;width:200px;height:200px;top:1%;left:50%;margin:5px 0 0 -100px'>"
                        "<div style='font:bold 18px verdana;text-align:center'>Web Rele'</div>"
                        "
<div style='text-align:center'>"));

      if(led1Status) bfill.emit_p(PSTR("<a href=\"/?RELAY1\"><img src=\"http://www.byte4geek.com/images/arduino/butON.png\"></a>
Stato Rele' 1 OFF
"));
      else bfill.emit_p(PSTR("<a href=\"/?RELAY1\"><img src=\"http://www.byte4geek.com/images/arduino/butOFF.png\"></a>
Stato rele' 1 ON
"));
      
      if(led2Status) bfill.emit_p(PSTR("
<a href=\"/?RELAY2\"><img src=\"http://www.byte4geek.com/images/arduino/butON.png\"></a>
Stato Rele' 2 OFF"));
      else bfill.emit_p(PSTR("
<a href=\"/?RELAY2\"><img src=\"http://www.byte4geek.com/images/arduino/butOFF.png\"></a>
Stato rele' 2 ON"));      

      bfill.emit_p(PSTR("
<a href=\"/\">Controlla lo stato dei Rele'</a></div></div></body></html>"));
      ether.httpServerReply(bfill.position());
   }
  }

modificato

 #include <EtherCard.h>

#define LED1PIN  2 //definisce il pin del LED 1
#define LED2PIN  3 //definisce il pin del LED 2
#define LED3PIN  4 //definisce il pin del LED 3

//settaggio dei valori statici
static byte mymac[] = {0x00,0x19,0xCB,0xF4,0x03,0x01};
static byte myip[] = {192,168,1,120};
static byte gwip[] = {192,168,1,254};
static byte netmask[] = {255,255,255,0};
static byte dnsip[] = {8,8,8,8};
byte Ethernet::buffer[700];

boolean led1Status;
boolean led2Status;
boolean led3Status;

void setup () {
 
  Serial.begin(57600);
  Serial.println("2 WebRele' Ip Statico");
 
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Accesso fallito all'Ethernet Shield");
 else
   Serial.println("Ethernet Shield initializzato");
 
  if (!ether.staticSetup(myip, gwip, dnsip, netmask ))
    Serial.println("Impossibile assegnare l'indirizzo");
  else
    Serial.println("Indirizo statico configurato");
    
  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.netmask);
  ether.printIp("Gateway:\t", ether.gwip);
  ether.printIp("DNS:\t\t", ether.dnsip);
   Serial.println();
  
  pinMode(LED1PIN, OUTPUT);
  pinMode(LED2PIN, OUTPUT);
  pinMode(LED3PIN, OUTPUT);
  
  digitalWrite(LED1PIN, LOW);
  digitalWrite(LED2PIN, LOW);
  digitalWrite(LED3PIN, LOW);
  
  led1Status = false;
  led2Status = false;  
  led3Status = false;
}
  
void loop() {
 
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  
  if(pos) {
    
      if(strstr((char *)Ethernet::buffer + pos, "GET /?RELAY1") != 0) {
        led1Status = !led1Status;
        digitalWrite(LED1PIN, led1Status);
        Serial.println("Ricevuto comando per Rele' 1");  
      }
      
      if(strstr((char *)Ethernet::buffer + pos, "GET /?RELAY2") != 0) {
        led2Status = !led2Status;
        digitalWrite(LED2PIN, led2Status);
        Serial.println("Ricevuto comando per Rele' 2");        
      }
      
        if(strstr((char *)Ethernet::buffer + pos, "GET /?RELAY3") != 0) {
        led3Status = !led3Status;
        digitalWrite(LED3PIN, led3Status);
        Serial.println("Ricevuto comando per Rele' 3");        
      }
      
      BufferFiller bfill = ether.tcpOffset();
      bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
                        "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
                        "<html><head><meta name='viewport' content='width=200px'/></head><body>"
                        "<div style='position:absolute;width:200px;height:200px;top:1%;left:50%;margin:5px 0 0 -100px'>"
                        "<div style='font:bold 18px verdana;text-align:center'>Web Rele'</div>"
                        "
<div style='text-align:center'>"));

      if(led1Status) bfill.emit_p(PSTR("<a href=\"/?RELAY1\"><img src=\"http://www.byte4geek.com/images/arduino/butON.png\"></a>
Stato Rele' 1 OFF
"));
      else bfill.emit_p(PSTR("<a href=\"/?RELAY1\"><img src=\"http://www.byte4geek.com/images/arduino/butOFF.png\"></a>
Stato rele' 1 ON
"));
      
      if(led2Status) bfill.emit_p(PSTR("
<a href=\"/?RELAY2\"><img src=\"http://www.byte4geek.com/images/arduino/butON.png\"></a>
Stato Rele' 2 OFF"));
      else bfill.emit_p(PSTR("
<a href=\"/?RELAY2\"><img src=\"http://www.byte4geek.com/images/arduino/butOFF.png\"></a>
Stato rele' 2 ON"));  
  
      if(led3Status) bfill.emit_p(PSTR("
<a href=\"/?RELAY3\"><img src=\"http://www.byte4geek.com/images/arduino/butON.png\"></a>
Stato Rele' 3 OFF"));
      else bfill.emit_p(PSTR("
<a href=\"/?RELAY3\"><img src=\"http://www.byte4geek.com/images/arduino/butOFF.png\"></a>
Stato rele' 3 ON"));     

      bfill.emit_p(PSTR("
<a href=\"/\">Controlla lo stato dei Rele'</a></div></div></body></html>"));
      ether.httpServerReply(bfill.position());
   }
  }

Senza analizzare il codice dirrei che hai un problema di RAM overflow.

l' Atzmega ha solo 2kBte RAM e il ENC non fa la comunicazione da sola ma Arduino deve gesire anche la connessioen Ethernet.

Soluzione:

  1. se modifichi l'impianto elettrico perdi la certificazione
  2. la maggior parte di schede relé non é adatta per i 230VAC
  3. usa una scheda Ethernet col W5100
  4. usa la macro (F)

Ciao Uwe

Ciao grazie della risposta, ho usato anche la F ma non ho risolto va sempre in blocco. Dici che i relè non sono dimensionati per la 230VAC? nelle specifiche c'è scritto 230vac 10A. Se aggiungo un modulo sd card e faccio caricare la parte web da una pagina esterna risparmio ram? grazie

Ciao Maurizio,
sei riuscito nel tuo intento? Anche io ho trovato quel codice e anche io ho cercato di modificarlo per utilizzare otto relé, ma ho avuto lo stesso problema ovviamente.
Avevo già pensato di utilizzare la macro (F), senza risultati purtroppo.
Il problema è che non vorrei utilizzare Arduino Mega e W5100 per 4-8 relè, è possibile trovare una via di mezzo?
Antonio