ESPHome 2026.6.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
6static const char *const 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 uint8_t checksum = 0;
46 for (uint8_t i : data) {
47 checksum += i;
48 }
49
50 if (checksum != 0) {
51 this->status_set_warning();
52 ESP_LOGW(TAG, "Checksum failure when reading from FS3000");
53 return;
54 }
55
56 // raw value information is 12 bits
57 uint16_t raw_value = (data[1] << 8) | data[2];
58 ESP_LOGV(TAG, "Got raw reading=%i", raw_value);
59
60 // convert and publish the raw value into m/s using the table of data points in the datasheet
61 this->publish_state(fit_raw_(raw_value));
62
64}
65
67 ESP_LOGCONFIG(TAG, "FS3000:");
68 LOG_I2C_DEVICE(this);
69 LOG_UPDATE_INTERVAL(this);
70 LOG_SENSOR(" ", "Air Velocity", this);
71}
72
73float FS3000Component::fit_raw_(uint16_t raw_value) {
74 // converts a raw value read from the FS3000 into a speed in m/s based on the
75 // reference data points given in the datasheet
76 // fits raw reading using a linear interpolation between each data point
77
78 uint8_t end = 8; // assume model 1005, which has 9 data points
79 if (this->model_ == FIFTEEN)
80 end = 12; // model 1015 has 13 data points
81
82 if (raw_value <= this->raw_data_points_[0]) { // less than smallest data point returns first data point
83 return this->mps_data_points_[0];
84 } else if (raw_value >= this->raw_data_points_[end]) { // greater than largest data point returns max speed
85 return this->mps_data_points_[end];
86 } else {
87 uint8_t i = 0;
88
89 // determine between which data points does the reading fall, i-1 and i
90 while (raw_value > this->raw_data_points_[i]) {
91 ++i;
92 }
93
94 // calculate the slope of the secant line between the two data points that surrounds the reading
95 float slope = (this->mps_data_points_[i] - this->mps_data_points_[i - 1]) /
96 (this->raw_data_points_[i] - this->raw_data_points_[i - 1]);
97
98 // return the interpolated value for the reading
99 return (float(raw_value - this->raw_data_points_[i - 1])) * slope + this->mps_data_points_[i - 1];
100 }
101}
102
103} // namespace esphome::fs3000
uint8_t checksum
Definition bl0906.h:3
void status_clear_warning()
Definition component.h:289
float fit_raw_(uint16_t raw_value)
Definition fs3000.cpp:73
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:230
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint8_t end[39]
Definition sun_gtil2.cpp:17