INSAT_MiniPRoject
Dependencies: MQTT NDefLib NetworkSocketAPI Servo Light_Sensor_Nucleo X_NUCLEO_IDW01M1v2 mbed
Fork of IDW01M1_Cloud_IBM by
main.cpp@25:9dab18979609, 2018-02-20 (annotated)
- Committer:
- Hamdi
- Date:
- Tue Feb 20 19:24:38 2018 +0000
- Revision:
- 25:9dab18979609
- Parent:
- 24:a59eb8bdc431
INSAT mini Project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 10:c7b62ce013ad | 1 | /* SpwfInterface NetworkSocketAPI Example Program |
mridup | 10:c7b62ce013ad | 2 | * Copyright (c) 2015 ARM Limited |
mridup | 10:c7b62ce013ad | 3 | * |
mridup | 10:c7b62ce013ad | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mridup | 10:c7b62ce013ad | 5 | * you may not use this file except in compliance with the License. |
mridup | 10:c7b62ce013ad | 6 | * You may obtain a copy of the License at |
mridup | 10:c7b62ce013ad | 7 | * |
mridup | 10:c7b62ce013ad | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mridup | 10:c7b62ce013ad | 9 | * |
mridup | 10:c7b62ce013ad | 10 | * Unless required by applicable law or agreed to in writing, software |
mridup | 10:c7b62ce013ad | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mridup | 10:c7b62ce013ad | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mridup | 10:c7b62ce013ad | 13 | * See the License for the specific language governing permissions and |
mridup | 10:c7b62ce013ad | 14 | * limitations under the License. |
mridup | 10:c7b62ce013ad | 15 | */ |
mridup | 10:c7b62ce013ad | 16 | |
mridup | 0:cbf8bc43bc9e | 17 | #include "mbed.h" |
Hamdi | 25:9dab18979609 | 18 | |
Hamdi | 25:9dab18979609 | 19 | |
mapellil | 14:641560b57584 | 20 | #include "SpwfInterface.h" |
Hamdi | 25:9dab18979609 | 21 | #include "XNucleo6180XA1.h" |
mridup | 4:1ed7f173eec5 | 22 | #include "TCPSocket.h" |
fabiombed | 13:0b31131bf711 | 23 | #include "MQTTClient.h" |
mapellil | 14:641560b57584 | 24 | #include "MQTTWiFi.h" |
Hamdi | 25:9dab18979609 | 25 | #include "Servo/Servo.h" |
Hamdi | 25:9dab18979609 | 26 | |
mapellil | 14:641560b57584 | 27 | #include <ctype.h> |
Hamdi | 25:9dab18979609 | 28 | //#include "x_nucleo_iks01a1.h" |
Hamdi | 25:9dab18979609 | 29 | //#include "X_NUCLEO_NFC01A1.h" |
Hamdi | 25:9dab18979609 | 30 | //#include "NDefLib/NDefNfcTag.h" |
mapellil | 23:43f9e34781f2 | 31 | #include "NDefLib/RecordType/RecordURI.h" |
fabiombed | 13:0b31131bf711 | 32 | |
Hamdi | 25:9dab18979609 | 33 | /* Definitions ---------------------------------------------------------------*/ |
Hamdi | 25:9dab18979609 | 34 | |
Hamdi | 25:9dab18979609 | 35 | #define VL6180X_I2C_SDA D14 |
Hamdi | 25:9dab18979609 | 36 | #define VL6180X_I2C_SCL D15 |
Hamdi | 25:9dab18979609 | 37 | |
Hamdi | 25:9dab18979609 | 38 | |
Hamdi | 25:9dab18979609 | 39 | /* Variables -----------------------------------------------------------------*/ |
Hamdi | 25:9dab18979609 | 40 | |
Hamdi | 25:9dab18979609 | 41 | static XNucleo6180XA1 *board = NULL; |
Hamdi | 25:9dab18979609 | 42 | int status; |
Hamdi | 25:9dab18979609 | 43 | |
Hamdi | 25:9dab18979609 | 44 | |
mridup | 0:cbf8bc43bc9e | 45 | //------------------------------------ |
mridup | 0:cbf8bc43bc9e | 46 | // Hyperterminal configuration |
mridup | 0:cbf8bc43bc9e | 47 | // 9600 bauds, 8-bit data, no parity |
mridup | 0:cbf8bc43bc9e | 48 | //------------------------------------ |
Hamdi | 25:9dab18979609 | 49 | //Serial pc(SERIAL_TX, SERIAL_RX); |
mridup | 3:dfb8c6c8c31b | 50 | DigitalOut myled(LED1); |
mapellil | 14:641560b57584 | 51 | bool quickstartMode = true; |
mapellil | 17:83d0cd810ed3 | 52 | |
Hamdi | 25:9dab18979609 | 53 | Servo myservo(D11); |
Hamdi | 25:9dab18979609 | 54 | |
Hamdi | 25:9dab18979609 | 55 | |
Hamdi | 25:9dab18979609 | 56 | |
Hamdi | 25:9dab18979609 | 57 | |
Hamdi | 25:9dab18979609 | 58 | |
Hamdi | 25:9dab18979609 | 59 | //float Moisture = 0 ; |
Hamdi | 25:9dab18979609 | 60 | |
Hamdi | 25:9dab18979609 | 61 | #define ORG_QUICKSTART // comment to connect to play.internetofthings.ibmcloud.co |
mapellil | 23:43f9e34781f2 | 62 | //#define SUBSCRIBE // uncomment to subscribe to broker msgs (not to be used with IBM broker) |
mapellil | 23:43f9e34781f2 | 63 | #define X_NUCLEO_NFC01A1_PRESENT // uncomment to add NFC support |
mapellil | 14:641560b57584 | 64 | |
mapellil | 14:641560b57584 | 65 | #define MQTT_MAX_PACKET_SIZE 250 |
mapellil | 14:641560b57584 | 66 | #define MQTT_MAX_PAYLOAD_SIZE 300 |
mapellil | 17:83d0cd810ed3 | 67 | |
mapellil | 14:641560b57584 | 68 | // Configuration values needed to connect to IBM IoT Cloud |
mapellil | 23:43f9e34781f2 | 69 | #define BROKER_URL ".messaging.internetofthings.ibmcloud.com"; |
mapellil | 17:83d0cd810ed3 | 70 | #ifdef ORG_QUICKSTART |
mapellil | 17:83d0cd810ed3 | 71 | #define ORG "quickstart" // connect to quickstart.internetofthings.ibmcloud.com/ For a registered connection, replace with your org |
mapellil | 17:83d0cd810ed3 | 72 | #define ID "" |
mapellil | 17:83d0cd810ed3 | 73 | #define AUTH_TOKEN "" |
mapellil | 21:78fac4c1b0fa | 74 | #define DEFAULT_TYPE_NAME "iotsample-mbed-Nucleo" |
mapellil | 23:43f9e34781f2 | 75 | #else // not def ORG_QUICKSTART |
Hamdi | 25:9dab18979609 | 76 | #define ORG "" // connect to play.internetofthings.ibmcloud.com/ For a registered connection, replace with your org |
Hamdi | 25:9dab18979609 | 77 | #define ID "" // For a registered connection, replace with your id 0080E1B8D765 |
mapellil | 23:43f9e34781f2 | 78 | #define AUTH_TOKEN ""// For a registered connection, replace with your auth-token |
Hamdi | 25:9dab18979609 | 79 | #define DEFAULT_TYPE_NAME "" |
mapellil | 17:83d0cd810ed3 | 80 | #endif |
mapellil | 23:43f9e34781f2 | 81 | #define TOPIC "iot-2/evt/status/fmt/json" |
mapellil | 17:83d0cd810ed3 | 82 | |
mapellil | 14:641560b57584 | 83 | #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type |
mapellil | 14:641560b57584 | 84 | #define MQTT_PORT 1883 |
mapellil | 14:641560b57584 | 85 | #define MQTT_TLS_PORT 8883 |
mapellil | 14:641560b57584 | 86 | #define IBM_IOT_PORT MQTT_PORT |
mapellil | 16:233b89a6b72f | 87 | // WiFi network credential |
Hamdi | 25:9dab18979609 | 88 | #define SSID "STM" // Network must be visible otherwise it can't connect |
Hamdi | 25:9dab18979609 | 89 | #define PASSW "STMdemoPWD" |
nikapov | 20:517b559ce91d | 90 | #warning "Wifi SSID & password empty" |
mapellil | 14:641560b57584 | 91 | |
mapellil | 14:641560b57584 | 92 | char id[30] = ID; // mac without colons |
mapellil | 14:641560b57584 | 93 | char org[12] = ORG; |
mapellil | 14:641560b57584 | 94 | int connack_rc = 0; // MQTT connack return code |
mapellil | 14:641560b57584 | 95 | const char* ip_addr = ""; |
mapellil | 14:641560b57584 | 96 | char* host_addr = ""; |
mapellil | 14:641560b57584 | 97 | char type[30] = TYPE; |
mapellil | 14:641560b57584 | 98 | char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode |
mapellil | 14:641560b57584 | 99 | bool netConnecting = false; |
mapellil | 23:43f9e34781f2 | 100 | int connectTimeout = 1000; |
mapellil | 14:641560b57584 | 101 | bool mqttConnecting = false; |
mapellil | 14:641560b57584 | 102 | bool netConnected = false; |
mapellil | 14:641560b57584 | 103 | bool connected = false; |
mapellil | 14:641560b57584 | 104 | int retryAttempt = 0; |
mapellil | 23:43f9e34781f2 | 105 | char subscription_url[MQTT_MAX_PAYLOAD_SIZE]; |
mapellil | 14:641560b57584 | 106 | |
Hamdi | 25:9dab18979609 | 107 | |
Hamdi | 25:9dab18979609 | 108 | int32_t axes[3]; |
Hamdi | 25:9dab18979609 | 109 | |
mapellil | 14:641560b57584 | 110 | |
mapellil | 23:43f9e34781f2 | 111 | MQTT::Message message; |
mapellil | 23:43f9e34781f2 | 112 | MQTTString TopicName={TOPIC}; |
mapellil | 23:43f9e34781f2 | 113 | MQTT::MessageData MsgData(TopicName, message); |
mapellil | 23:43f9e34781f2 | 114 | |
mapellil | 23:43f9e34781f2 | 115 | void subscribe_cb(MQTT::MessageData & msgMQTT) { |
mapellil | 23:43f9e34781f2 | 116 | char msg[MQTT_MAX_PAYLOAD_SIZE]; |
mapellil | 23:43f9e34781f2 | 117 | msg[0]='\0'; |
mapellil | 23:43f9e34781f2 | 118 | strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen); |
mapellil | 23:43f9e34781f2 | 119 | printf ("--->>> subscribe_cb msg: %s\n\r", msg); |
mapellil | 23:43f9e34781f2 | 120 | } |
mapellil | 23:43f9e34781f2 | 121 | |
mapellil | 23:43f9e34781f2 | 122 | int subscribe(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) |
mapellil | 23:43f9e34781f2 | 123 | { |
mapellil | 23:43f9e34781f2 | 124 | char* pubTopic = TOPIC; |
mapellil | 23:43f9e34781f2 | 125 | return client->subscribe(pubTopic, MQTT::QOS1, subscribe_cb); |
mapellil | 23:43f9e34781f2 | 126 | } |
mapellil | 14:641560b57584 | 127 | |
mapellil | 14:641560b57584 | 128 | int connect(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) |
mapellil | 23:43f9e34781f2 | 129 | { |
mapellil | 23:43f9e34781f2 | 130 | const char* iot_ibm = BROKER_URL; |
mapellil | 23:43f9e34781f2 | 131 | |
mapellil | 14:641560b57584 | 132 | |
mapellil | 14:641560b57584 | 133 | char hostname[strlen(org) + strlen(iot_ibm) + 1]; |
mapellil | 14:641560b57584 | 134 | sprintf(hostname, "%s%s", org, iot_ibm); |
mapellil | 14:641560b57584 | 135 | SpwfSAInterface& WiFi = ipstack->getWiFi(); |
mapellil | 24:a59eb8bdc431 | 136 | // ip_addr = WiFi.get_ip_address(); |
mapellil | 14:641560b57584 | 137 | // Construct clientId - d:org:type:id |
mapellil | 23:43f9e34781f2 | 138 | char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; |
mapellil | 23:43f9e34781f2 | 139 | sprintf(clientId, "d:%s:%s:%s", org, type, id); |
Hamdi | 25:9dab18979609 | 140 | sprintf(subscription_url, "%s.%s/#/device/%s/light/", org, "internetofthings.ibmcloud.com",id); |
mapellil | 23:43f9e34781f2 | 141 | |
mapellil | 14:641560b57584 | 142 | // Network debug statements |
mapellil | 14:641560b57584 | 143 | LOG("=====================================\n\r"); |
mapellil | 14:641560b57584 | 144 | LOG("Connecting WiFi.\n\r"); |
mapellil | 23:43f9e34781f2 | 145 | LOG("Nucleo IP ADDRESS: %s\n\r", WiFi.get_ip_address()); |
mapellil | 23:43f9e34781f2 | 146 | LOG("Nucleo MAC ADDRESS: %s\n\r", WiFi.get_mac_address()); |
mapellil | 23:43f9e34781f2 | 147 | LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT); |
mapellil | 24:a59eb8bdc431 | 148 | // for(int i = 0; clientId[i]; i++){ // set lowercase mac |
mapellil | 24:a59eb8bdc431 | 149 | // clientId[i] = tolower(clientId[i]); |
mapellil | 17:83d0cd810ed3 | 150 | // } |
mapellil | 24:a59eb8bdc431 | 151 | LOG("Client ID: %s\n\r", clientId); |
mapellil | 23:43f9e34781f2 | 152 | LOG("Topic: %s\n\r",TOPIC); |
mapellil | 23:43f9e34781f2 | 153 | LOG("Subscription URL: %s\n\r", subscription_url); |
mapellil | 14:641560b57584 | 154 | LOG("=====================================\n\r"); |
mapellil | 14:641560b57584 | 155 | |
mapellil | 14:641560b57584 | 156 | netConnecting = true; |
mapellil | 14:641560b57584 | 157 | ipstack->open(&ipstack->getWiFi()); |
mapellil | 23:43f9e34781f2 | 158 | int rc = ipstack->connect(hostname, IBM_IOT_PORT, connectTimeout); |
mapellil | 14:641560b57584 | 159 | if (rc != 0) |
mapellil | 14:641560b57584 | 160 | { |
mapellil | 14:641560b57584 | 161 | WARN("IP Stack connect returned: %d\n", rc); |
mapellil | 14:641560b57584 | 162 | return rc; |
mapellil | 14:641560b57584 | 163 | } |
mapellil | 23:43f9e34781f2 | 164 | printf ("--->TCP Connected\n\r"); |
mapellil | 14:641560b57584 | 165 | netConnected = true; |
mapellil | 14:641560b57584 | 166 | netConnecting = false; |
mapellil | 14:641560b57584 | 167 | |
mapellil | 14:641560b57584 | 168 | // MQTT Connect |
mapellil | 14:641560b57584 | 169 | mqttConnecting = true; |
mapellil | 14:641560b57584 | 170 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
mapellil | 17:83d0cd810ed3 | 171 | data.MQTTVersion = 4; |
mapellil | 17:83d0cd810ed3 | 172 | data.struct_version=0; |
mapellil | 14:641560b57584 | 173 | data.clientID.cstring = clientId; |
mapellil | 14:641560b57584 | 174 | |
mapellil | 14:641560b57584 | 175 | if (!quickstartMode) |
mapellil | 14:641560b57584 | 176 | { |
mapellil | 14:641560b57584 | 177 | data.username.cstring = "use-token-auth"; |
mapellil | 14:641560b57584 | 178 | data.password.cstring = auth_token; |
mapellil | 23:43f9e34781f2 | 179 | } |
mapellil | 14:641560b57584 | 180 | if ((rc = client->connect(data)) == 0) |
mapellil | 14:641560b57584 | 181 | { |
mapellil | 14:641560b57584 | 182 | connected = true; |
mapellil | 23:43f9e34781f2 | 183 | printf ("--->MQTT Connected\n\r"); |
mapellil | 23:43f9e34781f2 | 184 | #ifdef SUBSCRIBE |
mapellil | 23:43f9e34781f2 | 185 | if (!subscribe(client, ipstack)) printf ("--->>>MQTT subscribed to: %s\n\r",TOPIC); |
mapellil | 23:43f9e34781f2 | 186 | #endif |
mapellil | 14:641560b57584 | 187 | } |
mapellil | 14:641560b57584 | 188 | else { |
mapellil | 14:641560b57584 | 189 | WARN("MQTT connect returned %d\n", rc); |
mapellil | 14:641560b57584 | 190 | } |
mapellil | 14:641560b57584 | 191 | if (rc >= 0) |
mapellil | 14:641560b57584 | 192 | connack_rc = rc; |
mapellil | 14:641560b57584 | 193 | mqttConnecting = false; |
mapellil | 14:641560b57584 | 194 | return rc; |
mapellil | 14:641560b57584 | 195 | } |
mapellil | 14:641560b57584 | 196 | |
mapellil | 14:641560b57584 | 197 | int getConnTimeout(int attemptNumber) |
mapellil | 14:641560b57584 | 198 | { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute |
mapellil | 14:641560b57584 | 199 | // after 20 attempts, retry every 10 minutes |
mapellil | 14:641560b57584 | 200 | return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; |
mapellil | 14:641560b57584 | 201 | } |
mapellil | 14:641560b57584 | 202 | |
mapellil | 14:641560b57584 | 203 | void attemptConnect(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) |
mapellil | 14:641560b57584 | 204 | { |
mapellil | 14:641560b57584 | 205 | connected = false; |
mapellil | 14:641560b57584 | 206 | |
mapellil | 14:641560b57584 | 207 | while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) |
mapellil | 14:641560b57584 | 208 | { |
mapellil | 14:641560b57584 | 209 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) { |
mapellil | 23:43f9e34781f2 | 210 | printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc); |
mapellil | 14:641560b57584 | 211 | return; // don't reattempt to connect if credentials are wrong |
mapellil | 23:43f9e34781f2 | 212 | } |
mapellil | 14:641560b57584 | 213 | int timeout = getConnTimeout(++retryAttempt); |
mapellil | 14:641560b57584 | 214 | WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); |
mapellil | 14:641560b57584 | 215 | |
mapellil | 14:641560b57584 | 216 | // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed |
mapellil | 23:43f9e34781f2 | 217 | // or maybe just add the proper members to do this disconnect and call attemptConnect(...) |
mapellil | 14:641560b57584 | 218 | // this works - reset the system when the retry count gets to a threshold |
mapellil | 14:641560b57584 | 219 | if (retryAttempt == 5) |
mapellil | 14:641560b57584 | 220 | NVIC_SystemReset(); |
mapellil | 14:641560b57584 | 221 | else |
mapellil | 14:641560b57584 | 222 | wait(timeout); |
mapellil | 14:641560b57584 | 223 | } |
mapellil | 14:641560b57584 | 224 | } |
mapellil | 14:641560b57584 | 225 | |
Hamdi | 25:9dab18979609 | 226 | |
Hamdi | 25:9dab18979609 | 227 | |
mapellil | 14:641560b57584 | 228 | int publish(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) |
mapellil | 14:641560b57584 | 229 | { |
mapellil | 14:641560b57584 | 230 | MQTT::Message message; |
mapellil | 23:43f9e34781f2 | 231 | char* pubTopic = TOPIC; |
Hamdi | 25:9dab18979609 | 232 | /* Creates the 6180XA1 expansion board singleton obj. */ |
Hamdi | 25:9dab18979609 | 233 | |
Hamdi | 25:9dab18979609 | 234 | |
Hamdi | 25:9dab18979609 | 235 | |
Hamdi | 25:9dab18979609 | 236 | |
Hamdi | 25:9dab18979609 | 237 | /* Initializes the 6180XA1 expansion board with default values. */ |
Hamdi | 25:9dab18979609 | 238 | status = board->init_board(); |
Hamdi | 25:9dab18979609 | 239 | |
mapellil | 14:641560b57584 | 240 | char buf[MQTT_MAX_PAYLOAD_SIZE]; |
Hamdi | 25:9dab18979609 | 241 | |
Hamdi | 25:9dab18979609 | 242 | uint32_t lux, dist; |
Hamdi | 25:9dab18979609 | 243 | |
Hamdi | 25:9dab18979609 | 244 | board->sensor_top->get_distance(&dist); |
Hamdi | 25:9dab18979609 | 245 | board->sensor_top->get_lux(&lux); |
Hamdi | 25:9dab18979609 | 246 | |
Hamdi | 25:9dab18979609 | 247 | |
Hamdi | 25:9dab18979609 | 248 | if ((lux<40)&&(lux>10)) |
Hamdi | 25:9dab18979609 | 249 | { for(int i=5; i<160; i++) { |
Hamdi | 25:9dab18979609 | 250 | myservo = i/160.0; |
Hamdi | 25:9dab18979609 | 251 | wait(0.01); |
Hamdi | 25:9dab18979609 | 252 | } |
Hamdi | 25:9dab18979609 | 253 | } |
Hamdi | 25:9dab18979609 | 254 | else |
Hamdi | 25:9dab18979609 | 255 | if ((lux<180)&&(lux>150)) |
Hamdi | 25:9dab18979609 | 256 | { for(int i=5; i<170; i++) { |
Hamdi | 25:9dab18979609 | 257 | myservo = i/170.0; |
Hamdi | 25:9dab18979609 | 258 | wait(0.01); |
Hamdi | 25:9dab18979609 | 259 | } |
Hamdi | 25:9dab18979609 | 260 | } |
Hamdi | 25:9dab18979609 | 261 | |
Hamdi | 25:9dab18979609 | 262 | // |
mapellil | 14:641560b57584 | 263 | sprintf(buf, |
Hamdi | 25:9dab18979609 | 264 | "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Distance\": %1.0d, \"Lux\": %1.0d}}", |
Hamdi | 25:9dab18979609 | 265 | dist,lux); |
mapellil | 14:641560b57584 | 266 | message.qos = MQTT::QOS0; |
mapellil | 14:641560b57584 | 267 | message.retained = false; |
mapellil | 14:641560b57584 | 268 | message.dup = false; |
mapellil | 14:641560b57584 | 269 | message.payload = (void*)buf; |
mapellil | 14:641560b57584 | 270 | message.payloadlen = strlen(buf); |
mapellil | 14:641560b57584 | 271 | |
mapellil | 24:a59eb8bdc431 | 272 | // LOG("Publishing %s\n\r", buf); |
mapellil | 24:a59eb8bdc431 | 273 | printf("Publishing %s\n\r", buf); |
mapellil | 14:641560b57584 | 274 | return client->publish(pubTopic, message); |
mapellil | 14:641560b57584 | 275 | } |
mridup | 4:1ed7f173eec5 | 276 | |
fabiombed | 13:0b31131bf711 | 277 | int main() |
fabiombed | 13:0b31131bf711 | 278 | { |
mapellil | 16:233b89a6b72f | 279 | const char * ssid = SSID; // Network must be visible otherwise it can't connect |
mapellil | 16:233b89a6b72f | 280 | const char * seckey = PASSW; |
mapellil | 24:a59eb8bdc431 | 281 | SpwfSAInterface spwf(D8, D2, false); |
mapellil | 14:641560b57584 | 282 | |
Hamdi | 25:9dab18979609 | 283 | DevI2C *device_i2c = new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL); |
Hamdi | 25:9dab18979609 | 284 | board = XNucleo6180XA1::instance(device_i2c, A3, A0, D13, D0); |
Hamdi | 25:9dab18979609 | 285 | |
Hamdi | 25:9dab18979609 | 286 | |
Hamdi | 25:9dab18979609 | 287 | |
Hamdi | 25:9dab18979609 | 288 | |
mapellil | 14:641560b57584 | 289 | // Timer tyeld; |
mapellil | 24:a59eb8bdc431 | 290 | myled=0; |
Hamdi | 25:9dab18979609 | 291 | |
mapellil | 14:641560b57584 | 292 | |
Hamdi | 25:9dab18979609 | 293 | printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n"); |
Hamdi | 25:9dab18979609 | 294 | printf("\r\nconnecting to AP\r\n"); |
fabiombed | 13:0b31131bf711 | 295 | |
mapellil | 17:83d0cd810ed3 | 296 | quickstartMode=false; |
mapellil | 16:233b89a6b72f | 297 | if (strcmp(org, "quickstart") == 0){quickstartMode = true;} |
mapellil | 16:233b89a6b72f | 298 | MQTTWiFi ipstack(spwf, ssid, seckey, NSAPI_SECURITY_WPA2); |
mapellil | 16:233b89a6b72f | 299 | MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); |
mapellil | 16:233b89a6b72f | 300 | if (quickstartMode){ |
mapellil | 24:a59eb8bdc431 | 301 | char mac[50]; // remove all : from mac |
mapellil | 17:83d0cd810ed3 | 302 | char *digit=NULL; |
mapellil | 17:83d0cd810ed3 | 303 | sprintf (id,"%s", ""); |
mapellil | 14:641560b57584 | 304 | sprintf (mac,"%s",ipstack.getWiFi().get_mac_address()); |
mapellil | 14:641560b57584 | 305 | digit = strtok (mac,":"); |
mapellil | 14:641560b57584 | 306 | while (digit != NULL) |
mapellil | 14:641560b57584 | 307 | { |
mapellil | 14:641560b57584 | 308 | strcat (id, digit); |
mapellil | 14:641560b57584 | 309 | digit = strtok (NULL, ":"); |
mapellil | 24:a59eb8bdc431 | 310 | } |
mapellil | 16:233b89a6b72f | 311 | } |
mapellil | 16:233b89a6b72f | 312 | attemptConnect(&client, &ipstack); |
mapellil | 16:233b89a6b72f | 313 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
mapellil | 16:233b89a6b72f | 314 | { |
mapellil | 16:233b89a6b72f | 315 | while (true) |
mapellil | 16:233b89a6b72f | 316 | wait(1.0); // Permanent failures - don't retry |
mapellil | 16:233b89a6b72f | 317 | } |
Hamdi | 25:9dab18979609 | 318 | |
mapellil | 24:a59eb8bdc431 | 319 | myled=1; |
mapellil | 16:233b89a6b72f | 320 | int count = 0; |
mapellil | 16:233b89a6b72f | 321 | // tyeld.start(); |
mapellil | 14:641560b57584 | 322 | while (true) |
mridup | 8:6df01cb43137 | 323 | { |
mapellil | 24:a59eb8bdc431 | 324 | if (++count == 100) |
mapellil | 14:641560b57584 | 325 | { // Publish a message every second |
mapellil | 23:43f9e34781f2 | 326 | if (publish(&client, &ipstack) != 0) { |
mapellil | 24:a59eb8bdc431 | 327 | myled=0; |
mapellil | 23:43f9e34781f2 | 328 | attemptConnect(&client, &ipstack); // if we have lost the connection |
mapellil | 24:a59eb8bdc431 | 329 | } else myled=1; |
mapellil | 14:641560b57584 | 330 | count = 0; |
mapellil | 14:641560b57584 | 331 | } |
mapellil | 14:641560b57584 | 332 | // int start = tyeld.read_ms(); |
mapellil | 24:a59eb8bdc431 | 333 | client.yield(10); // allow the MQTT client to receive messages |
mapellil | 14:641560b57584 | 334 | // printf ("tyeld: %d\n\r",tyeld.read_ms()-start); |
mridup | 3:dfb8c6c8c31b | 335 | } |
mridup | 4:1ed7f173eec5 | 336 | } |