int main(int argc, char **argv) { if (argc > 1) pcsensor_debug(1); int passes = 0; float tempc = 0.0000; do { usb_dev_handle* lvr_winusb = pcsensor_open(); if (!lvr_winusb) { /* Open fails sometime, sleep and try again */ sleep(3); } else { tempc = pcsensor_get_temperature(lvr_winusb); pcsensor_close(lvr_winusb); } ++passes; /* Read fails silently with a 0.0 return, so repeat until not zero or until we have read the same zero value 3 times (just in case temp is really dead on zero */ } while ((tempc > -0.0001 && tempc < 0.0001) || passes >= 4); if (!((tempc > -0.0001 && tempc < 0.0001) || passes >= 4)) { /* Apply calibrations */ tempc = (tempc * scale) + offset; struct tm *utc; time_t t; t = time(NULL); utc = gmtime(&t); char dt[80]; strftime(dt, 80, "%Y-%m-%d %H:%M:%S", utc); // YYYY-mm-dd HH:MM:SS //strftime(dt, 80, "%d-%b-%Y %H:%M", utc); // dd-mmm-YYYY HH:MM printf("%s,%f\n", dt, tempc); fflush(stdout); return 0; } else { return 1; } }
/** * Read the temperature in Celcius from the provided device number 0 indexed * @param int device_id * @return float */ float read_temperature_c(int device_id) { pcsensor_devices *available_devices = setup_libusb_access(); float tempc = 0; if (available_devices->sensor_count == 0) { return FLT_MIN; } if ((device_id - 1) > available_devices->sensor_count) { return FLT_MIN; } if (available_devices->sensors[device_id] == NULL) { return FLT_MIN; } usb_dev_handle *device_handle = pcsensor_open(available_devices->sensors[device_id]); pcsensor_get_temperature(device_handle, &tempc); pcsensor_close(device_handle); return tempc; }
int main(){ float tempc = 0.0000; char c; usb_dev_handle* lvr_winusb = pcsensor_open(); if (!lvr_winusb) { /* Open fails sometime */ printf("open failed\n"); fflush(stdout); return 1; } while (1) { /*printf("press enter to get temp\n");*/ fflush(stdout); while((c = getchar()) != '\n' && c != EOF){} tempc = pcsensor_get_temperature(lvr_winusb); if (!(tempc > -0.0001 && tempc < 0.0001)) { /* Apply calibrations */ tempc = (tempc * scale) + offset; printf("%f\n", tempc); fflush(stdout); } else { printf("Temperature is zero, probably error\n"); fflush(stdout); } } pcsensor_close(lvr_winusb); return 0; }
int main(){ // check for presence of env vars if (!getenv(ENV_URL)) { printf("Necessary environment vars not found, please set %s !\n", ENV_URL); return -1; } char* url; url = getenv(ENV_URL); int passes = 0; float tempc = 0.0000; do { usb_dev_handle* lvr_winusb = pcsensor_open(); if (!lvr_winusb) { /* Open fails sometime, sleep and try again */ sleep(3); } else { tempc = pcsensor_get_temperature(lvr_winusb); pcsensor_close(lvr_winusb); } ++passes; } /* Read fails silently with a 0.0 return, so repeat until not zero or until we have read the same zero value 3 times (just in case temp is really dead on zero */ while ((tempc > -0.0001 && tempc < 0.0001) || passes >= 4); if (!((tempc > -0.0001 && tempc < 0.0001) || passes >= 4)) { /* Apply calibrations */ tempc = (tempc * scale) + offset; struct tm *utc; time_t t; t = time(NULL); utc = localtime(&t); char dt[80]; strftime(dt, 80, "%Y-%m-%dT%H:%M:%S%z", utc); char json[200]; sprintf(json, "{\"collection\":\"%s\",\"timestamp\":\"%s\",\"data\":{\"temp\":%.1f}}", COLLECTION, dt, tempc); printf("%s ", json); // init curl CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Accept: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "charsets: utf-8"); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json); res = curl_easy_perform(curl); /* Check for errors */ if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } /* always cleanup */ curl_easy_cleanup(curl); } curl_global_cleanup(); printf("\n"); return 0; } else { return 1; } }