diff --git a/embadet/main/apple.c b/embadet/main/apple.c index 421a618..274226e 100644 --- a/embadet/main/apple.c +++ b/embadet/main/apple.c @@ -1,53 +1,54 @@ #include "apple.h" - int app_main() { + // Fill struct + struct appledata send; + getmac(send.mac); + send.upTime = getUpTime(); + send.temp = getTemp(); + send.batteryVoltage = 3.3; - //fill struct - struct appledata send; - getmac(send.mac) ; - send.upTime = getUpTime(); - send.temp = getTemp(); - send.battaryVoltage = 3.3; + printf("Hello \n"); + printf("%d\n", send.upTime); + printf("%.4f\n", send.temp); - printf("%.4f",send.temp); - - //end func - return 0; + // End function + return 0; } - -void getmac(uint8_t *buf){ - // Get MAC address of the WiFi station interface - esp_read_mac(buf, ESP_MAC_WIFI_STA); +void getmac(uint8_t *buf) { + // Get MAC address of the WiFi station interface + esp_read_mac(buf, ESP_MAC_WIFI_STA); } -int getUpTime(){ - int uptime = (xTaskGetTickCount() * (1000/configTICK_RATE_HZ)); - +int getUpTime() { + // Get system uptime in milliseconds + uint32_t uptime = (xTaskGetTickCount() * (1000 / configTICK_RATE_HZ)); return uptime; } float getTemp() { - float temp = 0; - // Create variable for handler + float temp = 0.0; ds18b20_handler_t sensor; - // Check for any initialization failures - if (!ds18b20_init(&sensor, GPIO_NUM_2, TEMP_RES_12_BIT)) - { - ESP_LOGE("TAG", "Failed to initalize DS18B20!"); - return 0; // Exit - } - //Print temperature with 4 decimal places - // (12 bit resolution measurement accuracy is 0.0625 Celsius) - //ESP_LOGI("TAG", "Temperature = %.4f", temp); - // Initalize conversion - ds18b20_convert_temp(&sensor); - temp = ds18b20_read_temp(&sensor); - - return temp; + // Initialize DS18B20 sensor + if (!ds18b20_init(&sensor, GPIO_NUM_2, TEMP_RES_12_BIT)) { + ESP_LOGE("DS18B20", "Failed to initialize DS18B20 sensor!"); + return -1.0; // Indicate an error with a negative value + } + // Convert temperature + ds18b20_convert_temp(&sensor); + + // Read the temperature + temp = ds18b20_read_temp(&sensor); + + // Check if the temperature is within a reasonable range for DS18B20 + if (temp < -55.0 || temp > 125.0) { + ESP_LOGE("DS18B20", "Temperature reading out of range: %.2f", temp); + return -1.0; // Indicate invalid reading + } + + return temp; } - diff --git a/embadet/main/apple.h b/embadet/main/apple.h index 47fd72b..b044f69 100644 --- a/embadet/main/apple.h +++ b/embadet/main/apple.h @@ -5,22 +5,22 @@ #include #include #include -#include // Die Header-Datei mit DS18B20-Funktionen +#include #include #include #include +#include // Include for uint8_t - -struct appledata{ +struct appledata { uint8_t mac[6]; float temp; - float battaryVoltage; - int upTime; + float batteryVoltage; // Corrected spelling + uint32_t upTime; // Use uint32_t for uptime }; +// Function declarations float getTemp(); int getUpTime(); void getmac(uint8_t *buf); - -#endif \ No newline at end of file +#endif // APPLE_H