-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
272 lines (233 loc) · 8.69 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*******************************************************************************
* Copyright (c) 2014, 2015 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Ian Craggs - make sure QoS2 processing works, and add device headers
*******************************************************************************/
/**
This is a sample program to illustrate the use of the MQTT Client library
on the mbed platform. The Client class requires two classes which mediate
access to system interfaces for networking and timing. As long as these two
classes provide the required public programming interfaces, it does not matter
what facilities they use underneath. In this program, they use the mbed
system libraries.
*/
#define MQTTCLIENT_QOS1 0
#define MQTTCLIENT_QOS2 0
#include "mbed.h"
#include "NTPClient.h"
#include "TLSSocket.h"
#include "MQTTClientMbedOs.h"
#include "MQTT_server_setting.h"
#include "mbed-trace/mbed_trace.h"
#include "mbed_events.h"
#include "mbedtls/error.h"
#define LED_ON MBED_CONF_APP_LED_ON
#define LED_OFF MBED_CONF_APP_LED_OFF
static volatile bool isPublish = false;
/* Flag to be set when received a message from the server. */
static volatile bool isMessageArrived = false;
/* Buffer size for a receiving message. */
const int MESSAGE_BUFFER_SIZE = 256;
/* Buffer for a receiving message. */
char messageBuffer[MESSAGE_BUFFER_SIZE];
/* Enable GPIO power for Wio target */
#if defined(TARGET_WIO_3G) || defined(TARGET_WIO_BG96)
DigitalOut GrovePower(GRO_POWR, 1);
#endif
// An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
// This is great because things such as network operations are illegal in ISR, so updating a resource in a button's fall() function is not allowed
EventQueue eventQueue;
Thread thread1;
/*
* Callback function called when a message arrived from server.
*/
void messageArrived(MQTT::MessageData& md)
{
// Copy payload to the buffer.
MQTT::Message &message = md.message;
if(message.payloadlen >= MESSAGE_BUFFER_SIZE) {
// TODO: handling error
} else {
memcpy(messageBuffer, message.payload, message.payloadlen);
}
messageBuffer[message.payloadlen] = '\0';
isMessageArrived = true;
}
/*
* Callback function called when the button1 is clicked.
*/
void btn1_rise_handler() {
isPublish = true;
}
int main(int argc, char* argv[])
{
thread_sleep_for(100);
mbed_trace_init();
const float version = 1.0;
bool isSubscribed = false;
NetworkInterface* network = NULL;
TLSSocket *socket = new TLSSocket; // Allocate on heap to avoid stack overflow.
MQTTClient* mqttClient = NULL;
DigitalOut led(MBED_CONF_APP_LED_PIN, LED_ON);
printf("HelloMQTT: version is %.2f\r\n", version);
printf("\r\n");
#ifdef MBED_MAJOR_VERSION
printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
#endif
printf("Opening network interface...\r\n");
{
network = NetworkInterface::get_default_instance();
if (!network) {
printf("Error! No network inteface found.\n");
return -1;
}
printf("Connecting to network\n");
nsapi_size_or_error_t ret = network->connect();
if (ret) {
printf("Unable to connect! returned %d\n", ret);
return -1;
}
}
printf("Network interface opened successfully.\r\n");
printf("\r\n");
// sync the real time clock (RTC)
NTPClient ntp(network);
ntp.set_server("time.google.com", 123);
time_t now = ntp.get_timestamp();
set_time(now);
printf("Time is now %s", ctime(&now));
printf("Connecting to host %s:%d ...\r\n", MQTT_SERVER_HOST_NAME, MQTT_SERVER_PORT);
{
nsapi_error_t ret = socket->open(network);
if (ret != NSAPI_ERROR_OK) {
printf("Could not open socket! Returned %d\n", ret);
return -1;
}
ret = socket->set_root_ca_cert(SSL_CA_PEM);
if (ret != NSAPI_ERROR_OK) {
printf("Could not set ca cert! Returned %d\n", ret);
return -1;
}
ret = socket->set_client_cert_key(SSL_CLIENT_CERT_PEM, SSL_CLIENT_PRIVATE_KEY_PEM);
if (ret != NSAPI_ERROR_OK) {
printf("Could not set keys! Returned %d\n", ret);
return -1;
}
ret = socket->connect(MQTT_SERVER_HOST_NAME, MQTT_SERVER_PORT);
if (ret != NSAPI_ERROR_OK) {
printf("Could not connect! Returned %d\n", ret);
return -1;
}
}
printf("Connection established.\r\n");
printf("\r\n");
printf("MQTT client is trying to connect the server ...\r\n");
{
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.clientID.cstring = (char *)MQTT_CLIENT_ID;
data.username.cstring = (char *)MQTT_USERNAME;
data.password.cstring = (char *)MQTT_PASSWORD;
mqttClient = new MQTTClient(socket);
int rc = mqttClient->connect(data);
if (rc != MQTT::SUCCESS) {
printf("ERROR: rc from MQTT connect is %d\r\n", rc);
return -1;
}
}
printf("Client connected.\r\n");
printf("\r\n");
printf("Client is trying to subscribe a topic \"%s\".\r\n", MQTT_TOPIC_SUB);
{
int rc = mqttClient->subscribe(MQTT_TOPIC_SUB, MQTT::QOS0, messageArrived);
if (rc != MQTT::SUCCESS) {
printf("ERROR: rc from MQTT subscribe is %d\r\n", rc);
return -1;
}
isSubscribed = true;
}
printf("Client has subscribed a topic \"%s\".\r\n", MQTT_TOPIC_SUB);
printf("\r\n");
// Enable button 1
InterruptIn btn1 = InterruptIn(MBED_CONF_APP_USER_BUTTON);
btn1.rise(btn1_rise_handler);
printf("To send a packet, push the button 1 on your board.\r\n\r\n");
// Turn off the LED to let users know connection process done.
led = LED_OFF;
while(1) {
/* Check connection */
if(!mqttClient->isConnected()){
break;
}
/* Pass control to other thread. */
if(mqttClient->yield() != MQTT::SUCCESS) {
break;
}
/* Received a control message. */
if(isMessageArrived) {
isMessageArrived = false;
// Just print it out here.
printf("\r\nMessage arrived:\r\n%s\r\n\r\n", messageBuffer);
}
/* Publish data */
if(isPublish) {
isPublish = false;
static unsigned short id = 0;
static unsigned int count = 0;
count++;
// When sending a message, LED lights blue.
led = LED_ON;
MQTT::Message message;
message.retained = false;
message.dup = false;
const size_t buf_size = 100;
char *buf = new char[buf_size];
message.payload = (void*)buf;
message.qos = MQTT::QOS0;
message.id = id++;
int ret = snprintf(buf, buf_size, "%d", count);
if(ret < 0) {
printf("ERROR: snprintf() returns %d.", ret);
continue;
}
message.payloadlen = ret;
// Publish a message.
printf("Publishing message.\r\n");
int rc = mqttClient->publish(MQTT_TOPIC_SUB, message);
if(rc != MQTT::SUCCESS) {
printf("ERROR: rc from MQTT publish is %d\r\n", rc);
}
printf("Message published.\r\n");
delete[] buf;
thread_sleep_for(200);
led = LED_OFF;
}
}
printf("The client has disconnected.\r\n");
if(mqttClient) {
if(isSubscribed) {
mqttClient->unsubscribe(MQTT_TOPIC_SUB);
}
if(mqttClient->isConnected())
mqttClient->disconnect();
delete mqttClient;
}
if(socket) {
socket->close();
}
if(network) {
network->disconnect();
// network is not created by new.
}
}