INSAT_MiniPRoject
Dependencies: MQTT NDefLib NetworkSocketAPI Servo Light_Sensor_Nucleo X_NUCLEO_IDW01M1v2 mbed
Fork of IDW01M1_Cloud_IBM by
Diff: main.cpp
- Revision:
- 24:a59eb8bdc431
- Parent:
- 23:43f9e34781f2
- Child:
- 25:9dab18979609
--- a/main.cpp Wed Oct 26 15:28:35 2016 +0000 +++ b/main.cpp Thu Nov 24 13:22:03 2016 +0000 @@ -31,7 +31,6 @@ //------------------------------------ Serial pc(SERIAL_TX, SERIAL_RX); DigitalOut myled(LED1); -SpwfSAInterface spwf(D8, D2, false); bool quickstartMode = true; #define ORG_QUICKSTART // comment to connect to play.internetofthings.ibmcloud.com @@ -109,7 +108,7 @@ char hostname[strlen(org) + strlen(iot_ibm) + 1]; sprintf(hostname, "%s%s", org, iot_ibm); SpwfSAInterface& WiFi = ipstack->getWiFi(); - ip_addr = WiFi.get_ip_address(); +// ip_addr = WiFi.get_ip_address(); // Construct clientId - d:org:type:id char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; sprintf(clientId, "d:%s:%s:%s", org, type, id); @@ -121,10 +120,10 @@ LOG("Nucleo IP ADDRESS: %s\n\r", WiFi.get_ip_address()); LOG("Nucleo MAC ADDRESS: %s\n\r", WiFi.get_mac_address()); LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT); -// for(int i = 0; clientId[i]; i++){ -// clientId[i] = tolower(clientId[i]); +// for(int i = 0; clientId[i]; i++){ // set lowercase mac +// clientId[i] = tolower(clientId[i]); // } - LOG("Client ID: %s id: %s\n\r", clientId, id); + LOG("Client ID: %s\n\r", clientId); LOG("Topic: %s\n\r",TOPIC); LOG("Subscription URL: %s\n\r", subscription_url); LOG("=====================================\n\r"); @@ -218,7 +217,8 @@ message.payload = (void*)buf; message.payloadlen = strlen(buf); - LOG("Publishing %s\n\r", buf); +// LOG("Publishing %s\n\r", buf); + printf("Publishing %s\n\r", buf); return client->publish(pubTopic, message); } @@ -226,9 +226,10 @@ { const char * ssid = SSID; // Network must be visible otherwise it can't connect const char * seckey = PASSW; + SpwfSAInterface spwf(D8, D2, false); // Timer tyeld; - + myled=0; DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL); i2c->frequency(400000); @@ -245,17 +246,16 @@ MQTTWiFi ipstack(spwf, ssid, seckey, NSAPI_SECURITY_WPA2); MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); if (quickstartMode){ - char mac[50]; + char mac[50]; // remove all : from mac char *digit=NULL; sprintf (id,"%s", ""); sprintf (mac,"%s",ipstack.getWiFi().get_mac_address()); - strcpy (mac, ipstack.getWiFi().get_mac_address()); digit = strtok (mac,":"); while (digit != NULL) { strcat (id, digit); digit = strtok (NULL, ":"); - } + } } attemptConnect(&client, &ipstack); if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) @@ -283,20 +283,22 @@ printf("Error Closing the session\r\n"); } }else printf("Error open Session\r\n"); -#endif +#endif + myled=1; int count = 0; // tyeld.start(); while (true) { - if (++count == /*100*/2) + if (++count == 100) { // Publish a message every second if (publish(&client, &ipstack) != 0) { + myled=0; attemptConnect(&client, &ipstack); // if we have lost the connection - } + } else myled=1; count = 0; } // int start = tyeld.read_ms(); - client.yield(/*10*/1); // allow the MQTT client to receive messages + client.yield(10); // allow the MQTT client to receive messages // printf ("tyeld: %d\n\r",tyeld.read_ms()-start); } }