-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_shutdown.c
85 lines (62 loc) · 2.04 KB
/
mqtt_shutdown.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <string.h>
#include<stdlib.h> // to use system() function
#include <mosquitto.h>
/* Compile with:
cc -o mqtt_shutdown_00 mqtt_shutdown_00.c -lmosquitto
*/
void shutdown(){
/*
char ch;
printf("Do you want to shutdown your pc now(y/n)?");
scanf("%c", &ch);
if(ch == 'y' || ch == 'Y'){ */
system("shutdown -P now");
//}
}
void on_connect(struct mosquitto *mosq, void *userdata, int rc)
{
mosquitto_subscribe(mosq, NULL, "PC/poweroff/#", 0);
}
void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *m)
{
char* topic = m->topic;
char* messaggio = m->payload;
//printf("%s %s\n", topic, messaggio);
if (!strcmp(topic,"PC/poweroff/" )) {
//printf("%s \n", topic);
if (!strcmp(messaggio,"off" )){
//printf("%s\n", messaggio);
//printf("Spengo il PC! \n");
shutdown();
mosquitto_disconnect(mosq);
}
}
}
int main(int argc, char **argv)
{
struct mosquitto *mosq;
int rc;
mosquitto_lib_init();
mosq = mosquitto_new(NULL, true, (void *)NULL);
if (!mosq) {
fprintf(stderr, "Error: Out of memory.\n");
mosquitto_lib_cleanup();
return (1);
}
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
//rc = mosquitto_connect(mosq, "test.mosquitto.org", 1883, 60);
rc = mosquitto_connect(mosq, "raspberrypi.local", 1883, 60);
if (rc) {
fprintf(stderr, "Unable to connect (%d).\n", rc);
mosquitto_lib_cleanup();
return (rc);
}
mosquitto_loop_forever(mosq, -1, 1);
printf("Esco \n");
/* Unreached */
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return (0);
}