最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arduino - How can I convert disk usage, memory usage, PSRAM usage, and network usage to percentages on ESP32? - Stack Overflow

programmeradmin1浏览0评论

I'm programming a miner with ESP32 using Arduino as the programming language and PlatformIo as the compiler. I'm currently developing the telemetry part, and I'm not sure how to get the memory usage, PSRAM memory, disk, and Wi-Fi signal data. I'm able to get the data, but not in percentages, and I don't know if I'm doing it right. Here I'm sharing a minimal and reproducible example written in the Arduino IDE (it's not the actual code, just the data collection part)

#include <SPIFFS.h>

float memory;
float memoryPsram;
float disk;  

void setup() {
  Serial.begin(115200);
  Serial.println("Arrancando telemetria: ");
  SPIFFS.begin();
}

void loop() {
  for (int i = 0; i < 10; i++)
  {
      memory += ESP.getHeapSize() > 0 ? (100 - (ESP.getFreeHeap() * 100 / ESP.getHeapSize())) : 0;
      memoryPsram += ESP.getPsramSize() > 0 ? (100 - (ESP.getFreePsram() * 100 / ESP.getPsramSize())) : 0; 
      disk += SPIFFS.totalBytes() > 0 ? (SPIFFS.usedBytes() * 100 / SPIFFS.totalBytes()) : 0;
  }
      
  Serial.print("Memory: ");
  Serial.println(memory/10);
  Serial.print("Memory Psram: ");
  Serial.println(memoryPsram/10);
  Serial.print("Disk: ");
  Serial.println(disk/10);
  delay(1000);
}

The output I am getting is

Memory: 476.00
Memory Psram: 0.00
Disk: 0.00

As you can see in the output, the memory value is 476, and I don't know how to convert that value to a percentage (from 0 to 100) or the PSRAM memory and disk usage values. Thank you very much for the help.

I'm programming a miner with ESP32 using Arduino as the programming language and PlatformIo as the compiler. I'm currently developing the telemetry part, and I'm not sure how to get the memory usage, PSRAM memory, disk, and Wi-Fi signal data. I'm able to get the data, but not in percentages, and I don't know if I'm doing it right. Here I'm sharing a minimal and reproducible example written in the Arduino IDE (it's not the actual code, just the data collection part)

#include <SPIFFS.h>

float memory;
float memoryPsram;
float disk;  

void setup() {
  Serial.begin(115200);
  Serial.println("Arrancando telemetria: ");
  SPIFFS.begin();
}

void loop() {
  for (int i = 0; i < 10; i++)
  {
      memory += ESP.getHeapSize() > 0 ? (100 - (ESP.getFreeHeap() * 100 / ESP.getHeapSize())) : 0;
      memoryPsram += ESP.getPsramSize() > 0 ? (100 - (ESP.getFreePsram() * 100 / ESP.getPsramSize())) : 0; 
      disk += SPIFFS.totalBytes() > 0 ? (SPIFFS.usedBytes() * 100 / SPIFFS.totalBytes()) : 0;
  }
      
  Serial.print("Memory: ");
  Serial.println(memory/10);
  Serial.print("Memory Psram: ");
  Serial.println(memoryPsram/10);
  Serial.print("Disk: ");
  Serial.println(disk/10);
  delay(1000);
}

The output I am getting is

Memory: 476.00
Memory Psram: 0.00
Disk: 0.00

As you can see in the output, the memory value is 476, and I don't know how to convert that value to a percentage (from 0 to 100) or the PSRAM memory and disk usage values. Thank you very much for the help.

Share Improve this question edited Mar 19 at 2:26 joaquinStrada asked Mar 19 at 2:18 joaquinStradajoaquinStrada 213 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

When writing code in C++ (and C), it is important to understand the data type that you are dealing with and the operation you are going to perform on the data.

Both ESP.getFreeHeap() and ESP.getHeapSize() return a data type of size_t (i.e. a uint32_t), For two size_t values of 338372 and 380644, when performing a division, th result is the integer part of the division, so it will result in 0 instead of floating point value of 0.8889. You need to explicitly cast one of the value to float before performing the division if you want the result to be in floating point.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.printf("FreeHeap: %zu, HeapSize: %zu, Heap Utilization: %.2f%%\n", 
    ESP.getFreeHeap(),
    ESP.getHeapSize(),
    (1 - static_cast<float>(ESP.getFreeHeap()) / ESP.getHeapSize()) * 100
  );
  
}

void loop() {

}

This code will print the correct division result as

FreeHeap: 338372, HeapSize: 380644, Heap Utilization: 11.11%

the varaibles that you are using like, memory, memoryPsram, and disk, you are initializing them at first but not reverting them to zero after each iteration of the loop,
so you should initialize these variables in loop with minimum values like this:

float memory = 0;
float memoryPsram = 0;
float disk = 0;

Later you should define these parameters i.e.ESP.getHeapSize(), ESP.getPsramSize(), and SPIFFS.totalBytes() in variables seperately for memory values and perform for loop, extensively you should check wheter these values are not null or zero, so that you can get accurate results from the calculations like this:

for (int i = 0; i < 10; i++) {
      size_t heapSize = ESP.getHeapSize();
      size_t freeHeap = ESP.getFreeHeap();
      size_t psramSize = ESP.getPsramSize();
      size_t freePsram = ESP.getFreePsram();
      size_t totalDisk = SPIFFS.totalBytes();
      size_t usedDisk = SPIFFS.usedBytes();

      memory += (heapSize > 0) ? (100.0 - ((float)freeHeap * 100.0 / heapSize)) : 0;
      memoryPsram += (psramSize > 0) ? (100.0 - ((float)freePsram * 100.0 / psramSize)) : 0;
      disk += (totalDisk > 0) ? ((float)usedDisk * 100.0 / totalDisk) : 0;
  }

Later you can print values of these variables with proper floating values like use 10.0 instead of 10:

  Serial.print("Memory: ");
  Serial.println(memory / 10.0);
  Serial.print("Memory Psram: ");
  Serial.println(memoryPsram / 10.0);
  Serial.print("Disk: ");
  Serial.println(disk / 10.0);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论