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:
Wed Feb 21 10:11:06 2018 +0100
Revision:
7:d18775ea6734
Parent:
5:efa13fc5d99a
masked wifi passw

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