Compare commits
No commits in common. "80df2dfc52156467829a1e80fd11322dd3a20c34" and "df4978fe34303e10e6245193fcdeb7c862bf83d6" have entirely different histories.
80df2dfc52
...
df4978fe34
@ -1,3 +1,3 @@
|
|||||||
all: main.c mqtt.c temperature.c device_id.c
|
all: main.c mqtt.c temperature.c
|
||||||
$(CC) -lmosquitto -lpthread -li2c main.c mqtt.c temperature.c device_id.c
|
$(CC) -lmosquitto -lpthread -li2c main.c mqtt.c temperature.c
|
||||||
|
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include "device_id.h"
|
|
||||||
|
|
||||||
#define DEVICE_ID_SIZE 5
|
|
||||||
|
|
||||||
char *get_device_id(void)
|
|
||||||
{
|
|
||||||
FILE *file = fopen("device_id.txt", "r");
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
char *device_id = generate_device_id();
|
|
||||||
|
|
||||||
file = fopen("device_id.txt", "w");
|
|
||||||
fprintf(file, "%s", device_id);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
return device_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *device_id = malloc(DEVICE_ID_SIZE + 1);
|
|
||||||
fgets(device_id, DEVICE_ID_SIZE + 1, file);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
return device_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *generate_device_id(void)
|
|
||||||
{
|
|
||||||
char *device_id = malloc(DEVICE_ID_SIZE + 1);
|
|
||||||
|
|
||||||
for (int i = 0; i < DEVICE_ID_SIZE; i++) {
|
|
||||||
device_id[i] = 'A' + (random() % 26);
|
|
||||||
}
|
|
||||||
|
|
||||||
device_id[DEVICE_ID_SIZE] = '\0';
|
|
||||||
|
|
||||||
return device_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
void destroy_device_id(char *device_id)
|
|
||||||
{
|
|
||||||
free(device_id);
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
|||||||
char *get_device_id(void);
|
|
||||||
|
|
||||||
char *generate_device_id(void);
|
|
||||||
|
|
||||||
void destroy_device_id(char *device_id);
|
|
||||||
|
|
23
iot/main.c
23
iot/main.c
@ -4,36 +4,23 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include "mqtt.h"
|
#include "mqtt.h"
|
||||||
#include "temperature.h"
|
#include "temperature.h"
|
||||||
#include "device_id.h"
|
|
||||||
|
|
||||||
void *watch_temperature(void *arg)
|
void *watch_temperature(void *arg)
|
||||||
{
|
{
|
||||||
char *device_id = get_device_id();
|
|
||||||
|
|
||||||
printf("Device ID: %s\n", device_id);
|
|
||||||
|
|
||||||
temperature_handle_t temp_handle = init_temperature();
|
temperature_handle_t temp_handle = init_temperature();
|
||||||
|
|
||||||
get_temperature(temp_handle);
|
get_temperature(temp_handle);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
double temperature = get_temperature(temp_handle);
|
double temperature = get_temperature(temp_handle);
|
||||||
size_t timestamp = time(NULL);
|
|
||||||
|
|
||||||
char *format = "{"
|
char *str = malloc(snprintf(NULL, 0, "%lf", temperature) + 1);
|
||||||
"\"temperature\": %lf,"
|
sprintf(str, "%lf", temperature);
|
||||||
"\"device_id\": \"%s\","
|
|
||||||
"\"timestamp\": %zu"
|
|
||||||
"}";
|
|
||||||
|
|
||||||
char *str = malloc(snprintf(NULL, 0, format, temperature, device_id, timestamp) + 1);
|
mqtt_send_message("/temperature", str);
|
||||||
sprintf(str, format, temperature, device_id, timestamp);
|
|
||||||
|
|
||||||
mqtt_send_message("temperature", str);
|
|
||||||
|
|
||||||
free(str);
|
free(str);
|
||||||
|
|
||||||
@ -42,8 +29,6 @@ void *watch_temperature(void *arg)
|
|||||||
sleep(60);
|
sleep(60);
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy_device_id(device_id);
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,8 +40,6 @@ void mqtt_on_connect(void)
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
|
||||||
|
|
||||||
init_mqtt();
|
init_mqtt();
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
Loading…
Reference in New Issue
Block a user