1 /* Standard includes. */
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 /* FreeRTOS includes. */
7 #include "FreeRTOS.h"
8 #include "task.h"
9 #include "queue.h"
10
11 /* FreeRTOS+TCP includes. */
12 #include "FreeRTOS_IP.h"
13 #include "FreeRTOS_Sockets.h"
14
15 #define MQTT_TASK 1
16 #include "MQTTClient.h"
17
18
messageArrived(MessageData * data)19 void messageArrived(MessageData* data)
20 {
21 printf("Message arrived on topic %.*s: %.*s\n", data->topicName->lenstring.len, data->topicName->lenstring.data,
22 data->message->payloadlen, data->message->payload);
23 }
24
prvMQTTEchoTask(void * pvParameters)25 static void prvMQTTEchoTask(void *pvParameters)
26 {
27 /* connect to m2m.eclipse.org, subscribe to a topic, send and receive messages regularly every 1 sec */
28 MQTTClient client;
29 Network network;
30 unsigned char sendbuf[80], readbuf[80];
31 int rc = 0,
32 count = 0;
33 MQTTPacket_connectData connectData = MQTTPacket_connectData_initializer;
34
35 pvParameters = 0;
36 NetworkInit(&network);
37 MQTTClientInit(&client, &network, 30000, sendbuf, sizeof(sendbuf), readbuf, sizeof(readbuf));
38
39 char* address = "iot.eclipse.org";
40 if ((rc = NetworkConnect(&network, address, 1883)) != 0)
41 printf("Return code from network connect is %d\n", rc);
42
43 #if defined(MQTT_TASK)
44 if ((rc = MQTTStartTask(&client)) != pdPASS)
45 printf("Return code from start tasks is %d\n", rc);
46 #endif
47
48 connectData.MQTTVersion = 3;
49 connectData.clientID.cstring = "FreeRTOS_sample";
50
51 if ((rc = MQTTConnect(&client, &connectData)) != 0)
52 printf("Return code from MQTT connect is %d\n", rc);
53 else
54 printf("MQTT Connected\n");
55
56 if ((rc = MQTTSubscribe(&client, "FreeRTOS/sample/#", 2, messageArrived)) != 0)
57 printf("Return code from MQTT subscribe is %d\n", rc);
58
59 while (++count)
60 {
61 MQTTMessage message;
62 char payload[30];
63
64 message.qos = 1;
65 message.retained = 0;
66 message.payload = payload;
67 sprintf(payload, "message number %d", count);
68 message.payloadlen = strlen(payload);
69
70 if ((rc = MQTTPublish(&client, "FreeRTOS/sample/a", &message)) != 0)
71 printf("Return code from MQTT publish is %d\n", rc);
72 #if !defined(MQTT_TASK)
73 if ((rc = MQTTYield(&client, 1000)) != 0)
74 printf("Return code from yield is %d\n", rc);
75 #endif
76 }
77
78 /* do not return */
79 }
80
81
vStartMQTTTasks(uint16_t usTaskStackSize,UBaseType_t uxTaskPriority)82 void vStartMQTTTasks(uint16_t usTaskStackSize, UBaseType_t uxTaskPriority)
83 {
84 BaseType_t x = 0L;
85
86 xTaskCreate(prvMQTTEchoTask, /* The function that implements the task. */
87 "MQTTEcho0", /* Just a text name for the task to aid debugging. */
88 usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
89 (void *)x, /* The task parameter, not used in this case. */
90 uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
91 NULL); /* The task handle is not used. */
92 }
93 /*-----------------------------------------------------------*/
94
95
96