๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[BSS ์์ ํ๊ณ ๊นจ๋ํ ์งํ์ฒ ํ์น ๋ฐ ์ดํ 2] MQTT ์ฌ์ฉํด์ topic ์ผ๋ก SUB/PUB serial ํต์ ๊ฐ ์ฃผ๊ณ ๋ฐ๊ธฐ ver.broker ๋ณธ๋ฌธ
๐ฉ๐ป IoT (Embedded)/Arduino
[BSS ์์ ํ๊ณ ๊นจ๋ํ ์งํ์ฒ ํ์น ๋ฐ ์ดํ 2] MQTT ์ฌ์ฉํด์ topic ์ผ๋ก SUB/PUB serial ํต์ ๊ฐ ์ฃผ๊ณ ๋ฐ๊ธฐ ver.broker
์ง์ง์ํ์นด 2023. 12. 22. 09:42728x90
๋ฐ์ํ
โญ MQTT ver.broker
โ pub.c
// pub.c
#include <stdio.h>
#include <mosquitto.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
// ์๋ฆฌ์ผ ์ฝ๋
typedef struct
{
int fd;
} SerialPort;
int serial_open2(SerialPort *port, const char *device)
{
port->fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if (port->fd == -1)
{
perror("Unable to open port");
return -1;
}
struct termios options;
tcgetattr(port->fd, &options);
cfsetispeed(&options, B9600);
// cfsetospeed(&options, B9600);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(port->fd, TCSANOW, &options);
return 0;
}
void serial_close2(SerialPort *port)
{
close(port->fd);
port->fd = -1;
}
int serial_read2(SerialPort *port, char *buffer, size_t size)
{
return read(port->fd, buffer, size);
}
int pub_main(int a)
{
int rc = a;
struct mosquitto *mosq;
SerialPort port;
if (serial_open2(&port, "/dev/ttyACM0") == -1)
{
return -1;
}
char buffer[256];
int bytesRead;
// ๋ชจ์คํคํ ์ด๊ธฐํ ์์
// ์ด๊ธฐํ
mosquitto_lib_init();
// ๋ชจ์คํค๋ ๋ฐํ์ ๊ฐ์ฒด์ ํด๋ผ์ด์ธํธ id ์์ฑ
mosq = mosquitto_new("publisher-test", true, NULL);
// ๋ชจ์คํคํ ์ด๊ธฐํ ์ฌ๊ธฐ๊น์ง
// Connecting ๋ธ๋ก์ปค
// ๋ธ๋ก์ปค์ ์ฐ๊ฒฐ ํ์ธ Keep-alive(x)
// ํต์ผ๋ผ์ด๋ธ(Keep-alive) ๋๋ฐ์ด์ค๊ฐ์ ๋ฐ์ดํฐ ๋งํฌ๊ฐ ์ ๋์ํ๊ณ ์๋์ง ํ์ธํ๋ ํ์
rc = mosquitto_connect(mosq, "192.168.0.51", 1883, 60);
// ๋ธ๋ก์ปค์ ์ฐ๊ฒฐ์ด x ์ถ๋ ฅ
if (rc != 0)
{
printf("Client could not connect to broker! Error Code: %d\n", rc);
mosquitto_destroy(mosq);
return -1;
}
printf("We are now connected to the broker!\n");
// ์๋ฆฌ์ผ
while (1)
{
bytesRead = serial_read2(&port, buffer, sizeof(buffer));
if (bytesRead > 0)
{
if (buffer[bytesRead - 1] == '\n')
{
buffer[bytesRead] = '\0';
mosquitto_publish(mosq, NULL, "test/t1", 15, buffer, 0, false);
}
}
}
serial_close2(&port);
return 0;
mosquitto_disconnect(mosq);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
// ๋ชจ์คํค๋ ์ฝ๋
int main()
{
pub_main();
}
โ pub.c
// sub.c
#include <stdio.h>
#include <stdlib.h>
#include <mosquitto.h>
#include "main.h"
void on_connect(struct mosquitto *mosq, void *obj, int rc) {
printf("LED: %d\n", * (int *) obj);
if(rc) {
printf("Error with result code: %d\n", rc);
exit(-1);
}
mosquitto_subscribe(mosq, NULL, "test/t1", 0);
}
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) {
printf("New message with topic %s: %s\n", msg->topic, (char *) msg->payload);
}
int main() {
int rc, id=12;
mosquitto_lib_init();
struct mosquitto *mosq;
mosq = mosquitto_new("subscribe-test", true, &id);
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
rc = mosquitto_connect(mosq, "192.168.0.51", 1883, 10);
if(rc) {
printf("Could not connect to Broker with return code %d\n", rc);
return -1;
}
mosquitto_loop_start(mosq);
printf("Press Enter to quit...\n");
getchar();
mosquitto_loop_stop(mosq, true);
mosquitto_disconnect(mosq);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
728x90
๋ฐ์ํ
'๐ฉโ๐ป IoT (Embedded) > Arduino' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments