working temp sensor
This commit is contained in:
parent
e4f0dab61c
commit
97dfb13b74
10 changed files with 503 additions and 1116 deletions
|
@ -1,45 +1,45 @@
|
|||
|
||||
#include "esp_log.h"
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <onewire.h>
|
||||
#include <ds18b20.h> // Die Header-Datei mit DS18B20-Funktionen
|
||||
#include <task.h>
|
||||
|
||||
#define GPIO_PIN 4 // GPIO Pin, an den der DS18B20 angeschlossen ist
|
||||
#define INTERVAL 100 // Intervall in Sekunden
|
||||
int app_main() {
|
||||
// Create variable for handler
|
||||
ds18b20_handler_t sensor;
|
||||
|
||||
void delay(int seconds) {
|
||||
time_t start_time = time(NULL);
|
||||
while (time(NULL) - start_time < seconds);
|
||||
}
|
||||
// Check for any initialization failures
|
||||
if (!ds18b20_init(&sensor, GPIO_NUM_2, TEMP_RES_12_BIT))
|
||||
{
|
||||
ESP_LOGE("TAG", "Failed to initalize DS18B20!");
|
||||
|
||||
int main() {
|
||||
ds18b20_addr_t sensor_address;
|
||||
return 0; // Exit
|
||||
}
|
||||
|
||||
// Sensoren auf dem Bus scannen (angenommen, dass nur ein Sensor angeschlossen ist)
|
||||
int sensor_count = ds18b20_scan_devices(GPIO_PIN, &sensor_address, 1);
|
||||
if (sensor_count <= 0) {
|
||||
printf("Kein DS18B20-Sensor gefunden!\n");
|
||||
return -1;
|
||||
}
|
||||
float temp = 0;
|
||||
|
||||
while (1) {
|
||||
// Temperaturmessung starten
|
||||
bool success = ds18b20_measure(GPIO_PIN, sensor_address, true);
|
||||
if (!success) {
|
||||
printf("Fehler bei der Temperaturmessung\n");
|
||||
} else {
|
||||
// Temperaturwert lesen
|
||||
float temperature = ds18b20_read_temperature(GPIO_PIN, sensor_address);
|
||||
if (temperature != temperature) { // NaN-Check
|
||||
printf("Fehler beim Lesen der Temperatur\n");
|
||||
} else {
|
||||
printf("Aktuelle Temperatur: %.2f°C\n", temperature);
|
||||
}
|
||||
}
|
||||
|
||||
// Wartezeit von 100 Sekunden
|
||||
delay(INTERVAL);
|
||||
}
|
||||
|
||||
// If you doesn't convert temperature you may read 85.0 Celsius,
|
||||
// as it is default temperature set by DS18B20 if convert command wasn't issued.
|
||||
|
||||
// Read temperature
|
||||
printf("hit");
|
||||
for(int i = 0; i < 30; ++i) {
|
||||
// Initalize conversion
|
||||
ds18b20_convert_temp(&sensor);
|
||||
temp = ds18b20_read_temp(&sensor);
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
printf("\n");
|
||||
printf( "Temperature = %.4f", temp);
|
||||
};
|
||||
|
||||
// Print temperature with 4 decimal places
|
||||
// (12 bit resolution measurement accuracy is 0.0625 Celsius)
|
||||
ESP_LOGI("TAG", "Temperature = %.4f", temp);
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue