Skip to content

Commit

Permalink
Add COZIR stream parser (#17)
Browse files Browse the repository at this point in the history
* add COZIRParser class + example
  • Loading branch information
RobTillaart authored Feb 25, 2022
1 parent dcc9e76 commit 5b0ecf8
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 11 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ This version of the library supports only the **Serial** interface.
Preferred is a hardware Serial port to connect the sensor but software Serial
does work too.

The library (since 0.3.4) a separate class to parse the STREAMING data.
See COZIRParser below.
The Cozir class is focussed on polling and sending commands.


#### Notes

- Read the datasheet before using this library.
Expand Down Expand Up @@ -212,16 +217,41 @@ Also the user must reset the operating mode either to **CZR_POLLING** or **CZR_S
See examples.


## COZIRParser

Class to parse the output of a COZIR sensor in stream mode.

## Description

(added in 0.3.4, experimental)

The COZIRparser object has a main function called **nextChar(c)**
It needs to be called with all incoming characters from the sensor.

If a new field is found **nextChar()** returns the field identifier
of the last parsed field (single char) to indicate its value has been
updated, otherwise it returns 0.
The updated value can be accessed with one of the functions, see cozir.h file.

An example **Cozir_stream_parse.ino** is added to show how to use this class.

Note: to send commands e.g. outputField selection, to the sensor the COZIR
class is used (which is mostly focussed on polling access).



## Future

- improve documentation
- COZIR Parser a separate readme?
- matrix functions vs sensor ?
- test
- test streaming mode
- test table / matrix ?
- add examples
- example COZIR with I2C display?
- example streaming mode parsing.
- example with GREEN YELLOW RED LED?
- examples for COZIRParser.
- COZIR I2C class for newer generation
~ same functional interface
- multiWire / pin a la PCF8574 lib
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# Cozir Changelog


## 0.3.4 2022-02-22
- added COZIRParser class for streaming mode
- added streaming examples
- minor edits


## 0.3.3 2022-02-21
- update readme,
- update + add examples
Expand Down
133 changes: 132 additions & 1 deletion cozir.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: Cozir.cpp
// AUTHOR: DirtGambit & Rob Tillaart
// VERSION: 0.3.3
// VERSION: 0.3.4
// PURPOSE: library for COZIR range of sensors for Arduino
// Polling Mode
// URL: https://github.com/RobTillaart/Cozir
Expand Down Expand Up @@ -417,4 +417,135 @@ uint16_t COZIR::_getEEPROM2(uint8_t address)



////////////////////////////////////////////////////////////////////////////////
//
// C0ZIRParser
//
C0ZIRParser::C0ZIRParser()
{
init();
}


void C0ZIRParser::init()
{
_light = 0;
_humidity = 0;
_LED_FILT = 0;
_LED_RAW = 0;
_LED_MAX = 0;
_zeroPoint = 0;
_temperature_RAW = 0;
_temperature_FILT = 0;
_LED_signal_FILT = 0;
_LED_signal_RAW = 0;
_temperature_Sensor = 0;
_CO2_FILT = 0;
_CO2_RAW = 0;
_samples = 0;
_PPM = 1; // Note default one
_value = 0;
_field = 0;
}


uint8_t C0ZIRParser::nextChar(char c)
{
uint8_t rv = 0;
switch(c)
{
case '0' ... '9':
_value *= 10;
_value += (c - '0');
break;
case 'L':
case 'T':
case 'H':
case 'z':
case 'Z':
case 'A':
case 'P':
rv = store();
_field = c;
_value = 0;
break;
default:
break;
}
return rv;
}


float C0ZIRParser::celsius()
{
return 0.1 * (_temperature_FILT - 1000.0);
}


//////////////////////////////////
//
// PRIVATE
//
uint8_t C0ZIRParser::store()
{
switch(_field)
{
// LIGHT related
case 'L':
_light = _value;
return _field;
case 'D':
_LED_FILT = _value;
return _field;
case 'd':
_LED_RAW = _value;
return _field;
case 'l':
_LED_MAX = _value;
return _field;
case 'o':
_LED_signal_FILT = _value;
return _field;
case 'O':
_LED_signal_RAW = _value;
return _field;

// TEMPERATURE & HUMIDITY
case 'V':
_temperature_RAW = _value;
return _field;
case 'v':
_temperature_Sensor = _value;
return _field;
case 'T':
_temperature_FILT = _value;
return _field;
case 'H':
_humidity = _value;
return _field;

// CO2 related
case 'z':
_CO2_RAW = _value;
return _field;
case 'Z':
_CO2_FILT = _value;
return _field;

// OTHER
case 'h':
_zeroPoint = _value;
return _field;
case 'a':
_samples = _value;
return _field;
case '.':
_PPM = _value;
return _field;
}
return 0;
}


// -- END OF FILE --

83 changes: 79 additions & 4 deletions cozir.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once
//
// FILE: Cozir.h
// AUTHOR: DirtGambit & Rob Tillaart
// VERSION: 0.3.3
// VERSION: 0.3.4
// PURPOSE: library for COZIR range of sensors for Arduino
// Polling Mode
// URL: https://github.com/RobTillaart/Cozir
Expand All @@ -15,12 +14,15 @@
#include "Arduino.h"


#define COZIR_LIB_VERSION (F("0.3.3"))
#define COZIR_LIB_VERSION (F("0.3.4"))


// OUTPUTFIELDS
// OUTPUT FIELDS
// See datasheet for details.
// These defines can be OR-ed for the SetOutputFields command
//
#define CZR_UNKNOWN_2 0x8000 // returns P 00128 ?
#define CZR_UNKNOWN_1 0x4000 // returns E 00016 ?
#define CZR_LIGHT 0x2000
#define CZR_HUMIDITY 0x1000
#define CZR_FILTLED 0x0800
Expand Down Expand Up @@ -165,5 +167,78 @@ class COZIR
};



////////////////////////////////////////////////////////////////////////////////
//
// C0ZIRParser
//
// used to parse the stream from a COZIR CO2 sensor.
// Note: one can comment fields / code not used to minimize footprint.
//
class C0ZIRParser
{
public:
C0ZIRParser();

void init();

// returns field char if a field is completed, 0 otherwise.
uint8_t nextChar(char c);

// FETCH LAST READ VALUES
float celsius();
float fahrenheit() { return (celsius() * 1.8) + 32; };
float kelvin() { return celsius() + 273.15; };
float humidity() { return 0.1 * _humidity; };

uint16_t light() { return _light; };
uint16_t ledFilt() { return _LED_FILT; };
uint16_t ledRaw() { return _LED_RAW; };
uint16_t ledMax() { return _LED_MAX; };
uint16_t ledSignalFilt() { return _LED_signal_FILT; };
uint16_t ledSignalRaw() { return _LED_signal_RAW; };

uint16_t zeroPoint() { return _zeroPoint; };
uint16_t tempFilt() { return _temperature_FILT; };
uint16_t tempRaw() { return _temperature_RAW; };
uint16_t tempSensor() { return _temperature_Sensor; };

uint16_t CO2() { return _CO2_FILT; };
uint16_t CO2Raw() { return _CO2_RAW; };

uint16_t samples() { return _samples; };
uint16_t getPPMFactor() { return _PPM; }


private:
// FIELD ID character
uint16_t _light; // L
uint16_t _humidity; // H
uint16_t _LED_FILT; // D
uint16_t _LED_RAW; // d
uint16_t _LED_MAX; // l // el not one
uint16_t _zeroPoint; // h
uint16_t _temperature_RAW; // V
uint16_t _temperature_FILT; // T
uint16_t _LED_signal_FILT; // o // oo not zero
uint16_t _LED_signal_RAW; // O // oo not zero
uint16_t _temperature_Sensor; // v
uint16_t _CO2_FILT; // Z
uint16_t _CO2_RAW; // z

// not output fields sec but useful.
uint16_t _samples; // a
uint16_t _PPM; // . // point


// parsing helpers
uint32_t _value;
uint8_t _field;

// returns field char if a field is completed, 0 otherwise.
uint8_t store();
};


// -- END OF FILE --

2 changes: 1 addition & 1 deletion examples/Cozir_SWSerial_echo/Cozir_SWSerial_echo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void setup()
{
sws.begin(9600);

Serial.begin(9600);
Serial.begin(115200);
Serial.println(__FILE__);
Serial.println();

Expand Down
2 changes: 1 addition & 1 deletion examples/Cozir_echo/Cozir_echo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void setup()
{
Serial1.begin(9600);

Serial.begin(9600);
Serial.begin(115200);
Serial.println(__FILE__);
Serial.println();

Expand Down
4 changes: 4 additions & 0 deletions examples/Cozir_interactive/Cozir_interactive.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ void loop()
char c = Serial.read();
switch (toupper(c))
{
case '0':
czr.setOperatingMode(CZR_COMMAND);
break;
case '1':
czr.setOperatingMode(CZR_STREAMING);
break;
Expand Down Expand Up @@ -118,6 +121,7 @@ void menu()
Serial.println();
Serial.println("-- COZIR GC0034 --");
Serial.println();
Serial.println(" 0 Command mode (low power)");
Serial.println(" 1 Continuous mode");
Serial.println(" 2 Polling mode");
Serial.println();
Expand Down
11 changes: 11 additions & 0 deletions examples/Cozir_stream_CO2/.arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
# - uno
- due
# - zero
- leonardo
# - m4
# - esp32
# - esp8266
- mega2560
Loading

0 comments on commit 5b0ecf8

Please sign in to comment.