ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
fs3000.cpp
Go to the documentation of this file.
1#include "fs3000.h"
2#include "esphome/core/log.h"
4
5namespace esphome::fs3000 {
6
7ESPHOME_LOG_TAG(TAG, "fs3000");
8
10 if (model_ == FIVE) {
11 // datasheet gives 9 points to interpolate from for the 1005 model
12 static const uint16_t RAW_DATA_POINTS_1005[9] = {409, 915, 1522, 2066, 2523, 2908, 3256, 3572, 3686};
13 static const float MPS_DATA_POINTS_1005[9] = {0.0, 1.07, 2.01, 3.0, 3.97, 4.96, 5.98, 6.99, 7.23};
14
15 std::copy(RAW_DATA_POINTS_1005, RAW_DATA_POINTS_1005 + 9, this->raw_data_points_);
16 std::copy(MPS_DATA_POINTS_1005, MPS_DATA_POINTS_1005 + 9, this->mps_data_points_);
17 } else if (model_ == FIFTEEN) {
18 // datasheet gives 13 points to extrapolate from for the 1015 model
19 static const uint16_t RAW_DATA_POINTS_1015[13] = {409, 1203, 1597, 1908, 2187, 2400, 2629,
20 2801, 3006, 3178, 3309, 3563, 3686};
21 static const float MPS_DATA_POINTS_1015[13] = {0.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 13.0, 15.0};
22
23 std::copy(RAW_DATA_POINTS_1015, RAW_DATA_POINTS_1015 + 13, this->raw_data_points_);
24 std::copy(MPS_DATA_POINTS_1015, MPS_DATA_POINTS_1015 + 13, this->mps_data_points_);
25 }
26}
27
29 // 5 bytes of data read from fs3000 sensor
30 // byte 1 - checksum
31 // byte 2 - (lower 4 bits) high byte of sensor reading
32 // byte 3 - (8 bits) low byte of sensor reading
33 // byte 4 - generic checksum data
34 // byte 5 - generic checksum data
35
36 uint8_t data[5];
37
38 if (!this->read_bytes_raw(data, 5)) {
39 this->status_set_warning();
40 ESP_LOGW(TAG, "Error reading data from FS3000");
41 this->publish_state(NAN);
42 return;
43 }
44
45 // checksum passes if the modulo 256 sum of the five bytes is 0
46 if (sum_bytes(data, sizeof(data)) != 0) {
47 this->status_set_warning();
48 ESP_LOGW(TAG, "Checksum failure when reading from FS3000");
49 return;
50 }
51
52 // raw value information is 12 bits
53 uint16_t raw_value = encode_uint16(data[1], data[2]);
54 ESP_LOGV(TAG, "Got raw reading=%i", raw_value);
55
56 // convert and publish the raw value into m/s using the table of data points in the datasheet
57 this->publish_state(fit_raw_(raw_value));
58
60}
61
63 ESP_LOGCONFIG(TAG, "FS3000:");
64 LOG_I2C_DEVICE(this);
65 LOG_UPDATE_INTERVAL(this);
66 LOG_SENSOR(" ", "Air Velocity", this);
67}
68
69float FS3000Component::fit_raw_(uint16_t raw_value) {
70 // converts a raw value read from the FS3000 into a speed in m/s based on the
71 // reference data points given in the datasheet
72 // fits raw reading using a linear interpolation between each data point
73
74 uint8_t end = 8; // assume model 1005, which has 9 data points
75 if (this->model_ == FIFTEEN)
76 end = 12; // model 1015 has 13 data points
77
78 if (raw_value <= this->raw_data_points_[0]) { // less than smallest data point returns first data point
79 return this->mps_data_points_[0];
80 } else if (raw_value >= this->raw_data_points_[end]) { // greater than largest data point returns max speed
81 return this->mps_data_points_[end];
82 } else {
83 uint8_t i = 0;
84
85 // determine between which data points does the reading fall, i-1 and i
86 while (raw_value > this->raw_data_points_[i]) {
87 ++i;
88 }
89
90 // calculate the slope of the secant line between the two data points that surrounds the reading
91 float slope = (this->mps_data_points_[i] - this->mps_data_points_[i - 1]) /
92 (this->raw_data_points_[i] - this->raw_data_points_[i - 1]);
93
94 // return the interpolated value for the reading
95 return (float(raw_value - this->raw_data_points_[i - 1])) * slope + this->mps_data_points_[i - 1];
96 }
97}
98
99} // namespace esphome::fs3000
void status_clear_warning()
Definition component.h:309
float fit_raw_(uint16_t raw_value)
Definition fs3000.cpp:69
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:262
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ESPHOME_LOG_TAG(TAG, "fs3000")
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Definition helpers.h:939
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
uint8_t end[39]
Definition sun_gtil2.cpp:17