989
Dependencies: mbed Servo X_NUCLEO_IKS01A2 X_NUCLEO_IDW01M1v2 NetworkSocketAPI NDefLib MQTT
Diff: main.cpp
- Revision:
- 17:83d0cd810ed3
- Parent:
- 16:233b89a6b72f
- Child:
- 18:2c229fa282fa
--- a/main.cpp Wed Sep 28 15:36:07 2016 +0000 +++ b/main.cpp Tue Oct 04 11:58:37 2016 +0000 @@ -30,21 +30,33 @@ DigitalOut myled(LED1); SpwfSAInterface spwf(D8, D2, false); bool quickstartMode = true; + +#define ORG_QUICKSTART // comment to connect to play.internetofthings.ibmcloud.com #define MQTT_MAX_PACKET_SIZE 250 #define MQTT_MAX_PAYLOAD_SIZE 300 + // Configuration values needed to connect to IBM IoT Cloud -#define ORG "quickstart" // For a registered connection, replace with your org -#define ID "" // For a registered connection, replace with your id -#define AUTH_TOKEN "" // For a registered connection, replace with your auth-token +#ifdef ORG_QUICKSTART +#define ORG "quickstart" // connect to quickstart.internetofthings.ibmcloud.com/ For a registered connection, replace with your org +#define ID "" +#define AUTH_TOKEN "" #define DEFAULT_TYPE_NAME "iotsample-mbed-NucleoF401RE" + +#else +#define ORG "play" // connect to play.internetofthings.ibmcloud.com/ For a registered connection, replace with your org +#define ID "testnucleo2" // For a registered connection, replace with your id +#define AUTH_TOKEN "centrallab"// For a registered connection, replace with your auth-token +#define DEFAULT_TYPE_NAME "sensor" +#endif + #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type #define MQTT_PORT 1883 #define MQTT_TLS_PORT 8883 #define IBM_IOT_PORT MQTT_PORT // WiFi network credential #define SSID "crespan" // Network must be visible otherwise it can't connect -#define PASSW "" +#define PASSW "Elfrontal0" #warning "Wifi password empty" char id[30] = ID; // mac without colons @@ -85,9 +97,9 @@ LOG("IP ADDRESS: %s\n\r", WiFi.get_ip_address()); LOG("MAC ADDRESS: %s\n\r", WiFi.get_mac_address()); LOG("Server Hostname: %s\n\r", hostname); - for(int i = 0; clientId[i]; i++){ - clientId[i] = tolower(clientId[i]); - } +// for(int i = 0; clientId[i]; i++){ +// clientId[i] = tolower(clientId[i]); +// } LOG("Client ID: %s\n\r", clientId); LOG("=====================================\n\r"); @@ -105,7 +117,8 @@ // MQTT Connect mqttConnecting = true; MQTTPacket_connectData data = MQTTPacket_connectData_initializer; - data.MQTTVersion = 3; + data.MQTTVersion = 4; + data.struct_version=0; data.clientID.cstring = clientId; if (!quickstartMode) @@ -199,12 +212,14 @@ pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n"); pc.printf("\r\nconnecting to AP\r\n"); + quickstartMode=false; if (strcmp(org, "quickstart") == 0){quickstartMode = true;} MQTTWiFi ipstack(spwf, ssid, seckey, NSAPI_SECURITY_WPA2); MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); if (quickstartMode){ char mac[50]; - char *digit=NULL; + char *digit=NULL; + sprintf (id,"%s", ""); sprintf (mac,"%s",ipstack.getWiFi().get_mac_address()); strcpy (mac, ipstack.getWiFi().get_mac_address()); digit = strtok (mac,":"); @@ -212,7 +227,7 @@ { strcat (id, digit); digit = strtok (NULL, ":"); - } + } } attemptConnect(&client, &ipstack); if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)