Skip to content

Instantly share code, notes, and snippets.

@gustavosinbandera1
Created March 2, 2020 19:18
Show Gist options
  • Save gustavosinbandera1/49ec9ada63cc6089c9d919b6a4411b63 to your computer and use it in GitHub Desktop.
Save gustavosinbandera1/49ec9ada63cc6089c9d919b6a4411b63 to your computer and use it in GitHub Desktop.
#include "HttpServerHandler.h"
static const char* TAG = "HTTP-SERVER-HANDLER";
namespace HttpServerHandler {
void readDeviceInfoHandler(HttpRequest* pHttpRequest, HttpResponse* pHttpResponse)
{
ostringstream s1;
std::uint16_t voltage = 0;
std::int16_t avg_current = 0;
std::int16_t avg_power = 0;
std::uint16_t temp = 0;
esc::FuelGauge& fuel_gauge = esc::FuelGauge::getInstance();
esp_err_t err = fuel_gauge.voltage(&voltage);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Can't get voltage, err = %s",
esp_err_to_name(err));
}
err = fuel_gauge.avgCurrent(&avg_current);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Can't get avg. current, err = %s",
esp_err_to_name(err));
}
err = fuel_gauge.avgPower(&avg_power);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Can't get avg. power, err = %s",
esp_err_to_name(err));
}
err = fuel_gauge.temperature(esc::TempMeasure::Internal, &temp);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Can't get internal temperature, err = %s",
esp_err_to_name(err));
}
ESP_LOGI("available memory ram--->>", "%d\n", heap_caps_get_free_size(MALLOC_CAP_DEFAULT));
pHttpResponse->setStatus(HttpResponse::HTTP_STATUS_OK, "OK");
pHttpResponse->addHeader(HttpRequest::HTTP_HEADER_CONTENT_TYPE, "application/json");
s1 << "{";
s1 << "\"voltage\":\"";
s1 << voltage;
s1 << "\",";
s1 << "\"avg_current\":\"";
s1 << avg_current;
s1 << "\",";
s1 << "\"temp\":\"";
s1 << temp;
s1 << "\",";
s1 << "\"avg_power\":\"";
s1 << avg_power;
s1 << "\",";
s1 << "\"free_memory\":\"";
s1 << heap_caps_get_free_size(MALLOC_CAP_DEFAULT);
s1 << "\"";
s1 << "}";
pHttpResponse->sendData(s1.str());
pHttpResponse->close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment