diff options
| -rw-r--r-- | analysis/e1anl/Makefile | 27 | ||||
| -rw-r--r-- | analysis/e1anl/__pycache__/agregate.cpython-313.pyc | bin | 0 -> 3423 bytes | |||
| -rw-r--r-- | analysis/e1anl/__pycache__/analyze.cpython-313.pyc | bin | 0 -> 2835 bytes | |||
| -rw-r--r-- | analysis/e1anl/requirements.txt | 16 | ||||
| -rw-r--r-- | analysis/e1anl/src/__pycache__/agregate.cpython-313.pyc | bin | 0 -> 3528 bytes | |||
| -rw-r--r-- | analysis/e1anl/src/__pycache__/analyze.cpython-313.pyc | bin | 0 -> 2962 bytes | |||
| -rw-r--r-- | analysis/e1anl/src/agregate.py | 71 | ||||
| -rw-r--r-- | analysis/e1anl/src/analyze.py | 49 | ||||
| -rw-r--r-- | analysis/e1anl/src/main.py | 17 | ||||
| -rw-r--r-- | analysis/rtt/Dockerfile | 29 | ||||
| -rw-r--r-- | analysis/rtt/Makefile | 22 | ||||
| -rw-r--r-- | analysis/rtt/src/logger.c | 86 | ||||
| -rw-r--r-- | analysis/rtt/src/logger.h | 9 | ||||
| -rw-r--r-- | analysis/rtt/src/main.c | 28 | ||||
| -rw-r--r-- | analysis/rtt/src/mqtt_client.c | 48 | ||||
| -rw-r--r-- | analysis/rtt/src/mqtt_client.h | 16 | ||||
| -rw-r--r-- | analysis/rtt/src/mqtt_rtt_logger.c | 117 | ||||
| -rw-r--r-- | firmware/.clang-format (renamed from .clang-format) | 0 | ||||
| -rw-r--r-- | firmware/.gitignore (renamed from .gitignore) | 0 | ||||
| -rw-r--r-- | firmware/CMakeLists.txt (renamed from CMakeLists.txt) | 0 | ||||
| -rw-r--r-- | firmware/Makefile (renamed from Makefile) | 2 | ||||
| -rw-r--r-- | firmware/include/config.h (renamed from include/config.h) | 0 | ||||
| -rw-r--r-- | firmware/include/mqtt.h (renamed from include/mqtt.h) | 0 | ||||
| -rw-r--r-- | firmware/include/wifi.h (renamed from include/wifi.h) | 0 | ||||
| -rw-r--r-- | firmware/include/wifi_scan.h (renamed from include/wifi_scan.h) | 0 | ||||
| -rw-r--r-- | firmware/lib/README (renamed from lib/README) | 0 | ||||
| -rw-r--r-- | firmware/platformio.ini (renamed from platformio.ini) | 0 | ||||
| -rw-r--r-- | firmware/sdkconfig.esp32dev (renamed from sdkconfig.esp32dev) | 0 | ||||
| -rw-r--r-- | firmware/src/CMakeLists.txt (renamed from src/CMakeLists.txt) | 0 | ||||
| -rw-r--r-- | firmware/src/main.c (renamed from src/main.c) | 0 | ||||
| -rw-r--r-- | firmware/src/mqtt.c (renamed from src/mqtt.c) | 0 | ||||
| -rw-r--r-- | firmware/src/wifi.c (renamed from src/wifi.c) | 0 | ||||
| -rw-r--r-- | firmware/src/wifi_scan.c (renamed from src/wifi_scan.c) | 0 | ||||
| -rw-r--r-- | firmware/test/rtt_test.c (renamed from test/rtt_test.c) | 0 |
34 files changed, 536 insertions, 1 deletions
diff --git a/analysis/e1anl/Makefile b/analysis/e1anl/Makefile new file mode 100644 index 0000000..7cbd4a3 --- /dev/null +++ b/analysis/e1anl/Makefile | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | # Makefile | ||
| 2 | |||
| 3 | VENV_NAME=venv | ||
| 4 | PYTHON=$(VENV_NAME)/bin/python | ||
| 5 | SRC=src | ||
| 6 | OUTPUT=output | ||
| 7 | |||
| 8 | .PHONY: venv install run analyze clean | ||
| 9 | |||
| 10 | venv: | ||
| 11 | python3 -m venv $(VENV_NAME) | ||
| 12 | |||
| 13 | install: venv | ||
| 14 | $(PYTHON) -m pip install --upgrade pip | ||
| 15 | $(PYTHON) -m pip install -r requirements.txt | ||
| 16 | |||
| 17 | run: install | ||
| 18 | mkdir -p $(OUTPUT) | ||
| 19 | $(PYTHON) $(SRC)/main.py | ||
| 20 | |||
| 21 | analyze: install | ||
| 22 | mkdir -p $(OUTPUT) | ||
| 23 | $(PYTHON) $(SRC)/analyze_metrics.py | ||
| 24 | |||
| 25 | clean: | ||
| 26 | rm -rf $(VENV_NAME) | ||
| 27 | rm -rf $(OUTPUT) | ||
diff --git a/analysis/e1anl/__pycache__/agregate.cpython-313.pyc b/analysis/e1anl/__pycache__/agregate.cpython-313.pyc new file mode 100644 index 0000000..21c7510 --- /dev/null +++ b/analysis/e1anl/__pycache__/agregate.cpython-313.pyc | |||
| Binary files differ | |||
diff --git a/analysis/e1anl/__pycache__/analyze.cpython-313.pyc b/analysis/e1anl/__pycache__/analyze.cpython-313.pyc new file mode 100644 index 0000000..0b6a4ce --- /dev/null +++ b/analysis/e1anl/__pycache__/analyze.cpython-313.pyc | |||
| Binary files differ | |||
diff --git a/analysis/e1anl/requirements.txt b/analysis/e1anl/requirements.txt new file mode 100644 index 0000000..3c9cae6 --- /dev/null +++ b/analysis/e1anl/requirements.txt | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | contourpy==1.3.3 | ||
| 2 | cycler==0.12.1 | ||
| 3 | fonttools==4.59.2 | ||
| 4 | kiwisolver==1.4.9 | ||
| 5 | matplotlib==3.10.6 | ||
| 6 | numpy==2.3.2 | ||
| 7 | packaging==25.0 | ||
| 8 | paho-mqtt==2.1.0 | ||
| 9 | pandas==2.3.2 | ||
| 10 | pillow==11.3.0 | ||
| 11 | pyparsing==3.2.3 | ||
| 12 | python-dateutil==2.9.0.post0 | ||
| 13 | pytz==2025.2 | ||
| 14 | seaborn==0.13.2 | ||
| 15 | six==1.17.0 | ||
| 16 | tzdata==2025.2 | ||
diff --git a/analysis/e1anl/src/__pycache__/agregate.cpython-313.pyc b/analysis/e1anl/src/__pycache__/agregate.cpython-313.pyc new file mode 100644 index 0000000..3a0d6cb --- /dev/null +++ b/analysis/e1anl/src/__pycache__/agregate.cpython-313.pyc | |||
| Binary files differ | |||
diff --git a/analysis/e1anl/src/__pycache__/analyze.cpython-313.pyc b/analysis/e1anl/src/__pycache__/analyze.cpython-313.pyc new file mode 100644 index 0000000..a891763 --- /dev/null +++ b/analysis/e1anl/src/__pycache__/analyze.cpython-313.pyc | |||
| Binary files differ | |||
diff --git a/analysis/e1anl/src/agregate.py b/analysis/e1anl/src/agregate.py new file mode 100644 index 0000000..1054288 --- /dev/null +++ b/analysis/e1anl/src/agregate.py | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | # mqtt_receiver.py | ||
| 2 | |||
| 3 | import paho.mqtt.client as mqtt | ||
| 4 | import csv | ||
| 5 | import time | ||
| 6 | from datetime import datetime | ||
| 7 | from collections import deque | ||
| 8 | import os | ||
| 9 | os.makedirs("output", exist_ok=True) | ||
| 10 | |||
| 11 | BROKER = "192.168.1.101" | ||
| 12 | SUB_TOPIC = "device/echo/in" | ||
| 13 | CSV_FILE = "output/rtt_throughput_log.csv" | ||
| 14 | WINDOW_SEC = 1 | ||
| 15 | |||
| 16 | msg_times = deque() | ||
| 17 | should_stop = False # flag to stop loop externally | ||
| 18 | |||
| 19 | |||
| 20 | def on_connect(client, userdata, flags, rc): | ||
| 21 | if rc == 0: | ||
| 22 | print("Connected to broker") | ||
| 23 | client.subscribe(SUB_TOPIC) | ||
| 24 | else: | ||
| 25 | print("Failed to connect, return code:", rc) | ||
| 26 | |||
| 27 | |||
| 28 | def on_message(client, userdata, msg): | ||
| 29 | try: | ||
| 30 | payload_str = msg.payload.decode() | ||
| 31 | sent_ms = int(payload_str) | ||
| 32 | received_ms = int(time.time() * 1000) | ||
| 33 | rtt = received_ms - sent_ms | ||
| 34 | timestamp = datetime.now().isoformat() | ||
| 35 | |||
| 36 | now_sec = time.time() | ||
| 37 | msg_times.append(now_sec) | ||
| 38 | while msg_times and msg_times[0] < now_sec - WINDOW_SEC: | ||
| 39 | msg_times.popleft() | ||
| 40 | throughput = len(msg_times) / WINDOW_SEC | ||
| 41 | |||
| 42 | with open(CSV_FILE, "a", newline="") as f: | ||
| 43 | writer = csv.writer(f) | ||
| 44 | writer.writerow([timestamp, sent_ms, received_ms, rtt, throughput]) | ||
| 45 | |||
| 46 | except Exception as e: | ||
| 47 | print("Error processing message:", e) | ||
| 48 | |||
| 49 | |||
| 50 | def collect_data(duration_sec): | ||
| 51 | global should_stop | ||
| 52 | should_stop = False | ||
| 53 | |||
| 54 | # Start CSV log | ||
| 55 | with open(CSV_FILE, "w", newline="") as f: | ||
| 56 | writer = csv.writer(f) | ||
| 57 | writer.writerow(["timestamp", "sent_ms", "received_ms", "rtt_ms", "throughput_msg_per_s"]) | ||
| 58 | |||
| 59 | client = mqtt.Client() | ||
| 60 | client.on_connect = on_connect | ||
| 61 | client.on_message = on_message | ||
| 62 | client.connect(BROKER, 1883, 60) | ||
| 63 | |||
| 64 | client.loop_start() | ||
| 65 | print(f"Zbieranie danych przez {duration_sec / 60:.1f} minut...") | ||
| 66 | |||
| 67 | time.sleep(duration_sec) | ||
| 68 | |||
| 69 | print("Zbieranie danych zakończone.") | ||
| 70 | client.loop_stop() | ||
| 71 | client.disconnect() | ||
diff --git a/analysis/e1anl/src/analyze.py b/analysis/e1anl/src/analyze.py new file mode 100644 index 0000000..6e73df3 --- /dev/null +++ b/analysis/e1anl/src/analyze.py | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | # analyze_metrics.py | ||
| 2 | |||
| 3 | import pandas as pd | ||
| 4 | import matplotlib.pyplot as plt | ||
| 5 | import seaborn as sns | ||
| 6 | |||
| 7 | import os | ||
| 8 | os.makedirs("output", exist_ok=True) | ||
| 9 | |||
| 10 | def analyze(): | ||
| 11 | CSV_FILE = "output/rtt_throughput_log.csv" | ||
| 12 | df = pd.read_csv(CSV_FILE) | ||
| 13 | |||
| 14 | df['sent_ms'] = df['sent_ms'].astype(int) | ||
| 15 | df['received_ms'] = df['received_ms'].astype(int) | ||
| 16 | df['rtt_ms'] = df['rtt_ms'].astype(int) | ||
| 17 | df['throughput_msg_per_s'] = df['throughput_msg_per_s'].astype(float) | ||
| 18 | |||
| 19 | mean_rtt = df['rtt_ms'].mean() | ||
| 20 | p95_rtt = df['rtt_ms'].quantile(0.95) | ||
| 21 | mean_throughput = df['throughput_msg_per_s'].mean() | ||
| 22 | |||
| 23 | print(f"Średnie RTT: {mean_rtt:.2f} ms") | ||
| 24 | print(f"RTT 95-percentyl: {p95_rtt:.2f} ms") | ||
| 25 | print(f"Średni throughput: {mean_throughput:.2f} msg/s") | ||
| 26 | |||
| 27 | plt.figure(figsize=(6,4)) | ||
| 28 | sns.barplot(x=['Variant A'], y=[mean_rtt], palette="Set2") | ||
| 29 | plt.title("Średnie RTT - wariant A") | ||
| 30 | plt.ylabel("RTT [ms]") | ||
| 31 | plt.tight_layout() | ||
| 32 | plt.savefig("output/rtt_mean_a.png") | ||
| 33 | plt.show() | ||
| 34 | |||
| 35 | plt.figure(figsize=(6,4)) | ||
| 36 | sns.barplot(x=['Variant A'], y=[p95_rtt], palette="Set3") | ||
| 37 | plt.title("RTT 95-percentyl - wariant A") | ||
| 38 | plt.ylabel("RTT [ms]") | ||
| 39 | plt.tight_layout() | ||
| 40 | plt.savefig("output/rtt_p95_a.png") | ||
| 41 | plt.show() | ||
| 42 | |||
| 43 | plt.figure(figsize=(6,4)) | ||
| 44 | sns.barplot(x=['Variant A'], y=[mean_throughput], palette="Set1") | ||
| 45 | plt.title("Średni throughput - wariant A") | ||
| 46 | plt.ylabel("Messages per second") | ||
| 47 | plt.tight_layout() | ||
| 48 | plt.savefig("output/throughput_a.png") | ||
| 49 | plt.show() | ||
diff --git a/analysis/e1anl/src/main.py b/analysis/e1anl/src/main.py new file mode 100644 index 0000000..b35eead --- /dev/null +++ b/analysis/e1anl/src/main.py | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | # main.py | ||
| 2 | |||
| 3 | from agregate import collect_data | ||
| 4 | from analyze import analyze | ||
| 5 | |||
| 6 | def main(): | ||
| 7 | DURATION_HOURS = 2 | ||
| 8 | DURATION_SECONDS = DURATION_HOURS * 3600 | ||
| 9 | |||
| 10 | print("Collecting data...") | ||
| 11 | collect_data(DURATION_SECONDS) | ||
| 12 | |||
| 13 | print("Analyzing data...") | ||
| 14 | analyze() | ||
| 15 | |||
| 16 | if __name__ == "__main__": | ||
| 17 | main() | ||
diff --git a/analysis/rtt/Dockerfile b/analysis/rtt/Dockerfile new file mode 100644 index 0000000..cb58f62 --- /dev/null +++ b/analysis/rtt/Dockerfile | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | # FROM debian:bookworm-slim | ||
| 2 | # RUN apt-get update && apt-get install -y \ | ||
| 3 | # gcc \ | ||
| 4 | # make \ | ||
| 5 | # libmosquitto-dev \ | ||
| 6 | # && rm -rf /var/lib/apt/lists/* | ||
| 7 | # WORKDIR /app | ||
| 8 | # COPY . . | ||
| 9 | # RUN make | ||
| 10 | |||
| 11 | FROM alpine:latest AS builder | ||
| 12 | |||
| 13 | RUN apk add --no-cache \ | ||
| 14 | gcc \ | ||
| 15 | make \ | ||
| 16 | musl-dev \ | ||
| 17 | mosquitto-dev | ||
| 18 | |||
| 19 | WORKDIR /app | ||
| 20 | COPY Makefile mqtt_rtt_logger.c ./ | ||
| 21 | RUN make | ||
| 22 | |||
| 23 | FROM alpine:latest | ||
| 24 | |||
| 25 | WORKDIR /app | ||
| 26 | |||
| 27 | COPY --from=builder /app/build/mqtt_rtt_logger ./mqtt_rtt_logger | ||
| 28 | |||
| 29 | CMD ["./mqtt_rtt_logger"] | ||
diff --git a/analysis/rtt/Makefile b/analysis/rtt/Makefile new file mode 100644 index 0000000..8b8d38e --- /dev/null +++ b/analysis/rtt/Makefile | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | TARGET = mqtt_rtt_logger | ||
| 2 | SRC_DIR = src | ||
| 3 | SRC = $(SRC_DIR)/mqtt_rtt_logger.c | ||
| 4 | OUT_DIR = build | ||
| 5 | CC = gcc | ||
| 6 | CFLAGS = -Wall -Wextra -O2 | ||
| 7 | LDFLAGS = -lmosquitto | ||
| 8 | DOCKER_IMAGE = mqtt_rtt_logger-c-mqtt-logger | ||
| 9 | |||
| 10 | all: $(OUT_DIR) $(OUT_DIR)/$(TARGET) | ||
| 11 | |||
| 12 | $(OUT_DIR): | ||
| 13 | mkdir -p $(OUT_DIR) | ||
| 14 | |||
| 15 | $(OUT_DIR)/$(TARGET): $(SRC) | ||
| 16 | $(CC) $(CFLAGS) -o $@ $(SRC) $(LDFLAGS) | ||
| 17 | |||
| 18 | clean: | ||
| 19 | rm -rf $(OUT_DIR) | ||
| 20 | |||
| 21 | docker-build: $(OUT_DIR) | ||
| 22 | docker run --rm -v "$(PWD):/app" -w /app $(DOCKER_IMAGE) make all | ||
diff --git a/analysis/rtt/src/logger.c b/analysis/rtt/src/logger.c new file mode 100644 index 0000000..ed5f6e5 --- /dev/null +++ b/analysis/rtt/src/logger.c | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | |||
| 2 | #include "logger.h" | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include <sys/time.h> | ||
| 7 | #include <time.h> | ||
| 8 | |||
| 9 | #define WINDOW_SEC 1 | ||
| 10 | #define MAX_TIMES 1000 | ||
| 11 | |||
| 12 | static FILE *csv_file = NULL; | ||
| 13 | static double msg_times[MAX_TIMES]; | ||
| 14 | static int msg_count = 0; | ||
| 15 | |||
| 16 | static long long current_time_ms() { | ||
| 17 | struct timeval tv; | ||
| 18 | gettimeofday(&tv, NULL); | ||
| 19 | return ((long long)tv.tv_sec * 1000) + (tv.tv_usec / 1000); | ||
| 20 | } | ||
| 21 | |||
| 22 | static double current_time_sec() { | ||
| 23 | struct timeval tv; | ||
| 24 | gettimeofday(&tv, NULL); | ||
| 25 | return (double)tv.tv_sec + (tv.tv_usec / 1e6); | ||
| 26 | } | ||
| 27 | |||
| 28 | static int update_throughput_window(double now) { | ||
| 29 | int i, new_count = 0; | ||
| 30 | for (i = 0; i < msg_count; i++) { | ||
| 31 | if (now - msg_times[i] < WINDOW_SEC) { | ||
| 32 | msg_times[new_count++] = msg_times[i]; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | msg_times[new_count++] = now; | ||
| 36 | msg_count = new_count; | ||
| 37 | return msg_count; | ||
| 38 | } | ||
| 39 | |||
| 40 | void logger_init(const char *filename) { | ||
| 41 | csv_file = fopen(filename, "w"); | ||
| 42 | if (!csv_file) { | ||
| 43 | perror("Cannot open CSV file"); | ||
| 44 | exit(EXIT_FAILURE); | ||
| 45 | } | ||
| 46 | fprintf(csv_file, | ||
| 47 | "timestamp,sent_ms,received_ms,rtt_ms,throughput_msg_per_s\n"); | ||
| 48 | fflush(csv_file); | ||
| 49 | } | ||
| 50 | |||
| 51 | void logger_cleanup() { | ||
| 52 | if (csv_file) { | ||
| 53 | fclose(csv_file); | ||
| 54 | csv_file = NULL; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | void logger_handle_message(const void *payload, int payloadlen) { | ||
| 59 | char *payload_str = malloc(payloadlen + 1); | ||
| 60 | if (!payload_str) | ||
| 61 | return; | ||
| 62 | memcpy(payload_str, payload, payloadlen); | ||
| 63 | payload_str[payloadlen] = '\0'; | ||
| 64 | |||
| 65 | long long sent_ms = atoll(payload_str); | ||
| 66 | free(payload_str); | ||
| 67 | |||
| 68 | long long received_ms = current_time_ms(); | ||
| 69 | long long rtt = received_ms - sent_ms; | ||
| 70 | |||
| 71 | double now_sec = current_time_sec(); | ||
| 72 | int throughput = update_throughput_window(now_sec); | ||
| 73 | |||
| 74 | time_t now = time(NULL); | ||
| 75 | struct tm *tm_info = localtime(&now); | ||
| 76 | char iso_time[32]; | ||
| 77 | strftime(iso_time, sizeof(iso_time), "%Y-%m-%dT%H:%M:%S", tm_info); | ||
| 78 | |||
| 79 | if (csv_file) { | ||
| 80 | fprintf(csv_file, "%s,%lld,%lld,%lld,%d\n", iso_time, sent_ms, received_ms, | ||
| 81 | rtt, throughput); | ||
| 82 | fflush(csv_file); | ||
| 83 | } | ||
| 84 | |||
| 85 | printf("RTT: %lld ms | Throughput: %d msg/s\n", rtt, throughput); | ||
| 86 | } | ||
diff --git a/analysis/rtt/src/logger.h b/analysis/rtt/src/logger.h new file mode 100644 index 0000000..f1602c9 --- /dev/null +++ b/analysis/rtt/src/logger.h | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | |||
| 2 | #ifndef LOGGER_H | ||
| 3 | #define LOGGER_H | ||
| 4 | |||
| 5 | void logger_init(const char *filename); | ||
| 6 | void logger_cleanup(); | ||
| 7 | void logger_handle_message(const void *payload, int payloadlen); | ||
| 8 | |||
| 9 | #endif // LOGGER_H | ||
diff --git a/analysis/rtt/src/main.c b/analysis/rtt/src/main.c new file mode 100644 index 0000000..13f7716 --- /dev/null +++ b/analysis/rtt/src/main.c | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | |||
| 2 | #include "logger.h" | ||
| 3 | #include "mqtt_client.h" | ||
| 4 | #include <stdio.h> | ||
| 5 | |||
| 6 | #define BROKER_ADDRESS "192.168.1.101" | ||
| 7 | #define BROKER_PORT 1883 | ||
| 8 | #define TOPIC "device/echo/in" | ||
| 9 | #define CSV_FILE "rtt_throughput_log.csv" | ||
| 10 | |||
| 11 | int main() { | ||
| 12 | mqtt_client_t client; | ||
| 13 | |||
| 14 | logger_init(CSV_FILE); | ||
| 15 | |||
| 16 | if (mqtt_client_init(&client, BROKER_ADDRESS, BROKER_PORT, TOPIC) != 0) { | ||
| 17 | fprintf(stderr, "MQTT initialization error\n"); | ||
| 18 | return 1; | ||
| 19 | } | ||
| 20 | |||
| 21 | printf("Connection established.Waiting for messages...\n"); | ||
| 22 | mqtt_client_loop(&client); | ||
| 23 | |||
| 24 | logger_cleanup(); | ||
| 25 | mqtt_client_cleanup(&client); | ||
| 26 | |||
| 27 | return 0; | ||
| 28 | } | ||
diff --git a/analysis/rtt/src/mqtt_client.c b/analysis/rtt/src/mqtt_client.c new file mode 100644 index 0000000..43a06d8 --- /dev/null +++ b/analysis/rtt/src/mqtt_client.c | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #include "mqtt_client.h" | ||
| 2 | #include "logger.h" | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | |||
| 6 | #define WINDOW_SEC 1 | ||
| 7 | |||
| 8 | static void on_message(struct mosquitto *mosq, void *userdata, | ||
| 9 | const struct mosquitto_message *msg); | ||
| 10 | |||
| 11 | int mqtt_client_init(mqtt_client_t *client, const char *broker_address, | ||
| 12 | int port, const char *topic) { | ||
| 13 | mosquitto_lib_init(); | ||
| 14 | client->mosq = mosquitto_new(NULL, true, NULL); | ||
| 15 | if (!client->mosq) { | ||
| 16 | fprintf(stderr, "Cannot create MQTT client\n"); | ||
| 17 | return 1; | ||
| 18 | } | ||
| 19 | |||
| 20 | mosquitto_message_callback_set(client->mosq, on_message); | ||
| 21 | |||
| 22 | if (mosquitto_connect(client->mosq, broker_address, port, 60) != | ||
| 23 | MOSQ_ERR_SUCCESS) { | ||
| 24 | fprintf(stderr, "Nie można połączyć się z brokerem MQTT\n"); | ||
| 25 | return 1; | ||
| 26 | } | ||
| 27 | |||
| 28 | if (mosquitto_subscribe(client->mosq, NULL, topic, 0) != MOSQ_ERR_SUCCESS) { | ||
| 29 | fprintf(stderr, "Cannot subscribe to topic\n"); | ||
| 30 | return 1; | ||
| 31 | } | ||
| 32 | |||
| 33 | return 0; | ||
| 34 | } | ||
| 35 | |||
| 36 | void mqtt_client_cleanup(mqtt_client_t *client) { | ||
| 37 | mosquitto_destroy(client->mosq); | ||
| 38 | mosquitto_lib_cleanup(); | ||
| 39 | } | ||
| 40 | |||
| 41 | void mqtt_client_loop(mqtt_client_t *client) { | ||
| 42 | mosquitto_loop_forever(client->mosq, -1, 1); | ||
| 43 | } | ||
| 44 | |||
| 45 | static void on_message(struct mosquitto *mosq, void *userdata, | ||
| 46 | const struct mosquitto_message *msg) { | ||
| 47 | logger_handle_message(msg->payload, msg->payloadlen); | ||
| 48 | } | ||
diff --git a/analysis/rtt/src/mqtt_client.h b/analysis/rtt/src/mqtt_client.h new file mode 100644 index 0000000..781e742 --- /dev/null +++ b/analysis/rtt/src/mqtt_client.h | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | |||
| 2 | #ifndef MQTT_CLIENT_H | ||
| 3 | #define MQTT_CLIENT_H | ||
| 4 | |||
| 5 | #include <mosquitto.h> | ||
| 6 | |||
| 7 | typedef struct { | ||
| 8 | struct mosquitto *mosq; | ||
| 9 | } mqtt_client_t; | ||
| 10 | |||
| 11 | int mqtt_client_init(mqtt_client_t *client, const char *broker_address, | ||
| 12 | int port, const char *topic); | ||
| 13 | void mqtt_client_cleanup(mqtt_client_t *client); | ||
| 14 | void mqtt_client_loop(mqtt_client_t *client); | ||
| 15 | |||
| 16 | #endif // MQTT_CLIENT_H | ||
diff --git a/analysis/rtt/src/mqtt_rtt_logger.c b/analysis/rtt/src/mqtt_rtt_logger.c new file mode 100644 index 0000000..60e5d2d --- /dev/null +++ b/analysis/rtt/src/mqtt_rtt_logger.c | |||
| @@ -0,0 +1,117 @@ | |||
| 1 | // #include <mosquitto.h> | ||
| 2 | // #include <stdio.h> | ||
| 3 | // #include <stdlib.h> | ||
| 4 | // #include <string.h> | ||
| 5 | // #include <sys/time.h> | ||
| 6 | // #include <time.h> | ||
| 7 | // #include <unistd.h> | ||
| 8 | // | ||
| 9 | // #define BROKER_ADDRESS "192.168.1.101" | ||
| 10 | // #define BROKER_PORT 1883 | ||
| 11 | // #define TOPIC "device/echo/in" | ||
| 12 | // #define CSV_FILE "rtt_throughput_log.csv" | ||
| 13 | // #define WINDOW_SEC 1 | ||
| 14 | // #define MAX_TIMES 1000 | ||
| 15 | // | ||
| 16 | // // Kolejka czasów przyjścia wiadomości (dla throughput) | ||
| 17 | // double msg_times[MAX_TIMES]; | ||
| 18 | // int msg_count = 0; | ||
| 19 | // | ||
| 20 | // // Zwraca aktualny czas w milisekundach | ||
| 21 | // long long current_time_ms() { | ||
| 22 | // struct timeval tv; | ||
| 23 | // gettimeofday(&tv, NULL); | ||
| 24 | // return ((long long)tv.tv_sec * 1000) + (tv.tv_usec / 1000); | ||
| 25 | // } | ||
| 26 | // | ||
| 27 | // // Zwraca aktualny czas w sekundach z ułamkiem | ||
| 28 | // double current_time_sec() { | ||
| 29 | // struct timeval tv; | ||
| 30 | // gettimeofday(&tv, NULL); | ||
| 31 | // return (double)tv.tv_sec + (tv.tv_usec / 1e6); | ||
| 32 | // } | ||
| 33 | // | ||
| 34 | // // Aktualizuje throughput window i zwraca liczbę wiadomości w oknie | ||
| 35 | // int update_throughput_window(double now) { | ||
| 36 | // int i, new_count = 0; | ||
| 37 | // for (i = 0; i < msg_count; i++) { | ||
| 38 | // if (now - msg_times[i] < WINDOW_SEC) { | ||
| 39 | // msg_times[new_count++] = msg_times[i]; | ||
| 40 | // } | ||
| 41 | // } | ||
| 42 | // msg_times[new_count++] = now; | ||
| 43 | // msg_count = new_count; | ||
| 44 | // return msg_count; | ||
| 45 | // } | ||
| 46 | // | ||
| 47 | // // Callback po odebraniu wiadomości | ||
| 48 | // void on_message(struct mosquitto *mosq, void *userdata, | ||
| 49 | // const struct mosquitto_message *msg) { | ||
| 50 | // char *payload = (char *)msg->payload; | ||
| 51 | // long long sent_ms = atoll(payload); | ||
| 52 | // long long received_ms = current_time_ms(); | ||
| 53 | // long long rtt = received_ms - sent_ms; | ||
| 54 | // | ||
| 55 | // double now_sec = current_time_sec(); | ||
| 56 | // int throughput = update_throughput_window(now_sec); | ||
| 57 | // | ||
| 58 | // // Timestamp ISO 8601 | ||
| 59 | // time_t now = time(NULL); | ||
| 60 | // struct tm *tm_info = localtime(&now); | ||
| 61 | // char iso_time[32]; | ||
| 62 | // strftime(iso_time, sizeof(iso_time), "%Y-%m-%dT%H:%M:%S", tm_info); | ||
| 63 | // | ||
| 64 | // // Zapisz do CSV | ||
| 65 | // FILE *f = fopen(CSV_FILE, "a"); | ||
| 66 | // if (f) { | ||
| 67 | // fprintf(f, "%s,%lld,%lld,%lld,%d\n", iso_time, sent_ms, received_ms, rtt, | ||
| 68 | // throughput); | ||
| 69 | // fclose(f); | ||
| 70 | // } | ||
| 71 | // | ||
| 72 | // // Wydruk | ||
| 73 | // printf("RTT: %lld ms | Throughput: %d msg/s\n", rtt, throughput); | ||
| 74 | // } | ||
| 75 | // | ||
| 76 | // int main() { | ||
| 77 | // printf("Start programu\n"); | ||
| 78 | // | ||
| 79 | // FILE *f = fopen(CSV_FILE, "w"); | ||
| 80 | // if (!f) { | ||
| 81 | // perror("Nie można otworzyć pliku CSV"); | ||
| 82 | // return 1; | ||
| 83 | // } | ||
| 84 | // fprintf(f, "timestamp,sent_ms,received_ms,rtt_ms,throughput_msg_per_s\n"); | ||
| 85 | // fclose(f); | ||
| 86 | // printf("Plik CSV utworzony\n"); | ||
| 87 | // | ||
| 88 | // mosquitto_lib_init(); | ||
| 89 | // struct mosquitto *mosq = mosquitto_new(NULL, true, NULL); | ||
| 90 | // if (!mosq) { | ||
| 91 | // fprintf(stderr, "Błąd tworzenia klienta MQTT\n"); | ||
| 92 | // return 1; | ||
| 93 | // } | ||
| 94 | // printf("Klient MQTT utworzony\n"); | ||
| 95 | // | ||
| 96 | // mosquitto_message_callback_set(mosq, on_message); | ||
| 97 | // | ||
| 98 | // int rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60); | ||
| 99 | // if (rc != MOSQ_ERR_SUCCESS) { | ||
| 100 | // fprintf(stderr, "Nie można połączyć się z brokerem MQTT, kod błędu: | ||
| 101 | // %d\n", | ||
| 102 | // rc); | ||
| 103 | // return 1; | ||
| 104 | // } | ||
| 105 | // printf("Połączono z brokerem MQTT\n"); | ||
| 106 | // | ||
| 107 | // mosquitto_subscribe(mosq, NULL, TOPIC, 0); | ||
| 108 | // printf("Subskrypcja tematu %s ustawiona\n", TOPIC); | ||
| 109 | // | ||
| 110 | // printf("Oczekiwanie na wiadomości...\n"); | ||
| 111 | // mosquitto_loop_forever(mosq, -1, 1); | ||
| 112 | // | ||
| 113 | // mosquitto_destroy(mosq); | ||
| 114 | // mosquitto_lib_cleanup(); | ||
| 115 | // | ||
| 116 | // return 0; | ||
| 117 | // } | ||
diff --git a/.clang-format b/firmware/.clang-format index b838e4d..b838e4d 100644 --- a/.clang-format +++ b/firmware/.clang-format | |||
diff --git a/.gitignore b/firmware/.gitignore index 7a5e174..7a5e174 100644 --- a/.gitignore +++ b/firmware/.gitignore | |||
diff --git a/CMakeLists.txt b/firmware/CMakeLists.txt index 083eafe..083eafe 100644 --- a/CMakeLists.txt +++ b/firmware/CMakeLists.txt | |||
diff --git a/Makefile b/firmware/Makefile index b9f8a1f..0fd1699 100644 --- a/Makefile +++ b/firmware/Makefile | |||
| @@ -30,4 +30,4 @@ debug-flags: | |||
| 30 | 30 | ||
| 31 | test: | 31 | test: |
| 32 | @echo "Starting unit tests..." | 32 | @echo "Starting unit tests..." |
| 33 | PLATFORMIO_BUILD_FLAGS="$(BUILD_FLAGS)" pio test -e $(ENV) -v | 33 | PLATFORMIO_BUILD_FLAGS="$(BUILD_FLAGS)" pio test -e $(ENV) -vvv |
diff --git a/include/config.h b/firmware/include/config.h index 467e1f7..467e1f7 100644 --- a/include/config.h +++ b/firmware/include/config.h | |||
diff --git a/include/mqtt.h b/firmware/include/mqtt.h index 4f46b5a..4f46b5a 100644 --- a/include/mqtt.h +++ b/firmware/include/mqtt.h | |||
diff --git a/include/wifi.h b/firmware/include/wifi.h index bdcbe76..bdcbe76 100644 --- a/include/wifi.h +++ b/firmware/include/wifi.h | |||
diff --git a/include/wifi_scan.h b/firmware/include/wifi_scan.h index dc18e7c..dc18e7c 100644 --- a/include/wifi_scan.h +++ b/firmware/include/wifi_scan.h | |||
diff --git a/lib/README b/firmware/lib/README index 9379397..9379397 100644 --- a/lib/README +++ b/firmware/lib/README | |||
diff --git a/platformio.ini b/firmware/platformio.ini index 4f199c3..4f199c3 100644 --- a/platformio.ini +++ b/firmware/platformio.ini | |||
diff --git a/sdkconfig.esp32dev b/firmware/sdkconfig.esp32dev index f99ba72..f99ba72 100644 --- a/sdkconfig.esp32dev +++ b/firmware/sdkconfig.esp32dev | |||
diff --git a/src/CMakeLists.txt b/firmware/src/CMakeLists.txt index 483bc0c..483bc0c 100644 --- a/src/CMakeLists.txt +++ b/firmware/src/CMakeLists.txt | |||
diff --git a/src/main.c b/firmware/src/main.c index 790959d..790959d 100644 --- a/src/main.c +++ b/firmware/src/main.c | |||
diff --git a/src/mqtt.c b/firmware/src/mqtt.c index 716feec..716feec 100644 --- a/src/mqtt.c +++ b/firmware/src/mqtt.c | |||
diff --git a/src/wifi.c b/firmware/src/wifi.c index 8d6b29d..8d6b29d 100644 --- a/src/wifi.c +++ b/firmware/src/wifi.c | |||
diff --git a/src/wifi_scan.c b/firmware/src/wifi_scan.c index e588cfc..e588cfc 100644 --- a/src/wifi_scan.c +++ b/firmware/src/wifi_scan.c | |||
diff --git a/test/rtt_test.c b/firmware/test/rtt_test.c index cdedabd..cdedabd 100644 --- a/test/rtt_test.c +++ b/firmware/test/rtt_test.c | |||
