Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MQTTEcho.c
00001 /* Standard includes. */ 00002 #include <stdint.h> 00003 #include <stdio.h> 00004 #include <stdlib.h> 00005 00006 /* FreeRTOS includes. */ 00007 #include "FreeRTOS.h" 00008 #include "task.h" 00009 #include "queue.h" 00010 00011 /* FreeRTOS+TCP includes. */ 00012 #include "FreeRTOS_IP.h" 00013 #include "FreeRTOS_Sockets.h" 00014 00015 #define MQTT_TASK 1 00016 #include "MQTTClient.h" 00017 00018 00019 void messageArrived(MessageData* data) 00020 { 00021 printf("Message arrived on topic %.*s: %.*s\n", data->topicName->lenstring.len, data->topicName->lenstring.data, 00022 data->message->payloadlen, data->message->payload); 00023 } 00024 00025 static void prvMQTTEchoTask(void *pvParameters) 00026 { 00027 /* connect to m2m.eclipse.org, subscribe to a topic, send and receive messages regularly every 1 sec */ 00028 MQTTClient client; 00029 Network network; 00030 unsigned char sendbuf[80], readbuf[80]; 00031 int rc = 0, 00032 count = 0; 00033 MQTTPacket_connectData connectData = MQTTPacket_connectData_initializer; 00034 00035 pvParameters = 0; 00036 NetworkInit(&network); 00037 MQTTClientInit(&client, &network, 30000, sendbuf, sizeof(sendbuf), readbuf, sizeof(readbuf)); 00038 00039 char* address = "iot.eclipse.org"; 00040 if ((rc = NetworkConnect(&network, address, 1883)) != 0) 00041 printf("Return code from network connect is %d\n", rc); 00042 00043 #if defined(MQTT_TASK) 00044 if ((rc = MQTTStartTask(&client)) != pdPASS) 00045 printf("Return code from start tasks is %d\n", rc); 00046 #endif 00047 00048 connectData.MQTTVersion = 3; 00049 connectData.clientID.cstring = "FreeRTOS_sample"; 00050 00051 if ((rc = MQTTConnect(&client, &connectData)) != 0) 00052 printf("Return code from MQTT connect is %d\n", rc); 00053 else 00054 printf("MQTT Connected\n"); 00055 00056 if ((rc = MQTTSubscribe(&client, "FreeRTOS/sample/#", 2, messageArrived)) != 0) 00057 printf("Return code from MQTT subscribe is %d\n", rc); 00058 00059 while (++count) 00060 { 00061 MQTTMessage message; 00062 char payload[30]; 00063 00064 message.qos = 1; 00065 message.retained = 0; 00066 message.payload = payload; 00067 sprintf(payload, "message number %d", count); 00068 message.payloadlen = strlen(payload); 00069 00070 if ((rc = MQTTPublish(&client, "FreeRTOS/sample/a", &message)) != 0) 00071 printf("Return code from MQTT publish is %d\n", rc); 00072 #if !defined(MQTT_TASK) 00073 if ((rc = MQTTYield(&client, 1000)) != 0) 00074 printf("Return code from yield is %d\n", rc); 00075 #endif 00076 } 00077 00078 /* do not return */ 00079 } 00080 00081 00082 void vStartMQTTTasks(uint16_t usTaskStackSize, UBaseType_t uxTaskPriority) 00083 { 00084 BaseType_t x = 0L; 00085 00086 xTaskCreate(prvMQTTEchoTask, /* The function that implements the task. */ 00087 "MQTTEcho0", /* Just a text name for the task to aid debugging. */ 00088 usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */ 00089 (void *)x, /* The task parameter, not used in this case. */ 00090 uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */ 00091 NULL); /* The task handle is not used. */ 00092 } 00093 /*-----------------------------------------------------------*/ 00094 00095
Generated on Wed Jul 13 2022 10:46:02 by
1.7.2