Add user and pass

This commit is contained in:
Partick Balsiger
2019-04-07 17:17:44 +02:00
parent 7363e03b74
commit e58e7b3727
25 changed files with 35 additions and 6 deletions

0
.gitignore vendored Normal file → Executable file
View File

0
.gitlab-ci.yml Normal file → Executable file
View File

0
.travis.yml Normal file → Executable file
View File

0
.vscode/extensions.json vendored Normal file → Executable file
View File

Binary file not shown.

BIN
.vscode/ipch/96c62a1ecada60cd/main.ipch vendored Normal file

Binary file not shown.

Binary file not shown.

BIN
.vscode/ipch/ac952b37c792bcdd/main.ipch vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

2
.vscode/settings.json vendored Normal file → Executable file
View File

@@ -1,6 +1,6 @@
{
"terminal.integrated.env.linux": {
"PATH": "/home/master/.platformio/penv/bin:/home/master/.platformio/penv:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl",
"PATH": "/home/master/.platformio/penv/bin:/home/master/.platformio/penv:/home/master/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/sbin:/usr/sbin",
"PLATFORMIO_CALLER": "vscode"
}
}

2
data/config.json Normal file → Executable file
View File

@@ -4,7 +4,7 @@
"apSSID": "sprocket",
"apPassword": "th3r31sn0Sp00n",
"connectTimeout": 20000,
"stationSSID": "MyAP",
"stationSSID": "th1ngs4p",
"stationPassword": "th3r31sn0Sp00n",
"meshSSID": "whateverYouLike",
"meshPassword": "somethingSneaky",

4
data/mqttConfig.json Normal file → Executable file
View File

@@ -2,5 +2,7 @@
"mqttClientName" : "sprocket",
"mqttBrokerHost" : "192.168.1.2",
"mqttBrokerPort" : 1883,
"mqttRootTopic" : "wirelos/sprocket"
"mqttRootTopic" : "wirelos/sprocket",
"mqttUser": "",
"mqttPass": ""
}

0
lib/readme.txt Normal file → Executable file
View File

0
library.json Normal file → Executable file
View File

11
platformio.ini Normal file → Executable file
View File

@@ -12,7 +12,6 @@ lib_deps =
TaskScheduler
SPIFFS
ArduinoJson
ArduinoOTA
https://gitlab.com/wirelos/sprocket-lib.git#develop
https://gitlab.com/wirelos/sprocket-network-wifi.git
@@ -35,3 +34,13 @@ monitor_baud = ${common.monitor_baud}
framework = ${common.framework}
lib_deps = ${common.lib_deps}
PubSubClient
[env:chat-esp01]
src_filter = +<*> -<examples/> +<examples/chat/>
platform = ${common.platform}
board = esp01
upload_speed = ${common.upload_speed}
monitor_baud = ${common.monitor_baud}
framework = ${common.framework}
lib_deps = ${common.lib_deps}
PubSubClient

8
src/MqttConfig.h Normal file → Executable file
View File

@@ -7,6 +7,8 @@
#define JSON_MQTT_BROKER_HOST "mqttBrokerHost"
#define JSON_MQTT_BROKER_PORT "mqttBrokerPort"
#define JSON_MQTT_TOPIC_ROOT "mqttRootTopic"
#define JSON_MQTT_USER "mqttUser"
#define JSON_MQTT_PASS "mqttPass"
struct MqttConfig
{
@@ -14,6 +16,8 @@ struct MqttConfig
const char *brokerHost;
int brokerPort;
const char *topicRoot;
const char *user;
const char *pass;
};
struct MqttConfigJson : public MqttConfig, public JsonStruct
@@ -24,6 +28,8 @@ struct MqttConfigJson : public MqttConfig, public JsonStruct
root[JSON_MQTT_BROKER_HOST] = brokerHost;
root[JSON_MQTT_BROKER_PORT] = brokerPort;
root[JSON_MQTT_TOPIC_ROOT] = topicRoot;
root[JSON_MQTT_USER] = user;
root[JSON_MQTT_PASS] = pass;
}
void fromJsonObject(JsonObject &json)
{
@@ -37,6 +43,8 @@ struct MqttConfigJson : public MqttConfig, public JsonStruct
brokerHost = getAttr(json, JSON_MQTT_BROKER_HOST);
brokerPort = getIntAttrFromJson(json, JSON_MQTT_BROKER_PORT);
topicRoot = getAttr(json, JSON_MQTT_TOPIC_ROOT);
user = getAttr(json, JSON_MQTT_USER);
pass = getAttr(json, JSON_MQTT_PASS);
valid = 1;
};
};

10
src/MqttPlugin.cpp Normal file → Executable file
View File

@@ -12,6 +12,8 @@ void MqttPlugin::applyConfig(MqttConfig cfg) {
brokerPort = cfg.brokerPort;
clientName = String(cfg.clientName);
topicRoot = String(cfg.topicRoot);
user = String(cfg.user);
pass = String(cfg.pass);
}
void MqttPlugin::applyConfigFromFile(const char* fileName) {
@@ -51,7 +53,13 @@ void MqttPlugin::connect()
if (!client->connected())
{
PRINT_MSG(Serial, "MQTT", "try connect");
if (client->connect(clientName.c_str()))
int connected = 0;
if(user.length() > 0 && pass.length() > 0){
connected = client->connect(clientName.c_str(), user.c_str(), pass.c_str());
} else {
client->connect(clientName.c_str());
}
if (connected)
{
// bind handlers to all local subscriptions
if (!subscribed)

2
src/MqttPlugin.h Normal file → Executable file
View File

@@ -37,6 +37,8 @@ private:
int brokerPort;
String clientName;
String topicRoot;
String user;
String pass;
int connectTries = 0;
int maxConnectTries = 5; // TODO add to config

0
src/examples/basic/config.h Normal file → Executable file
View File

2
src/examples/basic/main.cpp Normal file → Executable file
View File

@@ -26,7 +26,7 @@ void setup()
});
sprocket->addTask(publishHeap);
sprocket->addPlugin(
new MqttPlugin({"mqttSprocket", "192.168.1.2", 1883, "wirelos/mqttSprocket/"}));
new MqttPlugin({"clientId", "192.168.1.2", 1883, "some/topic", "myuser", "mypass"}));
network = new WiFiNet(
SPROCKET_MODE,

0
src/examples/chat/config.h Normal file → Executable file
View File

0
src/examples/chat/main.cpp Normal file → Executable file
View File