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 Jan 05 14:53:46 2018 +0100
Revision:
4:df4138621205
Parent:
2:e3846f091b6b
Child:
5:efa13fc5d99a
added Wifi SSID/passw passing through NFC

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 4:df4138621205 22 #include "NDefNfcTag.h"
mapellil 0:e477c0f8b2e4 23 #include "NDefLib/RecordType/RecordURI.h"
mapellil 4:df4138621205 24 #include "RecordWifiConf.h"
mapellil 0:e477c0f8b2e4 25 #include "MQTTNetwork.h"
mapellil 0:e477c0f8b2e4 26 #include "MQTTmbed.h"
mapellil 0:e477c0f8b2e4 27
mapellil 0:e477c0f8b2e4 28
mapellil 0:e477c0f8b2e4 29 //------------------------------------
mapellil 0:e477c0f8b2e4 30 // Hyperterminal configuration
mapellil 0:e477c0f8b2e4 31 // 9600 bauds, 8-bit data, no parity
mapellil 0:e477c0f8b2e4 32 //------------------------------------
mapellil 0:e477c0f8b2e4 33 static Serial pc(SERIAL_TX, SERIAL_RX);
mapellil 0:e477c0f8b2e4 34 static DigitalOut myled(LED1);
mapellil 0:e477c0f8b2e4 35 static bool quickstartMode = true; // set to false to connect with authentication tocken
mapellil 0:e477c0f8b2e4 36 static bool BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 37
mapellil 0:e477c0f8b2e4 38 #define ORG_QUICKSTART // comment to connect to play.internetofthings.ibmcloud.com
mapellil 0:e477c0f8b2e4 39 //#define SUBSCRIBE // uncomment to subscribe to broker msgs (not to be used with IBM broker)
mapellil 2:e3846f091b6b 40 #define X_NUCLEO_NFC01A1_PRESENT // uncomment to add NFC support
mapellil 0:e477c0f8b2e4 41
mapellil 0:e477c0f8b2e4 42 #define MQTT_MAX_PACKET_SIZE 400
mapellil 0:e477c0f8b2e4 43 #define MQTT_MAX_PAYLOAD_SIZE 300
mapellil 0:e477c0f8b2e4 44
mapellil 0:e477c0f8b2e4 45 // Configuration values needed to connect to IBM IoT Cloud
mapellil 0:e477c0f8b2e4 46 #ifdef ORG_QUICKSTART
mapellil 0:e477c0f8b2e4 47 #define ORG "quickstart" // connect to quickstart.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
mapellil 0:e477c0f8b2e4 48 #define ID ""
mapellil 0:e477c0f8b2e4 49 #define AUTH_TOKEN ""
mapellil 4:df4138621205 50 //#define DEFAULT_TYPE_NAME "iotsample-mbed-Nucleo"
mapellil 4:df4138621205 51 #define DEFAULT_TYPE_NAME "sensor"
mapellil 0:e477c0f8b2e4 52 #else // not def ORG_QUICKSTART
mapellil 2:e3846f091b6b 53 #define ORG MQTT_ORG_ID // connect to ORG.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
mapellil 2:e3846f091b6b 54 #define ID MQTT_DEVICE_ID // For a registered connection is your device id
mapellil 2:e3846f091b6b 55 #define AUTH_TOKEN MQTT_DEVICE_PASSWORD // For a registered connection is a device auth-token
mapellil 2:e3846f091b6b 56 #define DEFAULT_TYPE_NAME MQTT_DEVICE_TYPE // For a registered connection is device type
mapellil 0:e477c0f8b2e4 57 #endif
mapellil 0:e477c0f8b2e4 58
mapellil 0:e477c0f8b2e4 59 #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type
mapellil 0:e477c0f8b2e4 60 #define IBM_IOT_PORT MQTT_PORT
mapellil 4:df4138621205 61
mapellil 4:df4138621205 62 #define MAXLEN_MBED_CONF_APP_WIFI_SSID 32 // same as WIFI_SSID_MAX_LEN in easy_connect
mapellil 4:df4138621205 63 #define MAXLEN_MBED_CONF_APP_WIFI_PASSWORD 64 // same as WIFI_PASSWORD_MAX_LEN
mapellil 4:df4138621205 64
mapellil 0:e477c0f8b2e4 65 static char id[30] = ID; // mac without colons
mapellil 0:e477c0f8b2e4 66 static char org[12] = ORG;
mapellil 0:e477c0f8b2e4 67 static int connack_rc = 0; // MQTT connack return code
mapellil 0:e477c0f8b2e4 68 static char type[30] = TYPE;
mapellil 0:e477c0f8b2e4 69 static char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
mapellil 0:e477c0f8b2e4 70 static bool netConnecting = false;
mapellil 0:e477c0f8b2e4 71 static int connectTimeout = 1000;
mapellil 0:e477c0f8b2e4 72 static bool mqttConnecting = false;
mapellil 0:e477c0f8b2e4 73 static bool netConnected = false;
mapellil 0:e477c0f8b2e4 74 static bool connected = false;
mapellil 0:e477c0f8b2e4 75 static int retryAttempt = 0;
mapellil 0:e477c0f8b2e4 76 static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
mapellil 4:df4138621205 77 static char ssid[MAXLEN_MBED_CONF_APP_WIFI_SSID]; // Network must be visible otherwise it can't connect
mapellil 4:df4138621205 78 static char seckey[MAXLEN_MBED_CONF_APP_WIFI_PASSWORD];
mapellil 0:e477c0f8b2e4 79
mapellil 0:e477c0f8b2e4 80 static LPS22HBSensor *pressure_sensor;
mapellil 0:e477c0f8b2e4 81 static HTS221Sensor *humidity_sensor;
mapellil 0:e477c0f8b2e4 82 static HTS221Sensor *temp_sensor1;
mapellil 0:e477c0f8b2e4 83
mapellil 4:df4138621205 84 #ifdef X_NUCLEO_NFC01A1_PRESENT
mapellil 4:df4138621205 85 // Read from NFC the Wifi record
mapellil 4:df4138621205 86 void NFCReadRecordWIFI (XNucleoNFC01A1 *nfcNucleo) {
mapellil 4:df4138621205 87
mapellil 4:df4138621205 88 NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
mapellil 4:df4138621205 89
mapellil 4:df4138621205 90 printf ("Write to NFC tag the WiFi record ...\n\r");
mapellil 4:df4138621205 91 for (int ReadSSIDPassw=0; ReadSSIDPassw!=1; wait_ms(1000)) {
mapellil 4:df4138621205 92 //open the i2c session with the nfc chip
mapellil 4:df4138621205 93 NDefLib::Message readMsg;
mapellil 4:df4138621205 94 if(tag.open_session(1)){
mapellil 4:df4138621205 95 tag.read(&readMsg);
mapellil 4:df4138621205 96 // printf ("---- N record %d\n\r", readMsg.get_N_records());
mapellil 4:df4138621205 97 if(readMsg.get_N_records()==0){
mapellil 4:df4138621205 98 printf("Error Read\r\n");
mapellil 4:df4138621205 99 }else {
mapellil 4:df4138621205 100 for(uint32_t i=0;i<readMsg.get_N_records();i++){
mapellil 4:df4138621205 101 NDefLib::Record *r = readMsg[i];
mapellil 4:df4138621205 102 // printf ("N record %d\n\r", readMsg.get_N_records());
mapellil 4:df4138621205 103 if (r != NULL) {
mapellil 4:df4138621205 104 printf ("Record RecordType_t: %d\n\r", r->get_type());
mapellil 4:df4138621205 105 if (r->get_type() == NDefLib::Record::TYPE_WIFI_CONF) {
mapellil 4:df4138621205 106 NDefLib::RecordWifiConf * temp = (NDefLib::RecordWifiConf*)r;
mapellil 4:df4138621205 107 sprintf (ssid, "%s", temp->get_network_ssid().c_str());
mapellil 4:df4138621205 108 sprintf (seckey, "%s", temp->get_network_key().c_str());
mapellil 4:df4138621205 109 printf ("Read SSID: %s Passw: %s\n\r", ssid, seckey);
mapellil 4:df4138621205 110 ReadSSIDPassw =1;
mapellil 4:df4138621205 111 }
mapellil 4:df4138621205 112 else if (r->get_type() == NDefLib::Record::TYPE_UNKNOWN) { printf ("NFC RECORD TYPE_UNKNOWN\n\r"); }
mapellil 4:df4138621205 113 }
mapellil 4:df4138621205 114 if (r != NULL) delete r;
mapellil 4:df4138621205 115 }//for all the NFC records
mapellil 4:df4138621205 116 }//nfc n records
mapellil 4:df4138621205 117 //close the i2c session
mapellil 4:df4138621205 118 if(!tag.close_session()){
mapellil 4:df4138621205 119 printf("Error Closing the session\r\n");
mapellil 4:df4138621205 120 }
mapellil 4:df4138621205 121 }else printf("Error open Session\r\n");
mapellil 4:df4138621205 122 }
mapellil 4:df4138621205 123 }
mapellil 4:df4138621205 124
mapellil 4:df4138621205 125 // add to NFC the HTTP_BROKER_URL URL swapping the two NFC records in order to set the HTTP_BROKER_URL as a first
mapellil 4:df4138621205 126 // record allowing to read URL from any device.
mapellil 4:df4138621205 127 void NFCWriteRecordURI (XNucleoNFC01A1 *nfcNucleo) {
mapellil 4:df4138621205 128
mapellil 4:df4138621205 129 NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
mapellil 4:df4138621205 130 //open the i2c session with the nfc chip
mapellil 4:df4138621205 131 if(tag.open_session()){
mapellil 4:df4138621205 132 //create the NDef message and record
mapellil 4:df4138621205 133 NDefLib::Message msg;
mapellil 4:df4138621205 134 tag.read(&msg);
mapellil 4:df4138621205 135 // printf ("---- N record present: %d\n\r", msg.get_N_records());
mapellil 4:df4138621205 136 NDefLib::Record *r = msg[0];
mapellil 4:df4138621205 137 if ((r != NULL) && (r->get_type() == NDefLib::Record::TYPE_WIFI_CONF)) { // first time record 0 must be wifi
mapellil 4:df4138621205 138 NDefLib::RecordWifiConf * const pWIFI = (NDefLib::RecordWifiConf*)r;
mapellil 4:df4138621205 139 NDefLib::Message msg2;
mapellil 4:df4138621205 140 NDefLib::RecordURI rURL(NDefLib::RecordURI::HTTPS, subscription_url);
mapellil 4:df4138621205 141 msg2.add_record(&rURL); // add a new HTTP record to msg2
mapellil 4:df4138621205 142 msg2.add_record(pWIFI); // copy a Wifi record to msg2
mapellil 4:df4138621205 143 //write the tag
mapellil 4:df4138621205 144 if(tag.write(msg2)){
mapellil 4:df4138621205 145 printf("Tag writed \r\n");
mapellil 4:df4138621205 146 }
mapellil 4:df4138621205 147 NDefLib::Message::remove_and_delete_all_record(msg);
mapellil 4:df4138621205 148 }
mapellil 4:df4138621205 149 //close the i2c session
mapellil 4:df4138621205 150 if(!tag.close_session()){
mapellil 4:df4138621205 151 printf("Error Closing the session\r\n");
mapellil 4:df4138621205 152 }
mapellil 4:df4138621205 153 }else printf("Error open Session\r\n");
mapellil 4:df4138621205 154 }
mapellil 4:df4138621205 155
mapellil 4:df4138621205 156 #endif
mapellil 4:df4138621205 157
mapellil 0:e477c0f8b2e4 158 #ifndef TARGET_SENSOR_TILE
mapellil 0:e477c0f8b2e4 159 static void BlueButtonPressed ()
mapellil 0:e477c0f8b2e4 160 {
mapellil 0:e477c0f8b2e4 161 BlueButtonToggle = true;
mapellil 0:e477c0f8b2e4 162 }
mapellil 0:e477c0f8b2e4 163 #endif
mapellil 0:e477c0f8b2e4 164
mapellil 0:e477c0f8b2e4 165 #ifdef SUBSCRIBE
mapellil 0:e477c0f8b2e4 166 void subscribe_cb(MQTT::MessageData & msgMQTT) {
mapellil 0:e477c0f8b2e4 167 char msg[MQTT_MAX_PAYLOAD_SIZE];
mapellil 0:e477c0f8b2e4 168 msg[0]='\0';
mapellil 0:e477c0f8b2e4 169 strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
mapellil 0:e477c0f8b2e4 170 printf ("--->>> subscribe_cb msg: %s\n\r", msg);
mapellil 0:e477c0f8b2e4 171 }
mapellil 0:e477c0f8b2e4 172
mapellil 0:e477c0f8b2e4 173 int subscribe(char *pubTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client)
mapellil 0:e477c0f8b2e4 174 {
mapellil 0:e477c0f8b2e4 175 return client->subscribe(pubTopic, MQTT::QOS1, subscribe_cb);
mapellil 0:e477c0f8b2e4 176 }
mapellil 0:e477c0f8b2e4 177 #endif
mapellil 0:e477c0f8b2e4 178
mapellil 0:e477c0f8b2e4 179 int connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network)
mapellil 0:e477c0f8b2e4 180 {
mapellil 0:e477c0f8b2e4 181 const char* iot_ibm = MQTT_BROKER_URL;
mapellil 0:e477c0f8b2e4 182 char hostname[strlen(org) + strlen(iot_ibm) + 1];
mapellil 0:e477c0f8b2e4 183
mapellil 0:e477c0f8b2e4 184 sprintf(hostname, "%s%s", org, iot_ibm);
mapellil 0:e477c0f8b2e4 185 // Construct clientId - d:org:type:id
mapellil 0:e477c0f8b2e4 186 char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
mapellil 0:e477c0f8b2e4 187 sprintf(clientId, "d:%s:%s:%s", org, type, id);
mapellil 4:df4138621205 188 sprintf(subscription_url, "%s.%s/#/device/%s/%s/", org, "internetofthings.ibmcloud.com", id, TYPE);
mapellil 0:e477c0f8b2e4 189
mapellil 0:e477c0f8b2e4 190 // Network debug statements
mapellil 0:e477c0f8b2e4 191 LOG("=====================================\n\r");
mapellil 0:e477c0f8b2e4 192 LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address());
mapellil 0:e477c0f8b2e4 193 LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address());
mapellil 0:e477c0f8b2e4 194 LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
mapellil 0:e477c0f8b2e4 195 // for(int i = 0; clientId[i]; i++){ // set lowercase mac
mapellil 0:e477c0f8b2e4 196 // clientId[i] = tolower(clientId[i]);
mapellil 0:e477c0f8b2e4 197 // }
mapellil 0:e477c0f8b2e4 198 LOG("Client ID: %s\n\r", clientId);
mapellil 0:e477c0f8b2e4 199 LOG("Topic: %s\n\r",MQTT_TOPIC);
mapellil 0:e477c0f8b2e4 200 LOG("Subscription URL: %s\n\r", subscription_url);
mapellil 0:e477c0f8b2e4 201 LOG("=====================================\n\r");
mapellil 0:e477c0f8b2e4 202 netConnecting = true;
mapellil 0:e477c0f8b2e4 203 int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT);
mapellil 0:e477c0f8b2e4 204 if (rc != 0)
mapellil 0:e477c0f8b2e4 205 {
mapellil 0:e477c0f8b2e4 206 printf("rc from TCP connect is %d\r\n", rc);
mapellil 0:e477c0f8b2e4 207 return rc;
mapellil 0:e477c0f8b2e4 208 }
mapellil 0:e477c0f8b2e4 209
mapellil 0:e477c0f8b2e4 210 printf ("--->TCP Connected\n\r");
mapellil 0:e477c0f8b2e4 211 netConnected = true;
mapellil 0:e477c0f8b2e4 212 netConnecting = false;
mapellil 0:e477c0f8b2e4 213
mapellil 0:e477c0f8b2e4 214 // MQTT Connect
mapellil 0:e477c0f8b2e4 215 mqttConnecting = true;
mapellil 0:e477c0f8b2e4 216 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
mapellil 0:e477c0f8b2e4 217 data.MQTTVersion = 4;
mapellil 0:e477c0f8b2e4 218 data.struct_version=0;
mapellil 0:e477c0f8b2e4 219 data.clientID.cstring = clientId;
mapellil 0:e477c0f8b2e4 220 data.keepAliveInterval = MQTT_KEEPALIVE; // in Sec
mapellil 0:e477c0f8b2e4 221 if (!quickstartMode)
mapellil 0:e477c0f8b2e4 222 {
mapellil 0:e477c0f8b2e4 223 data.username.cstring = "use-token-auth";
mapellil 0:e477c0f8b2e4 224 data.password.cstring = auth_token;
mapellil 2:e3846f091b6b 225 printf ("AutToken: %s\n\r", auth_token);
mapellil 0:e477c0f8b2e4 226 }
mapellil 0:e477c0f8b2e4 227 if ((rc = client->connect(data)) != 0) {
mapellil 0:e477c0f8b2e4 228 printf("rc from MQTT connect is %d\r\n", rc);
mapellil 0:e477c0f8b2e4 229 connack_rc = rc;
mapellil 0:e477c0f8b2e4 230 return rc;
mapellil 0:e477c0f8b2e4 231 }
mapellil 0:e477c0f8b2e4 232 connected = true;
mapellil 0:e477c0f8b2e4 233 printf ("--->MQTT Connected\n\r");
mapellil 0:e477c0f8b2e4 234 #ifdef SUBSCRIBE
mapellil 0:e477c0f8b2e4 235 int rc=0;
mapellil 0:e477c0f8b2e4 236 if ((rc=subscribe(MQTT_TOPIC, client)) == 0) LOG ("--->>>MQTT subscribed to: %s\n\r",MQTT_TOPIC);
mapellil 0:e477c0f8b2e4 237 else LOG ("--->>>ERROR MQTT subscribe : %s\n\r",MQTT_TOPIC);
mapellil 0:e477c0f8b2e4 238 #endif
mapellil 0:e477c0f8b2e4 239 mqttConnecting = false;
mapellil 0:e477c0f8b2e4 240 connack_rc = rc;
mapellil 0:e477c0f8b2e4 241 return rc;
mapellil 0:e477c0f8b2e4 242 }
mapellil 0:e477c0f8b2e4 243
mapellil 0:e477c0f8b2e4 244
mapellil 0:e477c0f8b2e4 245 int getConnTimeout(int attemptNumber)
mapellil 0:e477c0f8b2e4 246 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
mapellil 0:e477c0f8b2e4 247 // after 20 attempts, retry every 10 minutes
mapellil 0:e477c0f8b2e4 248 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
mapellil 0:e477c0f8b2e4 249 }
mapellil 0:e477c0f8b2e4 250
mapellil 0:e477c0f8b2e4 251
mapellil 0:e477c0f8b2e4 252 void attemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network)
mapellil 0:e477c0f8b2e4 253 {
mapellil 0:e477c0f8b2e4 254 connected = false;
mapellil 0:e477c0f8b2e4 255
mapellil 0:e477c0f8b2e4 256 while (connect(client, mqttNetwork, network) != MQTT_CONNECTION_ACCEPTED)
mapellil 0:e477c0f8b2e4 257 {
mapellil 0:e477c0f8b2e4 258 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
mapellil 0:e477c0f8b2e4 259 printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);
mapellil 0:e477c0f8b2e4 260 return; // don't reattempt to connect if credentials are wrong
mapellil 0:e477c0f8b2e4 261 }
mapellil 0:e477c0f8b2e4 262 int timeout = getConnTimeout(++retryAttempt);
mapellil 0:e477c0f8b2e4 263 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
mapellil 0:e477c0f8b2e4 264
mapellil 0:e477c0f8b2e4 265 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
mapellil 0:e477c0f8b2e4 266 // or maybe just add the proper members to do this disconnect and call attemptConnect(...)
mapellil 0:e477c0f8b2e4 267 // this works - reset the system when the retry count gets to a threshold
mapellil 0:e477c0f8b2e4 268 if (retryAttempt == 5)
mapellil 0:e477c0f8b2e4 269 NVIC_SystemReset();
mapellil 0:e477c0f8b2e4 270 else
mapellil 0:e477c0f8b2e4 271 wait(timeout);
mapellil 0:e477c0f8b2e4 272 }
mapellil 0:e477c0f8b2e4 273 }
mapellil 0:e477c0f8b2e4 274
mapellil 0:e477c0f8b2e4 275 int publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client)
mapellil 0:e477c0f8b2e4 276 {
mapellil 0:e477c0f8b2e4 277 MQTT::Message message;
mapellil 0:e477c0f8b2e4 278 const char* pubTopic = MQTT_TOPIC;
mapellil 0:e477c0f8b2e4 279
mapellil 0:e477c0f8b2e4 280 char buf[MQTT_MAX_PAYLOAD_SIZE];
mapellil 0:e477c0f8b2e4 281 float temp, press, hum;
mapellil 0:e477c0f8b2e4 282
mapellil 4:df4138621205 283 if (!client->isConnected()) { printf ("publish failed: MQTT disconnected\n\r"); return MQTT::FAILURE; }
mapellil 0:e477c0f8b2e4 284 temp_sensor1->get_temperature(&temp);
mapellil 0:e477c0f8b2e4 285 pressure_sensor->get_pressure(&press);
mapellil 0:e477c0f8b2e4 286 humidity_sensor->get_humidity(&hum);
mapellil 0:e477c0f8b2e4 287 sprintf(buf,
mapellil 0:e477c0f8b2e4 288 "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%0.4f,\"Pressure\":%0.4f,\"Humidity\":%0.4f}}",
mapellil 0:e477c0f8b2e4 289 temp, press, hum);
mapellil 0:e477c0f8b2e4 290 message.qos = MQTT::QOS0;
mapellil 0:e477c0f8b2e4 291 message.retained = false;
mapellil 0:e477c0f8b2e4 292 message.dup = false;
mapellil 0:e477c0f8b2e4 293 message.payload = (void*)buf;
mapellil 0:e477c0f8b2e4 294 message.payloadlen = strlen(buf);
mapellil 0:e477c0f8b2e4 295
mapellil 0:e477c0f8b2e4 296 if( (message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE )
mapellil 0:e477c0f8b2e4 297 printf("message too long!\r\n");
mapellil 0:e477c0f8b2e4 298
mapellil 0:e477c0f8b2e4 299 LOG("Publishing %s\n\r", buf);
mapellil 0:e477c0f8b2e4 300 return client->publish(pubTopic, message);
mapellil 0:e477c0f8b2e4 301 }
mapellil 0:e477c0f8b2e4 302
mapellil 0:e477c0f8b2e4 303 int main()
mapellil 0:e477c0f8b2e4 304 {
mapellil 0:e477c0f8b2e4 305 myled=0;
mapellil 0:e477c0f8b2e4 306 DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL);
mapellil 0:e477c0f8b2e4 307 i2c->frequency(400000);
mapellil 0:e477c0f8b2e4 308
mapellil 0:e477c0f8b2e4 309 XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(i2c);
mapellil 0:e477c0f8b2e4 310 pressure_sensor = mems_expansion_board->pt_sensor;
mapellil 0:e477c0f8b2e4 311 temp_sensor1 = mems_expansion_board->ht_sensor;
mapellil 0:e477c0f8b2e4 312 humidity_sensor = mems_expansion_board->ht_sensor;
mapellil 0:e477c0f8b2e4 313 pressure_sensor->enable();
mapellil 0:e477c0f8b2e4 314 temp_sensor1->enable();
mapellil 0:e477c0f8b2e4 315 humidity_sensor->enable();
mapellil 0:e477c0f8b2e4 316
mapellil 0:e477c0f8b2e4 317 #if !defined (TARGET_SENSOR_TILE)
mapellil 0:e477c0f8b2e4 318 InterruptIn BlueButton(USER_BUTTON);
mapellil 0:e477c0f8b2e4 319 BlueButton.fall(&BlueButtonPressed);
mapellil 0:e477c0f8b2e4 320 BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 321 #endif
mapellil 0:e477c0f8b2e4 322
mapellil 4:df4138621205 323 pc.printf("\r\nCloud_IBM_MbedOS Application\r\n");
mapellil 0:e477c0f8b2e4 324 pc.printf("\r\nconnecting to AP\r\n");
mapellil 0:e477c0f8b2e4 325
mapellil 0:e477c0f8b2e4 326 quickstartMode=false;
mapellil 0:e477c0f8b2e4 327 if (strcmp(org, "quickstart") == 0){quickstartMode = true;}
mapellil 4:df4138621205 328
mapellil 4:df4138621205 329 #ifdef X_NUCLEO_NFC01A1_PRESENT
mapellil 4:df4138621205 330 // program NFC with broker URL
mapellil 4:df4138621205 331 XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(*i2c, NULL, XNucleoNFC01A1::DEFAULT_GPO_PIN, XNucleoNFC01A1::DEFAULT_RF_DISABLE_PIN, NC,NC,NC);
mapellil 4:df4138621205 332 NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
mapellil 4:df4138621205 333 printf("NFC Init done: !\r\n");
mapellil 4:df4138621205 334 //open the i2c session with the nfc chip
mapellil 4:df4138621205 335 NFCReadRecordWIFI (nfcNucleo);
mapellil 4:df4138621205 336 NetworkInterface* network = easy_connect(true, ssid, seckey); // Wifi SSID and passw from NFC tag
mapellil 4:df4138621205 337 #else
mapellil 4:df4138621205 338 NetworkInterface* network = easy_connect(true); // // Wifi SSID and passw from mbed_app.json
mapellil 4:df4138621205 339 #endif
mapellil 0:e477c0f8b2e4 340 if (!network) {
mapellil 0:e477c0f8b2e4 341 printf ("Error easy_connect\n\r");
mapellil 0:e477c0f8b2e4 342 return -1;
mapellil 4:df4138621205 343 }
mapellil 0:e477c0f8b2e4 344 MQTTNetwork mqttNetwork(network);
mapellil 0:e477c0f8b2e4 345 MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
mapellil 0:e477c0f8b2e4 346
mapellil 0:e477c0f8b2e4 347 if (quickstartMode){
mapellil 0:e477c0f8b2e4 348 char mac[50]; // remove all : from mac
mapellil 0:e477c0f8b2e4 349 char *digit=NULL;
mapellil 0:e477c0f8b2e4 350 sprintf (id,"%s", "");
mapellil 0:e477c0f8b2e4 351 sprintf (mac,"%s",network->get_mac_address());
mapellil 0:e477c0f8b2e4 352 digit = strtok (mac,":");
mapellil 0:e477c0f8b2e4 353 while (digit != NULL)
mapellil 0:e477c0f8b2e4 354 {
mapellil 0:e477c0f8b2e4 355 strcat (id, digit);
mapellil 0:e477c0f8b2e4 356 digit = strtok (NULL, ":");
mapellil 0:e477c0f8b2e4 357 }
mapellil 0:e477c0f8b2e4 358 }
mapellil 0:e477c0f8b2e4 359 printf ("ATTEMPT CONNECT\n\r");
mapellil 0:e477c0f8b2e4 360 attemptConnect(&client, &mqttNetwork, network);
mapellil 0:e477c0f8b2e4 361 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)
mapellil 0:e477c0f8b2e4 362 {
mapellil 0:e477c0f8b2e4 363 printf ("---ERROR line : %d\n\r", __LINE__);
mapellil 0:e477c0f8b2e4 364 while (true)
mapellil 0:e477c0f8b2e4 365 wait(1.0); // Permanent failures - don't retry
mapellil 0:e477c0f8b2e4 366 }
mapellil 4:df4138621205 367
mapellil 4:df4138621205 368 #ifdef X_NUCLEO_NFC01A1_PRESENT
mapellil 4:df4138621205 369 NFCWriteRecordURI (nfcNucleo);
mapellil 4:df4138621205 370 #endif
mapellil 0:e477c0f8b2e4 371 myled=1;
mapellil 0:e477c0f8b2e4 372 int count = 0;
mapellil 0:e477c0f8b2e4 373 while (true)
mapellil 0:e477c0f8b2e4 374 {
mapellil 0:e477c0f8b2e4 375 if (BlueButtonToggle == false && connected == true) {
mapellil 0:e477c0f8b2e4 376 if (++count == 6)
mapellil 0:e477c0f8b2e4 377 { // Publish a message every 3 second
mapellil 0:e477c0f8b2e4 378 if (publish(&client) != MQTT::SUCCESS) {
mapellil 0:e477c0f8b2e4 379 myled=0;
mapellil 0:e477c0f8b2e4 380 count=0;
mapellil 0:e477c0f8b2e4 381 client.disconnect();
mapellil 0:e477c0f8b2e4 382 mqttNetwork.disconnect();
mapellil 0:e477c0f8b2e4 383 attemptConnect(&client, &mqttNetwork, network); // if we have lost the connection
mapellil 0:e477c0f8b2e4 384 } else {
mapellil 0:e477c0f8b2e4 385 myled=1;
mapellil 0:e477c0f8b2e4 386 count=0;
mapellil 0:e477c0f8b2e4 387 }
mapellil 0:e477c0f8b2e4 388 }
mapellil 0:e477c0f8b2e4 389 client.yield(500); // allow the MQTT client to receive subscribe messages and manage keep alive
mapellil 0:e477c0f8b2e4 390 } else if (BlueButtonToggle == true && connected == true){ // disconnect MQTT
mapellil 4:df4138621205 391 printf ("--->> MQTT Disconnect\n\r");
mapellil 4:df4138621205 392 connected = false;
mapellil 0:e477c0f8b2e4 393 count = 0;
mapellil 0:e477c0f8b2e4 394 BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 395 #ifdef SUBSCRIBE
mapellil 0:e477c0f8b2e4 396 // unsubscribe(const char* topicFilter); // unsubscribe if subscribed
mapellil 0:e477c0f8b2e4 397 #endif
mapellil 0:e477c0f8b2e4 398 client.disconnect();
mapellil 4:df4138621205 399 printf ("--->> TCP Disconnect\n\r");
mapellil 0:e477c0f8b2e4 400 mqttNetwork.disconnect();
mapellil 0:e477c0f8b2e4 401 } else if (BlueButtonToggle == true && connected == false) {
mapellil 0:e477c0f8b2e4 402 connected = true;
mapellil 0:e477c0f8b2e4 403 BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 404 } else wait (0.5);
mapellil 0:e477c0f8b2e4 405 }
mapellil 0:e477c0f8b2e4 406 }
mapellil 0:e477c0f8b2e4 407