static float getTemperature (LPNXCTRLAPP pApp) { static int nUpdate = -1; static float fTemp = 0.0; if (nUpdate < 0 || nUpdate > TEMP_UPDATE_COUNT) { float fTmp = (getTMP36Value(pApp) + TMP36_DELTA - 0.5) * 100; float fCPUTemp = getCPUTemp()/1000.0; fCPUTemp = (fCPUTemp > 120) ? (fCPUTemp - 74) : fCPUTemp; if (fCPUTemp < CPUTEMP_SSG) fTmp -= (fCPUTemp > CPUTEMP_BASE) ? (fCPUTemp - CPUTEMP_BASE) : 0; else { float fD = (fCPUTemp > CPUTEMP_BASE) ? (fCPUTemp - CPUTEMP_BASE + SSG_DELTA) : 0; if (fD > TMP_MAX_DELTA) fD = TMP_MAX_DELTA; fTmp -= fD; } CPUTEMP = fCPUTemp; if (fTmp < -30 || fTmp > 50) fTmp = 0; fTemp = fTmp; nUpdate = 0; } else nUpdate++; return fTemp; }
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; }