gcc 컴파일러 사용을 위해 MinGW 설치 필요
https://m.blog.naver.com/dorergiverny/223032334186


...가상 IoT 시스템 구현 실패...
paho.mqtt.c 빌드 왜 실패되는걸까...
Windows에서 실행하려고 MinGW, CMake, mosquitto 다 설치했는데 paho.mqtt.c 빌드 실패...
→ Windows는 나중에 다시 해보기
sudo apt update
sudo apt install mosquitto mosquitto-clients libmosquitto-dev

// client_light.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mosquitto.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <brightness>\n", argv[0]);
return 1;
}
int brightness = atoi(argv[1]);
struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
mosquitto_connect(mosq, "localhost", 1883, 60);
char payload[20];
snprintf(payload, 20, "%d", brightness);
mosquitto_publish(mosq, NULL, "room/light", strlen(payload), payload, 0, false);
mosquitto_disconnect(mosq);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
// client_switch.c
#include <stdio.h>
#include <stdlib.h>
#include <mosquitto.h>
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) {
int brightness = atoi((char *)msg->payload);
printf("room/light b'%s'\n", (char *)msg->payload);
if (brightness <= 20) {
printf("Turn on light\n");
} else {
printf("Turn off light\n");
}
}
void on_connect(struct mosquitto *mosq, void *obj, int rc) {
printf("rc: %d\n", rc);
if (rc == 0) {
mosquitto_subscribe(mosq, NULL, "room/light", 0);
}
}
void on_subscribe(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos) {
printf("Subscribed: %d\n", mid);
}
int main() {
mosquitto_lib_init();
struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
mosquitto_subscribe_callback_set(mosq, on_subscribe);
mosquitto_connect(mosq, "localhost", 1883, 60);
mosquitto_loop_forever(mosq, -1, 1);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
gcc -o client_light client_light.c -lmosquitto
gcc -o client_switch client_switch.c -lmosquitto


// client_light.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mosquitto.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <brightness>\n", argv[0]);
return 1;
}
int brightness = atoi(argv[1]);
struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
if (mosquitto_connect(mosq, "localhost", 1883, 60)) {
fprintf(stderr, "Unable to connect.\n");
return 1;
}
char payload[20];
snprintf(payload, 20, "%d", brightness);
mosquitto_publish(mosq, NULL, "room/get/light", strlen(payload), payload, 0, false);
mosquitto_disconnect(mosq);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
// client_switch.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mosquitto.h>
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) {
printf("%s b'%s'\n", msg->topic, (char *)msg->payload);
char result[20];
if (strcmp((char *)msg->payload, "ON") == 0) {
printf("Turn on light\n");
strcpy(result, "Turn on complete");
} else {
printf("Turn off light\n");
strcpy(result, "Turn off complete");
}
mosquitto_publish(mosq, NULL, "room/get/switch", strlen(result), result, 0, false);
}
void on_connect(struct mosquitto *mosq, void *obj, int rc) {
printf("rc: %d\n", rc);
if (rc == 0) {
mosquitto_subscribe(mosq, NULL, "room/put/switch", 0);
}
}
void on_subscribe(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos) {
printf("Subscribed: %d\n", mid);
}
int main() {
mosquitto_lib_init();
struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
mosquitto_subscribe_callback_set(mosq, on_subscribe);
if (mosquitto_connect(mosq, "localhost", 1883, 60)) {
fprintf(stderr, "Unable to connect.\n");
return 1;
}
mosquitto_loop_forever(mosq, -1, 1);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
// server_iot.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mosquitto.h>
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) {
if (strcmp(msg->topic, "room/get/light") == 0) {
printf("LIGHT : %s b'%s'\n", msg->topic, (char *)msg->payload);
int brightness = atoi((char *)msg->payload);
char command[4];
if (brightness <= 20) {
strcpy(command, "ON");
} else {
strcpy(command, "OFF");
}
mosquitto_publish(mosq, NULL, "room/put/switch", strlen(command), command, 0, false);
} else if (strcmp(msg->topic, "room/get/switch") == 0) {
printf("SWITCH: %s b'%s'\n", msg->topic, (char *)msg->payload);
}
}
void on_connect(struct mosquitto *mosq, void *obj, int rc) {
printf("rc: %d\n", rc);
if (rc == 0) {
mosquitto_subscribe(mosq, NULL, "room/get/light", 0);
mosquitto_subscribe(mosq, NULL, "room/get/switch", 0);
}
}
void on_subscribe(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos) {
printf("Subscribed: %d\n", mid);
}
int main() {
mosquitto_lib_init();
struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
mosquitto_subscribe_callback_set(mosq, on_subscribe);
if (mosquitto_connect(mosq, "localhost", 1883, 60)) {
fprintf(stderr, "Unable to connect.\n");
return 1;
}
mosquitto_loop_forever(mosq, -1, 1);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
gcc -o client_light client_light.c -lmosquitto
gcc -o client_switch client_switch.c -lmosquitto
gcc -o server_iot server_iot.c -lmosquitto

...
...
void on_light_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) {
printf("LIGHT : %s b'%s'\n", msg->topic, (char *)msg->payload);
int brightness = atoi((char *)msg->payload);
char command[4];
if (brightness <= 20) {
strcpy(command, "ON");
} else {
strcpy(command, "OFF");
}
mosquitto_publish(mosq, NULL, "room/put/switch", strlen(command), command, 0, false);
}
void on_switch_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) {
printf("SWITCH: %s b'%s'\n", msg->topic, (char *)msg->payload);
}
...
...
int main() {
mosquitto_lib_init();
struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
mosquitto_connect_callback_set(mosq, on_connect);
--------------------- 이 부분이 문제 -------------------------
mosquitto_message_callback_set(mosq, on_light_message);
mosquitto_message_callback_set(mosq, on_switch_message);
------------------------------------------------------------
mosquitto_subscribe_callback_set(mosq, on_subscribe);
...
...

...
...
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) {
if (strcmp(msg->topic, "room/get/light") == 0) {
printf("LIGHT : %s b'%s'\n", msg->topic, (char *)msg->payload);
int brightness = atoi((char *)msg->payload);
char command[4];
if (brightness <= 20) {
strcpy(command, "ON");
} else {
strcpy(command, "OFF");
}
mosquitto_publish(mosq, NULL, "room/put/switch", strlen(command), command, 0, false);
} else if (strcmp(msg->topic, "room/get/switch") == 0) {
printf("SWITCH: %s b'%s'\n", msg->topic, (char *)msg->payload);
}
}
...
...
int main() {
mosquitto_lib_init();
struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
mosquitto_subscribe_callback_set(mosq, on_subscribe);
...
...