Duy_new_test_Water_Monitor

Dependencies:   MQTT NDefLib

Fork of Cloud_IBM_MbedOS by ST

Committer:
mapellil
Date:
Fri Dec 01 11:05:04 2017 +0100
Revision:
2:e3846f091b6b
Parent:
0:e477c0f8b2e4
Child:
3:e1f6c5af437e
Added registered device (no TLS)

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 2:e3846f091b6b 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 2:e3846f091b6b 51 #define ORG MQTT_ORG_ID // connect to ORG.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
mapellil 2:e3846f091b6b 52 #define ID MQTT_DEVICE_ID // For a registered connection is your device id
mapellil 2:e3846f091b6b 53 #define AUTH_TOKEN MQTT_DEVICE_PASSWORD // For a registered connection is a device auth-token
mapellil 2:e3846f091b6b 54 #define DEFAULT_TYPE_NAME MQTT_DEVICE_TYPE // For a registered connection is device type
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 2:e3846f091b6b 107 sprintf(subscription_url, "%s.%s/#/device/%s/%s/", org, "internetofthings.ibmcloud.com", id, DEFAULT_TYPE_NAME);
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 2:e3846f091b6b 144 printf ("AutToken: %s\n\r", auth_token);
mapellil 0:e477c0f8b2e4 145 }
mapellil 0:e477c0f8b2e4 146 if ((rc = client->connect(data)) != 0) {
mapellil 0:e477c0f8b2e4 147 printf("rc from MQTT connect is %d\r\n", rc);
mapellil 0:e477c0f8b2e4 148 connack_rc = rc;
mapellil 0:e477c0f8b2e4 149 return rc;
mapellil 0:e477c0f8b2e4 150 }
mapellil 0:e477c0f8b2e4 151 connected = true;
mapellil 0:e477c0f8b2e4 152 printf ("--->MQTT Connected\n\r");
mapellil 0:e477c0f8b2e4 153 #ifdef SUBSCRIBE
mapellil 0:e477c0f8b2e4 154 int rc=0;
mapellil 0:e477c0f8b2e4 155 if ((rc=subscribe(MQTT_TOPIC, client)) == 0) LOG ("--->>>MQTT subscribed to: %s\n\r",MQTT_TOPIC);
mapellil 0:e477c0f8b2e4 156 else LOG ("--->>>ERROR MQTT subscribe : %s\n\r",MQTT_TOPIC);
mapellil 0:e477c0f8b2e4 157 #endif
mapellil 0:e477c0f8b2e4 158 mqttConnecting = false;
mapellil 0:e477c0f8b2e4 159 connack_rc = rc;
mapellil 0:e477c0f8b2e4 160 return rc;
mapellil 0:e477c0f8b2e4 161 }
mapellil 0:e477c0f8b2e4 162
mapellil 0:e477c0f8b2e4 163
mapellil 0:e477c0f8b2e4 164 int getConnTimeout(int attemptNumber)
mapellil 0:e477c0f8b2e4 165 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
mapellil 0:e477c0f8b2e4 166 // after 20 attempts, retry every 10 minutes
mapellil 0:e477c0f8b2e4 167 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
mapellil 0:e477c0f8b2e4 168 }
mapellil 0:e477c0f8b2e4 169
mapellil 0:e477c0f8b2e4 170
mapellil 0:e477c0f8b2e4 171 void attemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network)
mapellil 0:e477c0f8b2e4 172 {
mapellil 0:e477c0f8b2e4 173 connected = false;
mapellil 0:e477c0f8b2e4 174
mapellil 0:e477c0f8b2e4 175 while (connect(client, mqttNetwork, network) != MQTT_CONNECTION_ACCEPTED)
mapellil 0:e477c0f8b2e4 176 {
mapellil 0:e477c0f8b2e4 177 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
mapellil 0:e477c0f8b2e4 178 printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);
mapellil 0:e477c0f8b2e4 179 return; // don't reattempt to connect if credentials are wrong
mapellil 0:e477c0f8b2e4 180 }
mapellil 0:e477c0f8b2e4 181 int timeout = getConnTimeout(++retryAttempt);
mapellil 0:e477c0f8b2e4 182 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
mapellil 0:e477c0f8b2e4 183
mapellil 0:e477c0f8b2e4 184 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
mapellil 0:e477c0f8b2e4 185 // or maybe just add the proper members to do this disconnect and call attemptConnect(...)
mapellil 0:e477c0f8b2e4 186 // this works - reset the system when the retry count gets to a threshold
mapellil 0:e477c0f8b2e4 187 if (retryAttempt == 5)
mapellil 0:e477c0f8b2e4 188 NVIC_SystemReset();
mapellil 0:e477c0f8b2e4 189 else
mapellil 0:e477c0f8b2e4 190 wait(timeout);
mapellil 0:e477c0f8b2e4 191 }
mapellil 0:e477c0f8b2e4 192 }
mapellil 0:e477c0f8b2e4 193
mapellil 0:e477c0f8b2e4 194 int publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client)
mapellil 0:e477c0f8b2e4 195 {
mapellil 0:e477c0f8b2e4 196 MQTT::Message message;
mapellil 0:e477c0f8b2e4 197 const char* pubTopic = MQTT_TOPIC;
mapellil 0:e477c0f8b2e4 198
mapellil 0:e477c0f8b2e4 199 char buf[MQTT_MAX_PAYLOAD_SIZE];
mapellil 0:e477c0f8b2e4 200 float temp, press, hum;
mapellil 0:e477c0f8b2e4 201
mapellil 0:e477c0f8b2e4 202 if (!client->isConnected()) { printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE; }
mapellil 0:e477c0f8b2e4 203 temp_sensor1->get_temperature(&temp);
mapellil 0:e477c0f8b2e4 204 pressure_sensor->get_pressure(&press);
mapellil 0:e477c0f8b2e4 205 humidity_sensor->get_humidity(&hum);
mapellil 0:e477c0f8b2e4 206 sprintf(buf,
mapellil 0:e477c0f8b2e4 207 "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%0.4f,\"Pressure\":%0.4f,\"Humidity\":%0.4f}}",
mapellil 0:e477c0f8b2e4 208 temp, press, hum);
mapellil 0:e477c0f8b2e4 209 message.qos = MQTT::QOS0;
mapellil 0:e477c0f8b2e4 210 message.retained = false;
mapellil 0:e477c0f8b2e4 211 message.dup = false;
mapellil 0:e477c0f8b2e4 212 message.payload = (void*)buf;
mapellil 0:e477c0f8b2e4 213 message.payloadlen = strlen(buf);
mapellil 0:e477c0f8b2e4 214
mapellil 0:e477c0f8b2e4 215 if( (message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE )
mapellil 0:e477c0f8b2e4 216 printf("message too long!\r\n");
mapellil 0:e477c0f8b2e4 217
mapellil 0:e477c0f8b2e4 218 LOG("Publishing %s\n\r", buf);
mapellil 0:e477c0f8b2e4 219 return client->publish(pubTopic, message);
mapellil 0:e477c0f8b2e4 220 }
mapellil 0:e477c0f8b2e4 221
mapellil 0:e477c0f8b2e4 222 int main()
mapellil 0:e477c0f8b2e4 223 {
mapellil 0:e477c0f8b2e4 224 myled=0;
mapellil 0:e477c0f8b2e4 225 DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL);
mapellil 0:e477c0f8b2e4 226 i2c->frequency(400000);
mapellil 0:e477c0f8b2e4 227
mapellil 0:e477c0f8b2e4 228 XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(i2c);
mapellil 0:e477c0f8b2e4 229 pressure_sensor = mems_expansion_board->pt_sensor;
mapellil 0:e477c0f8b2e4 230 temp_sensor1 = mems_expansion_board->ht_sensor;
mapellil 0:e477c0f8b2e4 231 humidity_sensor = mems_expansion_board->ht_sensor;
mapellil 0:e477c0f8b2e4 232 pressure_sensor->enable();
mapellil 0:e477c0f8b2e4 233 temp_sensor1->enable();
mapellil 0:e477c0f8b2e4 234 humidity_sensor->enable();
mapellil 0:e477c0f8b2e4 235
mapellil 0:e477c0f8b2e4 236 #if !defined (TARGET_SENSOR_TILE)
mapellil 0:e477c0f8b2e4 237 InterruptIn BlueButton(USER_BUTTON);
mapellil 0:e477c0f8b2e4 238 BlueButton.fall(&BlueButtonPressed);
mapellil 0:e477c0f8b2e4 239 BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 240 #endif
mapellil 0:e477c0f8b2e4 241
mapellil 0:e477c0f8b2e4 242 pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");
mapellil 0:e477c0f8b2e4 243 pc.printf("\r\nconnecting to AP\r\n");
mapellil 0:e477c0f8b2e4 244
mapellil 0:e477c0f8b2e4 245 quickstartMode=false;
mapellil 0:e477c0f8b2e4 246 if (strcmp(org, "quickstart") == 0){quickstartMode = true;}
mapellil 0:e477c0f8b2e4 247 NetworkInterface* network = easy_connect(true);
mapellil 0:e477c0f8b2e4 248 if (!network) {
mapellil 0:e477c0f8b2e4 249 printf ("Error easy_connect\n\r");
mapellil 0:e477c0f8b2e4 250 return -1;
mapellil 0:e477c0f8b2e4 251 }
mapellil 0:e477c0f8b2e4 252 MQTTNetwork mqttNetwork(network);
mapellil 0:e477c0f8b2e4 253 MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
mapellil 0:e477c0f8b2e4 254
mapellil 0:e477c0f8b2e4 255 if (quickstartMode){
mapellil 0:e477c0f8b2e4 256 char mac[50]; // remove all : from mac
mapellil 0:e477c0f8b2e4 257 char *digit=NULL;
mapellil 0:e477c0f8b2e4 258 sprintf (id,"%s", "");
mapellil 0:e477c0f8b2e4 259 sprintf (mac,"%s",network->get_mac_address());
mapellil 0:e477c0f8b2e4 260 digit = strtok (mac,":");
mapellil 0:e477c0f8b2e4 261 while (digit != NULL)
mapellil 0:e477c0f8b2e4 262 {
mapellil 0:e477c0f8b2e4 263 strcat (id, digit);
mapellil 0:e477c0f8b2e4 264 digit = strtok (NULL, ":");
mapellil 0:e477c0f8b2e4 265 }
mapellil 0:e477c0f8b2e4 266 }
mapellil 0:e477c0f8b2e4 267 printf ("ATTEMPT CONNECT\n\r");
mapellil 0:e477c0f8b2e4 268 attemptConnect(&client, &mqttNetwork, network);
mapellil 0:e477c0f8b2e4 269 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)
mapellil 0:e477c0f8b2e4 270 {
mapellil 0:e477c0f8b2e4 271 printf ("---ERROR line : %d\n\r", __LINE__);
mapellil 0:e477c0f8b2e4 272 while (true)
mapellil 0:e477c0f8b2e4 273 wait(1.0); // Permanent failures - don't retry
mapellil 0:e477c0f8b2e4 274 }
mapellil 0:e477c0f8b2e4 275 #ifdef X_NUCLEO_NFC01A1_PRESENT
mapellil 0:e477c0f8b2e4 276 // program NFC with broker URL
mapellil 0:e477c0f8b2e4 277 XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(*i2c, NULL, XNucleoNFC01A1::DEFAULT_GPO_PIN, XNucleoNFC01A1::DEFAULT_RF_DISABLE_PIN, NC,NC,NC);
mapellil 0:e477c0f8b2e4 278 NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
mapellil 0:e477c0f8b2e4 279 printf("NFC Init done: !\r\n");
mapellil 0:e477c0f8b2e4 280 //open the i2c session with the nfc chip
mapellil 0:e477c0f8b2e4 281 if(tag.open_session()){
mapellil 0:e477c0f8b2e4 282 //create the NDef message and record
mapellil 0:e477c0f8b2e4 283 NDefLib::Message msg;
mapellil 0:e477c0f8b2e4 284 NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTPS, subscription_url);
mapellil 0:e477c0f8b2e4 285 msg.add_record(&rUri);
mapellil 0:e477c0f8b2e4 286 //write the tag
mapellil 0:e477c0f8b2e4 287 if(tag.write(msg)){
mapellil 0:e477c0f8b2e4 288 printf("Tag writed \r\n");
mapellil 0:e477c0f8b2e4 289 }
mapellil 0:e477c0f8b2e4 290 //close the i2c session
mapellil 0:e477c0f8b2e4 291 if(!tag.close_session()){
mapellil 0:e477c0f8b2e4 292 printf("Error Closing the session\r\n");
mapellil 0:e477c0f8b2e4 293 }
mapellil 0:e477c0f8b2e4 294 }else printf("Error open Session\r\n");
mapellil 0:e477c0f8b2e4 295 #endif
mapellil 0:e477c0f8b2e4 296 myled=1;
mapellil 0:e477c0f8b2e4 297 int count = 0;
mapellil 0:e477c0f8b2e4 298 while (true)
mapellil 0:e477c0f8b2e4 299 {
mapellil 0:e477c0f8b2e4 300 if (BlueButtonToggle == false && connected == true) {
mapellil 0:e477c0f8b2e4 301 if (++count == 6)
mapellil 0:e477c0f8b2e4 302 { // Publish a message every 3 second
mapellil 0:e477c0f8b2e4 303 if (publish(&client) != MQTT::SUCCESS) {
mapellil 0:e477c0f8b2e4 304 myled=0;
mapellil 0:e477c0f8b2e4 305 count=0;
mapellil 0:e477c0f8b2e4 306 client.disconnect();
mapellil 0:e477c0f8b2e4 307 mqttNetwork.disconnect();
mapellil 0:e477c0f8b2e4 308 attemptConnect(&client, &mqttNetwork, network); // if we have lost the connection
mapellil 0:e477c0f8b2e4 309 } else {
mapellil 0:e477c0f8b2e4 310 myled=1;
mapellil 0:e477c0f8b2e4 311 count=0;
mapellil 0:e477c0f8b2e4 312 }
mapellil 0:e477c0f8b2e4 313 }
mapellil 0:e477c0f8b2e4 314 client.yield(500); // allow the MQTT client to receive subscribe messages and manage keep alive
mapellil 0:e477c0f8b2e4 315 } else if (BlueButtonToggle == true && connected == true){ // disconnect MQTT
mapellil 0:e477c0f8b2e4 316 printf ("--->> Disconnect\n\r");
mapellil 0:e477c0f8b2e4 317 connected = false;
mapellil 0:e477c0f8b2e4 318 count = 0;
mapellil 0:e477c0f8b2e4 319 BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 320 #ifdef SUBSCRIBE
mapellil 0:e477c0f8b2e4 321 // unsubscribe(const char* topicFilter); // unsubscribe if subscribed
mapellil 0:e477c0f8b2e4 322 #endif
mapellil 0:e477c0f8b2e4 323 client.disconnect();
mapellil 0:e477c0f8b2e4 324 mqttNetwork.disconnect();
mapellil 0:e477c0f8b2e4 325 } else if (BlueButtonToggle == true && connected == false) {
mapellil 0:e477c0f8b2e4 326 connected = true;
mapellil 0:e477c0f8b2e4 327 BlueButtonToggle = false;
mapellil 0:e477c0f8b2e4 328 } else wait (0.5);
mapellil 0:e477c0f8b2e4 329 }
mapellil 0:e477c0f8b2e4 330 }
mapellil 0:e477c0f8b2e4 331