You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This command pulls the latest model from the Edge Impulse container registry and runs an HTTP server on port `1337`, allowing external applications to send sensor data and receive model predictions. It uses an API key to authenticate requests.
@@ -528,38 +529,67 @@ The M4 core needs to have the **`rpc-flow-sensor.ino`** uploaded to collect and
528
529
#include <Arduino.h>
529
530
#include <RPC.h>
530
531
#include <SerialRPC.h>
531
-
#include <FlowSensor.h>
532
+
#include <FlowSensor.h> // Include the FlowSensor library
533
+
534
+
// Define Serial Debug Output (For Portenta X8 Carrier Usage)
535
+
#if defined(ARDUINO_PORTENTA_X8)
536
+
#define SerialDebug Serial1 // Use Serial1 for debugging on Portenta X8 Carrier
537
+
#else
538
+
#define SerialDebug Serial
539
+
#endif
532
540
533
541
// Define Flow Sensor Type and Pin
534
542
#define SENSOR_TYPE YFS201
535
-
#define SENSOR_PIN PC_7 // Flow sensor signal pin
543
+
#define SENSOR_PIN PC_7 // Flow sensor signal pin
536
544
545
+
// Create Flow Sensor Object
537
546
FlowSensor flowSensor(SENSOR_TYPE, SENSOR_PIN);
538
547
539
548
void count() {
540
549
flowSensor.count();
541
550
}
542
551
543
-
// Function to calculate and return flow rate (for RPC)
552
+
// Function to get Flow Rate (for RPC)
544
553
float getFlowRate() {
545
554
flowSensor.read();
546
555
float flowRate = flowSensor.getFlowRate_m(); // Get flow rate in L/min
547
556
548
557
if (isnan(flowRate) || isinf(flowRate)) {
549
-
return 0.0; // Default to 0 if no valid reading
558
+
return 0.0; // Default to 0 if no valid reading
550
559
}
551
560
return flowRate;
552
561
}
553
562
554
563
void setup() {
555
-
flowSensor.begin(count);
564
+
SerialDebug.begin(115200);
565
+
//while (!SerialDebug);
566
+
567
+
SerialDebug.println("Starting Flow Sensor ");
568
+
569
+
flowSensor.begin(count); // Initialize the Flow Sensor
The compose file will require the **container, arguments and port values** that were generated when deployed the machine learning model as explained [here](#deployment-and-real-time-inference).
623
656
624
657
The `main.py` script receives flow sensor data from the M4 core and sends it to the inference container.
625
658
626
659
```python
660
+
#!/usr/bin/env python3
627
661
import os
628
662
import time
663
+
import requests # type:ignore
664
+
import signal
665
+
import sys
629
666
import json
630
667
import argparse
631
-
from msgpackrpc import Address as RpcAddress, Client as RpcClient, error as RpcError
632
668
633
-
# Retrieve M4 Proxy settings from environment variables (or use defaults)
result = client.call("classification", request_data)
717
-
print(f"Sent to {m4_proxy_host}:{m4_proxy_port}: {request_data}. Result: {result}")
747
+
res = client.call('classification', request_data)
718
748
except RpcError.TimeoutError:
719
-
print("Unable to send classification data to M4: RPC Timeout.")
749
+
print("Unable to retrieve data from the M4: RPC Timeout. - B")
750
+
751
+
print(f"Sent to {m4_proxy_host} on port {m4_proxy_port}: {request_data}. Result is {res}.")
720
752
else:
721
753
print("No classification found.")
722
754
755
+
723
756
if__name__=="__main__":
724
-
parser = argparse.ArgumentParser(description="Get flow sensor data and send it to inference container for classification")
757
+
parser = argparse.ArgumentParser(description="Get 1 second of sensors data and send to inference container for classification")
725
758
parser.add_argument("host", help="The hostname or IP address of the inference server")
726
759
parser.add_argument("port", type=int, help="The port number of the inference server")
727
760
761
+
# Parse the arguments
728
762
args = parser.parse_args()
729
763
730
-
print("Classifying Flow Sensor Data with AI... Press Ctrl+C to stop.")
764
+
# Signal handler to handle Ctrl+C (SIGINT) gracefully
765
+
defsignal_handler(_sig, _frame):
766
+
print("Exiting...")
767
+
sys.exit(0)
731
768
732
-
try:
733
-
get_sensors_and_classify(args.host, args.port)
734
-
exceptKeyboardInterrupt:
735
-
print("Exiting gracefully...")
769
+
# Register signal handler for graceful exit on Ctrl+C
770
+
signal.signal(signal.SIGINT, signal_handler)
771
+
772
+
print("Classifying Flow Sensor Data with AI... Press Ctrl+C to stop.")
773
+
print(f"Running model type: {model_type}")
774
+
# Run the capture, upload, and process function
775
+
get_sensors_and_classify(args.host, args.port)
736
776
```
737
777
738
778
The complete project files can be downloaded in [this section](#download-the-project-code).
@@ -771,6 +811,20 @@ docker compose logs -f -n 10
771
811
772
812
The system will start to collect flow sensor data, process it with the Edge Impulse model inside a Docker container and detect anomalies.
773
813
814
+
For more controlled execution:
815
+
816
+
```bash
817
+
docker compose up -d
818
+
```
819
+
820
+
```bash
821
+
docker exec -it <container-id> sh
822
+
```
823
+
824
+
```bash
825
+
python3 -u /app/python/main.py ei-inference 1337
826
+
```
827
+
774
828
### Arduino Cloud Integration
775
829
776
830
Now that the Portenta X8 is running the Docker container with the trained inference model, the next step is integrating Arduino Cloud for active monitoring and logging of anomaly detection results.
Once the files are updated, it needs to be pushed to the Portenta X8 again, navigate to the project directory and use the following commands:
@@ -963,6 +1020,20 @@ Once deployed, the system will begin to get flow sensor data, classify it using
963
1020
964
1021

965
1022
1023
+
For more controlled execution:
1024
+
1025
+
```bash
1026
+
docker compose --env-file .env up -d
1027
+
```
1028
+
1029
+
```bash
1030
+
docker exec -it <container-id> sh
1031
+
```
1032
+
1033
+
```bash
1034
+
python3 -u /app/python/main.py ei-inference 1337
1035
+
```
1036
+
966
1037
This integration allows real-time anomaly detection and cloud-based monitoring, combining edge inference on Portenta X8 with Arduino Cloud analytics. Users can remotely track flow rate anomalies, set up alerts and analyze historical trends to improve predictive maintenance of the system's point of interest.
0 commit comments