Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDF v4 Ethernet/Network Fixes #4526

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

troyhacks
Copy link
Contributor

@troyhacks troyhacks commented Jan 28, 2025

IDF v4 moves from SYSTEM_EVENT_WIFI_STA_GOT_IP to ARDUINO_EVENT_WIFI_STA_GOT_IP (for example)

Also some improvements to disable WiFi when Ethernet is connected, which ensures things like Art-Net and DDP use the Ethernet connection when present.

Summary by CodeRabbit

  • New Features
    • Enhanced Ethernet connectivity: The system now manages connections more intelligently by prioritizing Ethernet and automatically adjusting WiFi behavior as needed.
  • Refactor
    • Updated connection status displays now clearly indicate whether the device is connected via Ethernet or WiFi, providing improved clarity on network status.

@troyhacks troyhacks mentioned this pull request Jan 28, 2025
1 task
@troyhacks troyhacks changed the title IDF v4 Network Fixes IDF v4 Ethernet/Network Fixes Jan 28, 2025
@troyhacks troyhacks marked this pull request as draft January 28, 2025 16:34
@netmindz netmindz added this to the 0.16.0 candidate milestone Jan 28, 2025
@troyhacks troyhacks marked this pull request as ready for review January 29, 2025 18:30
@troyhacks
Copy link
Contributor Author

troyhacks commented Jan 29, 2025

This requires Tasmota based on v4.4.6. The tests with Tasmota on v4.4.4 were very buggy.

Use this: https://github.com/tasmota/platform-espressif32/releases/download/2023.10.04/platform-espressif32.zip

This should also improve Ethernet on builds against IDF v3 as well.

Copy link
Collaborator

@blazoncek blazoncek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see soemone else tackle network code. 😄
While working on ESP-NOW sync I've made quite a few improvements in network logic including fix for detecting Eth disconnect.

I did choose to embrace Arduino events instead.

@@ -169,6 +169,12 @@ int getSignalQuality(int rssi)
return quality;
}

#if ESP_IDF_VERSION_MAJOR >= 4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went the other way around:

#if defined(ESP8266)
  #define ARDUINO_EVENT_WIFI_AP_STADISCONNECTED WIFI_EVENT_SOFTAPMODE_STADISCONNECTED
  #define ARDUINO_EVENT_WIFI_AP_STACONNECTED    WIFI_EVENT_SOFTAPMODE_STACONNECTED
  #define ARDUINO_EVENT_WIFI_STA_GOT_IP         WIFI_EVENT_STAMODE_GOT_IP
  #define ARDUINO_EVENT_WIFI_STA_CONNECTED      WIFI_EVENT_STAMODE_CONNECTED
  #define ARDUINO_EVENT_WIFI_STA_DISCONNECTED   WIFI_EVENT_STAMODE_DISCONNECTED
#elif defined(ARDUINO_ARCH_ESP32) && !defined(ESP_ARDUINO_VERSION_MAJOR) //ESP_IDF_VERSION_MAJOR==3
  // not strictly IDF v3 but Arduino core related
  #define ARDUINO_EVENT_WIFI_AP_STADISCONNECTED SYSTEM_EVENT_AP_STADISCONNECTED
  #define ARDUINO_EVENT_WIFI_AP_STACONNECTED    SYSTEM_EVENT_AP_STACONNECTED
  #define ARDUINO_EVENT_WIFI_STA_LOST_IP        SYSTEM_EVENT_STA_LOST_IP
  #define ARDUINO_EVENT_WIFI_STA_GOT_IP         SYSTEM_EVENT_STA_GOT_IP
  #define ARDUINO_EVENT_WIFI_STA_CONNECTED      SYSTEM_EVENT_STA_CONNECTED
  #define ARDUINO_EVENT_WIFI_STA_DISCONNECTED   SYSTEM_EVENT_STA_DISCONNECTED
  #define ARDUINO_EVENT_WIFI_AP_START           SYSTEM_EVENT_AP_START
  #define ARDUINO_EVENT_WIFI_AP_STOP            SYSTEM_EVENT_AP_STOP
  #define ARDUINO_EVENT_WIFI_SCAN_DONE          SYSTEM_EVENT_SCAN_DONE
  #define ARDUINO_EVENT_ETH_START               SYSTEM_EVENT_ETH_START
  #define ARDUINO_EVENT_ETH_CONNECTED           SYSTEM_EVENT_ETH_CONNECTED
  #define ARDUINO_EVENT_ETH_DISCONNECTED        SYSTEM_EVENT_ETH_DISCONNECTED
#endif

@netmindz
Copy link
Member

netmindz commented Feb 9, 2025

@coderabbitai review

Copy link

coderabbitai bot commented Feb 9, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

coderabbitai bot commented Feb 9, 2025

Walkthrough

The changes update the handling of Ethernet events in the network code. In network.cpp, new event definitions for Ethernet are introduced, and the WiFiEvent function is extended to manage Ethernet events—particularly modifying the action for SYSTEM_EVENT_ETH_GOT_IP and removing some debug output. In wled.cpp, the handleConnection() method now prints a URL with the local IP and prints a distinct message if the connection is via Ethernet, disconnecting WiFi in that case.

Changes

File(s) Change Summary
wled00/network.cpp Added new Ethernet event macros (CONNECTED, DISCONNECTED, START, GOT_IP) conditional on ESP-IDF v4+; updated WiFiEvent() to handle SYSTEM_EVENT_ETH_GOT_IP and adjusted logging.
wled00/wled.cpp Modified WLED::handleConnection() to output a URL containing the IP address and to check the connection type; if Ethernet is active, prints a message and disconnects WiFi.

Sequence Diagram(s)

sequenceDiagram
    participant EVT as Event Trigger
    participant WF as WiFiEvent handler
    participant NW as Network System
    EVT->>WF: Network event (e.g., ETH_GOT_IP)
    WF->>NW: Check if Ethernet is active
    alt Ethernet active and AP inactive
        NW-->>WF: Ethernet connected
        WF->>NW: Disconnect WiFi
    else Both connections active
        NW-->>WF: Log message: 'WiFi remains active'
    end
Loading
sequenceDiagram
    participant SYS as System
    participant CON as handleConnection()
    participant NET as Network Check
    SYS->>CON: Connection established
    CON->>NET: Determine connection type
    alt Connection is Ethernet
        NET-->>CON: Ethernet detected
        CON->>SYS: Print "Connected! IP address: http://" + IP + " via Ethernet (disabling WiFi)"
        CON->>SYS: Disconnect WiFi
    else Connection is WiFi
        NET-->>CON: WiFi detected
        CON->>SYS: Print "Connected! IP address: http://" + IP + " via WiFi"
    end
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
wled00/network.cpp (2)

172-177: Consider using a more comprehensive event mapping approach.

The current approach only maps Ethernet events for IDF v4. Consider using a more comprehensive approach that handles all events across different ESP versions and architectures.

Based on past review feedback, consider using this more comprehensive approach:

-#if ESP_IDF_VERSION_MAJOR >= 4
-  #define SYSTEM_EVENT_ETH_CONNECTED ARDUINO_EVENT_ETH_CONNECTED
-  #define SYSTEM_EVENT_ETH_DISCONNECTED ARDUINO_EVENT_ETH_DISCONNECTED
-  #define SYSTEM_EVENT_ETH_START ARDUINO_EVENT_ETH_START
-  #define SYSTEM_EVENT_ETH_GOT_IP ARDUINO_EVENT_ETH_GOT_IP
-#endif
+#if defined(ESP8266)
+  #define ARDUINO_EVENT_WIFI_AP_STADISCONNECTED WIFI_EVENT_SOFTAPMODE_STADISCONNECTED
+  #define ARDUINO_EVENT_WIFI_AP_STACONNECTED    WIFI_EVENT_SOFTAPMODE_STACONNECTED
+  #define ARDUINO_EVENT_WIFI_STA_GOT_IP         WIFI_EVENT_STAMODE_GOT_IP
+  #define ARDUINO_EVENT_WIFI_STA_CONNECTED      WIFI_EVENT_STAMODE_CONNECTED
+  #define ARDUINO_EVENT_WIFI_STA_DISCONNECTED   WIFI_EVENT_STAMODE_DISCONNECTED
+#elif defined(ARDUINO_ARCH_ESP32) && !defined(ESP_ARDUINO_VERSION_MAJOR)
+  #define ARDUINO_EVENT_WIFI_AP_STADISCONNECTED SYSTEM_EVENT_AP_STADISCONNECTED
+  #define ARDUINO_EVENT_WIFI_AP_STACONNECTED    SYSTEM_EVENT_AP_STACONNECTED
+  #define ARDUINO_EVENT_WIFI_STA_LOST_IP        SYSTEM_EVENT_STA_LOST_IP
+  #define ARDUINO_EVENT_WIFI_STA_GOT_IP         SYSTEM_EVENT_STA_GOT_IP
+  #define ARDUINO_EVENT_WIFI_STA_CONNECTED      SYSTEM_EVENT_STA_CONNECTED
+  #define ARDUINO_EVENT_WIFI_STA_DISCONNECTED   SYSTEM_EVENT_STA_DISCONNECTED
+  #define ARDUINO_EVENT_WIFI_AP_START           SYSTEM_EVENT_AP_START
+  #define ARDUINO_EVENT_WIFI_AP_STOP            SYSTEM_EVENT_AP_STOP
+  #define ARDUINO_EVENT_WIFI_SCAN_DONE          SYSTEM_EVENT_SCAN_DONE
+  #define ARDUINO_EVENT_ETH_START               SYSTEM_EVENT_ETH_START
+  #define ARDUINO_EVENT_ETH_CONNECTED           SYSTEM_EVENT_ETH_CONNECTED
+  #define ARDUINO_EVENT_ETH_DISCONNECTED        SYSTEM_EVENT_ETH_DISCONNECTED
+#endif

187-198: LGTM! Consider adding error handling for WiFi disconnect.

The logic for managing WiFi/Ethernet coexistence is well-implemented. However, consider adding error handling for the WiFi disconnect operation.

Consider adding error handling:

 case SYSTEM_EVENT_ETH_GOT_IP:
   if (Network.isEthernet()) {
     if (!apActive) {
       DEBUG_PRINTLN(F("WiFi Connected *and* ETH Connected. Disabling WIFi"));
-      WiFi.disconnect(true);
+      if (!WiFi.disconnect(true)) {
+        DEBUG_PRINTLN(F("Failed to disconnect WiFi"));
+      }
     } else {
       DEBUG_PRINTLN(F("WiFi Connected *and* ETH Connected. Leaving AP WiFi active"));
     }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 32a75c1 and 9b5b26d.

📒 Files selected for processing (2)
  • wled00/network.cpp (2 hunks)
  • wled00/wled.cpp (1 hunks)
🔇 Additional comments (1)
wled00/wled.cpp (1)

1013-1023: LGTM! Clear and informative connection status output.

The changes improve user experience by:

  1. Adding HTTP URL prefix for easy copy-paste
  2. Clearly indicating the connection type (Ethernet/WiFi)
  3. Properly handling WiFi/Ethernet coexistence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants