Implement generating device ID
This commit is contained in:
parent
df4978fe34
commit
10d36e7ccc
@ -1,3 +1,3 @@
|
|||||||
all: main.c mqtt.c temperature.c
|
all: main.c mqtt.c temperature.c device_id.c
|
||||||
$(CC) -lmosquitto -lpthread -li2c main.c mqtt.c temperature.c
|
$(CC) -lmosquitto -lpthread -li2c main.c mqtt.c temperature.c device_id.c
|
||||||
|
|
||||||
|
46
iot/device_id.c
Normal file
46
iot/device_id.c
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#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);
|
||||||
|
}
|
||||||
|
|
6
iot/device_id.h
Normal file
6
iot/device_id.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
char *get_device_id(void);
|
||||||
|
|
||||||
|
char *generate_device_id(void);
|
||||||
|
|
||||||
|
void destroy_device_id(char *device_id);
|
||||||
|
|
12
iot/main.c
12
iot/main.c
@ -4,12 +4,18 @@
|
|||||||
#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);
|
||||||
@ -20,7 +26,7 @@ void *watch_temperature(void *arg)
|
|||||||
char *str = malloc(snprintf(NULL, 0, "%lf", temperature) + 1);
|
char *str = malloc(snprintf(NULL, 0, "%lf", temperature) + 1);
|
||||||
sprintf(str, "%lf", temperature);
|
sprintf(str, "%lf", temperature);
|
||||||
|
|
||||||
mqtt_send_message("/temperature", str);
|
mqtt_send_message("temperature", str);
|
||||||
|
|
||||||
free(str);
|
free(str);
|
||||||
|
|
||||||
@ -29,6 +35,8 @@ void *watch_temperature(void *arg)
|
|||||||
sleep(60);
|
sleep(60);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
destroy_device_id(device_id);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,6 +48,8 @@ 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