diff --git a/.gitignore b/.gitignore index 3e2a754..8dcb28b 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/data/config.json b/data/config.json new file mode 100644 index 0000000..82c9e85 --- /dev/null +++ b/data/config.json @@ -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 +} \ No newline at end of file diff --git a/data/mqttConfig.json b/data/mqttConfig.json new file mode 100644 index 0000000..fac46fe --- /dev/null +++ b/data/mqttConfig.json @@ -0,0 +1,6 @@ +{ + "mqttClientName" : "sprocket", + "mqttBrokerHost" : "192.168.1.2", + "mqttBrokerPort" : 1883, + "mqttRootTopic" : "wirelos/sprocket" +} \ No newline at end of file diff --git a/src/MqttPlugin.cpp b/src/MqttPlugin.cpp index 61b4d25..0914337 100644 --- a/src/MqttPlugin.cpp +++ b/src/MqttPlugin.cpp @@ -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()); diff --git a/src/examples/chat/main.cpp b/src/examples/chat/main.cpp index 3bd4d1d..0ebbbd1 100644 --- a/src/examples/chat/main.cpp +++ b/src/examples/chat/main.cpp @@ -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()