1 /*******************************************************************************
2 * Copyright (c) 2014 IBM Corp.
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * Contributors:
14 * Ian Craggs - initial API and implementation and/or initial documentation
15 * Sergio R. Caprile - clarifications and/or documentation extension
16 *******************************************************************************/
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "cmsis_os2.h"
23 #include "ohos_init.h"
24
25 #include "MQTTClient.h"
26 #include "wifi_connect.h"
27
28 #define MQTT_SERVERIP "192.168.0.179"
29 #define MQTT_SERVERPORT 1883
30 #define MQTT_CMD_TIMEOUT_MS 2000
31 #define MQTT_KEEP_ALIVE_MS 2000
32 #define MQTT_DELAY_2S 200
33 #define MQTT_DELAY_500_MS 50
34 #define MQTT_VERSION 3
35 #define MQTT_QOS 2
36 #define MQTT_TASK_STACK_SIZE (1024 * 10)
37
38 static unsigned char sendBuf[1000];
39 static unsigned char readBuf[1000];
40
41 Network network;
42
messageArrived(MessageData * data)43 void messageArrived(MessageData *data)
44 {
45 printf("Message arrived on topic %.*s: %.*s\n", data->topicName->lenstring.len,
46 data->topicName->lenstring.data, data->message->payloadlen, data->message->payload);
47 }
48
MQTTDemoTask(void)49 static void MQTTDemoTask(void)
50 {
51 WifiConnect("BearPi", "123456789");
52 printf("Starting ...\n");
53 int rc, count = 0;
54 MQTTClient client;
55
56 NetworkInit(&network);
57 printf("NetworkConnect ...\n");
58
59 NetworkConnect(&network, MQTT_SERVERIP, MQTT_SERVERPORT);
60 printf("MQTTClientInit ...\n");
61 MQTTClientInit(&client, &network, MQTT_CMD_TIMEOUT_MS, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
62
63 MQTTString clientId = MQTTString_initializer;
64 clientId.cstring = "bearpi";
65
66 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
67 data.clientID = clientId;
68 data.willFlag = 0;
69 data.MQTTVersion = MQTT_VERSION;
70 data.keepAliveInterval = MQTT_KEEP_ALIVE_MS;
71 data.cleansession = 1;
72
73 printf("MQTTConnect ...\n");
74 rc = MQTTConnect(&client, &data);
75 if (rc != 0) {
76 printf("MQTTConnect: %d\n", rc);
77 NetworkDisconnect(&network);
78 MQTTDisconnect(&client);
79 osDelay(MQTT_DELAY_2S);
80 }
81
82 printf("MQTTSubscribe ...\n");
83 rc = MQTTSubscribe(&client, "substopic", MQTT_QOS, messageArrived);
84 if (rc != 0) {
85 printf("MQTTSubscribe: %d\n", rc);
86 osDelay(MQTT_DELAY_2S);
87 }
88 while (++count) {
89 MQTTMessage message;
90 char payload[30];
91
92 message.qos = MQTT_QOS;
93 message.retained = 0;
94 message.payload = payload;
95 (void)sprintf_s(payload, sizeof(payload), "message number %d", count);
96 message.payloadlen = strlen(payload);
97
98 if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0) {
99 printf("Return code from MQTT publish is %d\n", rc);
100 NetworkDisconnect(&network);
101 MQTTDisconnect(&client);
102 }
103 osDelay(MQTT_DELAY_500_MS);
104 }
105 }
MQTTDemo(void)106 static void MQTTDemo(void)
107 {
108 osThreadAttr_t attr;
109
110 attr.name = "MQTTDemoTask";
111 attr.attr_bits = 0U;
112 attr.cb_mem = NULL;
113 attr.cb_size = 0U;
114 attr.stack_mem = NULL;
115 attr.stack_size = MQTT_TASK_STACK_SIZE;
116 attr.priority = osPriorityNormal;
117
118 if (osThreadNew((osThreadFunc_t)MQTTDemoTask, NULL, &attr) == NULL) {
119 printf("[MQTT_Demo] Failed to create MQTTDemoTask!\n");
120 }
121 }
122
123 APP_FEATURE_INIT(MQTTDemo);