๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[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:42
728x90
๋ฐ˜์‘ํ˜•

โญ 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
๋ฐ˜์‘ํ˜•
Comments