Publisher for IBM Quickstart and Watson IoT cloud.

Dependencies:   MQTT NDefLib X_NUCLEO_IKS01A2 X_NUCLEO_NFC01A1

Fork of Cloud_IBM_MbedOS by ST Expansion SW Team

To start the demo the following expansion boards are required

X_NUCLEO_IDW01M1v2, X_NUCLEO_IKS01A2, X_NUCLEO_NFC01A1

and as MCU board the NUCLEO-L476RG as it include a True Random Number Generator needed for TLS.

After having mounted the board stack on the Nucleo board the below steps should be followed:

  • In case the X-NUCLEO-NFC-01A1 is on the board stack the WiFi SSID and password can be passed through the NFC tag by means of: 1) enabling the NFC support defining the X_NUCLEO_NFC01A1_PRESENT and recompiling, 2) when prompted on hyperterminal, programming the SSID and password to NFC using the Android app "NFCtools"
  • In case the NFC is not present, you local WiFi SSID and password can be programmed to mbed_app.json file and compiling and flashing the binary. Make sure the Wifi network has visible SSID.
  • Reset the Nucleo board and after few seconds the Nucleo green led will be on (it means the Nucleo is connected to the local Wifi and to the IBM cloud server)
  • Read the NFC tag with an Android device and the browser will be automatically opened and directed to the specific IBM quickstart demo page where the environmental values are displayed in form of a x-y graph. The values are updated every few seconds. On the Hyperterminal is possible to see the values sent to the IBM cloud server and the board mac address to be entered on the IBM quickstart web page if a manual connection is needed (eg. to connect from a PC browser).

In case of registered connection ( internetofthings.ibmcloud.com ) is needed ( no TLS ) comment the #define ORG_QUICKSTART than check in the mbed_app.json the following fields and change them according to your IBM MQTT broker account, MQTT_ORG_ID, MQTT_DEVICE_PASSWORD, MQTT_DEVICE_ID, MQTT_DEVICE_TYPE.

In case of registered connection ( internetofthings.ibmcloud.com ) with TLS encryption is needed, uncomment the #define TLS_EN and make sure the certificate (SSL_CA_PEM) is still valid.

In the default case the application connect to quickstart.internetofthings.ibmcloud.com without any encryption not authentication.

Committer:
mapellil
Date:
Fri Nov 17 08:46:13 2017 +0000
Revision:
0:e477c0f8b2e4
Child:
2:e3846f091b6b
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mapellil 0:e477c0f8b2e4 1 /* SpwfInterface NetworkSocketAPI Example Program
mapellil 0:e477c0f8b2e4 2 * Copyright (c) 2015 ARM Limited
mapellil 0:e477c0f8b2e4 3 *
mapellil 0:e477c0f8b2e4 4 * Licensed under the Apache License, Version 2.0 (the "License");
mapellil 0:e477c0f8b2e4 5 * you may not use this file except in compliance with the License.
mapellil 0:e477c0f8b2e4 6 * You may obtain a copy of the License at
mapellil 0:e477c0f8b2e4 7 *
mapellil 0:e477c0f8b2e4 8 * http://www.apache.org/licenses/LICENSE-2.0
mapellil 0:e477c0f8b2e4 9 *
mapellil 0:e477c0f8b2e4 10 * Unless required by applicable law or agreed to in writing, software
mapellil 0:e477c0f8b2e4 11 * distributed under the License is distributed on an "AS IS" BASIS,
mapellil 0:e477c0f8b2e4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mapellil 0:e477c0f8b2e4 13 * See the License for the specific language governing permissions and
mapellil 0:e477c0f8b2e4 14 * limitations under the License.
mapellil 0:e477c0f8b2e4 15 */
mapellil 0:e477c0f8b2e4 16
mapellil 0:e477c0f8b2e4 17 #include "mbed.h"
mapellil 0:e477c0f8b2e4 18 #include "easy-connect.h"
mapellil 0:e477c0f8b2e4 19 #include "MQTTClient.h"
mapellil 0:e477c0f8b2e4 20 #include "XNucleoIKS01A2.h"
mapellil 0:e477c0f8b2e4 21 #include "XNucleoNFC01A1.h"
mapellil 0:e477c0f8b2e4 22 #include "NDefLib/NDefNfcTag.h"
mapellil 0:e477c0f8b2e4 23 #include "NDefLib/RecordType/RecordURI.h"
mapellil 0:e477c0f8b2e4 24 #include "MQTTNetwork.h"
mapellil 0:e477c0f8b2e4 25 #include "MQTTmbed.h"
mapellil 0:e477c0f8b2e4 26
mapellil 0:e477c0f8b2e4 27
mapellil 0:e477c0f8b2e4 28 //------------------------------------
mapellil 0:e477c0f8b2e4 29 // Hyperterminal configuration
mapellil 0:e477c0f8b2e4 30 // 9600 bauds, 8-bit data, no parity
mapellil 0:e477c0f8b2e4 31 //------------------------------------
mapellil 0:e477c0f8b2e4 32 static Serial pc(SERIAL_TX, SERIAL_RX);
mapellil 0:e477c0f8b2e4 33 static DigitalOut myled(LED1);
mapellil 0:e477c0f8b2e4 34 static bool quickstartMode = true; // set to false to connect with authentication tocken
mapellil 0:e477c0f8b2e4 35 static bool BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 36
mapellil 0:e477c0f8b2e4 37 #define ORG_QUICKSTART // comment to connect to play.internetofthings.ibmcloud.com
mapellil 0:e477c0f8b2e4 38 //#define SUBSCRIBE // uncomment to subscribe to broker msgs (not to be used with IBM broker)
mapellil 0:e477c0f8b2e4 39 //#define X_NUCLEO_NFC01A1_PRESENT // uncomment to add NFC support
mapellil 0:e477c0f8b2e4 40
mapellil 0:e477c0f8b2e4 41 #define MQTT_MAX_PACKET_SIZE 400
mapellil 0:e477c0f8b2e4 42 #define MQTT_MAX_PAYLOAD_SIZE 300
mapellil 0:e477c0f8b2e4 43
mapellil 0:e477c0f8b2e4 44 // Configuration values needed to connect to IBM IoT Cloud
mapellil 0:e477c0f8b2e4 45 #ifdef ORG_QUICKSTART
mapellil 0:e477c0f8b2e4 46 #define ORG "quickstart" // connect to quickstart.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
mapellil 0:e477c0f8b2e4 47 #define ID ""
mapellil 0:e477c0f8b2e4 48 #define AUTH_TOKEN ""
mapellil 0:e477c0f8b2e4 49 #define DEFAULT_TYPE_NAME "iotsample-mbed-Nucleo"
mapellil 0:e477c0f8b2e4 50 #else // not def ORG_QUICKSTART
mapellil 0:e477c0f8b2e4 51 #define ORG "play" // connect to play.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
mapellil 0:e477c0f8b2e4 52 #define ID "" // For a registered connection, replace with your id
mapellil 0:e477c0f8b2e4 53 #define AUTH_TOKEN ""// For a registered connection, replace with your auth-token
mapellil 0:e477c0f8b2e4 54 #define DEFAULT_TYPE_NAME "sensor"
mapellil 0:e477c0f8b2e4 55 #endif
mapellil 0:e477c0f8b2e4 56
mapellil 0:e477c0f8b2e4 57 #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type
mapellil 0:e477c0f8b2e4 58 #define IBM_IOT_PORT MQTT_PORT
mapellil 0:e477c0f8b2e4 59
mapellil 0:e477c0f8b2e4 60 static char id[30] = ID; // mac without colons
mapellil 0:e477c0f8b2e4 61 static char org[12] = ORG;
mapellil 0:e477c0f8b2e4 62 static int connack_rc = 0; // MQTT connack return code
mapellil 0:e477c0f8b2e4 63 static char type[30] = TYPE;
mapellil 0:e477c0f8b2e4 64 static char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
mapellil 0:e477c0f8b2e4 65 static bool netConnecting = false;
mapellil 0:e477c0f8b2e4 66 static int connectTimeout = 1000;
mapellil 0:e477c0f8b2e4 67 static bool mqttConnecting = false;
mapellil 0:e477c0f8b2e4 68 static bool netConnected = false;
mapellil 0:e477c0f8b2e4 69 static bool connected = false;
mapellil 0:e477c0f8b2e4 70 static int retryAttempt = 0;
mapellil 0:e477c0f8b2e4 71 static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
mapellil 0:e477c0f8b2e4 72
mapellil 0:e477c0f8b2e4 73 static LPS22HBSensor *pressure_sensor;
mapellil 0:e477c0f8b2e4 74 static HTS221Sensor *humidity_sensor;
mapellil 0:e477c0f8b2e4 75 static HTS221Sensor *temp_sensor1;
mapellil 0:e477c0f8b2e4 76
mapellil 0:e477c0f8b2e4 77 #ifndef TARGET_SENSOR_TILE
mapellil 0:e477c0f8b2e4 78 static void BlueButtonPressed ()
mapellil 0:e477c0f8b2e4 79 {
mapellil 0:e477c0f8b2e4 80 BlueButtonToggle = true;
mapellil 0:e477c0f8b2e4 81 }
mapellil 0:e477c0f8b2e4 82 #endif
mapellil 0:e477c0f8b2e4 83
mapellil 0:e477c0f8b2e4 84 #ifdef SUBSCRIBE
mapellil 0:e477c0f8b2e4 85 void subscribe_cb(MQTT::MessageData & msgMQTT) {
mapellil 0:e477c0f8b2e4 86 char msg[MQTT_MAX_PAYLOAD_SIZE];
mapellil 0:e477c0f8b2e4 87 msg[0]='\0';
mapellil 0:e477c0f8b2e4 88 strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
mapellil 0:e477c0f8b2e4 89 printf ("--->>> subscribe_cb msg: %s\n\r", msg);
mapellil 0:e477c0f8b2e4 90 }
mapellil 0:e477c0f8b2e4 91
mapellil 0:e477c0f8b2e4 92 int subscribe(char *pubTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client)
mapellil 0:e477c0f8b2e4 93 {
mapellil 0:e477c0f8b2e4 94 return client->subscribe(pubTopic, MQTT::QOS1, subscribe_cb);
mapellil 0:e477c0f8b2e4 95 }
mapellil 0:e477c0f8b2e4 96 #endif
mapellil 0:e477c0f8b2e4 97
mapellil 0:e477c0f8b2e4 98 int connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network)
mapellil 0:e477c0f8b2e4 99 {
mapellil 0:e477c0f8b2e4 100 const char* iot_ibm = MQTT_BROKER_URL;
mapellil 0:e477c0f8b2e4 101 char hostname[strlen(org) + strlen(iot_ibm) + 1];
mapellil 0:e477c0f8b2e4 102
mapellil 0:e477c0f8b2e4 103 sprintf(hostname, "%s%s", org, iot_ibm);
mapellil 0:e477c0f8b2e4 104 // Construct clientId - d:org:type:id
mapellil 0:e477c0f8b2e4 105 char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
mapellil 0:e477c0f8b2e4 106 sprintf(clientId, "d:%s:%s:%s", org, type, id);
mapellil 0:e477c0f8b2e4 107 sprintf(subscription_url, "%s.%s/#/device/%s/sensor/", org, "internetofthings.ibmcloud.com",id);
mapellil 0:e477c0f8b2e4 108
mapellil 0:e477c0f8b2e4 109 // Network debug statements
mapellil 0:e477c0f8b2e4 110 LOG("=====================================\n\r");
mapellil 0:e477c0f8b2e4 111 LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address());
mapellil 0:e477c0f8b2e4 112 LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address());
mapellil 0:e477c0f8b2e4 113 LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
mapellil 0:e477c0f8b2e4 114 // for(int i = 0; clientId[i]; i++){ // set lowercase mac
mapellil 0:e477c0f8b2e4 115 // clientId[i] = tolower(clientId[i]);
mapellil 0:e477c0f8b2e4 116 // }
mapellil 0:e477c0f8b2e4 117 LOG("Client ID: %s\n\r", clientId);
mapellil 0:e477c0f8b2e4 118 LOG("Topic: %s\n\r",MQTT_TOPIC);
mapellil 0:e477c0f8b2e4 119 LOG("Subscription URL: %s\n\r", subscription_url);
mapellil 0:e477c0f8b2e4 120 LOG("=====================================\n\r");
mapellil 0:e477c0f8b2e4 121 netConnecting = true;
mapellil 0:e477c0f8b2e4 122 int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT);
mapellil 0:e477c0f8b2e4 123 if (rc != 0)
mapellil 0:e477c0f8b2e4 124 {
mapellil 0:e477c0f8b2e4 125 printf("rc from TCP connect is %d\r\n", rc);
mapellil 0:e477c0f8b2e4 126 return rc;
mapellil 0:e477c0f8b2e4 127 }
mapellil 0:e477c0f8b2e4 128
mapellil 0:e477c0f8b2e4 129 printf ("--->TCP Connected\n\r");
mapellil 0:e477c0f8b2e4 130 netConnected = true;
mapellil 0:e477c0f8b2e4 131 netConnecting = false;
mapellil 0:e477c0f8b2e4 132
mapellil 0:e477c0f8b2e4 133 // MQTT Connect
mapellil 0:e477c0f8b2e4 134 mqttConnecting = true;
mapellil 0:e477c0f8b2e4 135 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
mapellil 0:e477c0f8b2e4 136 data.MQTTVersion = 4;
mapellil 0:e477c0f8b2e4 137 data.struct_version=0;
mapellil 0:e477c0f8b2e4 138 data.clientID.cstring = clientId;
mapellil 0:e477c0f8b2e4 139 data.keepAliveInterval = MQTT_KEEPALIVE; // in Sec
mapellil 0:e477c0f8b2e4 140 if (!quickstartMode)
mapellil 0:e477c0f8b2e4 141 {
mapellil 0:e477c0f8b2e4 142 data.username.cstring = "use-token-auth";
mapellil 0:e477c0f8b2e4 143 data.password.cstring = auth_token;
mapellil 0:e477c0f8b2e4 144 }
mapellil 0:e477c0f8b2e4 145 if ((rc = client->connect(data)) != 0) {
mapellil 0:e477c0f8b2e4 146 printf("rc from MQTT connect is %d\r\n", rc);
mapellil 0:e477c0f8b2e4 147 connack_rc = rc;
mapellil 0:e477c0f8b2e4 148 return rc;
mapellil 0:e477c0f8b2e4 149 }
mapellil 0:e477c0f8b2e4 150 connected = true;
mapellil 0:e477c0f8b2e4 151 printf ("--->MQTT Connected\n\r");
mapellil 0:e477c0f8b2e4 152 #ifdef SUBSCRIBE
mapellil 0:e477c0f8b2e4 153 int rc=0;
mapellil 0:e477c0f8b2e4 154 if ((rc=subscribe(MQTT_TOPIC, client)) == 0) LOG ("--->>>MQTT subscribed to: %s\n\r",MQTT_TOPIC);
mapellil 0:e477c0f8b2e4 155 else LOG ("--->>>ERROR MQTT subscribe : %s\n\r",MQTT_TOPIC);
mapellil 0:e477c0f8b2e4 156 #endif
mapellil 0:e477c0f8b2e4 157 mqttConnecting = false;
mapellil 0:e477c0f8b2e4 158 connack_rc = rc;
mapellil 0:e477c0f8b2e4 159 return rc;
mapellil 0:e477c0f8b2e4 160 }
mapellil 0:e477c0f8b2e4 161
mapellil 0:e477c0f8b2e4 162
mapellil 0:e477c0f8b2e4 163 int getConnTimeout(int attemptNumber)
mapellil 0:e477c0f8b2e4 164 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
mapellil 0:e477c0f8b2e4 165 // after 20 attempts, retry every 10 minutes
mapellil 0:e477c0f8b2e4 166 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
mapellil 0:e477c0f8b2e4 167 }
mapellil 0:e477c0f8b2e4 168
mapellil 0:e477c0f8b2e4 169
mapellil 0:e477c0f8b2e4 170 void attemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network)
mapellil 0:e477c0f8b2e4 171 {
mapellil 0:e477c0f8b2e4 172 connected = false;
mapellil 0:e477c0f8b2e4 173
mapellil 0:e477c0f8b2e4 174 while (connect(client, mqttNetwork, network) != MQTT_CONNECTION_ACCEPTED)
mapellil 0:e477c0f8b2e4 175 {
mapellil 0:e477c0f8b2e4 176 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
mapellil 0:e477c0f8b2e4 177 printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);
mapellil 0:e477c0f8b2e4 178 return; // don't reattempt to connect if credentials are wrong
mapellil 0:e477c0f8b2e4 179 }
mapellil 0:e477c0f8b2e4 180 int timeout = getConnTimeout(++retryAttempt);
mapellil 0:e477c0f8b2e4 181 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
mapellil 0:e477c0f8b2e4 182
mapellil 0:e477c0f8b2e4 183 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
mapellil 0:e477c0f8b2e4 184 // or maybe just add the proper members to do this disconnect and call attemptConnect(...)
mapellil 0:e477c0f8b2e4 185 // this works - reset the system when the retry count gets to a threshold
mapellil 0:e477c0f8b2e4 186 if (retryAttempt == 5)
mapellil 0:e477c0f8b2e4 187 NVIC_SystemReset();
mapellil 0:e477c0f8b2e4 188 else
mapellil 0:e477c0f8b2e4 189 wait(timeout);
mapellil 0:e477c0f8b2e4 190 }
mapellil 0:e477c0f8b2e4 191 }
mapellil 0:e477c0f8b2e4 192
mapellil 0:e477c0f8b2e4 193 int publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client)
mapellil 0:e477c0f8b2e4 194 {
mapellil 0:e477c0f8b2e4 195 MQTT::Message message;
mapellil 0:e477c0f8b2e4 196 const char* pubTopic = MQTT_TOPIC;
mapellil 0:e477c0f8b2e4 197
mapellil 0:e477c0f8b2e4 198 char buf[MQTT_MAX_PAYLOAD_SIZE];
mapellil 0:e477c0f8b2e4 199 float temp, press, hum;
mapellil 0:e477c0f8b2e4 200
mapellil 0:e477c0f8b2e4 201 if (!client->isConnected()) { printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE; }
mapellil 0:e477c0f8b2e4 202 temp_sensor1->get_temperature(&temp);
mapellil 0:e477c0f8b2e4 203 pressure_sensor->get_pressure(&press);
mapellil 0:e477c0f8b2e4 204 humidity_sensor->get_humidity(&hum);
mapellil 0:e477c0f8b2e4 205 sprintf(buf,
mapellil 0:e477c0f8b2e4 206 "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%0.4f,\"Pressure\":%0.4f,\"Humidity\":%0.4f}}",
mapellil 0:e477c0f8b2e4 207 temp, press, hum);
mapellil 0:e477c0f8b2e4 208 message.qos = MQTT::QOS0;
mapellil 0:e477c0f8b2e4 209 message.retained = false;
mapellil 0:e477c0f8b2e4 210 message.dup = false;
mapellil 0:e477c0f8b2e4 211 message.payload = (void*)buf;
mapellil 0:e477c0f8b2e4 212 message.payloadlen = strlen(buf);
mapellil 0:e477c0f8b2e4 213
mapellil 0:e477c0f8b2e4 214 if( (message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE )
mapellil 0:e477c0f8b2e4 215 printf("message too long!\r\n");
mapellil 0:e477c0f8b2e4 216
mapellil 0:e477c0f8b2e4 217 LOG("Publishing %s\n\r", buf);
mapellil 0:e477c0f8b2e4 218 return client->publish(pubTopic, message);
mapellil 0:e477c0f8b2e4 219 }
mapellil 0:e477c0f8b2e4 220
mapellil 0:e477c0f8b2e4 221 int main()
mapellil 0:e477c0f8b2e4 222 {
mapellil 0:e477c0f8b2e4 223 myled=0;
mapellil 0:e477c0f8b2e4 224 DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL);
mapellil 0:e477c0f8b2e4 225 i2c->frequency(400000);
mapellil 0:e477c0f8b2e4 226
mapellil 0:e477c0f8b2e4 227 XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(i2c);
mapellil 0:e477c0f8b2e4 228 pressure_sensor = mems_expansion_board->pt_sensor;
mapellil 0:e477c0f8b2e4 229 temp_sensor1 = mems_expansion_board->ht_sensor;
mapellil 0:e477c0f8b2e4 230 humidity_sensor = mems_expansion_board->ht_sensor;
mapellil 0:e477c0f8b2e4 231 pressure_sensor->enable();
mapellil 0:e477c0f8b2e4 232 temp_sensor1->enable();
mapellil 0:e477c0f8b2e4 233 humidity_sensor->enable();
mapellil 0:e477c0f8b2e4 234
mapellil 0:e477c0f8b2e4 235 #if !defined (TARGET_SENSOR_TILE)
mapellil 0:e477c0f8b2e4 236 InterruptIn BlueButton(USER_BUTTON);
mapellil 0:e477c0f8b2e4 237 BlueButton.fall(&BlueButtonPressed);
mapellil 0:e477c0f8b2e4 238 BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 239 #endif
mapellil 0:e477c0f8b2e4 240
mapellil 0:e477c0f8b2e4 241 pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");
mapellil 0:e477c0f8b2e4 242 pc.printf("\r\nconnecting to AP\r\n");
mapellil 0:e477c0f8b2e4 243
mapellil 0:e477c0f8b2e4 244 quickstartMode=false;
mapellil 0:e477c0f8b2e4 245 if (strcmp(org, "quickstart") == 0){quickstartMode = true;}
mapellil 0:e477c0f8b2e4 246 NetworkInterface* network = easy_connect(true);
mapellil 0:e477c0f8b2e4 247 if (!network) {
mapellil 0:e477c0f8b2e4 248 printf ("Error easy_connect\n\r");
mapellil 0:e477c0f8b2e4 249 return -1;
mapellil 0:e477c0f8b2e4 250 }
mapellil 0:e477c0f8b2e4 251 MQTTNetwork mqttNetwork(network);
mapellil 0:e477c0f8b2e4 252 MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
mapellil 0:e477c0f8b2e4 253
mapellil 0:e477c0f8b2e4 254 if (quickstartMode){
mapellil 0:e477c0f8b2e4 255 char mac[50]; // remove all : from mac
mapellil 0:e477c0f8b2e4 256 char *digit=NULL;
mapellil 0:e477c0f8b2e4 257 sprintf (id,"%s", "");
mapellil 0:e477c0f8b2e4 258 sprintf (mac,"%s",network->get_mac_address());
mapellil 0:e477c0f8b2e4 259 digit = strtok (mac,":");
mapellil 0:e477c0f8b2e4 260 while (digit != NULL)
mapellil 0:e477c0f8b2e4 261 {
mapellil 0:e477c0f8b2e4 262 strcat (id, digit);
mapellil 0:e477c0f8b2e4 263 digit = strtok (NULL, ":");
mapellil 0:e477c0f8b2e4 264 }
mapellil 0:e477c0f8b2e4 265 }
mapellil 0:e477c0f8b2e4 266 printf ("ATTEMPT CONNECT\n\r");
mapellil 0:e477c0f8b2e4 267 attemptConnect(&client, &mqttNetwork, network);
mapellil 0:e477c0f8b2e4 268 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)
mapellil 0:e477c0f8b2e4 269 {
mapellil 0:e477c0f8b2e4 270 printf ("---ERROR line : %d\n\r", __LINE__);
mapellil 0:e477c0f8b2e4 271 while (true)
mapellil 0:e477c0f8b2e4 272 wait(1.0); // Permanent failures - don't retry
mapellil 0:e477c0f8b2e4 273 }
mapellil 0:e477c0f8b2e4 274 #ifdef X_NUCLEO_NFC01A1_PRESENT
mapellil 0:e477c0f8b2e4 275 // program NFC with broker URL
mapellil 0:e477c0f8b2e4 276 XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(*i2c, NULL, XNucleoNFC01A1::DEFAULT_GPO_PIN, XNucleoNFC01A1::DEFAULT_RF_DISABLE_PIN, NC,NC,NC);
mapellil 0:e477c0f8b2e4 277 NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
mapellil 0:e477c0f8b2e4 278 printf("NFC Init done: !\r\n");
mapellil 0:e477c0f8b2e4 279 //open the i2c session with the nfc chip
mapellil 0:e477c0f8b2e4 280 if(tag.open_session()){
mapellil 0:e477c0f8b2e4 281 //create the NDef message and record
mapellil 0:e477c0f8b2e4 282 NDefLib::Message msg;
mapellil 0:e477c0f8b2e4 283 NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTPS, subscription_url);
mapellil 0:e477c0f8b2e4 284 msg.add_record(&rUri);
mapellil 0:e477c0f8b2e4 285 //write the tag
mapellil 0:e477c0f8b2e4 286 if(tag.write(msg)){
mapellil 0:e477c0f8b2e4 287 printf("Tag writed \r\n");
mapellil 0:e477c0f8b2e4 288 }
mapellil 0:e477c0f8b2e4 289 //close the i2c session
mapellil 0:e477c0f8b2e4 290 if(!tag.close_session()){
mapellil 0:e477c0f8b2e4 291 printf("Error Closing the session\r\n");
mapellil 0:e477c0f8b2e4 292 }
mapellil 0:e477c0f8b2e4 293 }else printf("Error open Session\r\n");
mapellil 0:e477c0f8b2e4 294 #endif
mapellil 0:e477c0f8b2e4 295 myled=1;
mapellil 0:e477c0f8b2e4 296 int count = 0;
mapellil 0:e477c0f8b2e4 297 while (true)
mapellil 0:e477c0f8b2e4 298 {
mapellil 0:e477c0f8b2e4 299 if (BlueButtonToggle == false && connected == true) {
mapellil 0:e477c0f8b2e4 300 if (++count == 6)
mapellil 0:e477c0f8b2e4 301 { // Publish a message every 3 second
mapellil 0:e477c0f8b2e4 302 if (publish(&client) != MQTT::SUCCESS) {
mapellil 0:e477c0f8b2e4 303 myled=0;
mapellil 0:e477c0f8b2e4 304 count=0;
mapellil 0:e477c0f8b2e4 305 client.disconnect();
mapellil 0:e477c0f8b2e4 306 mqttNetwork.disconnect();
mapellil 0:e477c0f8b2e4 307 attemptConnect(&client, &mqttNetwork, network); // if we have lost the connection
mapellil 0:e477c0f8b2e4 308 } else {
mapellil 0:e477c0f8b2e4 309 myled=1;
mapellil 0:e477c0f8b2e4 310 count=0;
mapellil 0:e477c0f8b2e4 311 }
mapellil 0:e477c0f8b2e4 312 }
mapellil 0:e477c0f8b2e4 313 client.yield(500); // allow the MQTT client to receive subscribe messages and manage keep alive
mapellil 0:e477c0f8b2e4 314 } else if (BlueButtonToggle == true && connected == true){ // disconnect MQTT
mapellil 0:e477c0f8b2e4 315 printf ("--->> Disconnect\n\r");
mapellil 0:e477c0f8b2e4 316 connected = false;
mapellil 0:e477c0f8b2e4 317 count = 0;
mapellil 0:e477c0f8b2e4 318 BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 319 #ifdef SUBSCRIBE
mapellil 0:e477c0f8b2e4 320 // unsubscribe(const char* topicFilter); // unsubscribe if subscribed
mapellil 0:e477c0f8b2e4 321 #endif
mapellil 0:e477c0f8b2e4 322 client.disconnect();
mapellil 0:e477c0f8b2e4 323 mqttNetwork.disconnect();
mapellil 0:e477c0f8b2e4 324 } else if (BlueButtonToggle == true && connected == false) {
mapellil 0:e477c0f8b2e4 325 connected = true;
mapellil 0:e477c0f8b2e4 326 BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 327 } else wait (0.5);
mapellil 0:e477c0f8b2e4 328 }
mapellil 0:e477c0f8b2e4 329 }
mapellil 0:e477c0f8b2e4 330