-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperatura.c
61 lines (54 loc) · 1.48 KB
/
temperatura.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Created by davi on 11/8/17.
//
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <wiringPi.h>
#include "temperatura.h"
#include "mqtt.h"
#include "debug.h"
#include "config.h"
double read_temp(void) {
FILE *ds18b20 = fopen(SENSOR_TEMPERATURA, "r");
double t = -1;
char crcVar[5];
if (ds18b20 == NULL) {
printf("Check connections %s\n",
SENSOR_TEMPERATURA);
perror("\n");
}
if (ds18b20 != NULL) {
if (!ferror(ds18b20)) {
fscanf(ds18b20, "%*x %*x %*x %*x %*x %*x %*x %*x %*x : crc=%*x %s",
crcVar);
if (strncmp(crcVar, "YES", 3) == 0) {
fscanf(ds18b20, "%*x %*x %*x %*x %*x %*x %*x %*x %*x t=%lf",
&t);
t /= 1000.0;
}
}
}
fclose(ds18b20);
return t;
}
void *thread_temperatura(void *pVoid) {
sensor_temperatura = 1;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
while (1) {
double temperatura = read_temp();
mqtt_pub_double("temperatura", temperatura);
printf("Temperatura: %.1f °C\n", temperatura);
delay(INTERVALO_TEMPERATURA);
}
#pragma clang diagnostic pop
}
void *temperatura(void *pVoid) {
pthread_t t1 = 0;
TRACE("Criando thread de temperatura\n");
pthread_create(&t1, NULL, thread_temperatura, NULL);
TRACE("Iniciando thread de temperatura\n");
pthread_join(t1, NULL);
return NULL;
}