esp8266_web_settings beta
VindriktningAirQuality.h
1/*
2 * Copyright (c) 2021, 2022 G. R. McDorman
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#pragma once
24
25#include <SoftwareSerial.h>
26
27#include "grmcdorman/device/Device.h"
28#include "grmcdorman/device/Accumulator.h"
29
30namespace grmcdorman::device
31{
61 {
62 public:
64
65 void setup() override;
66 void loop() override;
67 bool publish(DynamicJsonDocument &json) const override;
68 DynamicJsonDocument as_json() const override;
69
80 virtual String get_status() const;
81
90 uint16_t get_pm25() const
91 {
92 return pm25.get_last_reading();
93 }
94 private:
95 static constexpr uint16_t vindriktning_message_size = 20;
104 void parse(const uint8_t *data);
112 bool is_valid_checksum(const uint8_t *data) const;
122 const uint8_t *find_message(const uint8_t *data, uint8_t byte_count) const;
123
128 enum class State
129 {
130 NEVER_READ,
131 NO_HEADER_FOUND,
132 READ
133 };
134 NoteSetting title;
135 ExclusiveOptionSetting serialDataPin;
136 InfoSettingHtml device_status;
137
138 ::grmcdorman::SettingInterface::settings_list_t settings;
139
140 SoftwareSerial sensorSerial;
141 uint32_t last_read_millis = 0;
142 State last_read_state = State::NEVER_READ;
143 static constexpr uint16_t buffer_size = 2 * vindriktning_message_size;
144 uint8_t buffer[buffer_size];
145 uint32_t rxBufIdx = 0;
146
147 static constexpr uint8_t HEADER_BYTE_0 = 0x16;
148 static constexpr uint8_t HEADER_BYTE_1 = 0x11;
149 static constexpr uint8_t HEADER_BYTE_2 = 0x0B;
150
151 Accumulator<uint32_t, 5> pm25;
152 };
153}
The generic device interface.
Definition: Device.h:46
This class supports the Ikea Vindriktning air quality sensor as a device.
Definition: VindriktningAirQuality.h:61
uint16_t get_pm25() const
Get the last PM 2.5 reading.
Definition: VindriktningAirQuality.h:90
void loop() override
Main loop.
Definition: VindriktningAirQuality.cpp:96
bool publish(DynamicJsonDocument &json) const override
Publish the value and attributes.
Definition: VindriktningAirQuality.cpp:179
virtual String get_status() const
Get a status report.
Definition: VindriktningAirQuality.cpp:203
void setup() override
Setup the device.
Definition: VindriktningAirQuality.cpp:88
DynamicJsonDocument as_json() const override
Get the values, as a JSON document.
Definition: VindriktningAirQuality.cpp:191