forked from coisme/Mbed-to-IBM-Watson-IoT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
352 lines (300 loc) · 12.3 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*******************************************************************************
* 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 "MQTTClientMbedOs.h"
#include "MQTT_server_setting.h"
#include "mbed-trace/mbed_trace.h"
#include "mbed_events.h"
#include "NTPClient.h"
#include "mbedtls/error.h"
#define TIME_JWT_EXP (60*60*24) // 24 hours (MAX)
// LED on/off - This could be different among boards.
#if defined(TARGET_NUCLEO_F767ZI)
// The following values do target the NUCLEO_F767ZI board.
#define LED_ON 1
#define LED_OFF 0
#else
#define LED_ON 0
#define LED_OFF 1
#endif
#include <string>
#include <map>
using ErrorCodesMap_t = std::map<nsapi_size_or_error_t, std::string>;
using IndexElement_t = ErrorCodesMap_t::value_type;
static ErrorCodesMap_t make_error_codes_map()
{
ErrorCodesMap_t eMap;
eMap.insert(IndexElement_t(NSAPI_ERROR_OK, std::string("\"no error\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_WOULD_BLOCK, std::string("\"no data is not available but call is non-blocking\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_UNSUPPORTED, std::string("\"unsupported functionality\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_PARAMETER, std::string("\"invalid configuration\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_NO_CONNECTION, std::string("\"not connected to a network\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_NO_SOCKET, std::string("\"socket not available for use\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_NO_ADDRESS, std::string("\"IP address is not known\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_NO_MEMORY, std::string("\"memory resource not available\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_NO_SSID, std::string("\"ssid not found\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_DNS_FAILURE, std::string("\"DNS failed to complete successfully\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_DHCP_FAILURE, std::string("\"DHCP failed to complete successfully\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_AUTH_FAILURE, std::string("\"connection to access point failed\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_DEVICE_ERROR, std::string("\"failure interfacing with the network processor\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_IN_PROGRESS, std::string("\"operation (eg connect) in progress\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_ALREADY, std::string("\"operation (eg connect) already in progress\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_IS_CONNECTED, std::string("\"socket is already connected\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_CONNECTION_LOST, std::string("\"connection lost\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_CONNECTION_TIMEOUT, std::string("\"connection timed out\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_ADDRESS_IN_USE, std::string("\"Address already in use\"")));
eMap.insert(IndexElement_t(NSAPI_ERROR_TIMEOUT, std::string("\"operation timed out\"")));
return eMap;
}
static ErrorCodesMap_t gs_ErrorCodesMap = make_error_codes_map();
std::string ToString(const nsapi_size_or_error_t & key)
{
return (gs_ErrorCodesMap.at(key));
}
// Flag to be set when a message needs to be published, i.e. BUTTON is pushed.
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 = 1024;
// Buffer for a receiving message.
char messageBuffer[MESSAGE_BUFFER_SIZE];
// Function prototypes
void handleMqttMessage(MQTT::MessageData& md);
void handleButtonRise();
#if defined(TARGET_WIO_3G) || defined(TARGET_WIO_BG96)
DigitalOut GrovePower(GRO_POWR, 1);
#undef BUTTON1
#define BUTTON1 D20
#endif
int main(int argc, char* argv[])
{
wait_ms(500);
mbed_trace_init();
const float version = 0.1;
NetworkInterface* network = NULL;
#if defined(TARGET_NUCLEO_F767ZI)
// The following values do target the NUCLEO_F767ZI board.
// Reference : en.DM00244518.pdf
DigitalOut led_green(LED1);
DigitalOut led_blue(LED2);
DigitalOut led_red(LED3);
#else
DigitalOut led_red(LED1, LED_OFF);
DigitalOut led_green(LED2, LED_OFF);
DigitalOut led_blue(LED3, LED_OFF);
#endif
printf("Mbed to Watson IoT : version is %.2f\r\n", version);
printf("\r\n");
// Turns on green LED to indicate processing initialization process
led_green = LED_ON;
printf("Opening network interface...\r\n");
{
network = NetworkInterface::get_default_instance(); // If true, prints out connection details.
if (!network) {
printf("Unable to open network interface.\r\n");
return -1;
}
nsapi_error_t net_status = NSAPI_ERROR_NO_CONNECTION;
while ((net_status = network->connect()) != NSAPI_ERROR_OK) {
printf("Unable to connect to network (%d). Retrying...\r\n", net_status);
}
}
printf("Network interface opened successfully.\r\n");
printf("\r\n");
// sync the real time clock (RTC)
NTPClient ntp(network);
const char* serverAddress = "time.google.com";
int port = 123;
ntp.set_server(const_cast<char *>(serverAddress), port);
time_t now = ntp.get_timestamp();
set_time(now);
printf("Time is now %s", ctime(&now));
std::string hostname = std::string(ORG_ID) + ".messaging.internetofthings.ibmcloud.com";
// Establish a network connection.
TLSSocket *socket = new TLSSocket; // Allocate on heap to avoid stack overflow.
printf("Connecting to host %s:%d ...\r\n", hostname.c_str(), 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;
}
if (SSL_CLIENT_CERT_PEM != NULL && SSL_CLIENT_PRIVATE_KEY_PEM != NULL) {
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(hostname.c_str(), 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");
const char* username = "use-token-auth";
std::string cid = std::string("d:") + ORG_ID + ":" + DEVICE_TYPE + ":" + DEVICE_ID;
// Establish a MQTT connection.
MQTTClient* mqttClient = new MQTTClient(socket);
printf("MQTT client is connecting to the service ...\r\n");
{
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 4; // 3 = 3.1 4 = 3.1.1
data.clientID.cstring = (char *)cid.c_str();
data.username.cstring = (char *)username;
data.password.cstring = (char *)TOKEN;
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");
// Network initialization done. Turn off the green LED
led_green = LED_OFF;
const char* mqtt_topic_pub = "iot-2/evt/myevt/fmt/text";
const char* mqtt_topic_sub = "iot-2/cmd/mycmd/fmt/text";
// Subscribe a topic.
bool isSubscribed = false;
printf("Client is trying to subscribe a topic \"%s\".\r\n", mqtt_topic_sub);
{
int rc = mqttClient->subscribe(mqtt_topic_sub, MQTT::QOS0, handleMqttMessage);
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 for publishing a message.
InterruptIn btn1(BUTTON1);
btn1.rise(&handleButtonRise);
printf("To send a packet, push the button 1 on your board.\r\n");
// Main loop
while (1)
{
// Client is disconnected.
if (!mqttClient->isConnected())
{
break;
}
// Waits a message and handles keepalive.
if (mqttClient->yield(100) != MQTT::SUCCESS)
{
break;
}
// Received a message.
if (isMessageArrived)
{
isMessageArrived = false;
printf("\r\nMessage arrived:\r\n%s\r\n", messageBuffer);
}
// Button is pushed - publish a message.
if (isPublish)
{
isPublish = false;
static unsigned int id = 0;
static unsigned int count = 0;
// When sending a message, blue LED lights.
led_blue = LED_ON;
MQTT::Message message;
message.retained = false;
message.dup = false;
const size_t len = 128;
char buf[len];
size_t actualLength = snprintf(buf, len, "Message #%d from %s.", count, DEVICE_ID);
message.payload = (void*)buf;
message.qos = MQTT::QOS0;
message.id = id++;
message.payloadlen = actualLength + 1; // Include the null terminator for safety and robustness.
// Publish a message.
printf("\r\nPublishing message to the topic %s:\r\n%s\r\n", mqtt_topic_pub, buf);
int rc = mqttClient->publish(mqtt_topic_pub, message);
if (rc != MQTT::SUCCESS)
{
printf("ERROR: rc from MQTT publish is %d\r\n", rc);
}
printf("Message published.\r\n");
count++;
led_blue = LED_OFF;
}
}
printf("The client has disconnected.\r\n");
if (mqttClient)
{
if (isSubscribed)
{
mqttClient->unsubscribe(mqtt_topic_sub);
mqttClient->setMessageHandler(mqtt_topic_sub, 0);
}
if (mqttClient->isConnected())
{
mqttClient->disconnect();
}
delete mqttClient;
}
if(socket) {
socket->close();
delete socket;
}
if (network)
{
network->disconnect();
// network is not created by new.
}
// Turn on the red LED when the program is done.
led_red = LED_ON;
}
/*
* Callback function called when a message arrived from server.
*/
void handleMqttMessage(MQTT::MessageData& md)
{
// Copy payload to the buffer.
MQTT::Message &message = md.message;
memcpy(messageBuffer, message.payload, message.payloadlen);
messageBuffer[message.payloadlen] = '\0';
isMessageArrived = true;
}
/*
* Callback function called when button is pushed.
*/
void handleButtonRise()
{
isPublish = true;
}