diff --git a/examples/sPSN_PingTest/sPSN_PingTest.ino b/examples/sPSN_PingTest/sPSN_PingTest.ino index 6269e756ac4ce73385ab88df8f4beb380ad1ef3c..4bb09a1373fd646919ffbbe8ff022b66f869c12b 100644 --- a/examples/sPSN_PingTest/sPSN_PingTest.ino +++ b/examples/sPSN_PingTest/sPSN_PingTest.ino @@ -12,7 +12,7 @@ * * Arduino3: sPSN_PingTest.ino -> reagiert auf PongTester * - *date: 01.12.2021 + *date: 04.07.2022 */ #include "Arduino.h" @@ -51,7 +51,13 @@ char payloadSend[] = "Ping"; bool ledState = true; void newData(char* topic, char* payload) { - Serial.println(payload); + if (!strcmp(payload, "Ping")){ + Serial.print(payload); + Serial.print(" -> "); + } else{ + Serial.print(payload); + Serial.println(" :-D"); + } digitalWrite(LED_BUILTIN, HIGH); } @@ -61,6 +67,8 @@ void setup() { Serial.begin(SERIAL_BAUD); delay(2000); + //Verwende eigene Nummer als Sende- und Empfangstopic + //(2-stellig mit führender 0) sprintf(topicPub, "%02d", MY_NUMBER); sprintf(topicSub, "%02d", MY_NUMBER); diff --git a/examples/sPSN_PongTester/sPSN_PongTester.ino b/examples/sPSN_PongTester/sPSN_PongTester.ino index 2eebc9ee99d3f2f45049a4c000dffe519018ca50..dd9166a8ad59347bedf01b93de7bcd70a1501042 100644 --- a/examples/sPSN_PongTester/sPSN_PongTester.ino +++ b/examples/sPSN_PongTester/sPSN_PongTester.ino @@ -13,7 +13,7 @@ * * Arduino3: sPSN_PingTest.ino -> reagiert auf PongTester * - *date: 01.12.2021 + *date: 04.07.2022 */ #include "Arduino.h" @@ -36,22 +36,14 @@ SoftwareSerial sSerial(10, 11); //Erzeuge Client-Instanz DidacticPSNetClient psnClient; -//Arrays für Empfangs- und Sende Topic -char topicSub[MAX_LEN_TOPICS] = ""; -char topicPub[MAX_LEN_TOPICS] = ""; - -char payloadSend[] = "Pong"; - -bool ledState = true; +bool recievedPings[MAX_NR_CLIENTS+1] = {false}; +bool newPing = false; void newData(char* topic, char* payload) { - Serial.println(topic); - //reply pong - strcpy(topicPub, topic); - if (!strcmp(payload, "Ping")) { //Empfangene Nachricht == Ping - Serial.print(" "); - Serial.print(topic); - psnClient.publish(topicPub, payloadSend); + if (!strcmp(payload, "Ping") && (atoi(topic) <= MAX_NR_CLIENTS)) { //Empfangene Nachricht == Ping + recievedPings[atoi(topic)] = true; + newPing = true; + psnClient.publish(topic, "Pong"); } } @@ -77,9 +69,23 @@ void setup() { Serial.println("\n\nReady for Clients..."); + + } void loop() { //Verarbeiten der Daten, prüfen ob Netzwerk frei und versenden der Daten psnClient.handleNetwork(); + + if(newPing){ + newPing = false; + Serial.print("Empfangene Clients: "); + for(int i = 0; i < MAX_NR_CLIENTS+1; i++){ + if(recievedPings[i]){ + Serial.print(i); + Serial.print(" | "); + } + } + Serial.println(); + } }