Skip to content

WIP: trying to fix MQTT #17

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions OpenCO2_Sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Preferences preferences;
#include <WiFiManager.h>
WiFiManager wifiManager;

#define airgradient
// #define airgradient
#ifdef airgradient
/* use https://github.com/geerlingguy/internet-pi to store values */
#include <WebServer.h>
Expand All @@ -43,7 +43,7 @@ const int port = 9925;
WebServer server(port);
#endif /* airgradient */

// #define MQTT
#define MQTT
#ifdef MQTT
#ifdef airgradient
#error only activate one: MQTT or airgradient
Expand Down Expand Up @@ -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?

RTC_DATA_ATTR uint16_t sensorStatus, serial0, serial1, serial2;

RTC_DATA_ATTR uint16_t co2 = 400;
Expand Down Expand Up @@ -158,8 +158,9 @@ void saveCredentials() {

void loadCredentials() {
preferences.begin("co2-sensor", true);
String s_mqtt_server = preferences.getString("mqtt_server", "");
String s_mqtt_port = preferences.getString("mqtt_port", "");
// hard coded my ip / port here so I don't have to set it up via web form
String s_mqtt_server = preferences.getString("mqtt_server", "10.10.10.17");
String s_mqtt_port = preferences.getString("mqtt_port", "1883");
String s_api_token = preferences.getString("api_token", "");
preferences.end();

Expand Down Expand Up @@ -566,6 +567,7 @@ void toggleWiFi() {
}

void startWiFi() {
char msg[100];
wifiManager.setSaveConfigCallback([]() {
#ifdef MQTT
saveCredentials();
Expand All @@ -586,10 +588,34 @@ void startWiFi() {
wifiManager.setConfigPortalBlocking(false);
wifiManager.autoConnect("OpenCO2 Sensor"); // name of broadcasted SSID

// is this needed?
while(WiFi.status() != WL_CONNECTED)
delay(10);
delay(5000);

Comment on lines +591 to +595
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.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.

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);
}
Comment on lines +602 to +615
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?

Serial.println(msg);
}else{
Serial.println("MQTT: no server and/or no port configured!");
}
#endif /* MQTT */

Expand Down Expand Up @@ -710,13 +736,18 @@ void loop() {
#ifdef MQTT
if (!error && !BatteryMode) {
if (WiFi.status() == WL_CONNECTED) {
mqttClient.beginMessage("co2_ppm");
mqttClient.print(co2);
mqttClient.endMessage();
mqttClient.beginMessage("temperature");
mqttClient.poll(); // needed?
int rc1, rc2, rc3;
rc1 = mqttClient.beginMessage("openco2/co2_ppm");
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

Serial.println(result);
mqttClient.beginMessage("openco2/temperature");
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.

mqttClient.print(humidity);
mqttClient.endMessage();
}
Expand Down