EthernetShield + relay Fehlersuche

Hallo zusammen

Ich bin nun endlich mal dazu gekommen alles zusammen zu bauen Gehäuse Steckdosen usw..
Nun habe ich folgendes Problem:
Wenn ich mehr als zwei Relay schalten will geht gar nichts mehr, endweder ich bekomme im Browser nur Unfug angezeigt oder
er lädt die Website des Ethernetshields gar nicht erst.

Mein Code:

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

int outputPin1 = 4; // Relay SchaltPIN 1
int outputPin2 = 5; // Relay SchaltPIN 2
int outputPin3 = 6; // Relay SchaltPIN 3
int outputPin4 = 7; // Relay SchaltPIN 4

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24}; // Ethernet Shield MAC Adresse                                                         
static uint8_t ip[4] = {192, 168, 1, 20}; // Webserver IP Adresse                
static uint16_t port = 80; // Webserver Port                                   

int statuspin1 = 0;
int statuspin2 = 0;
int statuspin3 = 0;
int statuspin4 = 0;

ETHER_28J60 e;

void setup()
{ 
  e.setup(mac, ip, port);
  pinMode(outputPin1, OUTPUT);
  pinMode(outputPin2, OUTPUT);
  pinMode(outputPin3, OUTPUT);
  /* pinMode(outputPin4, OUTPUT);*/

}

void loop()
{
  
 char* params;
  if (params = e.serviceRequest())
  {
// Dose 1

    e.print("<table><tr><td>Steckdose 1</td>");

    if (strcmp(params, "?1=on") == 0)
    {
      digitalWrite(outputPin1, HIGH);
    }
    if (strcmp(params, "?1=off") == 0)
    {
      digitalWrite(outputPin1, LOW);
    }
    
statuspin1 = digitalRead(outputPin1);
if (statuspin1 == true)
{
  e.print("<td><font color=green>aktiv</font></td><td><A HREF='?1=off'>Aus</A></td>");
}
else
{
  e.print("<td><font color=red>inaktiv</font></td><td><A HREF='?1=on'>An</A></td>");
}
    
// Dose 2 
    e.print("<tr><td>Steckdose 2</td>");
 
    if (strcmp(params, "?2=on") == 0)
    {
      digitalWrite(outputPin2, HIGH);
    }
    if (strcmp(params, "?2=off") == 0)
    {
      digitalWrite(outputPin2, LOW);
    }
 
statuspin2 = digitalRead(outputPin2);
if (statuspin2 == true)
{
  e.print("<td><font color=green>aktiv</font></td><td><A HREF='?2=off'>Aus</A></td>");
}
else
{
  e.print("<td><font color=red>inaktiv</font></td><td><A HREF='?2=on'>An</A></td>");
}

// Dose 3 
    e.print("<table><tr><td>Steckdose 3</td>");
 
    if (strcmp(params, "?3=on") == 0)
    {
      digitalWrite(outputPin3, HIGH);
    }
    if (strcmp(params, "?3=off") == 0)
    {
      digitalWrite(outputPin3, LOW);
    }
 
statuspin3 = digitalRead(outputPin3);
if (statuspin3 == true)
{
  e.print("<td><font color=green>aktiv</font></td><td><A HREF='?3=off'>Aus</A></td>");
}
else
{
  e.print("<td><font color=red>inaktiv</font></td><td><A HREF='?3=on'>An</A></td>");
}
/*
// Dose 4 
    e.print("<tr><td><b>Steckdose 4</b></td>");
 
    if (strcmp(params, "?cmd4=on") == 0)
    {
      digitalWrite(outputPin4, HIGH);
    }
    if (strcmp(params, "?cmd4=off") == 0)
    {
      digitalWrite(outputPin4, LOW);
    }
 
statuspin4 = digitalRead(outputPin4);
if (statuspin4 == true)
{
  e.print("<td><font color=green>aktiv</font></td>");
   e.print("<td><A HREF='?cmd4=off'>Ausschalten</A></td>");
}
else
{
  e.print("<td><font color=red>inaktiv</font></td>");
  e.print("<td><A HREF='?cmd4=on'>Anschalten</A></td>");
}
*/ 
    e.respond();
  }  
}

Sind nur Dose 1 und 2 im Script sprich ich kommentiere alles was Dose 3 und 4 betrifft aus funktioniert es super.
Kann es sein das das zuviel Code ist? Beim uploaden bekomme ich Binary sketch size: 5500 bytes (of a 14336 byte maximum)
Sollte also IO sein, ist ein selbstgelöteter Arduino mit einem Atmega 168
http://zyrusthc.homeip.net/fotoarchiv/displayimage.php?album=18&pid=434#top_display_media
http://zyrusthc.homeip.net/fotoarchiv/displayimage.php?album=18&pid=430#top_display_media

Wo kann ich den Fehler suchen oder woran liegt es das er nicht mehr als 2 Dosen schalten will?

Greeez Oli

Habe soeben noch diesen Code gefunden der mit meinen Shield funktioniert, aber ich habe keine Ahnung wie ich den Umschreibe das ich 4 Buttons habe die Pin 4-7 schalten. Vielleicht kann mir jemand den Weg weisen.

#include "etherShield.h"


// please modify the following two lines. mac and ip have to be unique
// in your local area network. You can not have the same numbers in
// two devices:
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24}; 
static uint8_t myip[4] = {192,168,1,20};
static char baseurl[]="http://192.168.1.20/";
static uint16_t mywwwport =80; // listen port for tcp/www (max range 1-254)



#define BUFFER_SIZE 500
static uint8_t buf[BUFFER_SIZE+1];
#define STR_BUFFER_SIZE 22
static char strbuf[STR_BUFFER_SIZE+1];

EtherShield es=EtherShield();

// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf, byte on_off);
int8_t analyse_cmd(char *str);

// LED cathode connects the Pin4, anode to 5V through 1K resistor
#define LED_PIN  4


void setup(){
  
   /*initialize enc28j60*/
	 es.ES_enc28j60Init(mymac);
   es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
   delay(10);
        
	/* Magjack leds configuration, see enc28j60 datasheet, page 11 */
	// LEDA=greed LEDB=yellow
	//
	// 0x880 is PHLCON LEDB=on, LEDA=on
	// enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
	es.ES_enc28j60PhyWrite(PHLCON,0x880);
	delay(500);
	//
	// 0x990 is PHLCON LEDB=off, LEDA=off
	// enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
	es.ES_enc28j60PhyWrite(PHLCON,0x990);
	delay(500);
	//
	// 0x880 is PHLCON LEDB=on, LEDA=on
	// enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
	es.ES_enc28j60PhyWrite(PHLCON,0x880);
	delay(500);
	//
	// 0x990 is PHLCON LEDB=off, LEDA=off
	// enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
	es.ES_enc28j60PhyWrite(PHLCON,0x990);
	delay(500);
	//
  // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
  // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
  es.ES_enc28j60PhyWrite(PHLCON,0x476);
	delay(100);
        
  //init the ethernet/ip layer:
  es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
  
 	pinMode(LED_PIN, OUTPUT); 
 	digitalWrite(LED_PIN, LOW);  // switch on LED
}

void loop(){
  uint16_t plen, dat_p;
  int8_t cmd;
  byte on_off = 1;

  plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);

	/*plen will ne unequal to zero if there is a valid packet (without crc error) */
  if(plen!=0){
	           
    // arp is broadcast if unknown but a host may also verify the mac address by sending it to a unicast address.
    if(es.ES_eth_type_is_arp_and_my_ip(buf,plen)){
      es.ES_make_arp_answer_from_request(buf);
      return;
    }

    // check if ip packets are for us:
    if(es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0){
      return;
    }
    
    if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
      es.ES_make_echo_reply_from_request(buf,plen);
      return;
    }
    
    // tcp port www start, compare only the lower byte
    if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==mywwwport){
      if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){
         es.ES_make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn does already send the syn,ack
         return;     
      }
      if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){
        es.ES_init_len_info(buf); // init some data structures
        dat_p=es.ES_get_tcp_data_pointer();
        if (dat_p==0){ // we can possibly have no data, just ack:
          if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){
            es.ES_make_tcp_ack_from_any(buf);
          }
          return;
        }
        if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
          	// head, post and other methods for possible status codes see:
            // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
            plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>"));
            goto SENDTCP;
        }
 	if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
                plen=print_webpage(buf, on_off);
            goto SENDTCP;
         }
        cmd=analyse_cmd((char *)&(buf[dat_p+5]));
        
        if (cmd==2){
                on_off=1;
        	digitalWrite(LED_PIN, LOW);  // switch on LED
        }
        else if (cmd==3){
                on_off=0;
        	digitalWrite(LED_PIN, HIGH);  // switch off LED
        }
        plen=print_webpage(buf, on_off);
        	
        	   plen=print_webpage(buf, on_off);
SENDTCP:  es.ES_make_tcp_ack_from_any(buf); // send ack for http get
           es.ES_make_tcp_ack_with_data(buf,plen); // send data       
      }
    }
  }
        
}
// The returned value is stored in the global var strbuf
uint8_t find_key_val(char *str,char *key)
{
        uint8_t found=0;
        uint8_t i=0;
        char *kp;
        kp=key;
        while(*str &&  *str!=' ' && found==0){
                if (*str == *kp){
                        kp++;
                        if (*kp == '\0'){
                                str++;
                                kp=key;
                                if (*str == '='){
                                        found=1;
                                }
                        }
                }else{
                        kp=key;
                }
                str++;
        }
        if (found==1){
                // copy the value to a buffer and terminate it with '\0'
                while(*str &&  *str!=' ' && *str!='&' && i<STR_BUFFER_SIZE){
                        strbuf[i]=*str;
                        i++;
                        str++;
                }
                strbuf[i]='\0';
        }
        return(found);
}

int8_t analyse_cmd(char *str)
{
        int8_t r=-1;
     
        if (find_key_val(str,"cmd")){
                if (*strbuf < 0x3a && *strbuf > 0x2f){
                        // is a ASCII number, return it
                        r=(*strbuf-0x30);
                }
        }
        return r;
}


uint16_t print_webpage(uint8_t *buf, byte on_off)
{

       int i=0;
    
        
        uint16_t plen;
        
 
        
        plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<center><p><h1>Welcome to Arduino Ethernet Shield V1.0  </h1></p> "));
         
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<hr>
<form METHOD=get action=\""));
        plen=es.ES_fill_tcp_data(buf,plen,baseurl);
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h2> REMOTE LED is  </h2> "));
 				plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h1><font color=\"#00FF00\"> "));
         
        if(on_off)
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("ON"));
        else 
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("OFF"));
        
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("  </font></h1>
 ") );
        
        if(on_off){
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=3>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Switch off\"></form>"));
        }
        else {
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=2>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Switch on\"></form>"));
        }
        
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</center><hr> <p> V1.0 <a href=\"http://www.nuelectronics.com\">www.nuelectronics.com<a>"));
  
        return(plen);
}

Bist Du sicher daß das Problem nicht die Stromversorgung ist, weil die Relais zuviel Strom brauchen?
Wies sieht die Stromversorgung aus?

Grüße Uwe

Stromversorgung ist ein externes Netzteil 5V.
Habe es damit getestet alle Relays einzuschalten und die Stromversorgung ist ausreichend:

void setup() {                
pinMode(4, OUTPUT);
pinMode(5, OUTPUT); 
pinMode(6, OUTPUT); 
pinMode(7, OUTPUT); 
}

void loop() {
  digitalWrite(4, HIGH);   
  digitalWrite(5, HIGH);   
  digitalWrite(6, HIGH);   
  digitalWrite(7, HIGH);   
  delay(2000);              
  digitalWrite(4, LOW);    
  digitalWrite(5, LOW);    
  digitalWrite(6, LOW);    
  digitalWrite(7, LOW);    
  delay(2000);            
}

Das Problem scheint wohl anders zu liegen, denn selbst wenn ich wie gesagt nur 2 nehme und ein paar Byte mehr reinbringe sprich etwas HTML Code zum Layout tritt das selbe Problem auf.

Deswegen währe schön wenn jemand wüsste wie ich den Alternativ Code den ich gepostet habe auf 4 Buttons erweitere.

Greeez Oli

Es könnte ein RAm-Problem sein, daß der Code zuviel RAM verbraucht.
Hast Du einen MEGA zur Verfügung und damit den sketche versucht?
Grüße Uwe

Nein leider nicht. Habe nur noch einen normalen Seeduino werde es gleich mal testen.

So getestet , selbes Problem mit dem Seeduino, ist ja auch blos ein 168er drauf.
Wie könnte ich das RAM Problem lösen? Mit einem 328er?

Habe es jetzt vorerst gelöst, musste allerdings nun auf jegliches HTML Layout verzichten und bin so auf 5412 Bytes runter gekommen,
Und damit funktionieren alles Dosen und ich hab im Webinterface einen Status:

Hier der Code:

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

int outputPin1 = 4; // Relay SchaltPIN 1
int outputPin2 = 5; // Relay SchaltPIN 2
int outputPin3 = 6; // Relay SchaltPIN 3
int outputPin4 = 7; // Relay SchaltPIN 4

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24}; // Ethernet Shield MAC Adresse                                                         
static uint8_t ip[4] = {192, 168, 1, 20}; // Webserver IP Adresse                
static uint16_t port = 80; // Webserver Port                                   


int statuspin1 = 0;
int statuspin2 = 0;
int statuspin3 = 0;
int statuspin4 = 0;


ETHER_28J60 e;

void setup()
{ 
  e.setup(mac, ip, port);
  pinMode(outputPin1, OUTPUT);
  pinMode(outputPin2, OUTPUT);
  pinMode(outputPin3, OUTPUT);
  pinMode(outputPin4, OUTPUT);
}

void loop()
{
 char* params;
  if (params = e.serviceRequest())
  {
    e.print("Dose 1 ");
    if (strcmp(params, "?1=on") == 0)    {      digitalWrite(outputPin1, HIGH);    }
    if (strcmp(params, "?1=off") == 0)    {      digitalWrite(outputPin1, LOW);    }
    statuspin1 = digitalRead(outputPin1);
    if (statuspin1 == true) {  e.print("aktiv <A HREF='?1=off'>Aus</A>
"); }
    else {  e.print("inaktiv <A HREF='?1=on'>An</A>
");}
     
    e.print("Dose 2 ");
    if (strcmp(params, "?2=on") == 0)    {      digitalWrite(outputPin2, HIGH);    }
    if (strcmp(params, "?2=off") == 0)    {      digitalWrite(outputPin2, LOW);    }
    statuspin2 = digitalRead(outputPin2);
    if (statuspin2 == true) {  e.print("aktiv <A HREF='?2=off'>Aus</A>
"); }
    else {  e.print("inaktiv <A HREF='?2=on'>An</A>
");}

    e.print("Dose 3 ");
    if (strcmp(params, "?3=on") == 0)    {      digitalWrite(outputPin3, HIGH);    }
    if (strcmp(params, "?3=off") == 0)    {      digitalWrite(outputPin3, LOW);    }
    statuspin3 = digitalRead(outputPin3);
    if (statuspin3 == true) {  e.print("aktiv <A HREF='?3=off'>Aus</A>
"); }
    else {  e.print("inaktiv <A HREF='?3=on'>An</A>
");}
    
    e.print("Dose 4 ");
    if (strcmp(params, "?4=on") == 0)    {      digitalWrite(outputPin4, HIGH);    }
    if (strcmp(params, "?4=off") == 0)    {      digitalWrite(outputPin4, LOW);    }
    statuspin4 = digitalRead(outputPin4);
    if (statuspin4 == true) {  e.print("aktiv <A HREF='?4=off'>Aus</A>"); }
    else {  e.print("inaktiv <A HREF='?4=on'>An</A>");}

    e.respond();
  }  
}

Alternativorschläge sind natürlich Willkommen.

Greeez Oli

Zyrusthc:
So getestet , selbes Problem mit dem Seeduino, ist ja auch blos ein 168er drauf.
Wie könnte ich das RAM Problem lösen? Mit einem 328er?

Der ATmega328 hat das doppelte an RAM wie der ATmega186.
Mit dem 328 könnte der Kode funktionieren.
Grüße Uwe

Nachdem ich nun ein paar 328er bestellt habe, hab ich eben mal das ganze mit einen 328er getestet. Leider das selbe Problem. Ich vermute mal es liegt an der ETHER_28J60.h und etherShield.h, das diese nicht für solch "grosse" Sketches gedacht sind.
Hmmmm, Schade das es nicht geht.

Mit dem anderen Sketch läuft ja das Projekt soweit.
Hier ein paar Pics für Interessierte:
http://zyrusthc.homeip.net/fotoarchiv/thumbnails.php?album=27

Dein Problem ist vermutlich folgendes. Solange die Antwort auf den Webrequest in ein einziges Antwortpaket passt, ist alles in Ordnung. Die Ethernetlib für den 28J60 kennt keine "normale" TCP-Verbindung, da der erforderliche Zähler für die Nummerierung der einzelnen Pakete nicht implementiert ist. Die normale MTU (Maximum Transmission Unit – Wikipedia) sind 1500 Byte, davon gehen aber auch einige für den IP und TCP Header verloren.
Versuchst Du mehr als (ca.) 1460 Bytes zu verschicken, müßtest Du 2 Pakete verschicken, dafür fehlt aber der passende Code in der Lib der das TCP-Protokol vollständig unterstützt.

Falls es aber ein RAM Problem ist, würde ich mal folgendes empfehlen:

http://arduiniana.org/libraries/flash/
Mit Hilfe der Flash-Bibliothek kannst Du den größten Teil der HTML-Ausgaben in den Programmspeicher des Arduino packen und es muss dafür nicht der SRAM herhalten. Das hat bei meinem Projekt auch recht gut geklappt.
Das Schreiben in die TCP-Verbindung klappt auch recht einfach:

void showHelp(Stream &client)
{
  
  FLASH_STRING(help,
  "\nArduino WoL Server.\n\n"
  "(c) 2011 - by Mario Keller\n"
  "\nsupported commands\n"
  "  ? | help           - print this help message\n"
  "  show run           - shows current config\n"
  "  wr                 - write current config to eeprom\n"
  "  rr                 - read config to eeprom (overwrites current config)\n"
  "  exit               - disconnects from WoL Server\n"
  "  setip <ip>         - set IP address\n"
  "  setgw <gw>         - set gateway\n"
  "  setnm <nm>         - set netmask\n"
  "  setbc <bc>         - set broadcast address\n"
  "  setmac <id> <mac>  - set WoL MAC for ID 0 to 7\n"
  "  enable <id>        - enable WoL for ID\n"
  "  disable <id>       - disable WoL for ID\n"
  "  retry <num>        - number of WoL packages sent for every MAC\n"
  "  delay <sec>        - number of seconds between WoL packages\n\n");
  help.print(client);
}