mirror of
https://gitlab.com/wirelos/sprocket-plugin-mqtt.git
synced 2025-12-14 13:51:27 +01:00
fix topic comparison
This commit is contained in:
12
.gitignore
vendored
12
.gitignore
vendored
@@ -65,14 +65,8 @@ typings/
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
|
||||
# build stuff
|
||||
/data/
|
||||
dist/
|
||||
|
||||
# sprocket stuff
|
||||
/config/
|
||||
/firmware
|
||||
.clang_complete
|
||||
.gcc-flags.json
|
||||
.pioenvs
|
||||
.piolibdeps
|
||||
.pioenvs
|
||||
.piolibdeps
|
||||
data/config.json
|
||||
13
data/config.json
Normal file
13
data/config.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"stationMode": 1,
|
||||
"hostname": "sprocket",
|
||||
"apSSID": "sprocket",
|
||||
"apPassword": "th3r31sn0Sp00n",
|
||||
"connectTimeout": 20000,
|
||||
"stationSSID": "MyAP",
|
||||
"stationPassword": "th3r31sn0Sp00n",
|
||||
"meshSSID": "whateverYouLike",
|
||||
"meshPassword": "somethingSneaky",
|
||||
"meshPort": 5555,
|
||||
"channel": 5
|
||||
}
|
||||
6
data/mqttConfig.json
Normal file
6
data/mqttConfig.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"mqttClientName" : "sprocket",
|
||||
"mqttBrokerHost" : "192.168.1.2",
|
||||
"mqttBrokerPort" : 1883,
|
||||
"mqttRootTopic" : "wirelos/sprocket"
|
||||
}
|
||||
@@ -98,8 +98,9 @@ void MqttPlugin::downstreamHandler(char *topic, uint8_t *payload, unsigned int l
|
||||
String topicStr = String(topic);
|
||||
int topicRootLength = topicRoot.length();
|
||||
if(topicStr.length() > topicRootLength){
|
||||
int baseTopicLength = topicStr.substring(0, topicRootLength).length();
|
||||
String localTopic = topicStr.substring(topicRootLength);
|
||||
if(localTopic.length() > 4){
|
||||
if(baseTopicLength == topicRootLength){
|
||||
String direction = localTopic.substring(0,4);
|
||||
if(direction == "/in/" ){
|
||||
String localSubTopic = localTopic.substring(direction.length());
|
||||
|
||||
@@ -22,13 +22,12 @@ void setup()
|
||||
|
||||
// this subscription gets shadowed by the MqttPlugin
|
||||
// to rout messages to the queue on topic wirelos/sprocket/chat/log
|
||||
sprocket->subscribe("chat/log", [](String msg) {
|
||||
// gets routed to MQTT broker
|
||||
PRINT_MSG(Serial, "CHAT", msg.c_str());
|
||||
});
|
||||
//sprocket->subscribe("chat/log", [](String msg) {
|
||||
// // gets routed to MQTT broker
|
||||
// PRINT_MSG(Serial, "CHAT", msg.c_str());
|
||||
//});
|
||||
publishHeap.set(TASK_SECOND * 5, TASK_FOREVER, []() {
|
||||
// locally publish a message
|
||||
sprocket->publish("chat/log", "hi there");
|
||||
sprocket->publish("out/chat/log", "hi there");
|
||||
});
|
||||
sprocket->addTask(publishHeap);
|
||||
|
||||
@@ -42,17 +41,29 @@ void setup()
|
||||
CONNECT_TIMEOUT);
|
||||
network->connect();
|
||||
|
||||
sprocket->activate();
|
||||
const char* mqChatTopic = "wirelos/chat/log";
|
||||
const char* outChatTopic = "out/chat/log";
|
||||
const char* chatUser = "user";
|
||||
|
||||
sprocket->subscribe("mqtt/connect", [](String msg) {
|
||||
sprocket->subscribe("mqtt/connect", [mqChatTopic, outChatTopic, chatUser](String msg) {
|
||||
if (msg.length() > 0)
|
||||
{
|
||||
mqttPlugin->client->subscribe("wirelos/sprocket/out/chat/log");
|
||||
sprocket->subscribe("/out/chat/log", [](String msg){
|
||||
PRINT_MSG(Serial, "CHAT", String("incoming: " +msg).c_str());
|
||||
mqttPlugin->client->subscribe(mqChatTopic);
|
||||
sprocket->subscribe(mqChatTopic, [](String msg) {
|
||||
PRINT_MSG(Serial, "CHAT", String("incoming: " + msg).c_str());
|
||||
//webApiPlugin->ws->textAll(msg);
|
||||
});
|
||||
|
||||
// send message from WS to this topic
|
||||
sprocket->subscribe(outChatTopic, [mqChatTopic, chatUser](String msg) {
|
||||
PRINT_MSG(Serial, "CHAT", msg.c_str());
|
||||
mqttPlugin->client->publish(mqChatTopic, (String(chatUser) + ": " + msg).c_str());
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
sprocket->activate();
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
||||
Reference in New Issue
Block a user