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