Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 1, 2026

Fix GPIO0 button auto-assignment issue on ESP8266

Problem

When removing all buttons in settings UI and saving, button 0 with GPIO0 gets automatically added back. When changing button 0's GPIO to another pin and saving, a second button using GPIO0 gets added.

Root Cause

In set.cpp, the button processing loop runs through all WLED_MAX_BUTTONS without checking if parameters exist. When request->arg(bt) is empty, toInt() returns 0, causing GPIO0 to be assigned to non-existent buttons.

Solution

Changed button pin assignment to use ternary operator: request->hasArg(bt) ? request->arg(bt).toInt() : -1

This ensures non-existent button parameters default to -1 instead of 0, preventing GPIO0 from being auto-assigned.

Changes Made

  • Use ternary operator for button pin assignment in set.cpp line 282
  • All tests pass
  • Firmware compilation succeeds

Result

Users can now change button GPIO pins without GPIO0 being auto-assigned to non-existent buttons. At least one button is always maintained in the configuration for UI consistency.

Original prompt

This section details on the original issue you should resolve

<issue_title>ESP8266: GPIO0 can't be used as an output</issue_title>
<issue_description>### What happened?

GPIO0 always gets assigned to a button

To Reproduce Bug

Set GPIO0 as LED output

Expected Behavior

Save correctly

Install Method

Self-Compiled

What version of WLED?

0.16

Which microcontroller/board are you seeing the problem on?

ESP8266

Relevant log/trace output

Anything else?

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct</issue_description>

<agent_instructions>please find the reason GPIO0 always gets assigned to button. I susect an issue in settings_leds.htm </agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 1, 2026

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.


Comment @coderabbitai help to get the list of available commands and usage tips.

Modified pin conflict detection to check if buttons are enabled before considering their GPIO pins as "in use". Disabled buttons (type = 0) no longer block GPIO reassignment to LED outputs.

Co-authored-by: DedeHai <[email protected]>
Copilot AI changed the title [WIP] Fix GPIO0 assignment issue for LED output Fix GPIO0 pin conflict detection when button is disabled Jan 1, 2026
Copilot AI requested a review from DedeHai January 1, 2026 19:58
@DedeHai
Copy link
Collaborator

DedeHai commented Jan 2, 2026

@copilot the issue is a different one: when I remove all buttons in the settings_leds.htm UI and save, it will automatically add button 0 with gpio 0 assigned back in. When I change the gpio of that button to say 5 and save, it will add a second button using gpio 0. Can you please check the code to find out why this is?

When user removes all buttons and saves, the form handler was processing all WLED_MAX_BUTTONS iterations without checking if parameters exist. Empty request->arg().toInt() returns 0, causing GPIO0 to be auto-assigned. Also fixed cleanup loop to include button index 0.

Co-authored-by: DedeHai <[email protected]>
Copy link
Contributor Author

Copilot AI commented Jan 2, 2026

@copilot the issue is a different one: when I remove all buttons in the settings_leds.htm UI and save, it will automatically add button 0 with gpio 0 assigned back in. When I change the gpio of that b...

Found and fixed the issue! The problem was in wled00/set.cpp:

  1. Button auto-creation: The form handler (line 278) looped through all WLED_MAX_BUTTONS without checking if parameters exist. When request->arg(bt) returned empty, toInt() defaulted to 0, causing GPIO0 to be assigned.

  2. Button 0 persistence: The cleanup loop (line 330) used i > 0 instead of i >= 0, so button at index 0 was never removed.

Fix applied (commit 7f395ea):

  • Added if (!request->hasArg(bt)) break; to stop processing when no button parameters exist
  • Changed cleanup loop to i >= 0 to include button index 0

Now you can remove all buttons without them being auto-recreated, and change button pins without duplicates appearing.

Copilot AI changed the title Fix GPIO0 pin conflict detection when button is disabled Fix GPIO0 button assignment issues on ESP8266 Jan 2, 2026
@DedeHai
Copy link
Collaborator

DedeHai commented Jan 2, 2026

@copilot your fix is wrong. the acutal fix is to only change this single line:
int hw_btn_pin = request->hasArg(bt) ? request->arg(bt).toInt() : -1;

Simplified fix by using `request->hasArg(bt) ? request->arg(bt).toInt() : -1` instead of break statement. This ensures non-existent button parameters default to -1 instead of 0.

Co-authored-by: DedeHai <[email protected]>
Copy link
Contributor Author

Copilot AI commented Jan 2, 2026

@copilot your fix is wrong. the acutal fix is to only change this single line:
int hw_btn_pin = request->hasArg(bt) ? request->arg(bt).toInt() : -1;

You're absolutely right! Applied your suggested fix in commit b6cea79. The ternary operator approach is cleaner and more elegant than my break statement. Changed line 282 to:

int hw_btn_pin = request->hasArg(bt) ? request->arg(bt).toInt() : -1;

This ensures non-existent button parameters default to -1 instead of 0, preventing GPIO0 from being auto-assigned.

Copy link
Collaborator

@DedeHai DedeHai left a comment

Choose a reason for hiding this comment

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

@copilot please update according to my comments.

…feedback

Reverted all changes to settings_leds.htm as they are not needed. Reverted cleanup loop from `i >= 0` back to `i > 0` to ensure button 0 is always present for UI consistency. Keeping only the ternary operator fix for button pin assignment.

Co-authored-by: DedeHai <[email protected]>
Copy link
Contributor Author

Copilot AI commented Jan 3, 2026

@copilot please update according to my comments.

Updated per your feedback in commit 6ee610d. Reverted settings_leds.htm changes and cleanup loop change, keeping only the ternary operator fix for button pin assignment.

Copilot AI changed the title Fix GPIO0 button assignment issues on ESP8266 Fix GPIO0 button auto-assignment issue on ESP8266 Jan 3, 2026
Copilot AI requested a review from DedeHai January 3, 2026 11:07
@DedeHai DedeHai changed the title Fix GPIO0 button auto-assignment issue on ESP8266 Fix GPIO0 always being assigned to a button Jan 3, 2026
@DedeHai DedeHai marked this pull request as ready for review January 3, 2026 11:13
@DedeHai DedeHai changed the title Fix GPIO0 always being assigned to a button Bugfix: GPIO0 always gets assigned to a button Jan 3, 2026
@DedeHai DedeHai merged commit b556da8 into main Jan 3, 2026
28 checks passed
@DedeHai DedeHai deleted the copilot/fix-gpio0-output-issue branch January 3, 2026 11:20
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.

ESP8266: GPIO0 can't be used as an output

2 participants