From 031acac45bc69f790d9e82ecfc8bf97164354d56 Mon Sep 17 00:00:00 2001 From: GK-M Date: Tue, 17 Mar 2026 08:26:54 +0100 Subject: [PATCH 1/9] all without mqtt --- main_blinky.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main_blinky.c b/main_blinky.c index 21b39fa..cfbeb80 100644 --- a/main_blinky.c +++ b/main_blinky.c @@ -256,6 +256,9 @@ void vSendData(void *pvParameters){ if (xSemaphoreTake(czekaj_na_dane, portMAX_DELAY) == pdTRUE){ printf("Przesyłanie danych do C2 \n"); + + + vTaskDelay(pdMS_TO_TICKS(2000)); xSemaphoreGive(czekaj_na_dane); From 1be6d970c9e282209f3fd96cd7991c5b67388565 Mon Sep 17 00:00:00 2001 From: GK-M Date: Tue, 17 Mar 2026 11:38:50 +0100 Subject: [PATCH 2/9] local data --- Makefile | 2 ++ main_blinky.c | 53 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index de2f1a0..e0ba2d4 100644 --- a/Makefile +++ b/Makefile @@ -25,10 +25,12 @@ INCLUDE_DIRS += -I${FREERTOS_PLUS_DIR}/Source/FreeRTOS-Plus-Trace/strea INCLUDE_DIRS += -I${FREERTOS_PLUS_DIR}/Source/FreeRTOS-Plus-Trace/kernelports/FreeRTOS/include INCLUDE_DIRS += -I${FREERTOS_PLUS_DIR}/Source/FreeRTOS-Plus-Trace/kernelports/FreeRTOS/ + SOURCE_FILES := $(wildcard *.c) SOURCE_FILES += $(wildcard ${FREERTOS_DIR}/Source/*.c) # Memory manager (use malloc() / free() ) SOURCE_FILES += ${KERNEL_DIR}/portable/MemMang/heap_3.c + # posix port SOURCE_FILES += ${KERNEL_DIR}/portable/ThirdParty/GCC/Posix/utils/wait_for_event.c SOURCE_FILES += ${KERNEL_DIR}/portable/ThirdParty/GCC/Posix/port.c diff --git a/main_blinky.c b/main_blinky.c index cfbeb80..7522fc5 100644 --- a/main_blinky.c +++ b/main_blinky.c @@ -1,6 +1,7 @@ #include // printf(), FILE, popen(), pclose() #include // ogólne funkcje standardowe #include // operacje na stringach +#include //bibliotek do bool #include "FreeRTOS.h" #include "task.h" @@ -10,6 +11,14 @@ #include "cJSON.h" // biblioteka do parsowania JSON +#include + +//Dane do mosquitto connect +#define IP_adress "10.255.150.118" +#define port 1883 +#define life_time 60 //Ile ma czekać na brokera (ping) + + // Klucz API z OpenWeatherMap #define OWM_API_KEY "d88799a668f54c560b3f420cd37685fd" @@ -60,6 +69,9 @@ TaskHandle_t send_data = NULL; //Handler od Timera TimerHandle_t myTimer = NULL; +//Handler kolejki +QueueHandle_t jsonQueue; + //Semafor do wysłania danych SemaphoreHandle_t czekaj_na_dane = NULL; static SemaphoreHandle_t dataReadySem = NULL; @@ -83,7 +95,7 @@ static int fetch_owm_json(char *json_buf, size_t buf_size, float latitudes, floa fp = popen(cmd, "r"); if (fp == NULL) { - return -1; + } while (!feof(fp) && total < buf_size - 1) @@ -227,7 +239,9 @@ void vWeatherTask(void *pvParameters){ printf("%s\n", json_text); free(json_text); } - + + xQueueSend(jsonQueue, data, portMAX_DELAY); + cJSON_Delete(data); data = NULL; xSemaphoreGive(czekaj_na_dane); @@ -243,13 +257,13 @@ void vWeatherTask(void *pvParameters){ } - -//Funckja odpowiedzialna za przesyłanie danych do C2 +//Funkcja odpowiedzialna za przesyłanie danych do C2 void vSendData(void *pvParameters){ (void)pvParameters; + int rc; for(;;){ - + if (xQueueReceive(jsonQueue, data, portMAX_DELAY) == pdPASS){ xSemaphoreTake(dataReadySem, portMAX_DELAY); printf("Można wysyłać dane \n"); @@ -257,16 +271,34 @@ void vSendData(void *pvParameters){ printf("Przesyłanie danych do C2 \n"); - + struct mosquitto *mosq = mosquitto_new("publisher-test", true, NULL); + if(mosq == NULL){ + printf("Blad tworzenia klienta\n"); + vTaskDelete(NULL); + } + rc = mosquitto_connect(mosq, IP_adress, port,life_time); - vTaskDelay(pdMS_TO_TICKS(2000)); + if(rc!= 0){ + printf("Problem z połączeniem z brokrem, error code %d \n", rc); + mosquitto_destroy(mosq); + vTaskDelete(NULL); + } + else{ + printf("Udało się połączyć z brokerem"); + mosquitto_publish(mosq, NULL,"projekt/pogoda/C2", strlen(data), data, 0,false); + mosquitto_disconnect(mosq); + mosquitto_destroy(mosq); + mosquitto_lib_cleanup(); + } + xSemaphoreGive(czekaj_na_dane); printf("Przekazanie semafora do take_dane \n"); } - } + } + } } @@ -282,6 +314,11 @@ void main_blinky(void){ czekaj_na_dane = xSemaphoreCreateMutex(); dataReadySem = xSemaphoreCreateBinary(); + jsonQueue = xQueueCreate(5, sizeof(data)); + + //Uruchomienie mqtt + mosquitto_lib_init(); + BaseType_t ok; ok = xTaskCreate( vWeatherTask, // funkcja taska From a2c6e66b125f3bee56c25c85aaa9bc8642715b73 Mon Sep 17 00:00:00 2001 From: GK-M Date: Tue, 17 Mar 2026 12:28:02 +0100 Subject: [PATCH 3/9] 17.03 --- main_blinky.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/main_blinky.c b/main_blinky.c index 7522fc5..51f99a3 100644 --- a/main_blinky.c +++ b/main_blinky.c @@ -128,7 +128,7 @@ void vWeatherTask(void *pvParameters){ printf("Ilość pomiarów: %d \n", count_lat * count_long); - + // Nieskończona pętla taska for (;;){ @@ -240,10 +240,8 @@ void vWeatherTask(void *pvParameters){ free(json_text); } - xQueueSend(jsonQueue, data, portMAX_DELAY); + xQueueSend(jsonQueue, &data, portMAX_DELAY); - cJSON_Delete(data); - data = NULL; xSemaphoreGive(czekaj_na_dane); printf("Przekazanie semafora do send_data \n"); @@ -261,11 +259,14 @@ void vWeatherTask(void *pvParameters){ void vSendData(void *pvParameters){ (void)pvParameters; + cJSON *data; int rc; for(;;){ - if (xQueueReceive(jsonQueue, data, portMAX_DELAY) == pdPASS){ + xSemaphoreTake(dataReadySem, portMAX_DELAY); printf("Można wysyłać dane \n"); + if (xQueueReceive(jsonQueue, &data, portMAX_DELAY) == pdPASS){ + printf("Przekazano dane \n"); if (xSemaphoreTake(czekaj_na_dane, portMAX_DELAY) == pdTRUE){ printf("Przesyłanie danych do C2 \n"); @@ -276,6 +277,7 @@ void vSendData(void *pvParameters){ printf("Blad tworzenia klienta\n"); vTaskDelete(NULL); } + rc = mosquitto_connect(mosq, IP_adress, port,life_time); if(rc!= 0){ @@ -289,9 +291,10 @@ void vSendData(void *pvParameters){ mosquitto_disconnect(mosq); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); - } - + + cJSON_Delete(data); + data = NULL; xSemaphoreGive(czekaj_na_dane); printf("Przekazanie semafora do take_dane \n"); @@ -314,7 +317,7 @@ void main_blinky(void){ czekaj_na_dane = xSemaphoreCreateMutex(); dataReadySem = xSemaphoreCreateBinary(); - jsonQueue = xQueueCreate(5, sizeof(data)); + jsonQueue = xQueueCreate(5, sizeof(cJSON *)); //Uruchomienie mqtt mosquitto_lib_init(); From 338074ded4c438f1aed6efe9facf85aa3c532b49 Mon Sep 17 00:00:00 2001 From: GK-M Date: Tue, 17 Mar 2026 12:56:07 +0100 Subject: [PATCH 4/9] add -lmosquitto && send data to broker --- Makefile | 6 ++---- main_blinky.c | 9 ++++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index e0ba2d4..e3df53f 100644 --- a/Makefile +++ b/Makefile @@ -60,12 +60,10 @@ SOURCE_FILES += ${FREERTOS_DIR}/Demo/Common/Minimal/StaticAllocation.c SOURCE_FILES += ${FREERTOS_DIR}/Demo/Common/Minimal/StreamBufferDemo.c SOURCE_FILES += ${FREERTOS_DIR}/Demo/Common/Minimal/StreamBufferInterrupt.c SOURCE_FILES += ${FREERTOS_DIR}/Demo/Common/Minimal/TaskNotify.c -SOURCE_FILES += ${FREERTOS_DIR}/Demo/Common/Minimal/TimerDemo.c - - +SOURCE_FILES += ${FREERTOS_DIR}/Demo/Common/Minimal/TimerDemo.c CFLAGS := -ggdb3 -LDFLAGS := -ggdb3 -pthread +LDFLAGS := -ggdb3 -pthread -lmosquitto CPPFLAGS := $(INCLUDE_DIRS) -DBUILD_DIR=\"$(BUILD_DIR_ABS)\" CPPFLAGS += -D_WINDOWS_ diff --git a/main_blinky.c b/main_blinky.c index 51f99a3..49f410a 100644 --- a/main_blinky.c +++ b/main_blinky.c @@ -287,7 +287,14 @@ void vSendData(void *pvParameters){ } else{ printf("Udało się połączyć z brokerem"); - mosquitto_publish(mosq, NULL,"projekt/pogoda/C2", strlen(data), data, 0,false); + + char *json_text = cJSON_PrintUnformatted(data); + + if (json_text != NULL){ + mosquitto_publish(mosq, NULL, "projekt/pogoda/C2",strlen(json_text), json_text, 0, false); + cJSON_free(json_text); + } + mosquitto_disconnect(mosq); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); From 73855e4916b3233cc5e5a3c825b7f954afcb92aa Mon Sep 17 00:00:00 2001 From: GK-M Date: Tue, 17 Mar 2026 17:01:53 +0100 Subject: [PATCH 5/9] logs for fetching data --- main_blinky.c | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/main_blinky.c b/main_blinky.c index 49f410a..ced79c5 100644 --- a/main_blinky.c +++ b/main_blinky.c @@ -30,8 +30,8 @@ //Punkty Pomiarowe const double latitudes[] = { //szerokości geograficzne 49.000000, - 49.449156, - 49.898311, + //49.449156, + //49.898311, //50.347467, //50.796622, //51.245778, @@ -79,42 +79,46 @@ static SemaphoreHandle_t dataReadySem = NULL; // Funkcja pobiera surowy JSON z OpenWeatherMap dla zadanej długości i szerokości geograficznej static int fetch_owm_json(char *json_buf, size_t buf_size, float latitudes, float longitudes) { - char cmd[4096]; + char cmd[1024]; FILE *fp; size_t total = 0; // liczba wczytanych bajtów - snprintf( - cmd , - sizeof(cmd), - "curl -s \"https://api.openweathermap.org/data/2.5/weather" - "?lat=%f&lon=%f&appid=%s&units=metric&lang=pl\"", - latitudes, longitudes, OWM_API_KEY - ); + snprintf( + cmd , + sizeof(cmd), + "curl -sS -f \"https://api.openweathermap.org/data/2.5/weather" + "?lat=%f&lon=%f&appid=%s&units=metric&lang=pl\"", + latitudes, longitudes, OWM_API_KEY + ); - //Sprawdzenie czy dobrze się wszytsko zapisuje fp = popen(cmd, "r"); - if (fp == NULL) - { - - } - while (!feof(fp) && total < buf_size - 1) - { + if (fp == NULL){ + printf("Blad uruchomienia procesu curl fp == NULL \n"); + exit(EXIT_FAILURE); + } + + while (!feof(fp) && total < buf_size - 1){ size_t n = fread(json_buf + total, 1, buf_size - 1 - total, fp); total += n; } + printf("-------------------------------------------"); + printf("\n"); + printf("Ilość pobranych danych to %d \n", (int)total); // Dodanie znaku końca stringa json_buf[total] = '\0'; // Zamknięcie strumienia po cur - pclose(fp); + int status = pclose(fp); - if (total == 0) - { - return -2; + if(status != 0){ + printf("curl zakonczyl sie bledem status != 0, status = %d\n", status); + exit(EXIT_FAILURE); + } + else{ + printf("Poprawnie zakończono proces odczytów danych z strony OWM \n"); } - return 0; } // Funkcja FreeRTOS odpowiedzialny za pobieranie i parsowanie pogody From f449f29c142b933f5a7d5ac3ed23505a1801935e Mon Sep 17 00:00:00 2001 From: GK-M Date: Tue, 17 Mar 2026 18:26:33 +0100 Subject: [PATCH 6/9] =?UTF-8?q?dodanie=20funkcji=20do=20wybierania=20klucz?= =?UTF-8?q?y=20do=20przes=C5=82ania?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main_blinky.c | 89 +++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/main_blinky.c b/main_blinky.c index ced79c5..6de73f1 100644 --- a/main_blinky.c +++ b/main_blinky.c @@ -121,6 +121,35 @@ static int fetch_owm_json(char *json_buf, size_t buf_size, float latitudes, floa } } + void get_data_from_parse_JSON(cJSON *parseJSON, cJSON *newJSON, const char *nazwa_obiektu, const char *dana){ + + if (parseJSON != NULL){ + + cJSON *obiekt = cJSON_GetObjectItemCaseSensitive(parseJSON, nazwa_obiektu); + + if(cJSON_IsObject(obiekt)){ + cJSON *zmienna = cJSON_GetObjectItemCaseSensitive(obiekt, dana); + if(cJSON_IsNumber(zmienna)){ + + printf("%s: %.5f \n",dana, zmienna -> valuedouble); + //Dopisanie do JSONA + + cJSON_AddNumberToObject(newJSON, dana, zmienna -> valuedouble); + } + else{ + printf("BŁAD W ODCZYCIE ZMIENNEJ: %s \n",dana); + } + } + else{ + printf("Element %s nie jest obiektem w %s \n",dana , nazwa_obiektu); + } + } + else{ + printf("Bład oczytu danych z JSONA z OWM \n"); + } +} + + // Funkcja FreeRTOS odpowiedzialny za pobieranie i parsowanie pogody void vWeatherTask(void *pvParameters){ (void)pvParameters; @@ -132,29 +161,29 @@ void vWeatherTask(void *pvParameters){ printf("Ilość pomiarów: %d \n", count_lat * count_long); - - // Nieskończona pętla taska for (;;){ ulTaskNotifyTake(pdTRUE, portMAX_DELAY); //Czekanie na callback timera + printf("Task vWeatherTask wybudzony przez timer \n"); xSemaphoreTake(czekaj_na_dane, portMAX_DELAY); + printf("Odczyt danych z OMW \n"); - cJSON *data = cJSON_CreateArray(); + cJSON *data = cJSON_CreateArray(); //Tablica na wybrane dane if (data == NULL) { - continue; + printf("Nie udało się zrobić tablicy do zapisu danych \n"); + exit(EXIT_FAILURE); } - printf("Task vWeatherTask wybudzony przez timer \n"); - for(int i = 0; i < count_lat; i++){ for(int j = 0; j < count_long; j++){ cJSON *item = cJSON_CreateObject(); if (item == NULL){ - continue; + printf("Nie udało się zrobić objektu do zapisu danych \n"); + exit(EXIT_FAILURE); } // Pobranie JSON-a z OpenWeatherMap @@ -162,46 +191,16 @@ void vWeatherTask(void *pvParameters){ // Parsowanie tekstu JSON do struktury cJSON cJSON *root = cJSON_Parse(json); - if (root != NULL){ - // Pobranie obiektu "main","coord" i "wind" z JSON-a - cJSON *coord = cJSON_GetObjectItemCaseSensitive(root, "coord"); - cJSON *main_obj = cJSON_GetObjectItemCaseSensitive(root, "main"); - cJSON *wind = cJSON_GetObjectItemCaseSensitive(root, "wind"); - // Pobranie tablicy "weather" do opisu pogody - cJSON *weather_arr = cJSON_GetObjectItemCaseSensitive(root, "weather"); - - // Pobieranie lot i lan - if(cJSON_IsObject(coord)){ - cJSON *lon = cJSON_GetObjectItemCaseSensitive(coord, "lon"); - cJSON *lat = cJSON_GetObjectItemCaseSensitive(coord, "lat"); - - if (cJSON_IsNumber(lon) && cJSON_IsNumber(lat)){ - - printf("Długość geograficzna: %.5f \t Szerokość geograficzna: %.5f \n", lon -> valuedouble, lat -> valuedouble ); - //Dopisanie do JSONA - cJSON_AddNumberToObject(item, "latitudes", lat ->valuedouble); - cJSON_AddNumberToObject(item, "longitudes", lon ->valuedouble); - } - } - //Pobieranie temp - if (cJSON_IsObject(main_obj)){ - cJSON *temp = cJSON_GetObjectItemCaseSensitive(main_obj, "temp"); + + get_data_from_parse_JSON(root, item, "coord", "lon"); + get_data_from_parse_JSON(root, item, "coord", "lat"); + get_data_from_parse_JSON(root, item, "main", "temp"); + get_data_from_parse_JSON(root, item, "wind", "speed"); - if (cJSON_IsNumber(temp)){ - printf("Temperatura: %.1f C\n", temp->valuedouble); - cJSON_AddNumberToObject(item, "temp", temp ->valuedouble); - } - } - //Pobieranie wind - if (cJSON_IsObject(wind)){ - cJSON *speed = cJSON_GetObjectItemCaseSensitive(wind, "speed"); - - if (cJSON_IsNumber(speed)){ - printf("Wiatr: %.2f m/s \n", speed -> valuedouble); - cJSON_AddNumberToObject(item, "wind speed", speed -> valuedouble); - } - } + if (root != NULL){ + cJSON *weather_arr = cJSON_GetObjectItemCaseSensitive(root, "weather"); + // Pobieranie opisu if (cJSON_IsArray(weather_arr)){ From 3709a77db24f4876cb628dfdc73f6e522f9b53c8 Mon Sep 17 00:00:00 2001 From: GK-M Date: Tue, 17 Mar 2026 18:30:15 +0100 Subject: [PATCH 7/9] without mosquitto --- main_blinky.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/main_blinky.c b/main_blinky.c index 6de73f1..d504ca8 100644 --- a/main_blinky.c +++ b/main_blinky.c @@ -192,7 +192,6 @@ void vWeatherTask(void *pvParameters){ // Parsowanie tekstu JSON do struktury cJSON cJSON *root = cJSON_Parse(json); - get_data_from_parse_JSON(root, item, "coord", "lon"); get_data_from_parse_JSON(root, item, "coord", "lat"); get_data_from_parse_JSON(root, item, "main", "temp"); @@ -201,7 +200,6 @@ void vWeatherTask(void *pvParameters){ if (root != NULL){ cJSON *weather_arr = cJSON_GetObjectItemCaseSensitive(root, "weather"); - // Pobieranie opisu if (cJSON_IsArray(weather_arr)){ // Pobieranie pierwszego element tablicy weather[0] @@ -268,6 +266,7 @@ void vSendData(void *pvParameters){ xSemaphoreTake(dataReadySem, portMAX_DELAY); printf("Można wysyłać dane \n"); + /* if (xQueueReceive(jsonQueue, &data, portMAX_DELAY) == pdPASS){ printf("Przekazano dane \n"); @@ -302,16 +301,16 @@ void vSendData(void *pvParameters){ mosquitto_destroy(mosq); mosquitto_lib_cleanup(); } - +*/ cJSON_Delete(data); data = NULL; xSemaphoreGive(czekaj_na_dane); printf("Przekazanie semafora do take_dane \n"); - } + //} } - } + //} } From 2f6e50fbb97d49bac895f9bbfefa0c932ffe1ad0 Mon Sep 17 00:00:00 2001 From: GK-M Date: Tue, 17 Mar 2026 22:38:18 +0100 Subject: [PATCH 8/9] working --- main_blinky.c | 60 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/main_blinky.c b/main_blinky.c index d504ca8..32556ce 100644 --- a/main_blinky.c +++ b/main_blinky.c @@ -12,6 +12,7 @@ #include "cJSON.h" // biblioteka do parsowania JSON #include +#include //Dane do mosquitto connect #define IP_adress "10.255.150.118" @@ -116,8 +117,10 @@ static int fetch_owm_json(char *json_buf, size_t buf_size, float latitudes, floa printf("curl zakonczyl sie bledem status != 0, status = %d\n", status); exit(EXIT_FAILURE); } + else{ printf("Poprawnie zakończono proces odczytów danych z strony OWM \n"); + return 0; } } @@ -255,62 +258,83 @@ void vWeatherTask(void *pvParameters){ } - //Funkcja odpowiedzialna za przesyłanie danych do C2 void vSendData(void *pvParameters){ (void)pvParameters; cJSON *data; int rc; + for(;;){ - xSemaphoreTake(dataReadySem, portMAX_DELAY); printf("Można wysyłać dane \n"); - /* + if (xQueueReceive(jsonQueue, &data, portMAX_DELAY) == pdPASS){ printf("Przekazano dane \n"); if (xSemaphoreTake(czekaj_na_dane, portMAX_DELAY) == pdTRUE){ printf("Przesyłanie danych do C2 \n"); - struct mosquitto *mosq = mosquitto_new("publisher-test", true, NULL); if(mosq == NULL){ printf("Blad tworzenia klienta\n"); - vTaskDelete(NULL); + exit(EXIT_FAILURE);; + } + + rc = mosquitto_username_pw_set(mosq, "mosq", "admin"); + if(rc != MOSQ_ERR_SUCCESS){ + printf("Blad ustawiania loginu i hasla: %s \n", mosquitto_strerror(rc)); + mosquitto_destroy(mosq); + exit(EXIT_FAILURE); } - rc = mosquitto_connect(mosq, IP_adress, port,life_time); + do { + errno = 0; + rc = mosquitto_connect(mosq, IP_adress, port, life_time); + } while(rc == MOSQ_ERR_ERRNO && errno == EINTR); if(rc!= 0){ printf("Problem z połączeniem z brokrem, error code %d \n", rc); + printf("mosquitto rc=%d (%s), errno=%d (%s) \n", + rc, + mosquitto_strerror(rc), + errno, + strerror(errno)); mosquitto_destroy(mosq); - vTaskDelete(NULL); + exit(EXIT_FAILURE); } + else{ - printf("Udało się połączyć z brokerem"); + printf("Udało się połączyć z brokerem \n"); char *json_text = cJSON_PrintUnformatted(data); - if (json_text != NULL){ - mosquitto_publish(mosq, NULL, "projekt/pogoda/C2",strlen(json_text), json_text, 0, false); - cJSON_free(json_text); + rc = mosquitto_publish(mosq, NULL, "projekt/pogoda/C2", + strlen(json_text), json_text, 0, false); + + if(rc != MOSQ_ERR_SUCCESS){ + printf("Blad publish: rc=%d (%s)\n", rc, mosquitto_strerror(rc)); + } + else{ + printf("Publish OK\n"); } + + rc = mosquitto_loop(mosq, 1000, 1); mosquitto_disconnect(mosq); mosquitto_destroy(mosq); - mosquitto_lib_cleanup(); + } -*/ + cJSON_Delete(data); data = NULL; xSemaphoreGive(czekaj_na_dane); printf("Przekazanie semafora do take_dane \n"); - //} + } } - //} + } } @@ -323,13 +347,15 @@ static void TimerCallback(TimerHandle_t xTimer) void main_blinky(void){ + //Uruchomienie mqtt + mosquitto_lib_init(); + czekaj_na_dane = xSemaphoreCreateMutex(); dataReadySem = xSemaphoreCreateBinary(); jsonQueue = xQueueCreate(5, sizeof(cJSON *)); - //Uruchomienie mqtt - mosquitto_lib_init(); + BaseType_t ok; ok = xTaskCreate( From d9cdefea6a2f68cacdb26948983dc23078cc4026 Mon Sep 17 00:00:00 2001 From: GK-M Date: Thu, 19 Mar 2026 15:53:07 +0100 Subject: [PATCH 9/9] 1 --- main_blinky.c | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/main_blinky.c b/main_blinky.c index 32556ce..548bf54 100644 --- a/main_blinky.c +++ b/main_blinky.c @@ -19,6 +19,10 @@ #define port 1883 #define life_time 60 //Ile ma czekać na brokera (ping) +//Logowanie się do usera odbierającego mqtt +#define USER_NAME "mosq" +#define PASS "admin" + // Klucz API z OpenWeatherMap #define OWM_API_KEY "d88799a668f54c560b3f420cd37685fd" @@ -77,7 +81,7 @@ QueueHandle_t jsonQueue; SemaphoreHandle_t czekaj_na_dane = NULL; static SemaphoreHandle_t dataReadySem = NULL; -// Funkcja pobiera surowy JSON z OpenWeatherMap dla zadanej długości i szerokości geograficznej +// Funkcja pobiera surowych JSON z OpenWeatherMap dla zadanej długości i szerokości geograficznej static int fetch_owm_json(char *json_buf, size_t buf_size, float latitudes, float longitudes) { char cmd[1024]; @@ -159,8 +163,8 @@ void vWeatherTask(void *pvParameters){ char json[JSON_BUF_SIZE]; // bufor na odpowiedź JSON //Wielkości tablic z pomiarami - int count_lat = ((int)sizeof(latitudes)/sizeof(latitudes[0])); - int count_long = ((int)sizeof(longitudes)/sizeof(longitudes[0])); + int count_lat = (sizeof(latitudes)/sizeof(latitudes[0])); + int count_long = (sizeof(longitudes)/sizeof(longitudes[0])); printf("Ilość pomiarów: %d \n", count_lat * count_long); @@ -169,7 +173,7 @@ void vWeatherTask(void *pvParameters){ ulTaskNotifyTake(pdTRUE, portMAX_DELAY); //Czekanie na callback timera printf("Task vWeatherTask wybudzony przez timer \n"); - xSemaphoreTake(czekaj_na_dane, portMAX_DELAY); + if(xSemaphoreTake(czekaj_na_dane, portMAX_DELAY) == pdTRUE){ printf("Odczyt danych z OMW \n"); @@ -254,7 +258,11 @@ void vWeatherTask(void *pvParameters){ //vTaskDelay(pdMS_TO_TICKS(PERIOD_ASK)); - } // nieskończony for + } + else{ + printf("Bład z pobraniem Semaphora: SemaphoreHandle_t czekaj_na_dane"); + } + } // nieskończony for } @@ -281,7 +289,7 @@ void vSendData(void *pvParameters){ exit(EXIT_FAILURE);; } - rc = mosquitto_username_pw_set(mosq, "mosq", "admin"); + rc = mosquitto_username_pw_set(mosq, USER_NAME, PASS); if(rc != MOSQ_ERR_SUCCESS){ printf("Blad ustawiania loginu i hasla: %s \n", mosquitto_strerror(rc)); mosquitto_destroy(mosq); @@ -295,11 +303,7 @@ void vSendData(void *pvParameters){ if(rc!= 0){ printf("Problem z połączeniem z brokrem, error code %d \n", rc); - printf("mosquitto rc=%d (%s), errno=%d (%s) \n", - rc, - mosquitto_strerror(rc), - errno, - strerror(errno)); + printf("mosquitto rc=%d (%s), errno=%d (%s) \n", rc, mosquitto_strerror(rc), errno, strerror(errno)); mosquitto_destroy(mosq); exit(EXIT_FAILURE); } @@ -309,25 +313,23 @@ void vSendData(void *pvParameters){ char *json_text = cJSON_PrintUnformatted(data); - rc = mosquitto_publish(mosq, NULL, "projekt/pogoda/C2", - strlen(json_text), json_text, 0, false); + rc = mosquitto_publish(mosq, NULL, "projekt/pogoda/C2", strlen(json_text), json_text, 0, false); if(rc != MOSQ_ERR_SUCCESS){ printf("Blad publish: rc=%d (%s)\n", rc, mosquitto_strerror(rc)); } else{ - printf("Publish OK\n"); - } + mosquitto_loop(mosq, 100, 1); + mosquitto_disconnect(mosq); + mosquitto_destroy(mosq); + printf("Publish OK \n"); - rc = mosquitto_loop(mosq, 1000, 1); - - mosquitto_disconnect(mosq); - mosquitto_destroy(mosq); - + } } - + //kasowanie pamięci / usuwanie wybranych danych cJSON_Delete(data); data = NULL; + xSemaphoreGive(czekaj_na_dane); printf("Przekazanie semafora do take_dane \n"); @@ -355,9 +357,9 @@ void main_blinky(void){ jsonQueue = xQueueCreate(5, sizeof(cJSON *)); - BaseType_t ok; + ok = xTaskCreate( vWeatherTask, // funkcja taska "Weather", // nazwa taska