2 * collectd - src/barometer.c
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; only version 2.1 of the License is
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "utils_cache.h"
30 #include <linux/i2c-dev.h>
33 /* ------------ MPL115 defines ------------ */
34 /* I2C address of the MPL115 sensor */
35 #define MPL115_I2C_ADDRESS 0x60
37 /* register addresses */
38 #define MPL115_ADDR_CONV 0x00
39 #define MPL115_ADDR_COEFFS 0x04
42 #define MPL115_NUM_CONV 4
43 #define MPL115_NUM_COEFFS 12
45 /* commands / addresses */
46 #define MPL115_CMD_CONVERT_PRESS 0x10
47 #define MPL115_CMD_CONVERT_TEMP 0x11
48 #define MPL115_CMD_CONVERT_BOTH 0x12
50 #define MPL115_CONVERSION_RETRIES 5
53 /* ------------ MPL3115 defines ------------ */
54 /* MPL3115 I2C address */
55 #define MPL3115_I2C_ADDRESS 0x60
57 /* register addresses (only the interesting ones) */
58 #define MPL3115_REG_STATUS 0x00
59 #define MPL3115_REG_OUT_P_MSB 0x01
60 #define MPL3115_REG_OUT_P_CSB 0x02
61 #define MPL3115_REG_OUT_P_LSB 0x03
62 #define MPL3115_REG_OUT_T_MSB 0x04
63 #define MPL3115_REG_OUT_T_LSB 0x05
64 #define MPL3115_REG_DR_STATUS 0x06
65 #define MPL3115_REG_WHO_AM_I 0x0C
66 #define MPL3115_REG_SYSMOD 0x11
67 #define MPL3115_REG_PT_DATA_CFG 0x13
68 #define MPL3115_REG_BAR_IN_MSB 0x14
69 #define MPL3115_REG_BAR_IN_LSB 0x15
70 #define MPL3115_REG_CTRL_REG1 0x26
71 #define MPL3115_REG_CTRL_REG2 0x27
72 #define MPL3115_REG_CTRL_REG3 0x28
73 #define MPL3115_REG_CTRL_REG4 0x29
74 #define MPL3115_REG_CTRL_REG5 0x2A
75 #define MPL3115_REG_OFF_P 0x2B
76 #define MPL3115_REG_OFF_T 0x2C
77 #define MPL3115_REG_OFF_H 0x2D
79 /* Register values, masks */
80 #define MPL3115_WHO_AM_I_RESP 0xC4
82 #define MPL3115_PT_DATA_DREM 0x04
83 #define MPL3115_PT_DATA_PDEF 0x02
84 #define MPL3115_PT_DATA_TDEF 0x01
86 #define MPL3115_DR_STATUS_TDR 0x02
87 #define MPL3115_DR_STATUS_PDR 0x04
88 #define MPL3115_DR_STATUS_PTDR 0x08
89 #define MPL3115_DR_STATUS_DR (MPL3115_DR_STATUS_TDR | MPL3115_DR_STATUS_PDR | MPL3115_DR_STATUS_PTDR)
91 #define MPL3115_DR_STATUS_TOW 0x20
92 #define MPL3115_DR_STATUS_POW 0x40
93 #define MPL3115_DR_STATUS_PTOW 0x80
95 #define MPL3115_CTRL_REG1_ALT 0x80
96 #define MPL3115_CTRL_REG1_RAW 0x40
97 #define MPL3115_CTRL_REG1_OST_MASK 0x38
98 #define MPL3115_CTRL_REG1_OST_1 0x00
99 #define MPL3115_CTRL_REG1_OST_2 0x08
100 #define MPL3115_CTRL_REG1_OST_4 0x10
101 #define MPL3115_CTRL_REG1_OST_8 0x18
102 #define MPL3115_CTRL_REG1_OST_16 0x20
103 #define MPL3115_CTRL_REG1_OST_32 0x28
104 #define MPL3115_CTRL_REG1_OST_64 0x30
105 #define MPL3115_CTRL_REG1_OST_128 0x38
106 #define MPL3115_CTRL_REG1_RST 0x04
107 #define MPL3115_CTRL_REG1_OST 0x02
108 #define MPL3115_CTRL_REG1_SBYB 0x01
109 #define MPL3115_CTRL_REG1_SBYB_MASK 0xFE
111 #define MPL3115_NUM_CONV_VALS 5
114 /* ------------ BMP085 defines ------------ */
115 /* I2C address of the BMP085 sensor */
116 #define BMP085_I2C_ADDRESS 0x77
118 /* register addresses */
119 #define BMP085_ADDR_ID_REG 0xD0
120 #define BMP085_ADDR_VERSION 0xD1
122 #define BMP085_ADDR_CONV 0xF6
124 #define BMP085_ADDR_CTRL_REG 0xF4
125 #define BMP085_ADDR_COEFFS 0xAA
128 #define BMP085_NUM_COEFFS 22
130 /* commands, values */
131 #define BMP085_CHIP_ID 0x55
133 #define BMP085_CMD_CONVERT_TEMP 0x2E
135 #define BMP085_CMD_CONVERT_PRESS_0 0x34
136 #define BMP085_CMD_CONVERT_PRESS_1 0x74
137 #define BMP085_CMD_CONVERT_PRESS_2 0xB4
138 #define BMP085_CMD_CONVERT_PRESS_3 0xF4
141 #define BMP085_TIME_CNV_TEMP 4500
143 #define BMP085_TIME_CNV_PRESS_0 4500
144 #define BMP085_TIME_CNV_PRESS_1 7500
145 #define BMP085_TIME_CNV_PRESS_2 13500
146 #define BMP085_TIME_CNV_PRESS_3 25500
149 /* ------------ Normalization ------------ */
150 /* Mean sea level pressure normalization methods */
152 #define MSLP_INTERNATIONAL 1
153 #define MSLP_DEU_WETT 2
155 /** Temperature reference history depth for averaging. See #get_reference_temperature */
156 #define REF_TEMP_AVG_NUM 5
159 /* ------------------------------------------ */
161 /** Supported sensor types */
169 static const char *config_keys[] =
173 "PressureOffset", /**< only for MPL3115 */
174 "TemperatureOffset", /**< only for MPL3115 */
180 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
182 static char * config_device = NULL; /**< I2C bus device */
183 static int config_oversample = 1; /**< averaging window */
185 static double config_press_offset = 0.0; /**< pressure offset */
186 static double config_temp_offset = 0.0; /**< temperature offset */
188 static double config_altitude = NAN; /**< altitude */
189 static int config_normalize = 0; /**< normalization method */
191 static _Bool configured = 0; /**< the whole plugin config status */
193 static int i2c_bus_fd = -1; /**< I2C bus device FD */
195 static enum Sensor_type sensor_type = Sensor_none; /**< detected/used sensor type */
197 static __s32 mpl3115_oversample = 0; /**< MPL3115 CTRL1 oversample setting */
199 // BMP085 configuration
200 static unsigned bmp085_oversampling; /**< BMP085 oversampling (0-3) */
201 static unsigned long bmp085_timeCnvPress; /**< BMP085 conversion time for pressure in us */
202 static __u8 bmp085_cmdCnvPress; /**< BMP085 pressure conversion command */
205 /* MPL115 conversion coefficients */
206 static double mpl115_coeffA0;
207 static double mpl115_coeffB1;
208 static double mpl115_coeffB2;
209 static double mpl115_coeffC12;
210 static double mpl115_coeffC11;
211 static double mpl115_coeffC22;
213 /* BMP085 conversion coefficients */
214 static short bmp085_AC1;
215 static short bmp085_AC2;
216 static short bmp085_AC3;
217 static unsigned short bmp085_AC4;
218 static unsigned short bmp085_AC5;
219 static unsigned short bmp085_AC6;
220 static short bmp085_B1;
221 static short bmp085_B2;
222 static short bmp085_MB;
223 static short bmp085_MC;
224 static short bmp085_MD;
228 /* ------------------------ averaging ring buffer ------------------------ */
229 /* Used only for MPL115. MPL3115 supports real oversampling in the device so */
230 /* no need for any postprocessing. */
232 static _Bool avg_initialized = 0; /**< already initialized by real values */
234 typedef struct averaging_s {
235 long int * ring_buffer;
236 int ring_buffer_size;
237 long int ring_buffer_sum;
238 int ring_buffer_head;
242 static averaging_t pressure_averaging = { NULL, 0, 0L, 0 };
243 static averaging_t temperature_averaging = { NULL, 0, 0L, 0 };
247 * Create / allocate averaging buffer
249 * The buffer is initialized with zeros.
251 * @param avg pointer to ring buffer to be allocated
252 * @param size requested buffer size
254 * @return Zero when successful
256 static int averaging_create(averaging_t *avg, int size)
258 avg->ring_buffer = calloc ((size_t) size, sizeof (*avg->ring_buffer));
259 if (avg->ring_buffer == NULL)
261 ERROR ("barometer: averaging_create - ring buffer allocation of size %d failed",
266 avg->ring_buffer_size = size;
267 avg->ring_buffer_sum = 0L;
268 avg->ring_buffer_head = 0;
275 * Delete / free existing averaging buffer
277 * @param avg pointer to the ring buffer to be deleted
279 static void averaging_delete(averaging_t * avg)
281 if (avg->ring_buffer != NULL)
283 free(avg->ring_buffer);
284 avg->ring_buffer = NULL;
286 avg->ring_buffer_size = 0;
287 avg->ring_buffer_sum = 0L;
288 avg->ring_buffer_head = 0;
293 * Add new sample to the averaging buffer
295 * A new averaged value is returned. Note that till the buffer is full
296 * returned value is inaccurate as it is an average of real values and initial
299 * @param avg pointer to the ring buffer
300 * @param sample new sample value
302 * @return Averaged sample value
304 static double averaging_add_sample(averaging_t * avg, long int sample)
308 avg->ring_buffer_sum += sample - avg->ring_buffer[avg->ring_buffer_head];
309 avg->ring_buffer[avg->ring_buffer_head] = sample;
310 avg->ring_buffer_head = (avg->ring_buffer_head+1) % avg->ring_buffer_size;
311 result = (double)(avg->ring_buffer_sum) / (double)(avg->ring_buffer_size);
313 DEBUG ("barometer: averaging_add_sample - added %ld, result = %lf",
321 /* ------------------------ temperature refference ------------------------ */
324 * Linked list type of temperature sensor references
326 typedef struct temperature_list_s {
327 char * sensor_name; /**< sensor name/reference */
328 size_t num_values; /**< number of values (usually one) */
329 _Bool initialized; /**< sensor already provides data */
330 struct temperature_list_s * next; /**< next in the list */
331 } temperature_list_t;
333 static temperature_list_t * temp_list = NULL;
337 * Add new sensor to the temperature reference list
339 * @param list the list
340 * @param sensor reference name (as provided by the config file)
342 * @return Zero when successful
344 static int temp_list_add(temperature_list_t * list, const char * sensor)
346 temperature_list_t * new_temp;
348 new_temp = (temperature_list_t *) malloc(sizeof(*new_temp));
352 new_temp->sensor_name = strdup(sensor);
353 new_temp->initialized = 0;
354 new_temp->num_values = 0;
355 if(new_temp->sensor_name == NULL)
361 new_temp->next = temp_list;
362 temp_list = new_temp;
368 * Delete the whole temperature reference list
370 * @param list the list to be deleted
372 static void temp_list_delete(temperature_list_t ** list)
374 temperature_list_t * tmp;
376 while (*list != NULL)
379 (*list) = (*list)->next;
380 free(tmp->sensor_name);
388 * Get reference temperature value
390 * First initially uc_get_rate_by_name is tried. At the startup due to nondeterministic
391 * order the temperature may not be read yet (then it fails and first measurment gives
392 * only absolute air pressure reading which is acceptable). Once it succedes (should be
393 * second measurement at the latest) we use average of few last readings from
394 * uc_get_history_by_name. It may take few readings to start filling so again we use
395 * uc_get_rate_by_name as a fallback.
396 * The idea is to use basic "noise" filtering (history averaging) across all the values
397 * which given sensor provides (up to given depth). Then we get minimum among
400 * @param result where the result is stored. When not available NAN is stored.
402 * @return Zero when successful
404 static int get_reference_temperature(double * result)
406 temperature_list_t * list = temp_list;
408 gauge_t * values = NULL; /**< rate values */
409 size_t values_num = 0; /**< number of rate values */
412 gauge_t values_history[REF_TEMP_AVG_NUM];
414 double avg_sum; /**< Value sum for computing average */
415 int avg_num; /**< Number of values for computing average */
416 double average; /**< Resulting value average */
425 /* First time need to read current rate to learn how many values are
426 there (typically for temperature it would be just one).
427 We do not expect dynamic changing of number of temperarure values
428 in runtime yet (are there any such cases?). */
429 if(!list->initialized)
431 if(uc_get_rate_by_name(list->sensor_name,
435 DEBUG ("barometer: get_reference_temperature - rate \"%s\" not found yet",
441 DEBUG ("barometer: get_reference_temperature - initialize \"%s\", %zu vals",
445 list->initialized = 1;
446 list->num_values = values_num;
448 for(i=0; i<values_num; ++i)
450 DEBUG ("barometer: get_reference_temperature - rate %d: %lf **",
453 if(!isnan(values[i]))
455 avg_sum += values[i];
463 /* It is OK to get here the first time as well, in the worst case
464 the history will full of NANs. */
465 if(uc_get_history_by_name(list->sensor_name,
470 ERROR ("barometer: get_reference_temperature - history \"%s\" lost",
472 list->initialized = 0;
473 list->num_values = 0;
478 for(i=0; i<REF_TEMP_AVG_NUM*list->num_values; ++i)
480 DEBUG ("barometer: get_reference_temperature - history %d: %lf",
483 if(!isnan(values_history[i]))
485 avg_sum += values_history[i];
490 if(avg_num == 0) /* still no history? fallback to current */
492 if(uc_get_rate_by_name(list->sensor_name,
496 ERROR ("barometer: get_reference_temperature - rate \"%s\" lost",
498 list->initialized = 0;
499 list->num_values = 0;
504 for(i=0; i<values_num; ++i)
506 DEBUG ("barometer: get_reference_temperature - rate last %d: %lf **",
509 if(!isnan(values[i]))
511 avg_sum += values[i];
521 ERROR ("barometer: get_reference_temperature - could not read \"%s\"",
523 list->initialized = 0;
524 list->num_values = 0;
528 average = avg_sum / (double) avg_num;
531 else if(*result>average)
535 } /* while sensor list */
539 ERROR("barometer: get_reference_temperature - no sensor available (yet?)");
542 DEBUG ("barometer: get_reference_temperature - temp is %lf", *result);
547 /* ------------------------ MPL115 access ------------------------ */
550 * Detect presence of a MPL115 pressure sensor.
552 * Unfortunately there seems to be no ID register so we just try to read first
553 * conversion coefficient from device at MPL115 address and hope it is really
554 * MPL115. We should use this check as the last resort (which would be the typical
555 * case anyway since MPL115 is the least accurate sensor).
556 * As a sideeffect will leave set I2C slave address.
558 * @return 1 if MPL115, 0 otherwise
560 static int MPL115_detect(void)
565 if (ioctl(i2c_bus_fd, I2C_SLAVE_FORCE, MPL115_I2C_ADDRESS) < 0)
567 ERROR("barometer: MPL115_detect problem setting i2c slave address to 0x%02X: %s",
569 sstrerror (errno, errbuf, sizeof (errbuf)));
573 res = i2c_smbus_read_byte_data(i2c_bus_fd, MPL115_ADDR_COEFFS);
576 DEBUG ("barometer: MPL115_detect - positive detection");
580 DEBUG ("barometer: MPL115_detect - negative detection");
585 * Read the MPL115 sensor conversion coefficients.
587 * These are (device specific) constants so we can read them just once.
589 * @return Zero when successful
591 static int MPL115_read_coeffs(void)
593 uint8_t mpl115_coeffs[MPL115_NUM_COEFFS] = { 0 };
596 int8_t sia0MSB, sia0LSB, sib1MSB, sib1LSB, sib2MSB, sib2LSB;
597 int8_t sic12MSB, sic12LSB, sic11MSB, sic11LSB, sic22MSB, sic22LSB;
598 int16_t sia0, sib1, sib2, sic12, sic11, sic22;
602 res = i2c_smbus_read_i2c_block_data(i2c_bus_fd,
604 STATIC_ARRAY_SIZE (mpl115_coeffs),
608 ERROR ("barometer: MPL115_read_coeffs - problem reading data: %s",
609 sstrerror (errno, errbuf, sizeof (errbuf)));
613 /* Using perhaps less elegant/efficient code, but more readable. */
614 /* a0: 16total 1sign 12int 4fract 0pad */
615 sia0MSB = mpl115_coeffs[0];
616 sia0LSB = mpl115_coeffs[1];
617 sia0 = (int16_t) sia0MSB <<8; /* s16 type, Shift to MSB */
618 sia0 += (int16_t) sia0LSB & 0x00FF; /* Add LSB to 16bit number */
619 mpl115_coeffA0 = (double) (sia0);
620 mpl115_coeffA0 /= 8.0; /* 3 fract bits */
622 /* b1: 16total 1sign 2int 13fract 0pad */
623 sib1MSB= mpl115_coeffs[2];
624 sib1LSB= mpl115_coeffs[3];
625 sib1 = sib1MSB <<8; /* Shift to MSB */
626 sib1 += sib1LSB & 0x00FF; /* Add LSB to 16bit number */
627 mpl115_coeffB1 = (double) (sib1);
628 mpl115_coeffB1 /= 8192.0; /* 13 fract */
630 /* b2: 16total 1sign 1int 14fract 0pad */
631 sib2MSB= mpl115_coeffs[4];
632 sib2LSB= mpl115_coeffs[5];
633 sib2 = sib2MSB <<8; /* Shift to MSB */
634 sib2 += sib2LSB & 0x00FF; /* Add LSB to 16bit number */
635 mpl115_coeffB2 = (double) (sib2);
636 mpl115_coeffB2 /= 16384.0; /* 14 fract */
638 /* c12: 14total 1sign 0int 13fract 9pad */
639 sic12MSB= mpl115_coeffs[6];
640 sic12LSB= mpl115_coeffs[7];
641 sic12 = sic12MSB <<8; /* Shift to MSB only by 8 for MSB */
642 sic12 += sic12LSB & 0x00FF;
643 mpl115_coeffC12 = (double) (sic12);
644 mpl115_coeffC12 /= 4.0; /* 16-14=2 */
645 mpl115_coeffC12 /= 4194304.0; /* 13+9=22 fract */
647 /* c11: 11total 1sign 0int 11fract 11pad */
648 sic11MSB= mpl115_coeffs[8];
649 sic11LSB= mpl115_coeffs[9];
650 sic11 = sic11MSB <<8; /* Shift to MSB only by 8 for MSB */
651 sic11 += sic11LSB & 0x00FF;
652 mpl115_coeffC11 = (double) (sic11);
653 mpl115_coeffC11 /= 32.0; /* 16-11=5 */
654 mpl115_coeffC11 /= 4194304.0; /* 11+11=22 fract */
656 /* c12: 11total 1sign 0int 10fract 15pad */
657 sic22MSB= mpl115_coeffs[10];
658 sic22LSB= mpl115_coeffs[11];
659 sic22 = sic22MSB <<8; /* Shift to MSB only by 8 for MSB */
660 sic22 += sic22LSB & 0x00FF;
661 mpl115_coeffC22 = (double) (sic22);
662 mpl115_coeffC22 /= 32.0; //16-11=5
663 mpl115_coeffC22 /= 33554432.0; /* 10+15=25 fract */
665 DEBUG("barometer: MPL115_read_coeffs: a0=%lf, b1=%lf, b2=%lf, c12=%lf, c11=%lf, c22=%lf",
677 * Convert raw adc values to real data using the sensor coefficients.
679 * @param adc_pressure adc pressure value to be converted
680 * @param adc_temp adc temperature value to be converted
681 * @param pressure computed real pressure
682 * @param temperature computed real temperature
684 static void MPL115_convert_adc_to_real(double adc_pressure,
687 double * temperature)
690 Pcomp = mpl115_coeffA0 + \
691 (mpl115_coeffB1 + mpl115_coeffC11*adc_pressure + mpl115_coeffC12*adc_temp) * adc_pressure + \
692 (mpl115_coeffB2 + mpl115_coeffC22*adc_temp) * adc_temp;
694 *pressure = ((1150.0-500.0) * Pcomp / 1023.0) + 500.0;
695 *temperature = (472.0 - adc_temp) / 5.35 + 25.0;
696 DEBUG ("barometer: MPL115_convert_adc_to_real - got %lf hPa, %lf C",
703 * Read sensor averegaed measurements
705 * @param pressure averaged measured pressure
706 * @param temperature averaged measured temperature
708 * @return Zero when successful
710 static int MPL115_read_averaged(double * pressure, double * temperature)
712 uint8_t mpl115_conv[MPL115_NUM_CONV] = { 0 };
716 int conv_temperature;
718 double adc_temperature;
724 /* start conversion of both temp and presure */
725 retries = MPL115_CONVERSION_RETRIES;
728 /* write 1 to start conversion */
729 res = i2c_smbus_write_byte_data (i2c_bus_fd,
730 MPL115_CMD_CONVERT_BOTH,
738 ERROR ("barometer: MPL115_read_averaged - requesting conversion: %s, " \
739 "will retry at most %d more times",
740 sstrerror (errno, errbuf, sizeof (errbuf)),
745 ERROR ("barometer: MPL115_read_averaged - requesting conversion: %s, "\
746 "too many failed retries",
747 sstrerror (errno, errbuf, sizeof (errbuf)));
752 usleep (10000); /* wait 10ms for the conversion */
754 retries=MPL115_CONVERSION_RETRIES;
757 res = i2c_smbus_read_i2c_block_data(i2c_bus_fd,
759 STATIC_ARRAY_SIZE (mpl115_conv),
767 ERROR ("barometer: MPL115_read_averaged - reading conversion: %s, " \
768 "will retry at most %d more times",
769 sstrerror (errno, errbuf, sizeof (errbuf)),
774 ERROR ("barometer: MPL115_read_averaged - reading conversion: %s, " \
775 "too many failed retries",
776 sstrerror (errno, errbuf, sizeof (errbuf)));
781 conv_pressure = ((mpl115_conv[0] << 8) | mpl115_conv[1]) >> 6;
782 conv_temperature = ((mpl115_conv[2] << 8) | mpl115_conv[3]) >> 6;
783 DEBUG ("barometer: MPL115_read_averaged, raw pressure ADC value = %d, " \
784 "raw temperature ADC value = %d",
788 adc_pressure = averaging_add_sample (&pressure_averaging, conv_pressure);
789 adc_temperature = averaging_add_sample (&temperature_averaging, conv_temperature);
791 MPL115_convert_adc_to_real(adc_pressure, adc_temperature, pressure, temperature);
793 DEBUG ("barometer: MPL115_read_averaged - averaged ADC pressure = %lf / temperature = %lf, " \
794 "real pressure = %lf hPa / temperature = %lf C",
803 /* ------------------------ MPL3115 access ------------------------ */
806 * Detect presence of a MPL3115 pressure sensor by checking register "WHO AM I"
808 * As a sideeffect will leave set I2C slave address.
810 * @return 1 if MPL3115, 0 otherwise
812 static int MPL3115_detect(void)
817 if (ioctl(i2c_bus_fd, I2C_SLAVE_FORCE, MPL3115_I2C_ADDRESS) < 0)
819 ERROR("barometer: MPL3115_detect problem setting i2c slave address to 0x%02X: %s",
821 sstrerror (errno, errbuf, sizeof (errbuf)));
825 res = i2c_smbus_read_byte_data(i2c_bus_fd, MPL3115_REG_WHO_AM_I);
826 if(res == MPL3115_WHO_AM_I_RESP)
828 DEBUG ("barometer: MPL3115_detect - positive detection");
832 DEBUG ("barometer: MPL3115_detect - negative detection");
837 * Adjusts oversampling to values supported by MPL3115
839 * MPL3115 supports only power of 2 in the range 1 to 128.
841 static void MPL3115_adjust_oversampling(void)
845 if(config_oversample > 100)
848 mpl3115_oversample = MPL3115_CTRL_REG1_OST_128;
850 else if(config_oversample > 48)
853 mpl3115_oversample = MPL3115_CTRL_REG1_OST_64;
855 else if(config_oversample > 24)
858 mpl3115_oversample = MPL3115_CTRL_REG1_OST_32;
860 else if(config_oversample > 12)
863 mpl3115_oversample = MPL3115_CTRL_REG1_OST_16;
865 else if(config_oversample > 6)
868 mpl3115_oversample = MPL3115_CTRL_REG1_OST_8;
870 else if(config_oversample > 3)
873 mpl3115_oversample = MPL3115_CTRL_REG1_OST_4;
875 else if(config_oversample > 1)
878 mpl3115_oversample = MPL3115_CTRL_REG1_OST_2;
883 mpl3115_oversample = MPL3115_CTRL_REG1_OST_1;
886 DEBUG("barometer: MPL3115_adjust_oversampling - correcting oversampling from %d to %d",
889 config_oversample = new_val;
893 * Read sensor averaged measurements
895 * @param pressure averaged measured pressure
896 * @param temperature averaged measured temperature
898 * @return Zero when successful
900 static int MPL3115_read(double * pressure, double * temperature)
904 __u8 data[MPL3115_NUM_CONV_VALS];
905 long int tmp_value = 0;
908 /* Set Active - activate the device from standby */
909 res = i2c_smbus_read_byte_data(i2c_bus_fd, MPL3115_REG_CTRL_REG1);
912 ERROR ("barometer: MPL3115_read - cannot read CTRL_REG1: %s",
913 sstrerror (errno, errbuf, sizeof (errbuf)));
917 res = i2c_smbus_write_byte_data(i2c_bus_fd,
918 MPL3115_REG_CTRL_REG1,
919 ctrl | MPL3115_CTRL_REG1_SBYB);
922 ERROR ("barometer: MPL3115_read - problem activating: %s",
923 sstrerror (errno, errbuf, sizeof (errbuf)));
927 /* base sleep is 5ms x OST */
928 usleep(5000 * config_oversample);
930 /* check the flags/status if ready */
931 res = i2c_smbus_read_byte_data(i2c_bus_fd, MPL3115_REG_STATUS);
934 ERROR ("barometer: MPL3115_read - cannot read status register: %s",
935 sstrerror (errno, errbuf, sizeof (errbuf)));
939 while ((res & MPL3115_DR_STATUS_DR) != MPL3115_DR_STATUS_DR)
941 /* try some extra sleep... */
944 /* ... and repeat the check. The conversion has to finish sooner or later. */
945 res = i2c_smbus_read_byte_data(i2c_bus_fd, MPL3115_REG_STATUS);
948 ERROR ("barometer: MPL3115_read - cannot read status register: %s",
949 sstrerror (errno, errbuf, sizeof (errbuf)));
954 /* Now read all the data in one block. There is address autoincrement. */
955 res = i2c_smbus_read_i2c_block_data(i2c_bus_fd,
956 MPL3115_REG_OUT_P_MSB,
957 MPL3115_NUM_CONV_VALS,
961 ERROR ("barometer: MPL3115_read - cannot read data registers: %s",
962 sstrerror (errno, errbuf, sizeof (errbuf)));
966 tmp_value = (data[0] << 16) | (data[1] << 8) | data[2];
967 *pressure = ((double) tmp_value) / 4.0 / 16.0 / 100.0;
968 DEBUG ("barometer: MPL3115_read - absolute pressure = %lf hPa", *pressure);
972 data[3] = ~data[3] + 1;
973 *temperature = data[3];
974 *temperature = - *temperature;
978 *temperature = data[3];
981 *temperature += (double)(data[4]) / 256.0;
982 DEBUG ("barometer: MPL3115_read - temperature = %lf C", *temperature);
988 * Initialize MPL3115 for barometeric measurements
990 * @return 0 if successful
992 static int MPL3115_init_sensor(void)
998 /* Reset the sensor. It will reset immediately without ACKing */
999 /* the transaction, so no error handling here. */
1000 i2c_smbus_write_byte_data(i2c_bus_fd,
1001 MPL3115_REG_CTRL_REG1,
1002 MPL3115_CTRL_REG1_RST);
1004 /* wait some time for the reset to finish */
1007 /* now it should be in standby already so we can go and configure it */
1009 /* Set temperature offset. */
1010 /* result = ADCtemp + offset [C] */
1011 offset = (__s8) (config_temp_offset * 16.0);
1012 res = i2c_smbus_write_byte_data(i2c_bus_fd, MPL3115_REG_OFF_T, offset);
1015 ERROR ("barometer: MPL3115_init_sensor - problem setting temp offset: %s",
1016 sstrerror (errno, errbuf, sizeof (errbuf)));
1020 /* Set pressure offset. */
1021 /* result = ADCpress + offset [hPa] */
1022 offset = (__s8) (config_press_offset * 100.0 / 4.0);
1023 res = i2c_smbus_write_byte_data(i2c_bus_fd, MPL3115_REG_OFF_P, offset);
1026 ERROR ("barometer: MPL3115_init_sensor - problem setting pressure offset: %s",
1027 sstrerror (errno, errbuf, sizeof (errbuf)));
1031 /* Enable Data Flags in PT_DATA_CFG - flags on both pressure and temp */
1032 res = i2c_smbus_write_byte_data(i2c_bus_fd,
1033 MPL3115_REG_PT_DATA_CFG,
1034 MPL3115_PT_DATA_DREM \
1035 | MPL3115_PT_DATA_PDEF \
1036 | MPL3115_PT_DATA_TDEF);
1039 ERROR ("barometer: MPL3115_init_sensor - problem setting PT_DATA_CFG: %s",
1040 sstrerror (errno, errbuf, sizeof (errbuf)));
1044 /* Set to barometer with an OSR */
1045 res = i2c_smbus_write_byte_data(i2c_bus_fd,
1046 MPL3115_REG_CTRL_REG1,
1047 mpl3115_oversample);
1050 ERROR ("barometer: MPL3115_init_sensor - problem configuring CTRL_REG1: %s",
1051 sstrerror (errno, errbuf, sizeof (errbuf)));
1058 /* ------------------------ BMP085 access ------------------------ */
1061 * Detect presence of a BMP085 pressure sensor by checking its ID register
1063 * As a sideeffect will leave set I2C slave address.
1065 * @return 1 if BMP085, 0 otherwise
1067 static int BMP085_detect(void)
1072 if (ioctl(i2c_bus_fd, I2C_SLAVE_FORCE, BMP085_I2C_ADDRESS) < 0)
1074 ERROR("barometer: BMP085_detect - problem setting i2c slave address to 0x%02X: %s",
1076 sstrerror (errno, errbuf, sizeof (errbuf)));
1080 res = i2c_smbus_read_byte_data(i2c_bus_fd, BMP085_ADDR_ID_REG);
1081 if(res == BMP085_CHIP_ID)
1083 DEBUG ("barometer: BMP085_detect - positive detection");
1086 res = i2c_smbus_read_byte_data(i2c_bus_fd, BMP085_ADDR_VERSION );
1089 ERROR("barometer: BMP085_detect - problem checking chip version: %s",
1090 sstrerror (errno, errbuf, sizeof (errbuf)));
1093 DEBUG ("barometer: BMP085_detect - chip version ML:0x%02X AL:0x%02X",
1099 DEBUG ("barometer: BMP085_detect - negative detection");
1105 * Adjusts oversampling settings to values supported by BMP085
1107 * BMP085 supports only 1,2,4 or 8 samples.
1109 static void BMP085_adjust_oversampling(void)
1113 if( config_oversample > 6 ) /* 8 */
1116 bmp085_oversampling = 3;
1117 bmp085_cmdCnvPress = BMP085_CMD_CONVERT_PRESS_3;
1118 bmp085_timeCnvPress = BMP085_TIME_CNV_PRESS_3;
1120 else if( config_oversample > 3 ) /* 4 */
1123 bmp085_oversampling = 2;
1124 bmp085_cmdCnvPress = BMP085_CMD_CONVERT_PRESS_2;
1125 bmp085_timeCnvPress = BMP085_TIME_CNV_PRESS_2;
1127 else if( config_oversample > 1 ) /* 2 */
1130 bmp085_oversampling = 1;
1131 bmp085_cmdCnvPress = BMP085_CMD_CONVERT_PRESS_1;
1132 bmp085_timeCnvPress = BMP085_TIME_CNV_PRESS_1;
1137 bmp085_oversampling = 0;
1138 bmp085_cmdCnvPress = BMP085_CMD_CONVERT_PRESS_0;
1139 bmp085_timeCnvPress = BMP085_TIME_CNV_PRESS_0;
1142 DEBUG("barometer: BMP085_adjust_oversampling - correcting oversampling from %d to %d",
1145 config_oversample = new_val;
1150 * Read the BMP085 sensor conversion coefficients.
1152 * These are (device specific) constants so we can read them just once.
1154 * @return Zero when successful
1156 static int BMP085_read_coeffs(void)
1159 __u8 coeffs[BMP085_NUM_COEFFS];
1162 res = i2c_smbus_read_i2c_block_data(i2c_bus_fd,
1168 ERROR ("barometer: BMP085_read_coeffs - problem reading data: %s",
1169 sstrerror (errno, errbuf, sizeof (errbuf)));
1173 bmp085_AC1 = ((int16_t) coeffs[0] <<8) | (int16_t) coeffs[1];
1174 bmp085_AC2 = ((int16_t) coeffs[2] <<8) | (int16_t) coeffs[3];
1175 bmp085_AC3 = ((int16_t) coeffs[4] <<8) | (int16_t) coeffs[5];
1176 bmp085_AC4 = ((uint16_t) coeffs[6] <<8) | (uint16_t) coeffs[7];
1177 bmp085_AC5 = ((uint16_t) coeffs[8] <<8) | (uint16_t) coeffs[9];
1178 bmp085_AC6 = ((uint16_t) coeffs[10] <<8) | (uint16_t) coeffs[11];
1179 bmp085_B1 = ((int16_t) coeffs[12] <<8) | (int16_t) coeffs[13];
1180 bmp085_B2 = ((int16_t) coeffs[14] <<8) | (int16_t) coeffs[15];
1181 bmp085_MB = ((int16_t) coeffs[16] <<8) | (int16_t) coeffs[17];
1182 bmp085_MC = ((int16_t) coeffs[18] <<8) | (int16_t) coeffs[19];
1183 bmp085_MD = ((int16_t) coeffs[20] <<8) | (int16_t) coeffs[21];
1185 DEBUG("barometer: BMP085_read_coeffs - AC1=%d, AC2=%d, AC3=%d, AC4=%u,"\
1186 " AC5=%u, AC6=%u, B1=%d, B2=%d, MB=%d, MC=%d, MD=%d",
1204 * Convert raw BMP085 adc values to real data using the sensor coefficients.
1206 * @param adc_pressure adc pressure value to be converted
1207 * @param adc_temp adc temperature value to be converted
1208 * @param pressure computed real pressure
1209 * @param temperature computed real temperature
1211 static void BMP085_convert_adc_to_real(long adc_pressure,
1212 long adc_temperature,
1214 double * temperature)
1219 unsigned long B4, B7;
1225 /* calculate real temperature */
1226 X1 = ( (adc_temperature - bmp085_AC6) * bmp085_AC5) >> 15;
1227 X2 = (bmp085_MC << 11) / (X1 + bmp085_MD);
1232 *temperature = (double)T * 0.1;
1234 /* calculate real pressure */
1235 /* in general X1, X2, X3 are recycled while values of B3, B4, B5, B6 are kept */
1239 X1 = ((bmp085_B2 * ((B6 * B6)>>12)) >> 11 );
1240 X2 = (((long)bmp085_AC2 * B6) >> 11);
1242 B3 = (((((long)bmp085_AC1 * 4) + X3) << bmp085_oversampling) + 2) >> 2;
1245 X1 = (((long)bmp085_AC3*B6) >> 13);
1246 X2 = (bmp085_B1*((B6*B6) >> 12) ) >> 16;
1247 X3 = ((X1 + X2) + 2 ) >> 2;
1248 B4 = ((long)bmp085_AC4* (unsigned long)(X3 + 32768)) >> 15;
1251 B7 = (unsigned long)(adc_pressure - B3)*(50000>>bmp085_oversampling);
1252 if( B7 < 0x80000000 )
1260 X1 = (P >> 8) * (P >> 8);
1261 X1 = (X1 * 3038) >> 16;
1262 X2 = ((-7357) * P) >> 16;
1263 P = P + ( ( X1 + X2 + 3791 ) >> 4);
1265 *pressure = P / 100.0; // in [hPa]
1266 DEBUG ("barometer: BMP085_convert_adc_to_real - got %lf hPa, %lf C",
1273 * Read compensated sensor measurements
1275 * @param pressure averaged measured pressure
1276 * @param temperature averaged measured temperature
1278 * @return Zero when successful
1280 static int BMP085_read(double * pressure, double * temperature)
1286 long adc_temperature;
1290 /* start conversion of temperature */
1291 res = i2c_smbus_write_byte_data( i2c_bus_fd,
1292 BMP085_ADDR_CTRL_REG,
1293 BMP085_CMD_CONVERT_TEMP );
1296 ERROR ("barometer: BMP085_read - problem requesting temperature conversion: %s",
1297 sstrerror (errno, errbuf, sizeof (errbuf)));
1301 usleep(BMP085_TIME_CNV_TEMP); /* wait for the conversion */
1303 res = i2c_smbus_read_i2c_block_data( i2c_bus_fd,
1309 ERROR ("barometer: BMP085_read - problem reading temperature data: %s",
1310 sstrerror (errno, errbuf, sizeof (errbuf)));
1314 adc_temperature = ( (unsigned short)measBuff[0] << 8 ) + measBuff[1];
1318 res = i2c_smbus_write_byte_data( i2c_bus_fd,
1319 BMP085_ADDR_CTRL_REG,
1320 bmp085_cmdCnvPress );
1323 ERROR ("barometer: BMP085_read - problem requesting pressure conversion: %s",
1324 sstrerror (errno, errbuf, sizeof (errbuf)));
1328 usleep(bmp085_timeCnvPress); /* wait for the conversion */
1330 res = i2c_smbus_read_i2c_block_data( i2c_bus_fd,
1336 ERROR ("barometer: BMP085_read - problem reading pressure data: %s",
1337 sstrerror (errno, errbuf, sizeof (errbuf)));
1341 adc_pressure = (long)((((ulong)measBuff[0]<<16) | ((ulong)measBuff[1]<<8) | (ulong)measBuff[2] ) >> (8 - bmp085_oversampling));
1344 DEBUG ("barometer: BMP085_read - raw pressure ADC value = %ld, " \
1345 "raw temperature ADC value = %ld",
1349 BMP085_convert_adc_to_real(adc_pressure, adc_temperature, pressure, temperature);
1356 /* ------------------------ Sensor detection ------------------------ */
1358 * Detect presence of a supported sensor.
1360 * As a sideeffect will leave set I2C slave address.
1361 * The detection is done in the order BMP085, MPL3115, MPL115 and stops after
1362 * first sensor beeing found.
1364 * @return detected sensor type
1366 static enum Sensor_type detect_sensor_type(void)
1369 return Sensor_BMP085;
1371 else if(MPL3115_detect())
1372 return Sensor_MPL3115;
1374 else if(MPL115_detect())
1375 return Sensor_MPL115;
1380 /* ------------------------ Common functionality ------------------------ */
1383 * Convert absolute pressure (in hPa) to mean sea level pressure
1385 * Implemented methods are:
1386 * - MSLP_NONE - no converions, returns absolute pressure
1388 * - MSLP_INTERNATIONAL - see http://en.wikipedia.org/wiki/Atmospheric_pressure#Altitude_atmospheric_pressure_variation
1389 * Requires #config_altitude
1391 * - MSLP_DEU_WETT - formula as recommended by the Deutsche Wetterdienst. See
1392 * http://de.wikipedia.org/wiki/Barometrische_H%C3%B6henformel#Theorie
1393 * Requires both #config_altitude and temperature reference(s).
1395 * @param abs_pressure absloute pressure to be converted
1397 * @return mean sea level pressure if successful, NAN otherwise
1399 static double abs_to_mean_sea_level_pressure(double abs_pressure)
1405 if (config_normalize >= MSLP_DEU_WETT)
1407 result = get_reference_temperature(&temp);
1414 switch(config_normalize)
1417 mean = abs_pressure;
1420 case MSLP_INTERNATIONAL:
1421 mean = abs_pressure / \
1422 pow(1.0 - 0.0065*config_altitude/288.15, 9.80665*0.0289644/(8.31447*0.0065));
1427 double E; /* humidity */
1430 E = 5.6402 * (-0.0916 + exp(0.06*temp) );
1432 E = 18.2194 * (1.0463 - exp(-0.0666*temp) );
1433 x = 9.80665 / (287.05 * (temp+273.15 + 0.12*E + 0.0065*config_altitude/2)) * config_altitude;
1434 mean = abs_pressure * exp(x);
1439 ERROR ("barometer: abs_to_mean_sea_level_pressure: wrong conversion method %d",
1441 mean = abs_pressure;
1445 DEBUG ("barometer: abs_to_mean_sea_level_pressure: absPressure = %lf hPa, method = %d, meanPressure = %lf hPa",
1453 /* ------------------------ main plugin callbacks ------------------------ */
1456 * Main plugin configuration callback (using simple config)
1458 * @param key configuration key we should process
1459 * @param value configuration value we should process
1461 * @return Zero when successful.
1463 static int collectd_barometer_config (const char *key, const char *value)
1465 DEBUG("barometer: collectd_barometer_config");
1467 if (strcasecmp (key, "Device") == 0)
1469 sfree (config_device);
1470 config_device = strdup (value);
1472 else if (strcasecmp (key, "Oversampling") == 0)
1474 int oversampling_tmp = atoi (value);
1475 if (oversampling_tmp < 1 || oversampling_tmp > 1024)
1477 WARNING ("barometer: collectd_barometer_config: invalid oversampling: %d." \
1478 " Allowed values are 1 to 1024 (for MPL115) or 1 to 128 (for MPL3115) or 1 to 8 (for BMP085).",
1482 config_oversample = oversampling_tmp;
1484 else if (strcasecmp (key, "Altitude") == 0)
1486 config_altitude = atof (value);
1488 else if (strcasecmp (key, "Normalization") == 0)
1490 int normalize_tmp = atoi (value);
1491 if (normalize_tmp < 0 || normalize_tmp > 2)
1493 WARNING ("barometer: collectd_barometer_config: invalid normalization: %d",
1497 config_normalize = normalize_tmp;
1499 else if (strcasecmp (key, "TemperatureSensor") == 0)
1501 if(temp_list_add(temp_list, value))
1506 else if (strcasecmp (key, "PressureOffset") == 0)
1508 config_press_offset = atof(value);
1510 else if (strcasecmp (key, "TemperatureOffset") == 0)
1512 config_temp_offset = atof(value);
1524 * Shutdown callback.
1526 * Close I2C and delete all the buffers.
1528 * @return Zero when successful (at the moment the only possible outcome)
1530 static int collectd_barometer_shutdown(void)
1532 DEBUG ("barometer: collectd_barometer_shutdown");
1534 if(sensor_type == Sensor_MPL115)
1536 averaging_delete (&pressure_averaging);
1537 averaging_delete (&temperature_averaging);
1539 temp_list_delete(&temp_list);
1546 sfree (config_device);
1554 * Plugin read callback for MPL115.
1556 * Dispatching will create values:
1557 * - <hostname>/barometer-mpl115/pressure-normalized
1558 * - <hostname>/barometer-mpl115/pressure-absolute
1559 * - <hostname>/barometer-mpl115/temperature
1561 * @return Zero when successful.
1563 static int MPL115_collectd_barometer_read (void)
1567 double pressure = 0.0;
1568 double temperature = 0.0;
1569 double norm_pressure = 0.0;
1571 value_list_t vl = VALUE_LIST_INIT;
1574 DEBUG("barometer: MPL115_collectd_barometer_read");
1581 /* Rather than delaying init, we will intitialize during first read. This
1582 way at least we have a better chance to have the reference temperature
1583 already available. */
1584 if(!avg_initialized)
1587 for(i=0; i<config_oversample-1; ++i)
1589 result = MPL115_read_averaged(&pressure, &temperature);
1592 ERROR ("barometer: MPL115_collectd_barometer_read - mpl115 read, ignored during init");
1594 DEBUG("barometer: MPL115_collectd_barometer_read - init %d / %d", i+1, config_oversample-1);
1597 avg_initialized = 1;
1600 result = MPL115_read_averaged(&pressure, &temperature);
1604 norm_pressure = abs_to_mean_sea_level_pressure(pressure);
1606 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
1607 sstrncpy (vl.plugin, "barometer", sizeof (vl.plugin));
1608 sstrncpy (vl.plugin_instance, "mpl115", sizeof (vl.plugin_instance));
1613 /* dispatch normalized air pressure */
1614 sstrncpy (vl.type, "pressure", sizeof (vl.type));
1615 sstrncpy (vl.type_instance, "normalized", sizeof (vl.type_instance));
1616 values[0].gauge = norm_pressure;
1617 plugin_dispatch_values (&vl);
1619 /* dispatch absolute air pressure */
1620 sstrncpy (vl.type, "pressure", sizeof (vl.type));
1621 sstrncpy (vl.type_instance, "absolute", sizeof (vl.type_instance));
1622 values[0].gauge = pressure;
1623 plugin_dispatch_values (&vl);
1625 /* dispatch sensor temperature */
1626 sstrncpy (vl.type, "temperature", sizeof (vl.type));
1627 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
1628 values[0].gauge = temperature;
1629 plugin_dispatch_values (&vl);
1636 * Plugin read callback for MPL3115.
1638 * Dispatching will create values:
1639 * - <hostname>/barometer-mpl3115/pressure-normalized
1640 * - <hostname>/barometer-mpl3115/pressure-absolute
1641 * - <hostname>/barometer-mpl3115/temperature
1643 * @return Zero when successful.
1645 static int MPL3115_collectd_barometer_read (void)
1649 double pressure = 0.0;
1650 double temperature = 0.0;
1651 double norm_pressure = 0.0;
1653 value_list_t vl = VALUE_LIST_INIT;
1656 DEBUG("barometer: MPL3115_collectd_barometer_read");
1663 result = MPL3115_read(&pressure, &temperature);
1667 norm_pressure = abs_to_mean_sea_level_pressure(pressure);
1669 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
1670 sstrncpy (vl.plugin, "barometer", sizeof (vl.plugin));
1671 sstrncpy (vl.plugin_instance, "mpl3115", sizeof (vl.plugin_instance));
1676 /* dispatch normalized air pressure */
1677 sstrncpy (vl.type, "pressure", sizeof (vl.type));
1678 sstrncpy (vl.type_instance, "normalized", sizeof (vl.type_instance));
1679 values[0].gauge = norm_pressure;
1680 plugin_dispatch_values (&vl);
1682 /* dispatch absolute air pressure */
1683 sstrncpy (vl.type, "pressure", sizeof (vl.type));
1684 sstrncpy (vl.type_instance, "absolute", sizeof (vl.type_instance));
1685 values[0].gauge = pressure;
1686 plugin_dispatch_values (&vl);
1688 /* dispatch sensor temperature */
1689 sstrncpy (vl.type, "temperature", sizeof (vl.type));
1690 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
1691 values[0].gauge = temperature;
1692 plugin_dispatch_values (&vl);
1699 * Plugin read callback for BMP085.
1701 * Dispatching will create values:
1702 * - <hostname>/barometer-bmp085/pressure-normalized
1703 * - <hostname>/barometer-bmp085/pressure-absolute
1704 * - <hostname>/barometer-bmp085/temperature
1706 * @return Zero when successful.
1708 static int BMP085_collectd_barometer_read (void)
1712 double pressure = 0.0;
1713 double temperature = 0.0;
1714 double norm_pressure = 0.0;
1716 value_list_t vl = VALUE_LIST_INIT;
1719 DEBUG("barometer: BMP085_collectd_barometer_read");
1726 result = BMP085_read(&pressure, &temperature);
1730 norm_pressure = abs_to_mean_sea_level_pressure(pressure);
1732 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
1733 sstrncpy (vl.plugin, "barometer", sizeof (vl.plugin));
1734 sstrncpy (vl.plugin_instance, "bmp085", sizeof (vl.plugin_instance));
1739 /* dispatch normalized air pressure */
1740 sstrncpy (vl.type, "pressure", sizeof (vl.type));
1741 sstrncpy (vl.type_instance, "normalized", sizeof (vl.type_instance));
1742 values[0].gauge = norm_pressure;
1743 plugin_dispatch_values (&vl);
1745 /* dispatch absolute air pressure */
1746 sstrncpy (vl.type, "pressure", sizeof (vl.type));
1747 sstrncpy (vl.type_instance, "absolute", sizeof (vl.type_instance));
1748 values[0].gauge = pressure;
1749 plugin_dispatch_values (&vl);
1751 /* dispatch sensor temperature */
1752 sstrncpy (vl.type, "temperature", sizeof (vl.type));
1753 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
1754 values[0].gauge = temperature;
1755 plugin_dispatch_values (&vl);
1762 * Initialization callback
1764 * Check config, initialize I2C bus access, conversion coefficients and averaging
1767 * @return Zero when successful.
1769 static int collectd_barometer_init (void)
1773 DEBUG ("barometer: collectd_barometer_init");
1775 if (config_device == NULL)
1777 ERROR("barometer: collectd_barometer_init I2C bus device not configured");
1781 if (config_normalize >= MSLP_INTERNATIONAL && isnan(config_altitude))
1783 ERROR("barometer: collectd_barometer_init no altitude configured " \
1784 "for mean sea level pressure normalization.");
1788 if (config_normalize == MSLP_DEU_WETT
1792 ERROR("barometer: collectd_barometer_init no temperature reference "\
1793 "configured for mean sea level pressure normalization.");
1798 i2c_bus_fd = open(config_device, O_RDWR);
1801 ERROR ("barometer: collectd_barometer_init problem opening I2C bus device \"%s\": %s (is loaded mod i2c-dev?)",
1803 sstrerror (errno, errbuf, sizeof (errbuf)));
1807 /* detect sensor type - this will also set slave address */
1808 sensor_type = detect_sensor_type();
1810 /* init correct sensor type */
1814 case Sensor_MPL3115:
1816 MPL3115_adjust_oversampling();
1818 if(MPL3115_init_sensor())
1821 plugin_register_read ("barometer", MPL3115_collectd_barometer_read);
1828 if (averaging_create (&pressure_averaging, config_oversample))
1830 ERROR("barometer: collectd_barometer_init pressure averaging init failed");
1834 if (averaging_create (&temperature_averaging, config_oversample))
1836 ERROR("barometer: collectd_barometer_init temperature averaging init failed");
1840 if (MPL115_read_coeffs() < 0)
1843 plugin_register_read ("barometer", MPL115_collectd_barometer_read);
1850 BMP085_adjust_oversampling();
1852 if (BMP085_read_coeffs() < 0)
1855 plugin_register_read ("barometer", BMP085_collectd_barometer_read);
1859 /* anything else -> error */
1861 ERROR("barometer: collectd_barometer_init - no supported sensor found");
1870 /* ------------------------ plugin register / entry point ------------------------ */
1873 * Plugin "entry" - register all callback.
1876 void module_register (void)
1878 plugin_register_config ("barometer",
1879 collectd_barometer_config,
1882 plugin_register_init ("barometer", collectd_barometer_init);
1883 plugin_register_shutdown ("barometer", collectd_barometer_shutdown);