Skip to content

Mqtt anonymous access option added #19

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ Therefore, to add Wi-Fi connectivity, we just need to also listen to the TX of t
The Ikea uC will do all that polling stuff for us.

As reported in #16, the transitions from Green to Yellow and Yellow to Red in the Ikea firmware are at around 30 and 100μg/m³.
| Color | Value | Comment |
|:------------- |:-------------:|:-----|
| Green | 0-35 | Good + Low |
| Amber | 36-85 | Ok + Medium |
| Red | 86- | Not good + High |
| Pulsing | --- | Startup mode for 10 seconds|

Info from (https://www.ikea.com/us/en/manuals/vindriktning-air-quality-sensor__AA-2289325-1_pub.pdf):

## ToDo

Expand Down
3 changes: 3 additions & 0 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ namespace Config {

char username[24] = "";
char password[24] = "";
char mqtt_anonymous[2] = "";

void save() {
DynamicJsonDocument json(512);
json["mqtt_server"] = mqtt_server;
json["username"] = username;
json["password"] = password;
json["mqtt_anonymous"] = mqtt_anonymous;

File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Expand Down Expand Up @@ -41,6 +43,7 @@ namespace Config {
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(username, json["username"]);
strcpy(password, json["password"]);
strcpy(mqtt_anonymous, json["mqtt_anonymous"]);
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/esp8266-vindriktning-particle-sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ WiFiManager wifiManager;
WiFiClient wifiClient;
PubSubClient mqttClient;

WiFiManagerParameter p_lineBreak_text_title("<p>MQTT Server Config:</p>");
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", Config::mqtt_server, sizeof(Config::mqtt_server));
WiFiManagerParameter custom_mqtt_user("user", "MQTT username", Config::username, sizeof(Config::username));
WiFiManagerParameter custom_mqtt_pass("pass", "MQTT password", Config::password, sizeof(Config::password));
WiFiManagerParameter p_lineBreak_text_mqtt_anonymous("<p>MQTT Anonymous? (bypass user/pass):</p>");
WiFiManagerParameter custom_mqtt_anonymous("mqtt_anónymous", "MQTT anonymous", "T", 2, "type=\"checkbox\"");

uint32_t lastMqttConnectionAttempt = 0;
const uint16_t mqttConnectionInterval = 60000; // 1 minute = 60 seconds = 60000 milliseconds
Expand Down Expand Up @@ -141,9 +144,12 @@ void setupWifi() {
wifiManager.setDebugOutput(false);
wifiManager.setSaveConfigCallback(saveConfigCallback);

wifiManager.addParameter(&p_lineBreak_text_title);
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_mqtt_user);
wifiManager.addParameter(&custom_mqtt_pass);
wifiManager.addParameter(&p_lineBreak_text_mqtt_anonymous);
wifiManager.addParameter(&custom_mqtt_anonymous);

WiFi.hostname(identifier);
wifiManager.autoConnect(identifier);
Expand All @@ -152,6 +158,7 @@ void setupWifi() {
strcpy(Config::mqtt_server, custom_mqtt_server.getValue());
strcpy(Config::username, custom_mqtt_user.getValue());
strcpy(Config::password, custom_mqtt_pass.getValue());
strcpy(Config::mqtt_anonymous, custom_mqtt_anonymous.getValue());

if (shouldSaveConfig) {
Config::save();
Expand All @@ -171,13 +178,24 @@ void resetWifiSettingsAndReboot() {

void mqttReconnect() {
for (uint8_t attempt = 0; attempt < 3; ++attempt) {
if (mqttClient.connect(identifier, Config::username, Config::password, MQTT_TOPIC_AVAILABILITY, 1, true, AVAILABILITY_OFFLINE)) {
if (strcmp(Config::mqtt_anonymous, "T") == 0) {
if (mqttClient.connect(identifier, MQTT_TOPIC_AVAILABILITY, 1, true, AVAILABILITY_OFFLINE)) {
mqttClient.publish(MQTT_TOPIC_AVAILABILITY, AVAILABILITY_ONLINE, true);
publishAutoConfig();

// Make sure to subscribe after polling the status so that we never execute commands with the default data
mqttClient.subscribe(MQTT_TOPIC_COMMAND);
break;
}
} else{
if (mqttClient.connect(identifier, Config::username, Config::password, MQTT_TOPIC_AVAILABILITY, 1, true, AVAILABILITY_OFFLINE)) {
mqttClient.publish(MQTT_TOPIC_AVAILABILITY, AVAILABILITY_ONLINE, true);
publishAutoConfig();

// Make sure to subscribe after polling the status so that we never execute commands with the default data
mqttClient.subscribe(MQTT_TOPIC_COMMAND);
break;
}
}
delay(5000);
}
Expand Down