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"
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
// 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
}
//Print temperature with 4 decimal places
// (12 bit resolution measurement accuracy is 0.0625 Celsius)
//ESP_LOGI("TAG", "Temperature = %.4f", temp);
// Initalize conversion
// Convert temperature
ds18b20_convert_temp(&sensor);
// Read the temperature
temp = ds18b20_read_temp(&sensor);
return temp;
// 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 <time.h>
#include <onewire.h>
#include <ds18b20.h> // Die Header-Datei mit DS18B20-Funktionen
#include <ds18b20.h>
#include <task.h>
#include <esp_wifi.h>
#include <FreeRTOSConfig.h>
#include <stdint.h> // 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
#endif // APPLE_H