przemobe / micropy-ENC28J60

ENC28J60 Ethernet chip driver for MicroPython (RP2)
GNU General Public License v3.0
22 stars 7 forks source link

Using your lib with a pico #1

Closed ph1lj-6321 closed 2 years ago

ph1lj-6321 commented 2 years ago

Hi I'm having a bit of trouble - I'm using a Pc running PacketSender in UDP mode, testing the Packet reception script on Pico

I think I should be seeing the rxBuf in the Thonny shell, I see this :-

MAC ADDR: 0e:5f:5f:20:58:28 ENC28J60 revision ID: 0x00

but thats all, on the PacketSending I'm sending out "thing" every second.

The only difference is that I'm using the instance of SPI with this detail

spi1 = SPI(0, baudrate=10000000, sck=Pin(6), mosi=Pin(7), miso=Pin(4)) eth = enc28j60.ENC28J60(spi1, Pin(5))

My enc is connected to those pins.

Does it have to be on SPI(1,........) ?

(the lib file is on the Pico in the /enc28j60 folder)

przemobe commented 2 years ago

Hello,

ENC28J60 revision ID: 0x00

Revision id 0x00 indicates issue with SPI communication. Please check if physical connection are correct. Please note that ie. Pin(6) corresponds to GPIO6 that is physical pin number 9 on Rasp. Pico board. On ENC28J60 side please check if the SCK and CLK pins have not been mixed up. (SCK shall be used).

Does it have to be on SPI(1,........) ?

No, it does not have to be SPI1. It could be SPI0.

Best Regards, Przemobe

ph1lj-6321 commented 2 years ago

OK, so using the Pico, it works - however if I use the default SPI 0 pins on the Tiny2040, no connection, if I then use the spi1 = SPI(0, baudrate=10000000, sck=Pin(2), mosi=Pin(3), miso=Pin(0)) eth = enc28j60.ENC28J60(spi1, Pin(1)) it does work - ok so I can use those.

if I use the "ntw" it returns the received data from Hercules - this is echoed back to Hercules - great.

What should I see if I use the sample Packet transmission - in Hercules I see nothing ?

Ideally I'd like to be able to send the RP2040 core temperature using udp, at 1 min intervals - hence why I'm asking about the Packet transmission.

przemobe commented 2 years ago

It's nice to see it works.

Here is a draft example for periodic sender:

#!/usr/bin/env python
# -*- coding: utf8 -*-

import Ntw
import time

class PeriodicSender:
    def __init__(self, ntw, tgt_addr, tgt_port, period_sec):
        self.ntw = ntw
        self.tgt_addr = bytes(tgt_addr)
        self.tgt_port = tgt_port
        self.period_sec = period_sec
        self.state = 0
        self.init_time = 0

    def loop(self):
        ctime = time.time()

        if 0 == self.state:
            print('Connecting...')
            ntw.connectIp4(self.tgt_addr)
            self.init_time = ctime
            self.state = 1

        elif 1 == self.state:
            if True == ntw.isConnectedIp4(self.tgt_addr):
                print('Connected')
                self.init_time = ctime
                self.state = 2
                self.send_data()
            elif ctime - self.init_time > 3:
                self.state = 0

        else: # 2 == self.state
            if ctime - self.init_time > self.period_sec:
                self.send_data()
                self.init_time += self.period_sec

    def send_data(self):
        n = self.ntw.sendUdp4(self.tgt_addr, self.tgt_port, '<134>I am alive!'.encode())
        if 0 > n:
            print(f'Fail to send data error={n}')
        else:
            print('Data sent')

if __name__ == '__main__':
    ntw = Ntw.Ntw()

    # Set static IP address
    ntw.setIPv4([192,168,40,233], [255,255,255,0], [192,168,40,1])

    # Create periodic sender
    sender = PeriodicSender(ntw, [192,168,40,129], 514, 60)

    while True:
        ntw.rxAllPkt()
        sender.loop()

Because ntw works in polling mode and there are no sockets a sender needs to handle states self.state for connection establishment: this is ARP request-respone (ntw.connectIp4(self.tgt_addr))

Later I will add it to the repo.

ph1lj-6321 commented 2 years ago

Perfect - it works well, thankyou

przemobe commented 2 years ago

Example added to repo: https://github.com/przemobe/micropy-ENC28J60/blob/main/examples/PeriodicSender.py