Skip to content

GK-M/RTOS

Repository files navigation

FreeRTOS Weather MQTT – Project C1

An application running on Raspberry Pi 5 using the FreeRTOS POSIX/Linux Simulator. The program periodically fetches weather data from the OpenWeatherMap API and publishes it to an MQTT broker.


How It Works

The application consists of two FreeRTOS tasks and one software timer.

Timer – Odczyt_Danych

  • Fires every 2 hours (default: PERIOD_ASK = 7 200 000 ms)
  • Wakes up the vWeatherTask task via xTaskNotifyGive

Task – vWeatherTask (data acquisition)

  1. Waits for a notification from the timer
  2. Iterates over a grid of measurement points defined in the latitudes[] and longitudes[] arrays
  3. For each point, performs an HTTP request to the OpenWeatherMap API using curl and retrieves a JSON response
  4. Parses the response using the cJSON library, extracting:
    • coord.lon / coord.lat – coordinates
    • main.temp – temperature in Celsius
    • wind.speed – wind speed in m/s
    • weather[0].description – weather description
  5. Builds a JSON array from all measurement points and sends it to jsonQueue
  6. Signals vSendData that data is ready via dataReadySem

Task – vSendData (MQTT publishing)

  1. Waits on the dataReadySem semaphore
  2. Receives the JSON array from the queue
  3. Serializes the JSON to a string and calls mosquitto_pub via system()
  4. Publishes the data to topic projekt/pogoda/C1 on the configured broker

File Structure

.
├── main_blinky.c                  # Main application logic (tasks, timer)
├── main.c                         # Entry point – calls main_blinky()
├── cJSON/
│   ├── cJSON.c                    # JSON parsing library
│   └── cJSON.h
├── FreeRTOSConfig.h               # FreeRTOS configuration
├── CMakeLists.txt                 # CMake build system
├── Makefile                       # Alternative build file
└── README.md

Requirements

  • Raspberry Pi 5 running Linux (Raspberry Pi OS or Ubuntu)
  • FreeRTOS POSIX/Linux Simulator
  • curl – for fetching data from OpenWeatherMap
  • mosquitto-clients – for publishing via MQTT (mosquitto_pub)
  • An API key from openweathermap.org
  • A running MQTT broker accessible on the network (e.g. Mosquitto)

Installing dependencies on Raspberry Pi OS / Debian

sudo apt update
sudo apt install curl mosquitto-clients build-essential cmake

Setup and Running

1. Clone the repository

git clone <REPOSITORY_URL>
cd <repository-directory>

2. Configure credentials and broker

In main_blinky.c, set the following defines:

#define MQTT_HOST    "your_broker_ip"
#define MQTT_PORT    1883
#define MQTT_TOPIC   "projekt/pogoda/C1"
#define OWM_API_KEY  "your_api_key"

Optionally, adjust the measurement points in the latitudes[] and longitudes[] arrays.

3. Build

mkdir build && cd build
cmake ..
make

Or using the Makefile:

make

4. Run

./FreeRTOS_Weather

MQTT Payload Format

Data published to the topic is a JSON array:

[
  {
    "lon": 14.1,
    "lat": 49.0,
    "temp": 18.45,
    "speed": 3.2,
    "description": "light clouds"
  }
]

FreeRTOS Synchronization

Mechanism Usage
xTimerCreate Periodic wake-up every 2 hours
xTaskNotifyGive Timer notifies vWeatherTask
xSemaphoreCreateMutex (czekaj_na_dane) Protects the data acquisition critical section
xSemaphoreCreateBinary (dataReadySem) Signals that data is ready to send
xQueueCreate (jsonQueue) Passes the JSON pointer between tasks

FreeRTOS POSIX/Linux Simulator

This project uses the FreeRTOS Simulator for Linux, which allows FreeRTOS code to run natively on a Linux system without dedicated embedded hardware. Each FreeRTOS task is mapped to a POSIX thread (pthread), so queues, semaphores, and timers all work as they would on a microcontroller. This makes Raspberry Pi 5 a convenient development and deployment platform.

More information: https://github.com/FreeRTOS/FreeRTOS-Simulator-for-Linux

About

FreeRTOS program synchronizing and sending files to VPS

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors