Correct usage of ethernet.init(SS_PIN)

Status
Not open for further replies.

jkoffman

Well-known member
Hi all,

I'm planning on using an alternate CS pin for my Ethernet module for the first time, and I just want to make sure I'm doing this correctly. The test I'm trying to do with this board will be to read Artnet data off the network. I'm using this as a test as it's something that I've had working on a previous board, so in theory it should work here once I change the select pin.

Since the Artnet library handles Ethernet initialization, should I just issue an "ethernet.init(XX)" before I call "artnet.begin()"?

For bonus points, can I use the same method to specify an alternate netmask?

Thanks!

Josh
 
Further to my question, I've been trying to unravel how the Artnet library uses the Ethernet library. What I haven't figured out is how the Ethernet instance is created. I can see in Artnet.cpp where Ethernet.begin is called, but no where do I see where the Ethernet object is created. I am sure there is an easy answer to this, I'm just not familiar enough with this language to get it...yet.

Hopefully this info helps me figure out where to properly use the Ethernet.init() function.

Thanks!

Josh
 
... but no where do I see where the Ethernet object is created.

Ethernet is a globally scoped object declared in Ethernet.h and defined in Ethernet.cpp.

From The C++ Programming Language Third Edition, Bjarne Stroustrup:
In principle, a variable defined outside any function (that is, global, namespace, and class static variables) is initialized before main() is invoked.

In an Arduino sketch, main() eventually calls setup() and loop().

Executive Summary: The global variable Ethernet is part of the Ethernet library and is created before setup() is run.
 
Last edited:
Ah, found it in Ethernet.h. Perfect, thank you! Based on this info, this should mean I can issue the .init before I start Artnet and it should work. I think.

As an extended question, would the way that Ethernet.h declares the Ethernet object preclude it from being used as is in a design that had multiple Ethernet ports? I have no intention to do this, I'm just trying to understand the pros and cons of having the library declare its own object.

Thank you!
 
Ah, found it in Ethernet.h. Perfect, thank you! Based on this info, this should mean I can issue the .init before I start Artnet and it should work. I think.

Yes.

would the way that Ethernet.h declares the Ethernet object preclude it from being used as is in a design that had multiple Ethernet ports?

It's exceedingly uncommon enough that I've never seen a case in the Arduino world where there were more than one Ethernet port. However, many other libraries use the idiom that each library will supply one global object per port, and named like Serial1, Serial2, Serial3, etc.

The advantage to using global objects is that the memory used is statically allocated and is known at compile-time. This makes managing memory usage in a micro-controller much easier and avoids allocation in the heap which might fail. As a general rule, we try to avoid allocating memory on the heap. In cases where that can't be achieved, (for example, allocating an buffer whose size comes from EEPROM, so size is not known at compile-time), then allocate only once and never free. We want to avoid the case where our device stops working because the heap gets fragmented, causing heap allocations to fail.
 
Thanks Paul! I believe I have it working. I called Ethernet.init(), then I called artnet.begin() which calls Ethernet.begin.

It was acting a bit weird yesterday but now seems working, I am working on figuring out what the initial issue was. Might be hardware, this isn't using a premade WizNet module.

Thanks!
 
Thinking this out a bit further, if I wanted to send and receive Artnet data on the same Ethernet connection, would the best practice be to create a single library that does both? I'm unclear if I can have two different things accessing the same Ethernet connection without colliding somehow. If I contained all of it in one library at least I could sort of control who was working and when.
 
Hi Paul,

Thanks for the reply! I am already using Nathanaël's excellent library for Artnet receive. Unless I'm missing something though, it doesn't transmit. I have been looking at Stephan's library here: https://github.com/rstephan/ArtnetWifi . It's based on Nathanaël's but expands it to transmit. He wrote it to target ESP modules, so I might have to do a bit of tweaking to get it to run on Teensy. I haven't started really digging into it yet.

What I'm trying to figure out is how I can receive and transmit at the same time. The current library allows for the use of a callback when it receives a frame. My current thought is that I then set a timer that on interrupt triggers the sending of a separate Artnet frame.

Thoughts?
 
Status
Not open for further replies.
Back
Top