![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Duy_new_test_Water_Monitor
Fork of Cloud_IBM_MbedOS by
main.cpp@3:e1f6c5af437e, 2017-12-05 (annotated)
- Committer:
- DuyLionTran
- Date:
- Tue Dec 05 17:12:57 2017 +0000
- Revision:
- 3:e1f6c5af437e
- Parent:
- 2:e3846f091b6b
IBM_Mbed_OS_Duy_Clone
Who changed what in which revision?
User | Revision | Line number | New 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" |
DuyLionTran | 3:e1f6c5af437e | 20 | //#include "XNucleoIKS01A2.h" |
DuyLionTran | 3:e1f6c5af437e | 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); |
DuyLionTran | 3:e1f6c5af437e | 34 | static bool quickstartMode = false; // set to false to connect with authentication tocken |
mapellil | 0:e477c0f8b2e4 | 35 | static bool BlueButtonToggle = false; |
mapellil | 0:e477c0f8b2e4 | 36 | |
DuyLionTran | 3:e1f6c5af437e | 37 | //#define ORG_QUICKSTART // comment to connect to play.internetofthings.ibmcloud.com |
DuyLionTran | 3:e1f6c5af437e | 38 | #define SUBSCRIBE // uncomment to subscribe to broker msgs (not to be used with IBM broker) |
DuyLionTran | 3:e1f6c5af437e | 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 |
DuyLionTran | 3:e1f6c5af437e | 46 | #define ORG "a4nvkh" // 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]; |
DuyLionTran | 3:e1f6c5af437e | 72 | char topicCMD[] = "iot-2/cmd/command/fmt/json"; |
mapellil | 0:e477c0f8b2e4 | 73 | |
DuyLionTran | 3:e1f6c5af437e | 74 | //static LPS22HBSensor *pressure_sensor; |
DuyLionTran | 3:e1f6c5af437e | 75 | //static HTS221Sensor *humidity_sensor; |
DuyLionTran | 3:e1f6c5af437e | 76 | //static HTS221Sensor *temp_sensor1; |
mapellil | 0:e477c0f8b2e4 | 77 | |
DuyLionTran | 3:e1f6c5af437e | 78 | //#ifndef TARGET_SENSOR_TILE |
mapellil | 0:e477c0f8b2e4 | 79 | static void BlueButtonPressed () |
mapellil | 0:e477c0f8b2e4 | 80 | { |
mapellil | 0:e477c0f8b2e4 | 81 | BlueButtonToggle = true; |
mapellil | 0:e477c0f8b2e4 | 82 | } |
DuyLionTran | 3:e1f6c5af437e | 83 | //#endif |
mapellil | 0:e477c0f8b2e4 | 84 | |
mapellil | 0:e477c0f8b2e4 | 85 | #ifdef SUBSCRIBE |
mapellil | 0:e477c0f8b2e4 | 86 | void subscribe_cb(MQTT::MessageData & msgMQTT) { |
mapellil | 0:e477c0f8b2e4 | 87 | char msg[MQTT_MAX_PAYLOAD_SIZE]; |
mapellil | 0:e477c0f8b2e4 | 88 | msg[0]='\0'; |
mapellil | 0:e477c0f8b2e4 | 89 | strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen); |
mapellil | 0:e477c0f8b2e4 | 90 | printf ("--->>> subscribe_cb msg: %s\n\r", msg); |
mapellil | 0:e477c0f8b2e4 | 91 | } |
mapellil | 0:e477c0f8b2e4 | 92 | |
mapellil | 0:e477c0f8b2e4 | 93 | int subscribe(char *pubTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client) |
mapellil | 0:e477c0f8b2e4 | 94 | { |
mapellil | 0:e477c0f8b2e4 | 95 | return client->subscribe(pubTopic, MQTT::QOS1, subscribe_cb); |
mapellil | 0:e477c0f8b2e4 | 96 | } |
mapellil | 0:e477c0f8b2e4 | 97 | #endif |
mapellil | 0:e477c0f8b2e4 | 98 | |
mapellil | 0:e477c0f8b2e4 | 99 | int connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) |
mapellil | 0:e477c0f8b2e4 | 100 | { |
mapellil | 0:e477c0f8b2e4 | 101 | const char* iot_ibm = MQTT_BROKER_URL; |
mapellil | 0:e477c0f8b2e4 | 102 | char hostname[strlen(org) + strlen(iot_ibm) + 1]; |
mapellil | 0:e477c0f8b2e4 | 103 | |
mapellil | 0:e477c0f8b2e4 | 104 | sprintf(hostname, "%s%s", org, iot_ibm); |
mapellil | 0:e477c0f8b2e4 | 105 | // Construct clientId - d:org:type:id |
mapellil | 0:e477c0f8b2e4 | 106 | char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; |
mapellil | 0:e477c0f8b2e4 | 107 | sprintf(clientId, "d:%s:%s:%s", org, type, id); |
mapellil | 2:e3846f091b6b | 108 | sprintf(subscription_url, "%s.%s/#/device/%s/%s/", org, "internetofthings.ibmcloud.com", id, DEFAULT_TYPE_NAME); |
mapellil | 0:e477c0f8b2e4 | 109 | |
mapellil | 0:e477c0f8b2e4 | 110 | // Network debug statements |
mapellil | 0:e477c0f8b2e4 | 111 | LOG("=====================================\n\r"); |
mapellil | 0:e477c0f8b2e4 | 112 | LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address()); |
mapellil | 0:e477c0f8b2e4 | 113 | LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address()); |
mapellil | 0:e477c0f8b2e4 | 114 | LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT); |
mapellil | 0:e477c0f8b2e4 | 115 | // for(int i = 0; clientId[i]; i++){ // set lowercase mac |
mapellil | 0:e477c0f8b2e4 | 116 | // clientId[i] = tolower(clientId[i]); |
mapellil | 0:e477c0f8b2e4 | 117 | // } |
mapellil | 0:e477c0f8b2e4 | 118 | LOG("Client ID: %s\n\r", clientId); |
mapellil | 0:e477c0f8b2e4 | 119 | LOG("Topic: %s\n\r",MQTT_TOPIC); |
mapellil | 0:e477c0f8b2e4 | 120 | LOG("Subscription URL: %s\n\r", subscription_url); |
mapellil | 0:e477c0f8b2e4 | 121 | LOG("=====================================\n\r"); |
mapellil | 0:e477c0f8b2e4 | 122 | netConnecting = true; |
mapellil | 0:e477c0f8b2e4 | 123 | int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT); |
mapellil | 0:e477c0f8b2e4 | 124 | if (rc != 0) |
mapellil | 0:e477c0f8b2e4 | 125 | { |
mapellil | 0:e477c0f8b2e4 | 126 | printf("rc from TCP connect is %d\r\n", rc); |
mapellil | 0:e477c0f8b2e4 | 127 | return rc; |
mapellil | 0:e477c0f8b2e4 | 128 | } |
mapellil | 0:e477c0f8b2e4 | 129 | |
mapellil | 0:e477c0f8b2e4 | 130 | printf ("--->TCP Connected\n\r"); |
mapellil | 0:e477c0f8b2e4 | 131 | netConnected = true; |
mapellil | 0:e477c0f8b2e4 | 132 | netConnecting = false; |
mapellil | 0:e477c0f8b2e4 | 133 | |
mapellil | 0:e477c0f8b2e4 | 134 | // MQTT Connect |
mapellil | 0:e477c0f8b2e4 | 135 | mqttConnecting = true; |
mapellil | 0:e477c0f8b2e4 | 136 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
mapellil | 0:e477c0f8b2e4 | 137 | data.MQTTVersion = 4; |
mapellil | 0:e477c0f8b2e4 | 138 | data.struct_version=0; |
mapellil | 0:e477c0f8b2e4 | 139 | data.clientID.cstring = clientId; |
mapellil | 0:e477c0f8b2e4 | 140 | data.keepAliveInterval = MQTT_KEEPALIVE; // in Sec |
mapellil | 0:e477c0f8b2e4 | 141 | if (!quickstartMode) |
mapellil | 0:e477c0f8b2e4 | 142 | { |
mapellil | 0:e477c0f8b2e4 | 143 | data.username.cstring = "use-token-auth"; |
mapellil | 0:e477c0f8b2e4 | 144 | data.password.cstring = auth_token; |
mapellil | 2:e3846f091b6b | 145 | printf ("AutToken: %s\n\r", auth_token); |
mapellil | 0:e477c0f8b2e4 | 146 | } |
mapellil | 0:e477c0f8b2e4 | 147 | if ((rc = client->connect(data)) != 0) { |
mapellil | 0:e477c0f8b2e4 | 148 | printf("rc from MQTT connect is %d\r\n", rc); |
mapellil | 0:e477c0f8b2e4 | 149 | connack_rc = rc; |
mapellil | 0:e477c0f8b2e4 | 150 | return rc; |
mapellil | 0:e477c0f8b2e4 | 151 | } |
mapellil | 0:e477c0f8b2e4 | 152 | connected = true; |
mapellil | 0:e477c0f8b2e4 | 153 | printf ("--->MQTT Connected\n\r"); |
mapellil | 0:e477c0f8b2e4 | 154 | #ifdef SUBSCRIBE |
DuyLionTran | 3:e1f6c5af437e | 155 | // int rc=0; |
DuyLionTran | 3:e1f6c5af437e | 156 | if ((rc=subscribe(topicCMD, client)) == 0) { |
DuyLionTran | 3:e1f6c5af437e | 157 | LOG ("--->>>MQTT subscribed to: %s\n\r",topicCMD); |
DuyLionTran | 3:e1f6c5af437e | 158 | } |
DuyLionTran | 3:e1f6c5af437e | 159 | else { |
DuyLionTran | 3:e1f6c5af437e | 160 | LOG ("--->>>ERROR MQTT subscribe : %s\n\r",topicCMD); |
DuyLionTran | 3:e1f6c5af437e | 161 | } |
mapellil | 0:e477c0f8b2e4 | 162 | #endif |
mapellil | 0:e477c0f8b2e4 | 163 | mqttConnecting = false; |
mapellil | 0:e477c0f8b2e4 | 164 | connack_rc = rc; |
mapellil | 0:e477c0f8b2e4 | 165 | return rc; |
mapellil | 0:e477c0f8b2e4 | 166 | } |
mapellil | 0:e477c0f8b2e4 | 167 | |
mapellil | 0:e477c0f8b2e4 | 168 | |
mapellil | 0:e477c0f8b2e4 | 169 | int getConnTimeout(int attemptNumber) |
mapellil | 0:e477c0f8b2e4 | 170 | { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute |
mapellil | 0:e477c0f8b2e4 | 171 | // after 20 attempts, retry every 10 minutes |
mapellil | 0:e477c0f8b2e4 | 172 | return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; |
mapellil | 0:e477c0f8b2e4 | 173 | } |
mapellil | 0:e477c0f8b2e4 | 174 | |
mapellil | 0:e477c0f8b2e4 | 175 | |
mapellil | 0:e477c0f8b2e4 | 176 | void attemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) |
mapellil | 0:e477c0f8b2e4 | 177 | { |
mapellil | 0:e477c0f8b2e4 | 178 | connected = false; |
mapellil | 0:e477c0f8b2e4 | 179 | |
mapellil | 0:e477c0f8b2e4 | 180 | while (connect(client, mqttNetwork, network) != MQTT_CONNECTION_ACCEPTED) |
mapellil | 0:e477c0f8b2e4 | 181 | { |
mapellil | 0:e477c0f8b2e4 | 182 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) { |
mapellil | 0:e477c0f8b2e4 | 183 | printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc); |
mapellil | 0:e477c0f8b2e4 | 184 | return; // don't reattempt to connect if credentials are wrong |
mapellil | 0:e477c0f8b2e4 | 185 | } |
mapellil | 0:e477c0f8b2e4 | 186 | int timeout = getConnTimeout(++retryAttempt); |
mapellil | 0:e477c0f8b2e4 | 187 | WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); |
mapellil | 0:e477c0f8b2e4 | 188 | |
mapellil | 0:e477c0f8b2e4 | 189 | // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed |
mapellil | 0:e477c0f8b2e4 | 190 | // or maybe just add the proper members to do this disconnect and call attemptConnect(...) |
mapellil | 0:e477c0f8b2e4 | 191 | // this works - reset the system when the retry count gets to a threshold |
mapellil | 0:e477c0f8b2e4 | 192 | if (retryAttempt == 5) |
mapellil | 0:e477c0f8b2e4 | 193 | NVIC_SystemReset(); |
mapellil | 0:e477c0f8b2e4 | 194 | else |
mapellil | 0:e477c0f8b2e4 | 195 | wait(timeout); |
mapellil | 0:e477c0f8b2e4 | 196 | } |
mapellil | 0:e477c0f8b2e4 | 197 | } |
mapellil | 0:e477c0f8b2e4 | 198 | |
mapellil | 0:e477c0f8b2e4 | 199 | int publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client) |
mapellil | 0:e477c0f8b2e4 | 200 | { |
mapellil | 0:e477c0f8b2e4 | 201 | MQTT::Message message; |
mapellil | 0:e477c0f8b2e4 | 202 | const char* pubTopic = MQTT_TOPIC; |
mapellil | 0:e477c0f8b2e4 | 203 | |
mapellil | 0:e477c0f8b2e4 | 204 | char buf[MQTT_MAX_PAYLOAD_SIZE]; |
mapellil | 0:e477c0f8b2e4 | 205 | float temp, press, hum; |
mapellil | 0:e477c0f8b2e4 | 206 | |
mapellil | 0:e477c0f8b2e4 | 207 | if (!client->isConnected()) { printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE; } |
DuyLionTran | 3:e1f6c5af437e | 208 | // temp_sensor1->get_temperature(&temp); |
DuyLionTran | 3:e1f6c5af437e | 209 | // pressure_sensor->get_pressure(&press); |
DuyLionTran | 3:e1f6c5af437e | 210 | // humidity_sensor->get_humidity(&hum); |
DuyLionTran | 3:e1f6c5af437e | 211 | temp = 17.7; |
DuyLionTran | 3:e1f6c5af437e | 212 | press = 70.7; |
DuyLionTran | 3:e1f6c5af437e | 213 | hum = 80.9; |
mapellil | 0:e477c0f8b2e4 | 214 | sprintf(buf, |
mapellil | 0:e477c0f8b2e4 | 215 | "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%0.4f,\"Pressure\":%0.4f,\"Humidity\":%0.4f}}", |
mapellil | 0:e477c0f8b2e4 | 216 | temp, press, hum); |
mapellil | 0:e477c0f8b2e4 | 217 | message.qos = MQTT::QOS0; |
mapellil | 0:e477c0f8b2e4 | 218 | message.retained = false; |
mapellil | 0:e477c0f8b2e4 | 219 | message.dup = false; |
mapellil | 0:e477c0f8b2e4 | 220 | message.payload = (void*)buf; |
mapellil | 0:e477c0f8b2e4 | 221 | message.payloadlen = strlen(buf); |
mapellil | 0:e477c0f8b2e4 | 222 | |
mapellil | 0:e477c0f8b2e4 | 223 | if( (message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE ) |
mapellil | 0:e477c0f8b2e4 | 224 | printf("message too long!\r\n"); |
mapellil | 0:e477c0f8b2e4 | 225 | |
mapellil | 0:e477c0f8b2e4 | 226 | LOG("Publishing %s\n\r", buf); |
mapellil | 0:e477c0f8b2e4 | 227 | return client->publish(pubTopic, message); |
mapellil | 0:e477c0f8b2e4 | 228 | } |
mapellil | 0:e477c0f8b2e4 | 229 | |
mapellil | 0:e477c0f8b2e4 | 230 | int main() |
mapellil | 0:e477c0f8b2e4 | 231 | { |
DuyLionTran | 3:e1f6c5af437e | 232 | // myled=0; |
DuyLionTran | 3:e1f6c5af437e | 233 | // DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL); |
DuyLionTran | 3:e1f6c5af437e | 234 | // i2c->frequency(400000); |
DuyLionTran | 3:e1f6c5af437e | 235 | // |
DuyLionTran | 3:e1f6c5af437e | 236 | // XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(i2c); |
DuyLionTran | 3:e1f6c5af437e | 237 | // pressure_sensor = mems_expansion_board->pt_sensor; |
DuyLionTran | 3:e1f6c5af437e | 238 | // temp_sensor1 = mems_expansion_board->ht_sensor; |
DuyLionTran | 3:e1f6c5af437e | 239 | // humidity_sensor = mems_expansion_board->ht_sensor; |
DuyLionTran | 3:e1f6c5af437e | 240 | // pressure_sensor->enable(); |
DuyLionTran | 3:e1f6c5af437e | 241 | // temp_sensor1->enable(); |
DuyLionTran | 3:e1f6c5af437e | 242 | // humidity_sensor->enable(); |
mapellil | 0:e477c0f8b2e4 | 243 | |
DuyLionTran | 3:e1f6c5af437e | 244 | //#if !defined (TARGET_SENSOR_TILE) |
DuyLionTran | 3:e1f6c5af437e | 245 | // InterruptIn BlueButton(USER_BUTTON); |
DuyLionTran | 3:e1f6c5af437e | 246 | // BlueButton.fall(&BlueButtonPressed); |
DuyLionTran | 3:e1f6c5af437e | 247 | BlueButtonToggle = false; |
DuyLionTran | 3:e1f6c5af437e | 248 | //#endif |
DuyLionTran | 3:e1f6c5af437e | 249 | pc.baud(115200); |
mapellil | 0:e477c0f8b2e4 | 250 | pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n"); |
mapellil | 0:e477c0f8b2e4 | 251 | pc.printf("\r\nconnecting to AP\r\n"); |
mapellil | 0:e477c0f8b2e4 | 252 | |
mapellil | 0:e477c0f8b2e4 | 253 | quickstartMode=false; |
mapellil | 0:e477c0f8b2e4 | 254 | if (strcmp(org, "quickstart") == 0){quickstartMode = true;} |
mapellil | 0:e477c0f8b2e4 | 255 | NetworkInterface* network = easy_connect(true); |
mapellil | 0:e477c0f8b2e4 | 256 | if (!network) { |
mapellil | 0:e477c0f8b2e4 | 257 | printf ("Error easy_connect\n\r"); |
mapellil | 0:e477c0f8b2e4 | 258 | return -1; |
mapellil | 0:e477c0f8b2e4 | 259 | } |
mapellil | 0:e477c0f8b2e4 | 260 | MQTTNetwork mqttNetwork(network); |
mapellil | 0:e477c0f8b2e4 | 261 | MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork); |
mapellil | 0:e477c0f8b2e4 | 262 | |
mapellil | 0:e477c0f8b2e4 | 263 | if (quickstartMode){ |
mapellil | 0:e477c0f8b2e4 | 264 | char mac[50]; // remove all : from mac |
mapellil | 0:e477c0f8b2e4 | 265 | char *digit=NULL; |
mapellil | 0:e477c0f8b2e4 | 266 | sprintf (id,"%s", ""); |
mapellil | 0:e477c0f8b2e4 | 267 | sprintf (mac,"%s",network->get_mac_address()); |
mapellil | 0:e477c0f8b2e4 | 268 | digit = strtok (mac,":"); |
mapellil | 0:e477c0f8b2e4 | 269 | while (digit != NULL) |
mapellil | 0:e477c0f8b2e4 | 270 | { |
mapellil | 0:e477c0f8b2e4 | 271 | strcat (id, digit); |
mapellil | 0:e477c0f8b2e4 | 272 | digit = strtok (NULL, ":"); |
mapellil | 0:e477c0f8b2e4 | 273 | } |
mapellil | 0:e477c0f8b2e4 | 274 | } |
mapellil | 0:e477c0f8b2e4 | 275 | printf ("ATTEMPT CONNECT\n\r"); |
mapellil | 0:e477c0f8b2e4 | 276 | attemptConnect(&client, &mqttNetwork, network); |
mapellil | 0:e477c0f8b2e4 | 277 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
mapellil | 0:e477c0f8b2e4 | 278 | { |
mapellil | 0:e477c0f8b2e4 | 279 | printf ("---ERROR line : %d\n\r", __LINE__); |
mapellil | 0:e477c0f8b2e4 | 280 | while (true) |
mapellil | 0:e477c0f8b2e4 | 281 | wait(1.0); // Permanent failures - don't retry |
mapellil | 0:e477c0f8b2e4 | 282 | } |
mapellil | 0:e477c0f8b2e4 | 283 | #ifdef X_NUCLEO_NFC01A1_PRESENT |
mapellil | 0:e477c0f8b2e4 | 284 | // program NFC with broker URL |
mapellil | 0:e477c0f8b2e4 | 285 | XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(*i2c, NULL, XNucleoNFC01A1::DEFAULT_GPO_PIN, XNucleoNFC01A1::DEFAULT_RF_DISABLE_PIN, NC,NC,NC); |
mapellil | 0:e477c0f8b2e4 | 286 | NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag(); |
mapellil | 0:e477c0f8b2e4 | 287 | printf("NFC Init done: !\r\n"); |
mapellil | 0:e477c0f8b2e4 | 288 | //open the i2c session with the nfc chip |
mapellil | 0:e477c0f8b2e4 | 289 | if(tag.open_session()){ |
mapellil | 0:e477c0f8b2e4 | 290 | //create the NDef message and record |
mapellil | 0:e477c0f8b2e4 | 291 | NDefLib::Message msg; |
mapellil | 0:e477c0f8b2e4 | 292 | NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTPS, subscription_url); |
mapellil | 0:e477c0f8b2e4 | 293 | msg.add_record(&rUri); |
mapellil | 0:e477c0f8b2e4 | 294 | //write the tag |
mapellil | 0:e477c0f8b2e4 | 295 | if(tag.write(msg)){ |
mapellil | 0:e477c0f8b2e4 | 296 | printf("Tag writed \r\n"); |
mapellil | 0:e477c0f8b2e4 | 297 | } |
mapellil | 0:e477c0f8b2e4 | 298 | //close the i2c session |
mapellil | 0:e477c0f8b2e4 | 299 | if(!tag.close_session()){ |
mapellil | 0:e477c0f8b2e4 | 300 | printf("Error Closing the session\r\n"); |
mapellil | 0:e477c0f8b2e4 | 301 | } |
mapellil | 0:e477c0f8b2e4 | 302 | }else printf("Error open Session\r\n"); |
mapellil | 0:e477c0f8b2e4 | 303 | #endif |
mapellil | 0:e477c0f8b2e4 | 304 | myled=1; |
mapellil | 0:e477c0f8b2e4 | 305 | int count = 0; |
mapellil | 0:e477c0f8b2e4 | 306 | while (true) |
mapellil | 0:e477c0f8b2e4 | 307 | { |
DuyLionTran | 3:e1f6c5af437e | 308 | if (BlueButtonToggle == false && connected == true) { |
mapellil | 0:e477c0f8b2e4 | 309 | if (++count == 6) |
mapellil | 0:e477c0f8b2e4 | 310 | { // Publish a message every 3 second |
DuyLionTran | 3:e1f6c5af437e | 311 | if (publish(&client) != MQTT::SUCCESS) { |
DuyLionTran | 3:e1f6c5af437e | 312 | myled=0; |
DuyLionTran | 3:e1f6c5af437e | 313 | count=0; |
DuyLionTran | 3:e1f6c5af437e | 314 | client.disconnect(); |
DuyLionTran | 3:e1f6c5af437e | 315 | mqttNetwork.disconnect(); |
DuyLionTran | 3:e1f6c5af437e | 316 | attemptConnect(&client, &mqttNetwork, network); // if we have lost the connection |
DuyLionTran | 3:e1f6c5af437e | 317 | } else { |
DuyLionTran | 3:e1f6c5af437e | 318 | myled=1; |
DuyLionTran | 3:e1f6c5af437e | 319 | count=0; |
DuyLionTran | 3:e1f6c5af437e | 320 | } |
mapellil | 0:e477c0f8b2e4 | 321 | } |
mapellil | 0:e477c0f8b2e4 | 322 | client.yield(500); // allow the MQTT client to receive subscribe messages and manage keep alive |
DuyLionTran | 3:e1f6c5af437e | 323 | } else if (BlueButtonToggle == true && connected == true){ // disconnect MQTT |
DuyLionTran | 3:e1f6c5af437e | 324 | printf ("--->> Disconnect\n\r"); |
DuyLionTran | 3:e1f6c5af437e | 325 | connected = false; |
DuyLionTran | 3:e1f6c5af437e | 326 | count = 0; |
DuyLionTran | 3:e1f6c5af437e | 327 | BlueButtonToggle = false; |
mapellil | 0:e477c0f8b2e4 | 328 | #ifdef SUBSCRIBE |
mapellil | 0:e477c0f8b2e4 | 329 | // unsubscribe(const char* topicFilter); // unsubscribe if subscribed |
mapellil | 0:e477c0f8b2e4 | 330 | #endif |
mapellil | 0:e477c0f8b2e4 | 331 | client.disconnect(); |
mapellil | 0:e477c0f8b2e4 | 332 | mqttNetwork.disconnect(); |
mapellil | 0:e477c0f8b2e4 | 333 | } else if (BlueButtonToggle == true && connected == false) { |
mapellil | 0:e477c0f8b2e4 | 334 | connected = true; |
mapellil | 0:e477c0f8b2e4 | 335 | BlueButtonToggle = false; |
mapellil | 0:e477c0f8b2e4 | 336 | } else wait (0.5); |
mapellil | 0:e477c0f8b2e4 | 337 | } |
mapellil | 0:e477c0f8b2e4 | 338 | } |
mapellil | 0:e477c0f8b2e4 | 339 |