adafruit / Adafruit_IO_Arduino

Arduino library to access Adafruit IO from WiFi, cellular, and ethernet modules.
Other
208 stars 108 forks source link

Fetch Mac address from Ethernet module instead of using hardcoded value. #172

Open tyeth opened 2 months ago

tyeth commented 2 months ago

A user emailed Adafruit IO support suggesting an improvement to the IO library to fetch the Ethernet mac address before starting the module:

Hello! I'm Gavin, I made an adafruit.io related application, and adafruit's blog also reported on this application: https://blog.adafruit.com/2024/05/24/a-raspberry-pi-pico-board-with-dvi-video-and-ethernet/

I am using "Adafruit IO Arduino 4.3", the MAC address for Ethernet initialization is defined and initialized in: Adafruit_IO_Arduino/src
/AdafruitIO_Ethernet.h

But there are some unreasonable things about doing this:
If the user uses several devices: they need to change the MAC address of each device in the library, otherwise multiple devices will not work at the same time (because the devices have the same MAC address);

Can my suggestion be changed to this?

STEUP() in Sketch:

   Ethernet.begin(mac);
   // connect to io.adafruit.com
   adafruit_io.connect();</p>

Change AdafruitIO_Ethernet.h to this, first read the MAC address from the WIZnet chip, and then re-initialize:

   void _connect() {
     byte macAddress_[6];
     Ethernet.MACAddress(macAddress_); //read mac address from Wiznet Chip
     if (Ethernet.begin(macAddress_) == 0) {
       _status = AIO_NET_DISCONNECTED;
       if (Ethernet.hardwareStatus() == EthernetNoHardware) {
         AIO_DEBUG_PRINTLN("Ethernet FeatherWing not found! Please recheck "
                           "wiring connections.");
         while(true)
           delay(1); // do nothing, no point running without Ethernet hardware
       }
     } else {
       // Ethernet.MACAddress(macAddress_);
       // for (int i = 0; i &lt; 6; i++) {
       // Serial.print(macAddress_[i], HEX);
       // if (i &lt; 5) Serial.print(':');
       // }
       _status = AIO_NET_CONNECTED;
     }
   }

This eliminates the need to change the MAC address every time you enter the library file.

tyeth commented 2 months ago

It's probably worth looking at #149 and closing that at the same time.