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