int main() { signal(SIGINT, sig_handler); //! [Interesting] // Instantiate a SHT1X sensor using D2 as the clock, and D3 as the // data pin. sht1x_context sensor = sht1x_init(2, 3); if (!sensor) { printf("sht1x_init() failed.\n"); return 1; } // Every 2 seconds, update and print values while (shouldRun) { if (sht1x_update(sensor)) { printf("sht1x_update() failed, exiting.\n"); break; } printf("Temperature: %f C\n", sht1x_get_temperature(sensor)); printf("Humidity: %f RH\n", sht1x_get_humidity(sensor)); printf("\n"); upm_delay(2); } //! [Interesting] printf("Exiting\n"); sht1x_close(sensor); return 0; }
int main(int argc, char *argv[]) { struct sht1x_device *devices = NULL; struct sht1x_device *current = NULL; int err, status, i; if (argc < 2) { printf("Usage: %s 1-2.3.4 [1-2.3.5] [2-6.1]\n", argv[0]); status = EXIT_FAILURE; goto done; } for (i = 1; i < argc; i++) { char config[4096]; FILE *cfg_fd; current = malloc(sizeof(struct sht1x_device)); if (current == NULL) { status = EXIT_FAILURE; goto closeall; } current->next = devices; devices = current; strncpy(current->name, argv[i], sizeof(current->name)); snprintf(current->rrdfile, sizeof(current->rrdfile), "%s.rrd", argv[i]); sht1x_open(current); temperhum_rrd_create(current->rrdfile); current->tc_offset = 0; snprintf(config, sizeof(config), "%s.conf", current->name); errno = 0; cfg_fd = fopen(config, "r"); if (cfg_fd != NULL) { char buf[1024]; if (fgets(buf, sizeof(buf), cfg_fd) != NULL) { errno = 0; current->tc_offset = strtod(buf, NULL); if (errno != 0) { fprintf(stderr, "Error reading config for %s (%s)\n", current->name, strerror(errno)); status = EXIT_FAILURE; goto closeall; } } fclose(cfg_fd); } else if (errno != ENOENT) { fprintf(stderr, "Error opening config for %s (%s)\n", current->name, strerror(errno)); status = EXIT_FAILURE; goto closeall; } err = sht1x_device_reset(current); if (err) { fprintf(stderr, "Error resetting device %s\n", current->name); status = EXIT_FAILURE; goto closeall; } current->status = sht1x_read_status(current); if (!current->status.valid) { fprintf(stderr, "Status read failed for %s\n", current->name); status = EXIT_FAILURE; goto closeall; } else { printf("%s; Status {" "Battery: %s, " "Heater: %s, " "Reload: %s, " "Resolution: %s }\n", current->name, current->status.low_battery == 0 ? "OK" : "LOW", current->status.heater == 0 ? "OFF" : "ON", current->status.no_reload == 0 ? "YES" : "NO", current->status.low_resolution == 0 ? "HIGH" : "LOW"); if (current->status.low_resolution || current->status.heater) { current->status.low_resolution = 0; current->status.heater = 0; if (sht1x_write_status(current, current->status)) { fprintf(stderr, "Status write failed for %s\n", current->name); status = EXIT_FAILURE; goto closeall; } } } } signal(SIGINT, temperhum_stop); signal(SIGTERM, temperhum_stop); while(!stop) { struct sht1x_readings readings; struct timespec tp; if (current == NULL) current = devices; readings = sht1x_getreadings(current, current->status.low_resolution); clock_gettime(CLOCK_REALTIME, &tp); printf("%lu.%09lu/%10s; T %6.2lf℃ / RH %6.2lf%% / DP %6.2lf℃\n", tp.tv_sec, tp.tv_nsec, current->name, readings.temperature_celsius, readings.relative_humidity, readings.dew_point); temperhum_rrd_update(current->rrdfile, tp.tv_sec, readings); current = current->next; } status = EXIT_SUCCESS; closeall: while (devices != NULL) { current = devices; sht1x_close(current); devices = current->next; free(current); } done: exit(status); }