diff --git a/examples/sPSN_PlotTopics/sPSN_PlotTopics.ino b/examples/sPSN_PlotTopics/sPSN_PlotTopics.ino new file mode 100644 index 0000000000000000000000000000000000000000..1a6f3957ec695b7d3d8ee2bb19250936bd58460a --- /dev/null +++ b/examples/sPSN_PlotTopics/sPSN_PlotTopics.ino @@ -0,0 +1,61 @@ +/** + *file: sPSN_PlotTopics + *author: letsgoING -> info@letsgoing.de + * + *description: + * Dieses Programm abboniert Beispiel-Topics und gibt diese auf dem SerialMonitor aus. + * + * parameter: + * MAX_LEN_PAYLOAD = 20 Zeichen (didacticNet.h) + * MAX_LEN_TOPICS = 10 Zeichen (didacticNet.h) + * + *date: 15.07.2022 + */ +#include <Arduino.h> +#include "SoftwareSerial.h" +#include "DidacticNet.h" + +#define SERIAL_BAUD 2400 //lege Geschwindigkeit für serielle Schnittstellen fest + +SoftwareSerial sSerial(10, 11); //Erzeuge SoftwareSerial-Instanz mit Rx = Pin10 -> Empfänger | Tx = Pin11 -> Sender + +DidacticPSNetClient psnClient; //Erzeuge PubSub-Client-Instanz + + +//Callback-Funktion - in diesem Beispiel nicht verwendet +void newData(char* topic, char* payload) { +} + + +void setup() { + + Serial.begin(SERIAL_BAUD); //Starte Serielle Schnittstelle (zum PC) + sSerial.begin(SERIAL_BAUD); //Starte SoftwareSerielle Schnittstelle (zu IR-Link-Modulen) + + psnClient.begin(sSerial, newData); //Starte PubSub Client an SoftwareSerial Schnittstelle + + //Beliebige Beispiel-Topics abbonieren -> zusätzliche Topics hinzufügen + psnClient.subscribe("temp1"); + psnClient.subscribe("temp2"); + psnClient.subscribe("light1"); + psnClient.subscribe("poti"); + + //lese Anzahl der abbonierten Topics aus + int countTopics = psnClient.getMaxNrTopics(); + Serial.print("Anzahl Topics: "); + Serial.println(countTopics); + + //Gebe alle Topics nacheinander aus + for(int i = 0; i < countTopics; i++){ + char topic[MAX_LEN_TOPICS]; + psnClient.getSubscribedTopic(topic, i); + + Serial.print("Topic "); + Serial.print(i); + Serial.print(": "); + Serial.println(topic); + } +} + +void loop() { +} diff --git a/keywords.txt b/keywords.txt index 542b41df93aae8db0b8510a1d1aca8d2970ec1eb..c9c84529f72a1a94ef443e511d959b4723edcb33 100644 --- a/keywords.txt +++ b/keywords.txt @@ -27,6 +27,9 @@ publishOnChange KEYWORD2 subscribe KEYWORD2 unsubscribe KEYWORD2 +getMaxNrTopic KEYWORD2 +getSubscribedTopic KEYWORD2 + timeElapsed KEYWORD2 readSerialData KEYWORD2 @@ -53,4 +56,4 @@ DN_ASCII_NL LITERAL1 DN_PUBLISH_SUCCESSULL LITERAL1 DN_ERROR_NO_ERROR LITERAL1 DN_ERROR_TOPIC_LEN LITERAL1 -DN_ERROR_PAYLOAD_LEN LITERAL1 \ No newline at end of file +DN_ERROR_PAYLOAD_LEN LITERAL1 diff --git a/src/DidacticNet.cpp b/src/DidacticNet.cpp index 380c12a0cbbd38ae648f239bb1bd96e48f73de42..a61796ce9761572ab2a616113aa0b4317953c4a6 100644 --- a/src/DidacticNet.cpp +++ b/src/DidacticNet.cpp @@ -268,7 +268,6 @@ int DidacticPSNetClient::subscribe(char* topic, int topicLength){ return error; } - bool DidacticPSNetClient::unsubscribe(char* topic){ return unsubscribe(topic, strlen(topic)); } @@ -331,6 +330,15 @@ int DidacticPSNetClient::getFreeTopicNr() { return -1; } +int DidacticPSNetClient::getSubscribedTopic(char* topic, int number){ + strcpy(topic, _topic[number]); + return 1; +} + +int DidacticPSNetClient::getMaxNrTopics(){ + return getFreeTopicNr(); +} + //************************************************************************** //Broker diff --git a/src/DidacticNet.h b/src/DidacticNet.h index 498e953e4186f43160d0752fe72f74d8c6891177..fe9321d6437090309426f7136fc1f16617c2f6d9 100644 --- a/src/DidacticNet.h +++ b/src/DidacticNet.h @@ -173,6 +173,10 @@ class DidacticPSNetClient : public DidacticPSNet DidacticPSNetClient(); ~DidacticPSNetClient(); + int getMaxNrTopics(); + int getSubscribedTopic(char*, int); + + int publish(char*, char*); int publish(char*, int, char*, int); int publish(char*, int);