I am trying to build my own sensors based on DIY instructions according to mysensors. I have successfully built a serial gateway that i connected to home assistant and linked trough configuration like this:
mysensors:
gateways:
- device: '/dev/ttyUSB0'
version: '2.3'
I have also built Air quality sensor based on CCS811 chip that reports CO2 and TVOC values back to serial gateway. the sketch I used for the sensor is as follows:
// Enable debug prints to serial monitor
#define MY_DEBUG
#define MY_DEBUG_VERBOSE_SIGNING
// Enable and select radio type attached
#define MY_RADIO_RF24
// Enable encryption
#include <MySensors.h> // MySensors Library
#include <Wire.h> // I2C library
#include "ccs811.h" // CCS811 library
// Wiring for ESP8266 NodeMCU boards: VDD to 3V3, GND to GND, SDA to D2, SCL to D1, nWAKE to D3 (or GND)
CCS811 ccs811(3); // nWAKE on 3
#define CHILD_ID_AIQ_CO2 0
#define CHILD_ID_AIQ_TVOC 1
uint32_t SLEEP_TIME = 3*1000; // Sleep time between reads (in milliseconds)
float valAIQ =0.0;
float lastAIQ =0.0;
float valAIQ_TVOC =0.0;
float lastAIQ_TVOC =0.0;
MyMessage msg_co2(CHILD_ID_AIQ_CO2, V_LEVEL);
MyMessage msg_co2_unit(CHILD_ID_AIQ_CO2, V_UNIT_PREFIX);
MyMessage msg_tvoc(CHILD_ID_AIQ_TVOC, V_LEVEL);
MyMessage msg_tvoc_unit(CHILD_ID_AIQ_TVOC, V_UNIT_PREFIX);
void setup() {
#ifdef MY_DEBUG
// Enable serial
Serial.begin(115200);
Serial.println("");
Serial.print("setup: ccs811 lib version: "); Serial.println(CCS811_VERSION);
#endif
// Enable I2C
Wire.begin();
// Enable CCS811
ccs811.set_i2cdelay(50); // Needed for ESP8266 because it doesn't handle I2C clock stretch correctly
bool ok= ccs811.begin();
while( !ok ) {
sleep(2000);
Serial.println("setup: CCS811 begin FAILED; Trying again");
ok= ccs811.begin();
}
#ifdef MY_DEBUG
// Print CCS811 versions
Serial.print("setup: hardware version: "); Serial.println(ccs811.hardware_version(),HEX);
Serial.print("setup: bootloader version: "); Serial.println(ccs811.bootloader_version(),HEX);
Serial.print("setup: application version: "); Serial.println(ccs811.application_version(),HEX);
#endif
// Start measuring
ok= ccs811.start(CCS811_MODE_1SEC);
if( !ok ) Serial.println("setup: CCS811 start FAILED");
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("AIQ Sensor CO2 CCS811", "1.0");
// Register all sensors to gateway (they will be created as child devices)
present(CHILD_ID_AIQ_CO2, S_AIR_QUALITY);
present(CHILD_ID_AIQ_TVOC, S_AIR_QUALITY);
send(msg_co2_unit.set("ppm"));
send(msg_tvoc_unit.set("ppb"));
}
void loop() {
// Read
uint16_t eco2, etvoc, errstat, raw;
ccs811.read(&eco2,&etvoc,&errstat,&raw);
// Print measurement results based on status
if( errstat==CCS811_ERRSTAT_OK ) {
if ((eco2 != lastAIQ)&&(abs(eco2-lastAIQ)>=10)) {
send(msg_co2.set((int32_t)ceil(eco2)));
lastAIQ = ceil(eco2);
}
if ((etvoc != lastAIQ_TVOC)&&(abs(etvoc-lastAIQ_TVOC)>=10)) {
send(msg_tvoc.set((int32_t)ceil(etvoc)));
lastAIQ_TVOC = ceil(etvoc);
}
#ifdef MY_DEBUG
Serial.print("CCS811: ");
Serial.print("eco2="); Serial.print(eco2); Serial.print(" ppm ");
Serial.print("etvoc="); Serial.print(etvoc); Serial.print(" ppb ");
Serial.println();
#endif
} else if( errstat==CCS811_ERRSTAT_OK_NODATA ) {
delay(1);
//Serial.println("CCS811: waiting for (new) data");
} else if( errstat & CCS811_ERRSTAT_I2CFAIL ) {
Serial.println("CCS811: I2C error");
} else {
Serial.print("CCS811: errstat="); Serial.print(errstat,HEX);
Serial.print("="); Serial.println( ccs811.errstat_str(errstat) );
}
// Wait
sleep(SLEEP_TIME); //sleep for: sleepTime
}
Now, I have several issues with my configuration/implementation. In home assistent the reported values seem to be treated as “text” state rather than “value” state. What i mean is that i get history of the sensor like this:
instead of a chart. Furthermore, I do not get the units I report from my sensor. According the documentation it should be possible.
What I have missed or done wrong?