modified: main/include/http_client.h

new file:   main/include/lib.h
	modified:   main/src/http_client.c
	modified:   main/src/main.c
This commit is contained in:
Sven Luethi committed 2024-07-02 00:23:11 +02:00
1 parent dc78008721
commit db7e8c3237
4 files changed
+96 -10

No files matched your search

+1 -1
View File
@@ -17,7 +17,7 @@ bool hc_check_updates();
struct hc_reg_res hc_reg_dev();
struct hc_dev_config hc_get_dev_conf();
void hc_send_sensor_data(int humidity, int temperature);
void hc_send_sensor_data(float humidity, float temperature);
#ifdef __cplusplus
}
+70
View File
@@ -0,0 +1,70 @@
#ifndef LIB_H
#define LIB_H
// Helper function to reverse a string
void reverse(char* str, int len) {
int i = 0, j = len - 1;
while (i < j) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
// Helper function to convert an integer to a string
int intToStr(int x, char str[], int d) {
int i = 0;
while (x) {
str[i++] = (x % 10) + '0';
x = x / 10;
}
// If the number of digits required is more, then add 0s at the beginning
while (i < d)
str[i++] = '0';
reverse(str, i);
str[i] = '\0';
return i;
}
char* floatToString(float num) {
// Allocate memory for the string
char* str = (char*)malloc(50 * sizeof(char));
if (str == NULL) {
// If memory allocation fails, return NULL
return NULL;
}
// Handle integer part
int intPart = (int)num;
int i = intToStr(intPart, str, 0);
// Handle fractional part
float fractionalPart = num - (float)intPart;
if (fractionalPart < 0) {
fractionalPart = -fractionalPart;
}
if (fractionalPart != 0) {
str[i++] = '.'; // Add decimal point
// Consider precision up to 6 digits after the decimal point
for (int j = 0; j < 6; j++) {
fractionalPart = fractionalPart * 10;
int fracIntPart = (int)fractionalPart;
str[i++] = fracIntPart + '0';
fractionalPart = fractionalPart - fracIntPart;
}
}
str[i] = '\0'; // Null-terminate the string
return str;
}
#endif