fix sensors

This commit is contained in:
Johannes Jöns 2024-10-14 00:12:05 +02:00
parent d71cb391c1
commit a44814c060
2 changed files with 43 additions and 42 deletions

View file

@ -1,53 +1,54 @@
#include "apple.h" #include "apple.h"
int app_main() { int app_main() {
// Fill struct
struct appledata send;
getmac(send.mac);
send.upTime = getUpTime();
send.temp = getTemp();
send.batteryVoltage = 3.3;
//fill struct printf("Hello \n");
struct appledata send; printf("%d\n", send.upTime);
getmac(send.mac) ; printf("%.4f\n", send.temp);
send.upTime = getUpTime();
send.temp = getTemp();
send.battaryVoltage = 3.3;
printf("%.4f",send.temp); // End function
return 0;
//end func
return 0;
} }
void getmac(uint8_t *buf) {
void getmac(uint8_t *buf){ // Get MAC address of the WiFi station interface
// Get MAC address of the WiFi station interface esp_read_mac(buf, ESP_MAC_WIFI_STA);
esp_read_mac(buf, ESP_MAC_WIFI_STA);
} }
int getUpTime(){ int getUpTime() {
int uptime = (xTaskGetTickCount() * (1000/configTICK_RATE_HZ)); // Get system uptime in milliseconds
uint32_t uptime = (xTaskGetTickCount() * (1000 / configTICK_RATE_HZ));
return uptime; return uptime;
} }
float getTemp() { float getTemp() {
float temp = 0; float temp = 0.0;
// Create variable for handler
ds18b20_handler_t sensor; ds18b20_handler_t sensor;
// Check for any initialization failures // Initialize DS18B20 sensor
if (!ds18b20_init(&sensor, GPIO_NUM_2, TEMP_RES_12_BIT)) if (!ds18b20_init(&sensor, GPIO_NUM_2, TEMP_RES_12_BIT)) {
{ ESP_LOGE("DS18B20", "Failed to initialize DS18B20 sensor!");
ESP_LOGE("TAG", "Failed to initalize DS18B20!"); return -1.0; // Indicate an error with a negative value
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;
// 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;
} }

View file

@ -5,22 +5,22 @@
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <onewire.h> #include <onewire.h>
#include <ds18b20.h> // Die Header-Datei mit DS18B20-Funktionen #include <ds18b20.h>
#include <task.h> #include <task.h>
#include <esp_wifi.h> #include <esp_wifi.h>
#include <FreeRTOSConfig.h> #include <FreeRTOSConfig.h>
#include <stdint.h> // Include for uint8_t
struct appledata {
struct appledata{
uint8_t mac[6]; uint8_t mac[6];
float temp; float temp;
float battaryVoltage; float batteryVoltage; // Corrected spelling
int upTime; uint32_t upTime; // Use uint32_t for uptime
}; };
// Function declarations
float getTemp(); float getTemp();
int getUpTime(); int getUpTime();
void getmac(uint8_t *buf); void getmac(uint8_t *buf);
#endif // APPLE_H
#endif