Author Topic: Using a ENC28J60 Ethernet Adapter with a Moteino Mega [SOLVED]  (Read 3195 times)

luisr320

  • Sr. Member
  • ****
  • Posts: 255
  • Country: pt
Using a ENC28J60 Ethernet Adapter with a Moteino Mega [SOLVED]
« on: February 26, 2017, 08:46:23 PM »
Hi!
After a while away from the hobby I'm back on the seddle and my next project requires an Ethernet Gatway connected to a Moteino Mega.
But I just can't make the ENC28J60 work.
I'm connecting MISO to pin 6, SCK to pin 7, and MOSI to pin 5. The SS/CS is the problem here. I tried connecting it to several places and nothing work. I'm using Ethercard.h library and on this library there is a file called enc28j60.h where pin 8 is assumed to be the CS pin.
Code: [Select]
    /**   @brief  Initialise network interface
    *     @param  size Size of data buffer
    *     @param  macaddr Pointer to 6 byte hardware (MAC) address
    *     @param  csPin Arduino pin used for chip select (enable network interface SPI bus). Default = 8
    *     @return <i>uint8_t</i> ENC28J60 firmware version or zero on failure.
    */
    static uint8_t initialize (const uint16_t size, const uint8_t* macaddr,
                               uint8_t csPin = 1);
I tried to use pin 8, changed the code to use pin 1 and nothing makes the board work.

I know there are here those who managed to do this so I ask for some guidance on how to tackle this.

As for the code I'm using it 's nothing fancy:
Code: [Select]
#include <EtherCard.h>
#define STATIC 0                                //Para definir um IP estático mudar de 0 para 1
                                                            //To set a static IP change from 0 to 1
#if STATIC       
static byte myip[] = { 192,168,1,70 };   //Definição do IP estático/Setting the static IP
static byte gwip[] = { 192,168,1,1 };     //Endereço IP do Gateway da rede/Gateway IP address of the network
#endif
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; //*MAC Address
                                                                 //*MAC Address da interface ethernet - Tem de ser único na sua rede
                                                                 //*MAC Address of the Ethernet interface - must be unique in your network
byte Ethernet::buffer[500];

const char page[] PROGMEM =               //Página HTML/HTML page
   "HTTP/1.0 200 OK\r\n"
   "Content-Type: text/html\r\n"
   "Refresh: 5\r\n"
   "\r\n"
   "<html>"
   "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>"
   "   <head>"
   "      <title>Arduino WebServer</title>"
   "   </head>"
   "   <body>"
   "   <center>"
   "   <h1>Módulo Ethernet ENC28J60</h1>"
   "   <hr>"
   "      <h3>Página de resposta do nosso Arduino.</h3>"
   "      <h4>Para mais informações visite o blogue TecnoDomos.</h4>"
   "   <a href='http://www.tecnodomos.blogspot.com'>TecnoDomos</a>"
   "   <hr>"
   "   </center>"
   "   </body>"
   "</html>"
;
void setup(){
   Serial.begin(57600);
   Serial.println( "Modulo Ethernet ENC28J60/ENC28J60 Ethernet module");
   Serial.println("\n[webserver]");


   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
      Serial.println( "Falha ao aceder ao controlador Ethernet/Failed to access Ethernet controller");

      #if STATIC
          ether.staticSetup(myip, gwip);
      #else
          if (!ether.dhcpSetup())
              Serial.println("DHCP falhou / DHCP failed");
      #endif

   Serial.println("Parametros da rede / Network parameters");
   ether.printIp("IP: ", ether.myip);
   ether.printIp("GW: ", ether.gwip);
   ether.printIp("DNS: ", ether.dnsip);
}

void loop(){
   if (ether.packetLoop(ether.packetReceive())) {
      memcpy_P(ether.tcpOffset(), page, sizeof page);
      ether.httpServerReply(sizeof page - 1);
   }
}

It will get me this on the Serial monitor:
Code: [Select]
Modulo Ethernet ENC28J60/ENC28J60 Ethernet module

[webserver]
DHCP falhou / DHCP failed
Parametros da rede / Network parameters
IP: 0.0.0.0
GW: 0.0.0.0
DNS: 0.0.0.0

Thank you
Luis
« Last Edit: March 02, 2017, 07:56:49 AM by Felix »

syrinxtech

  • Sr. Member
  • ****
  • Posts: 347
  • Country: us
    • Syrinx Technologies
Re: [NEED HELP] Using a ENC28J60 Ethernet Adapter with a Moteino Mega
« Reply #1 on: February 27, 2017, 09:44:28 PM »
Luisr320,

I used the UIPEthernet library which allows you to easily change the CS pin.

Inside the folder, look in the "src" directory and then the "utility" directory.

In the file named "Enc28J60Network.h", change the following:

1)  #define ENC28J60_CONTROL_CS

syrinxtech

  • Sr. Member
  • ****
  • Posts: 347
  • Country: us
    • Syrinx Technologies
Re: [NEED HELP] Using a ENC28J60 Ethernet Adapter with a Moteino Mega
« Reply #2 on: February 27, 2017, 09:46:11 PM »
Luisr320,

I used the UIPEthernet library which allows you to easily change the CS pin.

Inside the folder, look in the "src" directory and then the "utility" directory.

In the file named "Enc28J60Network.h", change the following:

1)  #define ENC28J60_CONTROL_CS <x>

Where X = pin used for CS

2) #define SPI_SS  <x>

This should be the same value you used for #1

luisr320

  • Sr. Member
  • ****
  • Posts: 255
  • Country: pt
Re: [NEED HELP] Using a ENC28J60 Ethernet Adapter with a Moteino Mega
« Reply #3 on: March 01, 2017, 08:39:02 PM »
Thank you, syrinxtech, for pointing me in the right direction.
I am now able to use the Blynk app to send a command to a Moteino Mega that has a ENC28J60 attached to it, connected to my network. By pressing a Widget button on the Blink app, the Moteino Mega receives it, and sends the instruction out trough the radio to a Moteino node, which activates a relay. Tomorrow I'll try to make a switch on the Moteino node send its position all the way back to the Blink app, to a Widget Led, that with show its state.

I tried doing this with the ESP8266 modules, not requiring any Arduino to control it but they don't work very well when you need to cross two floors. This will be used to open and close a garage door for a friend project.

I'll post the whole project in the project section as soon as I have all the details figured out.

syrinxtech

  • Sr. Member
  • ****
  • Posts: 347
  • Country: us
    • Syrinx Technologies
Re: [SOLVED] Using a ENC28J60 Ethernet Adapter with a Moteino Mega
« Reply #4 on: March 02, 2017, 06:35:55 AM »
I'm glad you got it working.  Looking forward to seeing your project.

I used Blynk and an ESP8266 to control a large fan in our bedroom.  My wife can use the app to turn the fan on and off through the cell phone app.  Right now I'm only using one of the two plugs available in the housing, but at some point I could add another electrical plug and control both outlets through the phone.


luisr320

  • Sr. Member
  • ****
  • Posts: 255
  • Country: pt
[HELP] Using a ENC28J60 Ethernet Adapter with a Moteino Mega
« Reply #5 on: March 03, 2017, 08:51:06 PM »
I'm stuck. :-\

As I mentioned before, I wanted to have a button activated on the Node Moteino to change the status of a Virtual LED on the Blink APP.
But I'm having some problems with the SPI bus not wanting to share its resources between the RFM69 radio and the ENG28J60 Ethernet adapter. I have assigned a different CS/SS pin to the Ethernet adapter, and I can send data from the Gateway to the Node. But not the other way around.

By pressing a button I send a struct with my data from the node (a Moteino) to the gateway  (a Moteino Mega) via RFM69. I can do this 4 to 5 times but then the gateway hangs up and nothing else comes trough.
I'm suspecting that when the RFM69 gets some some data, it activates some interrupt that messes with the ENC28J60 interrupts.

I can spend the whole day doing the opposite without problems: I can send data from the gateway to the node by radio with no problems. But as soon as the gateway receives some data, it will crash and stop working, or the Ethernet adapter looses contact with the Blynk cloud server.

So my question is: is there a way to disable the RFM69 radio interrupts when the Ethernet adapter is using them?

Luis
« Last Edit: March 03, 2017, 08:55:17 PM by luisr320 »

luisr320

  • Sr. Member
  • ****
  • Posts: 255
  • Country: pt
Using a ENC28J60 Ethernet Adapter with a Moteino Mega [SOLVED]
« Reply #6 on: March 03, 2017, 09:45:38 PM »
Sometimes, when you have to clearly explain your problem to someone else, you end up answering to your own questions.
It seems that I was on the right track. It is a problem of interrupts. Looking on the internet for some guidance I ended up on the Arduino Playground and there it was:

interrupts()

Description

Re-enables interrupts (after they've been disabled by noInterrupts()). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code.

Example

void setup() {}

void loop()
{
  noInterrupts();
  // critical, time-sensitive code here
  interrupts();
  // other code here
}



Blynk only runs a simple function that is run over and over again in the loop: blynk(). This functions handles all the Ethernet traffic. So by disabling the interrupts before Blynk() and then enabling them again after Blynk(), no more crashes. I suppose that during this very short period of time that the interrupts are disabled that no traffic is received by the radio, but I can live with that.

But how is this possible? If I disable the interrupts, how can the ENC29J60 Ethernet adapter work with the SPI bus?

My test rig:


Blynk app:
« Last Edit: March 03, 2017, 09:57:40 PM by luisr320 »