Методы получения температуры процессора Raspberry Pi с примерами кода

Чтобы узнать температуру процессора на Raspberry Pi, вы можете использовать различные методы в зависимости от предпочитаемого вами языка программирования. Вот несколько примеров на разных языках:

  1. Python:

    import os
    def get_cpu_temperature():
    res = os.popen("vcgencmd measure_temp").readline()
    return float(res.replace("temp=", "").replace("'C\n", ""))
    temperature = get_cpu_temperature()
    print(f"CPU Temperature: {temperature} °C")
  2. Сценарий оболочки:

    #!/bin/bash
    cpu_temp=$(cat /sys/class/thermal/thermal_zone0/temp)
    cpu_temp=$(echo "scale=2; $cpu_temp/1000" | bc)
    echo "CPU Temperature: $cpu_temp °C"
  3. C++:

    #include <iostream>
    #include <fstream>
    #include <string>
    float get_cpu_temperature() {
    std::ifstream file("/sys/class/thermal/thermal_zone0/temp");
    std::string line;
    std::getline(file, line);
    float temperature = std::stof(line) / 1000;
    return temperature;
    }
    int main() {
    float temperature = get_cpu_temperature();
    std::cout << "CPU Temperature: " << temperature << " °C" << std::endl;
    return 0;
    }