Skip to content

Commit

Permalink
Added BTCAPIs + HTTPclient
Browse files Browse the repository at this point in the history
- Getting BTC price in USD
  • Loading branch information
BitMaker-hub committed Jul 28, 2024
1 parent aecb8af commit c494831
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 296 deletions.
4 changes: 3 additions & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SRCS
"./displays/displayDriver.c"
"./displays/ui.c"
"./displays/ui_helpers.c"
"./displays/APIs.c"
"./displays/images/ui_font_DigitalNumbers16.c"
"./displays/images/ui_font_DigitalNumbers28.c"
"./displays/images/ui_font_DigitalNumbers16.c"
Expand Down Expand Up @@ -61,6 +62,7 @@ INCLUDE_DIRS
"esp_app_format"
"esp_event"
"esp_http_server"
"esp_http_client"
"esp_netif"
"esp_timer"
"esp_wifi"
Expand Down Expand Up @@ -105,4 +107,4 @@ else()
add_dependencies(${COMPONENT_LIB} web_ui_dist)

spiffs_create_partition_image(www ${WEB_SRC_DIR}/dist/axe-os FLASH_IN_PROJECT DEPENDS web_ui_dist)
endif()
endif()
94 changes: 94 additions & 0 deletions main/displays/APIs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "APIs.h"
#include <string.h>
#include <stdlib.h>
#include "esp_log.h"
#include "esp_timer.h"
#include "esp_http_client.h"
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "cJSON.h"

static char tag[] = "APIsHelper";
static char *response_buffer = NULL; // Buffer para acumular la respuesta completa
static int response_length = 0; // Longitud actual del buffer
static unsigned long mBTCUpdate = 0;
static unsigned int bitcoin_price = 0; // Establece esta variable globalmente

// Handler de eventos para la respuesta HTTP
esp_err_t http_event_handler(esp_http_client_event_t *evt) {
switch(evt->event_id) {
case HTTP_EVENT_ON_DATA:
if (!esp_http_client_is_chunked_response(evt->client)) {
// Aumentar el buffer con nuevos datos
char *new_buffer = realloc(response_buffer, response_length + evt->data_len + 1);
if (new_buffer == NULL) {
ESP_LOGE(tag, "Failed to allocate memory for response buffer");
return ESP_ERR_NO_MEM;
}
response_buffer = new_buffer;
memcpy(response_buffer + response_length, (char*)evt->data, evt->data_len);
response_length += evt->data_len;
response_buffer[response_length] = '\0'; // Asegurarse de que el buffer es una cadena válida
}
break;
case HTTP_EVENT_ON_FINISH:
// Intentar parsear el JSON completo al final de la transmisión
//ESP_LOGI(tag, "Final JSON received: %s", response_buffer);
cJSON *json = cJSON_Parse(response_buffer);
if (json != NULL) {
cJSON *bpi = cJSON_GetObjectItem(json, "bpi");
if (bpi != NULL) {
cJSON *usd = cJSON_GetObjectItem(bpi, "USD");
if (usd != NULL) {
cJSON *rate_float = cJSON_GetObjectItem(usd, "rate_float");
if (cJSON_IsNumber(rate_float)) {
bitcoin_price = (int) rate_float->valuedouble;
ESP_LOGI(tag, "Bitcoin price in USD: %d", bitcoin_price);
}
}
}
cJSON_Delete(json);
} else {
ESP_LOGE(tag, "Failed to parse JSON");
}
// Liberar el buffer después de procesar
free(response_buffer);
response_buffer = NULL;
response_length = 0;
break;
default:
break;
}
return ESP_OK;
}

unsigned int getBTCprice(void) {
static char price_str[32];

if ((mBTCUpdate == 0) || (esp_timer_get_time() / 1000 - mBTCUpdate > UPDATE_BTC_min * 60)) {
esp_http_client_config_t config = {
.url = getBTCAPI,
.event_handler = http_event_handler,
};

esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_perform(client);

if (err == ESP_OK) {
ESP_LOGI("HTTP", "HTTP Status = %d, content_length = %lld",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
mBTCUpdate = esp_timer_get_time() / 1000;
} else {
ESP_LOGE("HTTP", "HTTP GET request failed: %s", esp_err_to_name(err));
}

esp_http_client_cleanup(client);
}

return bitcoin_price;
}
15 changes: 15 additions & 0 deletions main/displays/APIs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef APIS_H
#define APIS_H


//Time update period
#define UPDATE_PERIOD_h 5

#define getBTCAPI "http://api.coindesk.com/v1/bpi/currentprice.json"
#define UPDATE_BTC_min 1


unsigned int getBTCprice(void);


#endif //APIS_H
13 changes: 13 additions & 0 deletions main/displays/displayDriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "esp_err.h"
#include "esp_log.h"
#include "lv_conf.h"
#include "APIs.h"
#include "../adc.h"
#include "esp_netif.h"
#include "nvs_config.h"
Expand Down Expand Up @@ -487,6 +488,17 @@ void display_updateCurrentSettings(GlobalState * GLOBAL_STATE){

}

unsigned int BTCprice = 0;
void display_updateBTCprice(void){
char price_str[32];

if ((screenStatus != SCREEN_BTCPRICE)&&(BTCprice != 0)) return;

BTCprice = getBTCprice();
snprintf(price_str, sizeof(price_str), "%u$", BTCprice);
lv_label_set_text(ui_lblBTCPrice, price_str); // Update label
}

void display_updateGlobalState(GlobalState * GLOBAL_STATE){
char strData[20];

Expand Down Expand Up @@ -516,6 +528,7 @@ void display_updateGlobalState(GlobalState * GLOBAL_STATE){
display_updateTime(module);
display_updateShares(module);
display_updateHashrate(module, GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power);
display_updateBTCprice();

uint16_t vcore = ADC_get_vcore();
snprintf(strData, sizeof(strData), "%umV", vcore);
Expand Down
196 changes: 0 additions & 196 deletions main/displays/images/ui_helpers.c

This file was deleted.

Loading

0 comments on commit c494831

Please sign in to comment.