int delete_arduino(arduino_t* arduino) { /* if (arduino->serial != -1) { */ /* serialport_close(arduino->serial); */ /* } */ arduino_disconnect(arduino); free(arduino); return 0; }
void arduino_reset_stack(arduino_t* arduino) { int err; err = arduino_connect(arduino); if (err != 0) return; err = arduino_set_state_(arduino, STATE_RESETSTACK); if (err != 0) { arduino_disconnect(arduino); return; } //return arduino_disconnect(arduino); }
int arduino_set_time(arduino_t* arduino, time_t time) { int err; err = arduino_connect(arduino); if (err != 0) return -1; err = arduino_set_time_(arduino, time); if (err != 0) { arduino_disconnect(arduino); return err; } //return arduino_disconnect(arduino); return 0; }
int arduino_pump(arduino_t* arduino, int seconds) { int err; err = arduino_connect(arduino); if (err != 0) return -1; err = arduino_pump_(arduino, seconds); if (err != 0) { arduino_disconnect(arduino); return err; } //return arduino_disconnect(arduino); return 0; }
int arduino_get_frames(arduino_t* arduino, int* frames) { int err; err = arduino_connect(arduino); if (err != 0) return -1; err = arduino_get_frames_(arduino, frames); if (err != 0) { arduino_disconnect(arduino); return err; } //return arduino_disconnect(arduino); return 0; }
int arduino_get_poweroff(arduino_t* arduino, int* minutes) { int err; err = arduino_connect(arduino); if (err != 0) return -1; err = arduino_get_poweroff_(arduino, minutes); if (err != 0) { arduino_disconnect(arduino); return err; } //return arduino_disconnect(arduino); return 0; }
int arduino_get_period(arduino_t* arduino, unsigned char* period) { int err; err = arduino_connect(arduino); if (err != 0) return -1; err = arduino_get_period_(arduino, period); if (err != 0) { arduino_disconnect(arduino); return err; } //return arduino_disconnect(arduino); return 0; }
int arduino_get_sensors(arduino_t* arduino, unsigned char* sensors) { int err; err = arduino_connect(arduino); if (err != 0) return -1; err = arduino_get_sensors_(arduino, sensors); if (err != 0) { arduino_disconnect(arduino); return err; } //return arduino_disconnect(arduino); return 0; }
datapoint_t* arduino_measure(arduino_t* arduino, int* num_points) { int err = -1; unsigned char sensors; int datastreams[32]; int num_streams = 0; datapoint_t* datapoints = NULL; float factors[32]; *num_points = 0; err = arduino_connect(arduino); if (err != 0) goto error_recovery; err = arduino_get_sensors_(arduino, &sensors); if (err != 0) goto error_recovery; if (sensors & SENSOR_TRH) { factors[num_streams] = 0.01f; datastreams[num_streams++] = DATASTREAM_T; factors[num_streams] = 0.01f; datastreams[num_streams++] = DATASTREAM_RH; } if (sensors & SENSOR_TRHX) { factors[num_streams] = 0.01f; datastreams[num_streams++] = DATASTREAM_TX; factors[num_streams] = 0.01f; datastreams[num_streams++] = DATASTREAM_RHX; } if (sensors & SENSOR_LUM) { factors[num_streams] = 1.0f; datastreams[num_streams++] = DATASTREAM_LUM; } if (sensors & SENSOR_USBBAT) { factors[num_streams] = 0.01f; datastreams[num_streams++] = DATASTREAM_USBBAT; } if (sensors & SENSOR_SOIL) { factors[num_streams] = 1.0f; datastreams[num_streams++] = DATASTREAM_SOIL; } err = arduino_set_state_(arduino, STATE_SUSPEND); if (err != 0) goto error_recovery; err = arduino_measure_(arduino); if (err != 0) goto error_recovery; sleep(60); // Sleep for one minute. stack_t stack; stack.framesize = num_streams + 1; stack.values[0] = 0; stack.frames = 1; for (int attempt = 0; attempt < 5; attempt++) { log_info("Arduino: Download attempt %d", attempt + 1); err = arduino_measurement0_(arduino); if (err != 0) goto error_recovery; for (int i = 0; i < num_streams; i++) { unsigned long value; err = arduino_read_value(arduino, &value, CMD_GETMEASUREMENT, sizeof(sensor_value_t)); if (err != 0) break; stack.values[i+1] = (sensor_value_t) value; } if (err == 0) break; } if (err == 0) datapoints = arduino_convert_stack_(arduino, &stack, datastreams, factors, num_points); error_recovery: if (err == 0) { log_info("Arduino: Download successful"); err = arduino_set_state_(arduino, STATE_MEASURING); } else { log_info("Arduino: Download failed"); err = arduino_set_state_(arduino, STATE_MEASURING); arduino_disconnect(arduino); } return datapoints; }
datapoint_t* arduino_read_data(arduino_t* arduino, int* num_points) { int err = -1; unsigned char sensors; int datastreams[32]; int num_streams = 0; datapoint_t* datapoints = NULL; float factors[32]; *num_points = 0; err = arduino_connect(arduino); if (err != 0) goto error_recovery; err = arduino_get_sensors_(arduino, &sensors); if (err != 0) goto error_recovery; if (sensors & SENSOR_TRH) { factors[num_streams] = 0.01f; datastreams[num_streams++] = DATASTREAM_T; factors[num_streams] = 0.01f; datastreams[num_streams++] = DATASTREAM_RH; } if (sensors & SENSOR_TRHX) { factors[num_streams] = 0.01f; datastreams[num_streams++] = DATASTREAM_TX; factors[num_streams] = 0.01f; datastreams[num_streams++] = DATASTREAM_RHX; } if (sensors & SENSOR_LUM) { factors[num_streams] = 1.0f; datastreams[num_streams++] = DATASTREAM_LUM; } if (sensors & SENSOR_USBBAT) { factors[num_streams] = 0.01f; datastreams[num_streams++] = DATASTREAM_USBBAT; } if (sensors & SENSOR_SOIL) { factors[num_streams] = 1.0f; datastreams[num_streams++] = DATASTREAM_SOIL; } stack_t stack; stack.framesize = num_streams + 1; err = arduino_set_state_(arduino, STATE_SUSPEND); if (err != 0) goto error_recovery; err = arduino_get_frames_(arduino, &stack.frames); if (err != 0) goto error_recovery; log_info("Arduino: Found %d measurement frames", stack.frames); if (stack.frames == 0) { err = arduino_set_state_(arduino, STATE_MEASURING); goto clean_exit; } err = arduino_get_checksum_(arduino, &stack.checksum); if (err != 0) goto error_recovery; log_info("Arduino: Checksum Arduino 0x%02x", stack.checksum); err = arduino_get_offset_(arduino, &stack.offset); if (err != 0) goto error_recovery; log_info("Arduino: Time offset Arduino %lu", (unsigned int) stack.offset); for (int attempt = 0; attempt < 5; attempt++) { log_info("Arduino: Download attempt %d", attempt + 1); err = arduino_copy_stack_(arduino, &stack); if (err != 0) continue; unsigned char* ptr = (unsigned char*) &stack.values[0]; int len = stack.frames * stack.framesize * sizeof(sensor_value_t); unsigned char checksum = crc8(0, ptr, len); log_info("Arduino: Checksum Linux 0x%02x", checksum); // /* for (int i = 0; i < len; i++) { */ /* fprintf(stderr, "%02x", ptr[i]); */ /* if ((i % 4) == 3) */ /* fprintf(stderr, "\n"); */ /* else */ /* fprintf(stderr, " "); */ /* } */ // if (checksum != stack.checksum) err = -1; if (err == 0) break; } if (err == 0) datapoints = arduino_convert_stack_(arduino, &stack, datastreams, factors, num_points); clean_exit: error_recovery: if (err == 0) { log_info("Arduino: Download successful"); err = arduino_set_state_(arduino, STATE_RESETSTACK); } else { log_info("Arduino: Download failed"); err = arduino_set_state_(arduino, STATE_MEASURING); arduino_disconnect(arduino); } return datapoints; }