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

ESP32: IDFv5.0 Fix mqtt module #3607

Merged
merged 3 commits into from
Jul 19, 2023

Conversation

serg3295
Copy link

@serg3295 serg3295 commented Jul 16, 2023

  • This PR is for the dev branch rather than for the release branch.
  • This PR is compliant with the other contributing guidelines as well (if not, please describe why).
  • I have thoroughly tested my contribution.
  • The code changes are reflected in the documentation at docs/*.

The mqtt module does not work with IDFv5.0
I think an additional user_config checks were added in new espressif/esp-mqtt submodule.

Error log:

NodeMCU ESP32 build unspecified powered by Lua 5.3.5 [5.3-int32-singlefp] on IDF v5.0.2

new m: 	userdata: 0x3ffcac84

> m:connect('mqtt://192.168.1.31')
W (131404) mqtt_client: Transport config set, but overridden by scheme from URI: transport = 1, uri scheme = mqtt
W (131404) mqtt_client: Client asked to stop, but was not started
Lua error: 	stdin:1: MQTT library failed to start
stack traceback:
	[C]: in method 'connect'
	stdin:1: in main chunk
	[C]: in ?
	[C]: in ?

> m:connect('192.168.1.31')
E (159984) mqtt_client: No scheme found
E (159984) mqtt_client: Failed to create transport list
Lua error: 	stdin:1: Error starting mqtt client
stack traceback:
	[C]: in method 'connect'
	stdin:1: in main chunk
	[C]: in ?
	[C]: in ?

This PR fixes that error.

Copy link
Member

@pjsg pjsg left a comment

Choose a reason for hiding this comment

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

LGTM

@serg3295
Copy link
Author

Small fixes.
Select next parameter in mqtt:connect() when port or secure are nil
Test cases:

m:connect('mqtt://192.168.1.31', nil, nil, 0, function()
    print("MQTT connected! Full uri.")
  end,
  function(client)
    print("MQTT disconnected! Full uri.")
  end
)

-- or
m:connect('mqtt://192.168.1.31', nil, 0)

@serg3295
Copy link
Author

serg3295 commented Jul 17, 2023

Sorry, my last commit incorrectly handles the case
mqtt:connect("server", function(client) print("connected") end, handle_mqtt_error)
An additional check on port==nil, secure==nil is required before n++;

Fixed.

Copy link
Member

@jmattsson jmattsson left a comment

Choose a reason for hiding this comment

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

Looks good to me, though I'm (still) not in a position to try it out myself.

@@ -419,7 +423,7 @@ static int mqtt_connect(lua_State* L) {

ESP_LOGD(TAG, "connect: mqtt_context*: %p", mqtt_context);

if (config.broker.address.uri != NULL)
if (config.broker.address.uri == NULL)
Copy link
Member

Choose a reason for hiding this comment

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

Well oops! 😳 That would be my mistake...

|`mqtt.CONNACK_REFUSED_SERVER_UNAVAILABLE`|3|The server is unavailable.|
|`mqtt.CONNACK_REFUSED_BAD_USER_OR_PASS`|4|The broker refused the specified username or password.|
|`mqtt.CONNACK_REFUSED_NOT_AUTHORIZED`|5|The username is not authorized.|

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I don't see any reference to these ever having existed in this module. I guess this was an esp8266 leftover?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I found these reason codes only in the mqtt-8266 module.

@jmattsson jmattsson merged commit 0415e2c into nodemcu:dev-esp32-idf5-testing Jul 19, 2023
16 checks passed
@jmattsson
Copy link
Member

Thank you for the PR @serg3295 ! Merge has been done!

@serg3295 serg3295 deleted the fix-mqtt branch July 19, 2023 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants