New Code for Arduino
This commit is contained in:
		| @@ -2,7 +2,6 @@ | |||||||
| // Erstellungsdatum: 22.01.2025 | // Erstellungsdatum: 22.01.2025 | ||||||
| // Hardware: Vision AI V2 Kit | // Hardware: Vision AI V2 Kit | ||||||
|  |  | ||||||
|  |  | ||||||
| #include <Seeed_Arduino_SSCMA.h> | #include <Seeed_Arduino_SSCMA.h> | ||||||
| #include <WiFi.h> | #include <WiFi.h> | ||||||
| #include <PubSubClient.h> | #include <PubSubClient.h> | ||||||
| @@ -14,7 +13,6 @@ const char* password = "DEIN_WIFI_PASSWORT"; | |||||||
| // MQTT-Broker-Zugangsdaten | // MQTT-Broker-Zugangsdaten | ||||||
| const char* mqtt_server = "DEINE_MQTT_BROKER_ADRESSE"; | const char* mqtt_server = "DEINE_MQTT_BROKER_ADRESSE"; | ||||||
| const char* mqtt_topic = "/vision_ai/data"; | const char* mqtt_topic = "/vision_ai/data"; | ||||||
|  |  | ||||||
| WiFiClient espClient; | WiFiClient espClient; | ||||||
| PubSubClient client(espClient); | PubSubClient client(espClient); | ||||||
| SSCMA AI; | SSCMA AI; | ||||||
| @@ -77,27 +75,53 @@ void loop() { | |||||||
|         mqttMessage += ",\"postprocess\":" + String(AI.perf().postprocess) + "},"; |         mqttMessage += ",\"postprocess\":" + String(AI.perf().postprocess) + "},"; | ||||||
|  |  | ||||||
|         mqttMessage += "\"boxes\": ["; |         mqttMessage += "\"boxes\": ["; | ||||||
|  |         bool validBoxFound = false; // Prüfen, ob mindestens ein gültiger Box-Eintrag existiert | ||||||
|  |  | ||||||
|         for (int i = 0; i < AI.boxes().size(); i++) { |         for (int i = 0; i < AI.boxes().size(); i++) { | ||||||
|             mqttMessage += "{\"target\":\"" + String(AI.boxes()[i].target) + "\","; |             if (AI.boxes()[i].score > 50) { // Bedingung: Nur bei Score > 50 verarbeiten | ||||||
|             mqttMessage += "\"score\":" + String(AI.boxes()[i].score) + ","; |                 validBoxFound = true; | ||||||
|             mqttMessage += "\"x\":" + String(AI.boxes()[i].x) + ","; |                 Serial.print("Box["); | ||||||
|             mqttMessage += "\"y\":" + String(AI.boxes()[i].y) + ","; |                 Serial.print(i); | ||||||
|             mqttMessage += "\"w\":" + String(AI.boxes()[i].w) + ","; |                 Serial.print("] target="); | ||||||
|             mqttMessage += "\"h\":" + String(AI.boxes()[i].h) + "},"; |                 Serial.print(AI.boxes()[i].target); | ||||||
|  |                 Serial.print(", score="); | ||||||
|  |                 Serial.print(AI.boxes()[i].score); | ||||||
|  |                 Serial.print(", x="); | ||||||
|  |                 Serial.print(AI.boxes()[i].x); | ||||||
|  |                 Serial.print(", y="); | ||||||
|  |                 Serial.print(AI.boxes()[i].y); | ||||||
|  |                 Serial.print(", w="); | ||||||
|  |                 Serial.print(AI.boxes()[i].w); | ||||||
|  |                 Serial.print(", h="); | ||||||
|  |                 Serial.println(AI.boxes()[i].h); | ||||||
|  |  | ||||||
|  |                 // Box-Daten zur MQTT-Nachricht hinzufügen | ||||||
|  |                 mqttMessage += "{\"target\":\"" + String(AI.boxes()[i].target) + "\","; | ||||||
|  |                 mqttMessage += "\"score\":" + String(AI.boxes()[i].score) + ","; | ||||||
|  |                 mqttMessage += "\"x\":" + String(AI.boxes()[i].x) + ","; | ||||||
|  |                 mqttMessage += "\"y\":" + String(AI.boxes()[i].y) + ","; | ||||||
|  |                 mqttMessage += "\"w\":" + String(AI.boxes()[i].w) + ","; | ||||||
|  |                 mqttMessage += "\"h\":" + String(AI.boxes()[i].h) + "},"; | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|         if (AI.boxes().size() > 0) { |  | ||||||
|  |         if (validBoxFound) { | ||||||
|             mqttMessage.remove(mqttMessage.length() - 1); // Letztes Komma entfernen |             mqttMessage.remove(mqttMessage.length() - 1); // Letztes Komma entfernen | ||||||
|         } |         } | ||||||
|         mqttMessage += "]}"; |         mqttMessage += "]}"; | ||||||
|  |  | ||||||
|         Serial.println("MQTT-Nachricht: " + mqttMessage); |         // Nur senden, wenn mindestens eine Box mit Score > 50 gefunden wurde | ||||||
|  |         if (validBoxFound) { | ||||||
|  |             Serial.println("MQTT-Nachricht: " + mqttMessage); | ||||||
|  |  | ||||||
|         // MQTT-Nachricht senden |             // MQTT-Nachricht senden | ||||||
|         if (client.publish(mqtt_topic, mqttMessage.c_str())) { |             if (client.publish(mqtt_topic, mqttMessage.c_str())) { | ||||||
|             Serial.println("Daten erfolgreich an MQTT gesendet!"); |                 Serial.println("Daten erfolgreich an MQTT gesendet!"); | ||||||
|  |             } else { | ||||||
|  |                 Serial.println("Fehler beim Senden der MQTT-Daten."); | ||||||
|  |             } | ||||||
|         } else { |         } else { | ||||||
|             Serial.println("Fehler beim Senden der MQTT-Daten."); |             Serial.println("Keine Boxen mit Score > 50 gefunden. Keine Daten gesendet."); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user