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

WIP: trying to fix MQTT #17

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ThomasWaldmann
Copy link
Contributor

No description provided.

@ThomasWaldmann
Copy link
Contributor Author

ThomasWaldmann commented May 7, 2024

I tried to get MQTT working (I have a working homeassistant / mosquitto / zigbee2mqtt setup), but so far failed.

It always fails at mqttClient.connect(...) and the error code is -1 (TIMEOUT) - takes 30s.

I found one bug in the original code: it tried to convert "1883" to integer using (int)mqtt_port, that didn't work. After I fixed that, I at least saw a connection in mosquitto logs. But it somehow sits there for 30s in .connect() and then the client closes the connection, for yet unknown reasons.

Copy link
Contributor Author

@ThomasWaldmann ThomasWaldmann left a comment

Choose a reason for hiding this comment

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

some comments

@@ -86,7 +86,7 @@ RTC_DATA_ATTR int ledbrightness, HWSubRev, font;
RTC_DATA_ATTR float maxBatteryVoltage;

/* TEST_MODE */
RTC_DATA_ATTR bool TEST_MODE;
RTC_DATA_ATTR bool TEST_MODE = 1; // wanted to see some log output on the serial monitor - is there a better way?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there?

Comment on lines +591 to +595
// is this needed?
while(WiFi.status() != WL_CONNECTED)
delay(10);
delay(5000);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought maybe the wifi isn't really ready or stable yet. But otoh, I am seeing the connection attempt in mosquitto.log, so guess this is not needed.

#ifdef MQTT
loadCredentials();
if(mqtt_server[0] != '\0' && mqtt_port[0] != '\0'){
mqttClient.connect(mqtt_server, (int)mqtt_port);
mqttClient.setId("OpenCO2"); // TODO: must be unique
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cosmetic, without that it uses Arduino-xxxxx

mqttClient.connect(mqtt_server, (int)mqtt_port);
mqttClient.setId("OpenCO2"); // TODO: must be unique
// mqttClient.setUsernamePassword("openco2", "openco2"); // TODO: if anon is not allowed, we'ld need this!
int mport = atoi(mqtt_port); // BUG FIX!
Copy link
Contributor Author

Choose a reason for hiding this comment

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

guess that was the only real improvement yet.

Comment on lines +602 to +615
if(!mqttClient.connect(mqtt_server, mport)) {
snprintf(msg, 100, "MQTT connection to %s:%d failed! Error code = %d",
mqtt_server, mport, mqttClient.connectError());

// we always end here, error code -1 (TIMEOUT) after 30s
// mosquitto server log:
// 1715114141: New connection from 10.10.10.123:57599 on port 1883.
// 1715114141: New client connected from 10.10.10.123:57599 as OpenCO2 (p2, c1, k60).
// 1715114171: Client OpenCO2 closed its connection.
// but why?

}else{
snprintf(msg, 100, "MQTT connection to %s:%d succeeded!", mqtt_server, mport);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

why?

rc2 = mqttClient.print(co2);
rc3 = mqttClient.endMessage();
char result[100];
snprintf(result, 100, "MQTT msg sent %d %d %d", rc1, rc2, rc3); // "MQTT msg sent 1 4 0"
Copy link
Contributor Author

@ThomasWaldmann ThomasWaldmann May 7, 2024

Choose a reason for hiding this comment

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

Undocumented return code, maybe:

1 = success
4 = length (having 4 digits CO2 right now, need to vent :)
0 = fail

mqttClient.print(temperature);
mqttClient.endMessage();
mqttClient.beginMessage("humidity");
mqttClient.beginMessage("openco2/humidity");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Guess some nesting is good, to not have it top-level without context.

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.

1 participant