robertnapoc
Posts: 5
Joined: Sat Dec 31, 2022 8:48 pm

ENC28J60 ethernet controller for Raspberry PI Pico

Sun Jan 01, 2023 12:28 pm

Hello. I am a beginner and I hope I could find some ideas/guidance on how to use this combination.

I wish to know how to use a Raspberry Pi Pico (the one without WiFi) with an ENC28J60 Ethernet controller. The reason for this combination is that those were the only available devices in my region and time of purchase.

My idea is to use the Pico as a receiver of data/commands over LAN, if possible through a socket protocol to turn on/off a relay for example, and possibly send a simple response back.

I've googled around to find some micro-python libraries and guides for something similar but did not got too far with this specific implementation.

I'm open to suggestions and if I should go with a different approach from sockets that can maybe achieve the same result, or if I should look into C language instead of micro-python. Or maybe if there are similar implementations with Arduino that I can adapt for RPi Pico to work with ENC28J60.

Thank you for your time.

hippy
Posts: 14447
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ENC28J60 ethernet controller for Raspberry PI Pico

Sun Jan 01, 2023 3:53 pm

There's this for MicroPython - https://github.com/przemobe/micropy-ENC28J60 - and Google turns up similar for Pico SDK and Arduino frameworks for RP2040.

There is no MicroPython support or drivers for ENC28J60 that I could see. WizNet would appear to me to be the best path for supported LAN networking.

User avatar
neilgl
Posts: 6386
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: ENC28J60 ethernet controller for Raspberry PI Pico

Sun Jan 01, 2023 4:06 pm


robertnapoc
Posts: 5
Joined: Sat Dec 31, 2022 8:48 pm

Re: ENC28J60 ethernet controller for Raspberry PI Pico

Sun Jan 01, 2023 5:03 pm

hippy wrote:
Sun Jan 01, 2023 3:53 pm
There's this for MicroPython - https://github.com/przemobe/micropy-ENC28J60 - and Google turns up similar for Pico SDK and Arduino frameworks for RP2040.

There is no MicroPython support or drivers for ENC28J60 that I could see. WizNet would appear to me to be the best path for supported LAN networking.
Thank you for your suggestion, and yes, currently I'm working with this library, the only unofficial driver for ENC28J60. I was able install the scripts/libraries and have my Pico somewhat connected to my LAN. From here on I still need to figure out how to set it up to be able to receive simple commands and, like the author of the repo says, MicroPython for Pico does not include socket library, and I need to find some other workarounds.

robertnapoc
Posts: 5
Joined: Sat Dec 31, 2022 8:48 pm

Re: ENC28J60 ethernet controller for Raspberry PI Pico

Sun Jan 01, 2023 5:07 pm

neilgl wrote:
Sun Jan 01, 2023 4:06 pm
Have you seen this https://www.raspberrypi.com/news/how-t ... y-pi-pico/
Yes, I think I've stumbled on this in my google searches. And It could have been a pretty good start to a solution if I could replicate the LAN8720 connections and setup (the one used in the blog) for what I have (ENC28J60)

User avatar
neilgl
Posts: 6386
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: ENC28J60 ethernet controller for Raspberry PI Pico

Mon Jan 02, 2023 10:11 am

Ah, I have used import socket on my pico-w and it worked fine to a socket server on a pi3, port 14445

Edit look at this post re enc28j60 https://forums.raspberrypi.com/viewtopic.php?t=328126

hippy
Posts: 14447
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ENC28J60 ethernet controller for Raspberry PI Pico

Mon Jan 02, 2023 11:45 am

neilgl wrote:
Mon Jan 02, 2023 10:11 am
Edit look at this post re enc28j60 https://forums.raspberrypi.com/viewtopic.php?t=328126
What it comes down to is having a driver which can receive and send packets to an ENC28J60, which also passes those to and takes from whatever pool of ethernet packets there are for the packet handler to deal with.

On a Pi it seems that driver exists, but for the Pico it's the bridging between the hardware handling and LwIP which is missing. Once that's done socket and other server handling, which MicroPython already supports, should auto-magically become available for use.

The situation for ENC28J60 is similar to Ethernet over USB. One can send and receive packets via the physical interface, but creating the glue between that and LwIP is missing. Some have managed to connect interfacing direct to LwIP using Pico SDK code, but I don't believe anyone has figured out how to craft the glue required for MicroPython.

I had a go at doing it for Ethernet over USB but don't have enough of an understanding to know what I should be doing, what MicroPython requires.

One could write a full TCP/IP stack separate to LwIP but, beyond responding to pings, that's a lot of work and something which integrates to what already exists in MicroPython is a lot better, far more useful. It just needs someone who knows what they are doing, knows what needs to be done, and how to, to do it.

User avatar
neilgl
Posts: 6386
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: ENC28J60 ethernet controller for Raspberry PI Pico

Mon Jan 02, 2023 12:17 pm

Oops forgot it was a pico

robertnapoc
Posts: 5
Joined: Sat Dec 31, 2022 8:48 pm

SOLVED ENC28J60 ethernet controller for Raspberry PI Pico

Tue Jan 17, 2023 8:58 am

Hello again, I think its time to close this post since I've mange to find a solution for my little project.

Like I've said above, my idea was to have a RPi Pico controlled, preferably thorough something like a socket connection using the ENC28J60 controller over LAN, and being able to do things like switch a relay, send temperature from a sensor, etc. I've manage to do pretty much all of that, running a "listening" client on pico, on a specific IP address that receives commands from, for example, a simple python script that opens, runs and sends commands over a socket connections for the said IP.

But the "downside" is that I've used C++ scripts inside Arduino IDE with external libraries for ENC28J60, I wasn't able to make it work with micro python, nor circuit python.

If somebody is interested I could make a separate post with how I've managed to make my specific project work. I won't describe it here since this is dedicated for micro python.

Papryk
Posts: 1
Joined: Mon Jun 05, 2023 12:22 pm

Re: ENC28J60 ethernet controller for Raspberry PI Pico

Mon Jun 05, 2023 12:29 pm

Hello,
I'm trying to do the similar thing for my little project. I want to control LED lights through TCP socket. Could you send me how you've made enc28j60 to work?

Nexus12
Posts: 23
Joined: Sat Jan 05, 2013 10:37 pm

Re: ENC28J60 ethernet controller for Raspberry PI Pico

Mon Jun 05, 2023 4:00 pm

Hi all

The examples shown here all worked for me :-

https://github.com/przemobe/micropy-ENC28J60

cheers

robertnapoc
Posts: 5
Joined: Sat Dec 31, 2022 8:48 pm

Re: ENC28J60 ethernet controller for Raspberry PI Pico

Mon Jun 05, 2023 5:53 pm

If you choose to use the Arduino IDE implementation with C++ here are some steps and a little example

more details about ENC28J60 wiring diagram here:
https://github.com/Juddling/pi-pico-enc28j60

for Arduino IDE:
-In Boards Manager, search and install Raspberry Pi Pico, then in Tools, Boards select the Raspberry Pi Pico
-In Library Manager, search and install EthernetENC
-make sure to press and hold the Pico flash button and then connect usb cable each time you want to write a new file on it with Arduino IDE.

simple example for switching 2 relays ON/OFF:
in this example, the server "gathers" a simple string like "A0:" bite by bite from the client and when it reaches the ":" character it switches the relay based on the command, then sends back a response, bite by bite to the client and then resets and starts listening again.

example:

Code: Select all

#include <SPI.h>
#include <EthernetENC.h>

const int board_led = 25;
const int A_DEV = 0;
const int B_DEV = 1;

byte mac[] = {
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 177);
EthernetServer server(80);


void setup()
{

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(A_DEV, OUTPUT);
  pinMode(B_DEV, OUTPUT);

  Ethernet.init(17);
  Ethernet.begin(mac, ip);

  if (Ethernet.hardwareStatus() == EthernetNoHardware)
  {
    while (true)
    {
      delay(1);
    }
  }

  while (Ethernet.linkStatus() == LinkOFF)
  {
    delay(1);
  }

  server.begin();

  pinMode(board_led, OUTPUT);
  digitalWrite(board_led, LOW);
  digitalWrite(A_DEV, HIGH);
  digitalWrite(B_DEV, HIGH);

}

void loop()
{
  EthernetClient client = server.available();

  if (Ethernet.linkStatus() == LinkOFF)
  {
    digitalWrite(board_led, HIGH);
  }
  else
  {
    digitalWrite(board_led, LOW);
  }

  if (Ethernet.hardwareStatus() == EthernetNoHardware)
  {
    digitalWrite(board_led, HIGH);
  }
  else
  {
    digitalWrite(board_led, LOW);
  }

  String command = "";
  String res = "";
  if (client)
  {
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        if (c == ':')
        {
          switch (command.charAt(0))
          {
          case 'A':
            if (command.charAt(1) == '1')
            {
              res = "DEV A ON";
              digitalWrite(A_DEV, LOW);
              for (int i = 0; i < res.length(); i++)
              {
                client.write(res.charAt(i));
              }
              res = "";
              break;
            }
            else
            {
              res = "DEV A OFF";
              digitalWrite(A_DEV, HIGH);
              for (int i = 0; i < res.length(); i++)
              {
                client.write(res.charAt(i));
              }
              res = "";
              break;
            }
          case 'B':
            if (command.charAt(1) == '1')
            {
              res = "DEV B ON";
              digitalWrite(B_DEV, LOW);
              for (int i = 0; i < res.length(); i++)
              {
                client.write(res.charAt(i));
              }
              res = "";
              break;
            }
            else
            {
              res = "DEV B OFF";
              digitalWrite(B_DEV, HIGH);
              for (int i = 0; i < res.length(); i++)
              {
                client.write(res.charAt(i));
              }
              res = "";
              break;
            }
          }
          command = "";
        }
        else
        {
          command = command + c;
        }
      }
    }
  }
  client.stop();
}

Return to “MicroPython”