안녕하세요.
WIZ550io 모듈을 이용해서 Socket 통신은 잘 사용하고 있습니다.
MCU는 STM32F407 입니다.
고객 요청으로 MQTT 프로토콜를 구현하려고 하는데 지원이 가능 한가요?
ioLibrary에 있는 MQTT Source 를 이용해서 시도 중인데 접속이 안되어 문의 합니다.
초기화는 mqtt_interface.h 를 참고해서 아래와 같이 했습니다.
관련소스만 붙혀넣었습니다.
wiz_NetInfo gWIZNETINFO = {
.mac = {0x00, 0x08, 0xdc, 0x76, 0xed, 0x03},
.ip = {192, 168, 0, 200},
.sn = {255, 255, 255, 0},
.gw = {192, 168, 0, 1},
.dns = {8, 8, 8, 8},
.dhcp = NETINFO_STATIC
};
unsigned char targetIP[4] = {192, 168, 0, 26}; // mqtt server IP
unsigned int targetPort = 1883; // mqtt server port
uint8_t mac_address[6] = {};
unsigned char tempBuffer[BUFFER_SIZE] = {};
struct opts_struct
{
char* clientid;
int nodelimiter;
char* delimiter;
enum QoS qos;
char* username;
char* password;
char* host;
int port;
int showtopics;
} opts ={ (char*)“stdout-subscriber”, 0, (char*)“\n”, QOS0, NULL, NULL, (char*)targetIP, 1883, 0 };
// @brief messageArrived callback function
void messageArrived(MessageData* md)
{
unsigned char testbuffer[100];
MQTTMessage* message = md->message;
if (opts.showtopics)
{
memcpy(testbuffer,(char*)message->payload,(int)message->payloadlen);
*(testbuffer + (int)message->payloadlen + 1) = "\n";
printf("%s\r\n",testbuffer);
}
if (opts.nodelimiter)
printf("%.*s", (int)message->payloadlen, (char*)message->payload);
else
printf("%.*s%s", (int)message->payloadlen, (char*)message->payload, opts.delimiter);
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals /
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_RTC_Init();
MX_SPI2_Init();
MX_TIM6_Init();
MX_TIM7_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_USART3_UART_Init();
MX_I2C1_Init();
/ USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim6); // Timer6 Start
HAL_TIM_Base_Start_IT(&htim7); // Timer7 Start
HAL_UART_Receive_IT(&huart1, &bytRx1_Data, 1); // PC Interface
HAL_UART_Receive_IT(&huart2, &bytRx2_Data, 1); // LCD Module
HAL_UART_Receive_IT(&huart3, &bytRx3_Data, 1); // SD Module
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)nlAdcValue, 4); // Start the ADC in DMA mode
Initialize_Variable();
reg_wizchip_cs_cbfunc(csEnable,csDisable);// CS function register
#ifdef USE_DMA
reg_wizchip_spi_cbfunc(spiDmaReadByte, spiDmaWriteByte);// SPI method callback registration
reg_wizchip_spiburst_cbfunc(spiReadBurst, spiWriteBurst);// use DMA
#else
reg_wizchip_spi_cbfunc(spiReadByte, spiWriteByte);// SPI method callback registration
#endif
wizchip_initialize();
if (gWIZNETINFO.dhcp == NETINFO_DHCP) // DHCP
{
wizchip_dhcp_init();
while (DHCP_run() != DHCP_IP_LEASED)
// wiz_delay_ms(1000);
HAL_Delay(1000);
}
else // static
{
ctlnetwork(CN_SET_NETINFO, &gWIZNETINFO);
printf(“\r\n----------STATIC Net Information--------------\r\n”);
print_network_information();
}
int rc = 0;
unsigned char buf[100];
Network n;
MQTTClient c;
printf("----------------------------------------------\r\n");
printf("MQTT Server IP address : %d.%d.%d.%d\n\r",targetIP[0],targetIP[1],targetIP[2],targetIP[3]);
printf("MQTT Server Port : %d\n\r",targetPort);
printf("----------------------------------------------\r\n");
NewNetwork(&n, TCP_SOCKET);
ConnectNetwork(&n, targetIP, targetPort);
MQTTClientInit(&c,&n,1000,buf,100,tempBuffer,2048);
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.willFlag = 0;
data.MQTTVersion = 3;
data.clientID.cstring = opts.clientid;
data.username.cstring = opts.username;
data.password.cstring = opts.password;
data.keepAliveInterval = 60;
data.cleansession = 1;
rc = MQTTConnect(&c, &data);
printf("Connected %d\r\n", rc);
opts.showtopics = 1;
printf("Subscribing to %s\r\n", "hello/wiznet");
rc = MQTTSubscribe(&c, "hello/wiznet", opts.qos, messageArrived);
printf("Subscribed %d\r\n", rc);
while(1)
{
/* Assigned IP through DHCP */
// if (gWIZNETINFO.dhcp == NETINFO_DHCP)
// DHCP_run();
// Check_tcpc(SOCKET_TCP, (uint8_t *)Tcp_Buf, ServerIp, ServerPort);
MQTTYield(&c, data.keepAliveInterval);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM7) // 1ms Timer
{
MilliTimer_Handler();
}
}
위 소스를 다운로드 하면 아래와 같이 동작합니다.
----------STATIC Net Information--------------
Mac address: 00:08:dc:76:ed:03
IP address : 192.168.0.200
SM Mask : 255.255.255.0
Gate way : 192.168.0.1
DNS Server : 8.8.8.8
----------------------------------------------
MQTT Server IP address : 192.168.0.26
MQTT Server Port : 1883
----------------------------------------------
Connected 255
Subscribing to hello/wiznet
Subscribed -1
위와 같이 MQTTConnect 와 MQTTSubscribe 함수에서 에러가 발생합니다.
PC 에 mosquitto broker를 설치했고 동작 확인도 했습니다.
MQTT 접속을 하기 위해 확인해야 할 부분을 있으면 알려주시거나
Publish, Subscribe 각각 예제 소스가 있으면 제공 받았으면 좋겠습니다.
도움 부탁드립니다. 수고하세요.