-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: main
Are you sure you want to change the base?
Conversation
I tried to get MQTT working (I have a working homeassistant / mosquitto / zigbee2mqtt setup), but so far failed. It always fails at I found one bug in the original code: it tried to convert "1883" to integer using |
There was a problem hiding this 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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there?
// is this needed? | ||
while(WiFi.status() != WL_CONNECTED) | ||
delay(10); | ||
delay(5000); | ||
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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! |
There was a problem hiding this comment.
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.
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); | ||
} |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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.
No description provided.