예제 #1
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);
}
예제 #2
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;
}
예제 #3
0
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;
}