Implement buzzer alarm
This commit is contained in:
parent
c91aa4b278
commit
48c621f1de
@ -1,5 +1,7 @@
|
||||
FILES=main.c brokers/mqtt.c brokers/amqp.c devices/temperature.c devices/display.c device_id.c
|
||||
FILES=main.c brokers/mqtt.c brokers/amqp.c devices/temperature.c devices/display.c devices/buzzer.c device_id.c
|
||||
|
||||
# libgpiod version must be < 2.0
|
||||
|
||||
all: $(FILES)
|
||||
$(CC) -lmosquitto -lrabbitmq -lpthread -li2c $(FILES)
|
||||
$(CC) -lmosquitto -lrabbitmq -lpthread -li2c -lgpiod $(FILES)
|
||||
|
||||
|
44
iot/devices/buzzer.c
Normal file
44
iot/devices/buzzer.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <gpiod.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "buzzer.h"
|
||||
|
||||
buzzer_handle_t init_buzzer()
|
||||
{
|
||||
struct gpiod_chip *chip = gpiod_chip_open_by_name("gpiochip1");
|
||||
if (chip == NULL) {
|
||||
fprintf(stderr, "Unable to find gpiochip1\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct gpiod_line *line = gpiod_chip_get_line(chip, 19);
|
||||
if (line == NULL) {
|
||||
fprintf(stderr, "Unable to get line 19\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
gpiod_line_request_output(line, "buzzer", 0);
|
||||
|
||||
return (buzzer_handle_t) {
|
||||
chip,
|
||||
line,
|
||||
};
|
||||
}
|
||||
|
||||
void sound_buzzer(buzzer_handle_t buzzer, int delay, int length)
|
||||
{
|
||||
for (int cycle = 0; cycle < length; cycle++) {
|
||||
gpiod_line_set_value(buzzer.line, 1);
|
||||
usleep(delay);
|
||||
gpiod_line_set_value(buzzer.line, 0);
|
||||
usleep(delay);
|
||||
}
|
||||
}
|
||||
|
||||
void destroy_buzzer(buzzer_handle_t buzzer)
|
||||
{
|
||||
gpiod_line_release(buzzer.line);
|
||||
gpiod_chip_close(buzzer.chip);
|
||||
}
|
||||
|
13
iot/devices/buzzer.h
Normal file
13
iot/devices/buzzer.h
Normal file
@ -0,0 +1,13 @@
|
||||
#include <gpiod.h>
|
||||
|
||||
typedef struct {
|
||||
struct gpiod_chip *chip;
|
||||
struct gpiod_line *line;
|
||||
} buzzer_handle_t;
|
||||
|
||||
buzzer_handle_t init_buzzer();
|
||||
|
||||
void sound_buzzer(buzzer_handle_t buzzer, int delay, int length);
|
||||
|
||||
void destroy_buzzer(buzzer_handle_t buzzer);
|
||||
|
25
iot/main.c
25
iot/main.c
@ -9,10 +9,29 @@
|
||||
#include "brokers/amqp.h"
|
||||
#include "devices/temperature.h"
|
||||
#include "devices/display.h"
|
||||
#include "devices/buzzer.h"
|
||||
#include "device_id.h"
|
||||
|
||||
void *sound_alarm(void *arg)
|
||||
{
|
||||
buzzer_handle_t buzzer = init_buzzer();
|
||||
|
||||
int delay = 170, length = 600;
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
sound_buzzer(buzzer, delay, length);
|
||||
|
||||
usleep(delay * 2 * length);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *watch_temperature(void *arg)
|
||||
{
|
||||
// TODO change
|
||||
int max_temperature = 27, min_temperature = 18;
|
||||
|
||||
char *device_id = get_device_id();
|
||||
|
||||
printf("Device ID: %s\n", device_id);
|
||||
@ -54,6 +73,12 @@ void *watch_temperature(void *arg)
|
||||
|
||||
free(str);
|
||||
|
||||
// Sound alarm if applicable
|
||||
if (temperature < min_temperature || temperature > max_temperature) {
|
||||
pthread_t alarm_thread;
|
||||
pthread_create(&alarm_thread, NULL, sound_alarm, NULL);
|
||||
}
|
||||
|
||||
printf("Temperature: %lf\n", temperature);
|
||||
|
||||
sleep(60);
|
||||
|
Loading…
Reference in New Issue
Block a user