//Staggered retry int retry_connection(Iotfclient *client) { int retry = 1; printf("Attempting to connect\n"); while(connectiotf(client) != SUCCESS) { printf("Retry Attempt #%d ", retry); int delay = reconnect_delay(retry++); printf(" next attempt in %d seconds\n", delay); sleep(delay); } }
int main(int argc, char **argv) { InitDHT(1); char* json; int lckStatus; int res; int sleepTimeout; struct config configstr; char *passwd; char *username; char msproxyUrl[MAXBUF]; //setup the syslog logging setlogmask(LOG_UPTO(LOGLEVEL)); openlog("iot", LOG_PID | LOG_CONS, LOG_USER); syslog(LOG_INFO, "**** IoT Raspberry Pi Sample has started ****"); // register the signal handler for USR1-user defined signal 1 if (signal(SIGUSR1, sig_handler) == SIG_ERR) syslog(LOG_CRIT, "Not able to register the signal handler\n"); if (signal(SIGINT, sig_handler) == SIG_ERR) syslog(LOG_CRIT, "Not able to register the signal handler\n"); //read the config file, to decide whether to goto quickstart or registered mode of operation isRegistered = get_config(configFile, &configstr); if (isRegistered) { syslog(LOG_INFO, "Running in Registered mode\n"); sprintf(msproxyUrl, "ssl://%s.messaging.internetofthings.ibmcloud.com:8883", configstr.org); if(strcmp(configstr.authmethod ,"token") != 0) { syslog(LOG_ERR, "Detected that auth-method is not token. Currently other authentication mechanisms are not supported, IoT process will exit."); syslog(LOG_INFO, "**** IoT Raspberry Pi Sample has ended ****"); closelog(); exit(1); } else { username = "******"; passwd = configstr.authtoken; } } else { syslog(LOG_INFO, "Running in Quickstart mode\n"); strcpy(msproxyUrl,"tcp://quickstart.messaging.internetofthings.ibmcloud.com:1883"); } // read the events char* mac_address = getmac("eth0"); getClientId(&configstr, mac_address); //the timeout between the connection retry int connDelayTimeout = 1; // default sleep for 1 sec int retryAttempt = 0; // initialize the MQTT connection init_mqtt_connection(&client, msproxyUrl, isRegistered, clientId, username, passwd); // Wait till we get a successful connection to IoT MQTT server while (!MQTTAsync_isConnected(client)) { connDelayTimeout = 1; // add extra delay(3,60,600) only when reconnecting if (connected == -1) { connDelayTimeout = reconnect_delay(++retryAttempt); //Try to reconnect after the retry delay syslog(LOG_ERR, "Failed connection attempt #%d. Will try to reconnect " "in %d seconds\n", retryAttempt, connDelayTimeout); connected = 0; init_mqtt_connection(&client, msproxyUrl, isRegistered, clientId, username, passwd); } fflush(stdout); sleep(connDelayTimeout); } // resetting the counters connDelayTimeout = 1; retryAttempt = 0; // count for the sine wave int count = 1; sleepTimeout = EVENTS_INTERVAL; //subscribe for commands - only on registered mode if (isRegistered) { subscribe(&client, subscribeTopic); } while (1) { JsonMessage json_message = { DEVICE_NAME, getCPUTemp(), sineVal( MIN_VALUE, MAX_VALUE, 16, count), GetCPULoad() , getHumidity(), getTemp()}; json = generateJSON(json_message); res = publishMQTTMessage(&client, publishTopic, json); syslog(LOG_DEBUG, "Posted the message with result code = %d\n", res); if (res == -3) { //update the connected to connection failed connected = -1; while (!MQTTAsync_isConnected(client)) { if (connected == -1) { connDelayTimeout = reconnect_delay(++retryAttempt); //Try to reconnect after the retry delay syslog(LOG_ERR, "Failed connection attempt #%d. " "Will try to reconnect in %d " "seconds\n", retryAttempt, connDelayTimeout); sleep(connDelayTimeout); connected = 0; reconnect(&client, isRegistered, username,passwd); } fflush(stdout); sleep(1); } // resetting the counters connDelayTimeout = 1; retryAttempt = 0; } fflush(stdout); free(json); count++; sleep(sleepTimeout); } return 0; }