Simulación de un sistema de monitorización de temperatura en tiempo real en C++

En este ejercicio, creará una aplicación en C++ que simula un sistema de monitoreo de temperatura en tiempo real. El programa generará lecturas aleatorias de temperatura a intervalos fijos, simulando el proceso de monitoreo de temperatura en tiempo real. El usuario podrá monitorear la temperatura y, si esta supera un umbral determinado, se activará una alerta. Esta simulación demuestra cómo gestionar el monitoreo de datos en tiempo real y las alertas en un programa mediante bucles y generación de números aleatorios.

Grupo

Programación en Tiempo Real en C++

Ojetivo

Escriba un programa en C++ que simule un sistema de monitoreo de temperatura. El programa debe generar lecturas de temperatura aleatorias a intervalos regulares. Mostrar la temperatura actual y generar una alerta si esta supera un umbral predefinido. El usuario debe poder especificar el valor del umbral.

Desarrolle una aplicación que simule un sistema de monitoreo de temperatura en tiempo real.

Ejemplo de Código C++

 Copiar Código C++
#include <iostream>   // Include the input-output stream library
#include <chrono>     // Include the chrono library for time manipulation
#include <thread>     // Include the thread library for sleeping functionality
#include <cstdlib>    // Include the standard library for random number generation
#include <ctime>      // Include the time library for seeding random numbers
using namespace std;

// Function to simulate the temperature monitoring
void monitorTemperature(int threshold) {
    srand(time(0));  // Seed the random number generator using the current time
    while (true) {  // Infinite loop to simulate continuous temperature monitoring
        int temperature = rand() % 100;  // Generate a random temperature between 0 and 99
        cout << "Current Temperature: " << temperature << "°C" << endl;

        // Check if the temperature exceeds the threshold
        if (temperature > threshold) {
            cout << "ALERT! Temperature exceeds threshold!" << endl;
        }

        // Wait for 2 seconds before generating the next reading
        this_thread::sleep_for(chrono::seconds(2));
    }
}

int main() {
    int threshold;
    cout << "Enter the temperature threshold for alert (°C): ";
    cin >> threshold;  // Get the threshold temperature from the user

    // Start the temperature monitoring system with the specified threshold
    monitorTemperature(threshold);

    return 0;
}

 Salida

Enter the temperature threshold for alert (°C): 50
Current Temperature: 32°C
Current Temperature: 76°C
ALERT! Temperature exceeds threshold!
Current Temperature: 49°C
Current Temperature: 88°C
ALERT! Temperature exceeds threshold!
Current Temperature: 25°C
...

Comparte este ejercicio C++

Más ejercicios de programación C++Programación en Tiempo Real en C++

¡Explora el conjunto de ejercicios de programación en C++! Diseñados específicamente para principiantes, estos ejercicios te ayudarán a desarrollar una sólida comprensión de los fundamentos de C++. Desde variables y tipos de datos hasta estructuras de control y funciones simples, cada ejercicio está diseñado para desafiarte gradualmente a medida que adquieres confianza en la programación en C++.