miercuri, 25 martie 2015

Placa de retea cu ENC28J60 si Arduino Mega

   In articolul precedent numit Placa de retea cu ENC28J60 si Arduino am prezentat pe scurt cum am conectat eu placa de retea la unArduino Uno... avandu-l ocupat pentru "teste de anduranta", am conectat o alta placa cu ENC28J60 la o placa Arduino Mega si incarcand sketch-ul am avut surpriza sa constat ca nu functioneaza.. chiar daca am schimbat legaturile pe pinii dedicati: MISO la pin 50, MOSI la pin 51, SCK la 52 si SS/CS la 53, cum e prezentat si in articolul How to use enc28j60 ethernet shield with Arduino Mega 2560
   Acolo se prezinta o modificare, pentru fortarea pinului CS/SS la pin 53.
1
2
3
4
5
6
7
8
9
10
...
if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
  {
    Serial.println(P("Failed to access Ethernet controller"));
  }
  else
  {
     Serial.println(P("Ethernet is fine and up"));
  }
...
   Pentru a functiona, in loc de litera P trebuie pus F.
   De asemenea, si in libraria Ethercard se mentioneaza acest lucru si prezinta comanda de trecerea a pinului 8 in 53:
VCC -   3.3V
GND -    GND
SCK - Pin 52
SO  - Pin 50
SI  - Pin 51
CS  - Pin 53 # Selectable with the ether.begin() function
# The default CS pin defaults to 8, so you have to set it on a mega:
ether.begin(sizeof Ethernet::buffer, mymac, 53)
   Din pacate, la teste nu reuseam sa-l fac functional si renuntasem... dar Luci, un coleg mai rabdator, a cautat si el pe net si a dat de filmuletul Arduino EthernetShield ENC28J60 Configuration - Parte1
   Inlocuind partea de mutare a comenzii de pe pinul 8 (de la Arduino Uno) pe 53 (la Arduino Mega), sketch-ul din articolul precedent arata asa:
//original sketch from http://www.lucadentella.it/2012/11/10/enc28j60-e-arduino-9/
// adapted sketch from http://nicuflorica.blogspot.ro/
#include <EtherCard.h>
#include <avr/wdt.h>
#define RELAY_PIN 2

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,0,199 };

byte Ethernet::buffer[500];
BufferFiller bfill;

char* on  = "ON";
char* off = "OFF";

boolean relayStatus;
char* relayLabel;
char* linkLabel;

void setup () {
 
 Serial.begin(9600);
 // 1st good version from https://www.youtube.com/watch?v=d2toibPesS4
/* if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
    Serial.println(F("Failed to access Ethernet controller"));  
*/

 // 2nd good version from http://en.code-bude.net/2013/06/22/how-to-use-enc28j60-ethernet-shield-with-arduino-mega-2560/ 
if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
  {
    Serial.println(F("Failed to access Ethernet controller"));
  }
  else
  {
     Serial.println(F("Ethernet is fine and up"));
  }

ether.staticSetup(myip);  // put static IP in local network
 
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);  
  relayStatus = false;
  relayLabel = off;
  linkLabel = on;
  
  wdt_enable(WDTO_1S);//This start the watchdog timer with a 1 second timeout
}
  
void loop() {
wdt_reset();//This resets the timer

long t = millis() / 1000;
  word h = t / 3600;
  byte m = (t / 60) % 60;
  byte s = t % 60; 

  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  
  if(pos) {
        
    if(strstr((char *)Ethernet::buffer + pos, "GET /?ON") != 0) {
      relayStatus = true;
      relayLabel = on;
      linkLabel = off;
    } else if(strstr((char *)Ethernet::buffer + pos, "GET /?OFF") != 0) {
      relayStatus = false;
      relayLabel = off;
      linkLabel = on;
    }
    digitalWrite(RELAY_PIN, relayStatus); 
  
    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"
    // from http://fabiotomio.com.br/blog/2014/05/07/arduino-modulo-ethernet-enc28j60/
 /*     "Content-Type: text/html\r\n"
      "Refresh: 5\r\n"
      "\r\n"
  */    
     
      "<html><head><meta name='viewport' content='width=200px'/></head><body>"
      "<div style='position:absolute;width:200px;height:200px;top:50%;left:50%;margin:-100px 0 0 -100px'>"
      "<div style='font:bold 14px verdana;text-align:center'>Relay is $S</div>"
      "<br><div style='text-align:center'>"
      "<a href='/?$S'><img src='http://www.lucadentella.it/files/bt_$S.png'></a>"
      ), relayLabel, linkLabel, linkLabel);
bfill.emit_p(PSTR("<p>"
"<h2>$D$D:$D$D:$D$D</h2>"),
h/10, h%10, m/10, m%10, s/10, s%10);
 bfill.emit_p(PSTR("</body></html>"));   

      ether.httpServerReply(bfill.position());
    }
}
// note: for watchdog see https://hackaday.io/project/3731-open-z3zzvw3/log/12362-the-brain-of-the-thingy
   In sketch am lasat si varianta din film, doar ca "comentata"...

   La adresa locala data de mine, 192.168.0.199 gasim 2 situatii:
 
   PS: Pentru a nu avea probleme, trebuie impus IP-ul prin comanda ether.staticSetup(myip); dupa cum se vede in imaginea urmatoare:
   PS2: Dupa cateva ore de teste, am observat ca este stabila pagina doar daca se trimit comenzi din reteaua locala, prin cablu, in cazul folosiri unui periferic (laptop, telefon, tableta) conectat prin "uairles" atunci ea se blocheaza. Solutia gasita de mine pentru a elimia aceasta ploblerma, este acea de a folosi pinul 8, ca in cazul placii Arduino Uno si renuntare ala "portarea" pinului 8 la 53...

   In montaj eu am mai pus si pinul de RESET...
  Sketch-ul devine simplu:
//original sketch from http://www.lucadentella.it/2012/11/10/enc28j60-e-arduino-9/
// adapted sketch by niq_ro from http://nicuflorica.blogspot.ro/
// direct link: http://nicuflorica.blogspot.ro/2015/03/placa-de-retea-cu-enc28j60-si-arduino_25.html
#include <EtherCard.h>
#include <avr/wdt.h> // for watchdog see https://hackaday.io/project/3731-open-z3zzvw3/log/12362-the-brain-of-the-thingy
#define RELAY_PIN 2

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,0,199 };
static byte gwip[] = {192,168,0,1};

byte Ethernet::buffer[500];
BufferFiller bfill;

char* on  = "ON";
char* off = "OFF";

boolean relayStatus;
char* relayLabel;
char* linkLabel;

void setup () {
 Serial.begin(9600);
 if(ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
  else
    Serial.println("Ethernet controller initialized");
 if (!ether.dhcpSetup())
    Serial.println("Failed to get configuration from DHCP");
  if(!ether.staticSetup(myip))
    Serial.println("Failed to set IP address");
 ether.printIp("My IP: ", ether.myip);
//   ether.printIp("Netmask: ", ether.mymask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);

  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);  
  relayStatus = false;
  relayLabel = off;
  linkLabel = on;
  
  wdt_enable(WDTO_1S);//This start the watchdog timer with a 1 second timeout
}
  
void loop() {
wdt_reset();//This resets the timer

long t = millis() / 1000;
  word h = t / 3600;
  byte m = (t / 60) % 60;
  byte s = t % 60; 

  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  
  if(pos) {
        
    if(strstr((char *)Ethernet::buffer + pos, "GET /?ON") != 0) {
      relayStatus = true;
      relayLabel = on;
      linkLabel = off;
    } else if(strstr((char *)Ethernet::buffer + pos, "GET /?OFF") != 0) {
      relayStatus = false;
      relayLabel = off;
      linkLabel = on;
    }
    digitalWrite(RELAY_PIN, relayStatus); 
  
    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"
    // from http://fabiotomio.com.br/blog/2014/05/07/arduino-modulo-ethernet-enc28j60/
 /*     "Content-Type: text/html\r\n"
      "Refresh: 5\r\n"
      "\r\n"
  */    
      "<html><head><meta name='viewport' content='width=200px'/></head><body>"
      "<div style='position:absolute;width:200px;height:200px;top:50%;left:50%;margin:-100px 0 0 -100px'>"
      "<div style='font:bold 14px verdana;text-align:center'>Relay is $S</div>"
      "<br><div style='text-align:center'>"
      "<a href='/?$S'><img src='http://www.lucadentella.it/files/bt_$S.png'></a>"
      ), relayLabel, linkLabel, linkLabel);
bfill.emit_p(PSTR("<p>"
"<h2>$D$D:$D$D:$D$D</h2>"),
h/10, h%10, m/10, m%10, s/10, s%10);
 bfill.emit_p(PSTR("</body></html>"));   

      ether.httpServerReply(bfill.position());
    }
}

Niciun comentariu:

Trimiteți un comentariu