esp8266_web_settings beta
Public Member Functions | List of all members
grmcdorman::device::VindriktningAirQuality Class Reference

This class supports the Ikea Vindriktning air quality sensor as a device. More...

#include <grmcdorman/device/VindriktningAirQuality.h>

Inheritance diagram for grmcdorman::device::VindriktningAirQuality:
grmcdorman::device::Device

Public Member Functions

void setup () override
 Setup the device. More...
 
void loop () override
 Main loop. More...
 
bool publish (DynamicJsonDocument &json) const override
 Publish the value and attributes. More...
 
DynamicJsonDocument as_json () const override
 Get the values, as a JSON document. More...
 
virtual String get_status () const
 Get a status report. More...
 
uint16_t get_pm25 () const
 Get the last PM 2.5 reading. More...
 
- Public Member Functions inherited from grmcdorman::device::Device
 Device (const __FlashStringHelper *device_name, const __FlashStringHelper *device_identifier)
 Construct a new Device object. More...
 
virtual ~Device ()
 Destroy the Device object.
 
const __FlashStringHelper * name () const
 Get the device name; used for UI names and IDs. More...
 
virtual const __FlashStringHelper * identifier () const
 Get the device identifier. More...
 
virtual void set_defaults ()
 Set defaults, if necessary. More...
 
virtual void set_devices (const std::vector< Device * > &list)
 For devices that support it, add devices to manage. More...
 
const definition_list_tget_definitions () const
 Get the definitions list. More...
 
const ::grmcdorman::SettingInterface::settings_list_t & get_settings () const
 Get the settings list. More...
 
bool is_enabled () const
 Get whether this device is enabled. More...
 
void set_enabled (bool state)
 Set whether this device is enabled. More...
 
void set (const String &setting, const String &value)
 If possible, set a setting's value. More...
 
String get (const String &setting) const
 Get a setting value, as a string. More...
 
virtual bool get_is_published () const
 Get whether the device readings have been published. More...
 
void set_is_published ()
 Set the device as having published readings. More...
 
void clear_is_published ()
 Set the device as not having published readings.
 

Additional Inherited Members

- Public Types inherited from grmcdorman::device::Device
typedef std::vector< const Definition * > definition_list_t
 The type containing a list of Definition objects.
 
- Static Public Member Functions inherited from grmcdorman::device::Device
static void set_system_identifiers (const __FlashStringHelper *firmware_name_value, const String &system_identifier_value=String())
 Set the system identifier values. More...
 
static const __FlashStringHelper * get_firmware_name ()
 Get the firmware prefix. More...
 
static const String & get_system_identifier ()
 Get the system identifier. More...
 
- Static Public Attributes inherited from grmcdorman::device::Device
static constexpr int D0 = 16
 D0 is GPIO16, HIGH at boot, not suitable for most usages.
 
static constexpr int D1 = 5
 D1 is GPIO5; often used as SCL.
 
static constexpr int D2 = 4
 D2 is GPIO4; often used as SDA.
 
static constexpr int D3 = 0
 D3 is GPIO0; pulled up; connected to FLASH button; not for input.
 
static constexpr int D4 = 2
 D4 is GPIO2; pulled up; HIGH at boot; on-board LED; not for input.
 
static constexpr int D5 = 14
 D5 is GPIO14; SPI (SCLK)
 
static constexpr int D6 = 12
 D6 is GPIO12; SPI (MISO)
 
static constexpr int D7 = 13
 D7 is GPIO13; SPI (MOSI)
 
static constexpr int D8 = 15
 D8 is GPIO15; pulled to GND; SPI (CS); not recommended.
 
static const ExclusiveOptionSetting::names_list_t data_line_names { FPSTR("D1"), FPSTR("D2"), FPSTR("D3"), FPSTR("D5"), FPSTR("D6"), FPSTR("D7")}
 Names for each configurable data line; see settingsMap.
 
- Protected Member Functions inherited from grmcdorman::device::Device
void initialize (definition_list_t &&definition_list, ::grmcdorman::SettingInterface::settings_list_t &&setting_list)
 Initialize the definition and setting lists. More...
 
- Static Protected Member Functions inherited from grmcdorman::device::Device
static int index_to_dataline (int index)
 Convert a data line index to a ESP data line. More...
 
static int dataline_to_index (int dataLine)
 Convert a data line to an index. More...
 
- Protected Attributes inherited from grmcdorman::device::Device
ToggleSetting enabled
 Whether this device is enabled.
 
- Static Protected Attributes inherited from grmcdorman::device::Device
static const int settingsMap [6]
 The set of data lines usable for communication. More...
 

Detailed Description

This class supports the Ikea Vindriktning air quality sensor as a device.

The Ikea device provides +5V power; in addition, one of the pads can be connected to an appropriate data pin (default D2) to monitor readings. This class will automatically monitor the connection for reading messages and accumulating readings, and will publish the average reading.

The serial message sent by the Vindriktning is 20 bytes in length; the first three bytes are 0x16, 0x11, and 0x0B. Bytes 5 and 6 contain a 16 bit sensor reading. The buffer also contains checksum bytes such that the sum of all bytes in the buffer is zero. Other bytes in the buffer are not currently documented here.

The maximum reading is around 500 ug/m^3; that means for the 32-bit sum used, which can hold up to 4,294,967,295, the maximum number of readings at that level is approximately 8,589,934. (If you do get readings that high, run for the hills.)

Readings appear to occur every 20 to 30 seconds; that means the summation counter (pm25_sum) will not overflow in any reasonable publishing interval (8,589,934 readings times 20 seconds is 171,798,691 seconds, over a year, which is 31,557,988 seconds).

When a valid reading is discovered in the received Vindriktning data, the reading is added to the sum and all buffered data up to and including the processed message is discarded. Any following data is retained.

Derived from work by Hypfer's GitHub project, https://github.com/Hypfer/esp8266-vindriktning-particle-sensor. All work on message deciphering comes from that project.

Member Function Documentation

◆ as_json()

DynamicJsonDocument grmcdorman::device::VindriktningAirQuality::as_json ( ) const
overridevirtual

Get the values, as a JSON document.

The structure is identical to the document created inside publish.

Returns
DynamicJsonDocument

Reimplemented from grmcdorman::device::Device.

◆ get_pm25()

uint16_t grmcdorman::device::VindriktningAirQuality::get_pm25 ( ) const
inline

Get the last PM 2.5 reading.

Note that this is not the value published to MQTT.

This may be zero if never read.

Returns
PM 2.5 reading.

◆ get_status()

String grmcdorman::device::VindriktningAirQuality::get_status ( ) const
virtual

Get a status report.

This is also used for the last_update info message, with the exception of various disabled states.

When the VindriktningAirQuality is disabled this returns an empty string.

Returns
String containing status report.

Reimplemented from grmcdorman::device::Device.

◆ loop()

void grmcdorman::device::VindriktningAirQuality::loop ( )
overridevirtual

Main loop.

This is called in the main application loop. This should avoid blocking as doing so will prevent the other devices from running.

An exception is a "device" which requires user interaction, e.g. WiFi association, before the rest of the system can run.

Implements grmcdorman::device::Device.

◆ publish()

bool grmcdorman::device::VindriktningAirQuality::publish ( DynamicJsonDocument &  json) const
overridevirtual

Publish the value and attributes.

The values and attributes for all sensors, i.e. all items listed in the Description instances, should added to the supplied JSON object. If the value is not available, or this device does not publish, return false.

The value, or values, and attributes, if any, are to be added as a single node to the json parameter. This includes the case for multiple sensors.

Parameters
[in,out]jsonJSON to receive the device values and attributes.
Returns
true if a value to be published was added; false otherwise.

Reimplemented from grmcdorman::device::Device.

◆ setup()

void grmcdorman::device::VindriktningAirQuality::setup ( )
overridevirtual

Setup the device.

Call once when the system boots. This should perform device-specific setup, if the device is enabled, as well as preparing UI settings (notably info settings) for communication.

This must be called after initial values are loaded.

Implements grmcdorman::device::Device.


The documentation for this class was generated from the following files: