Skip to content

Commit

Permalink
0.3.5 Cozir
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Mar 4, 2022
1 parent e146531 commit d593f36
Show file tree
Hide file tree
Showing 23 changed files with 414 additions and 71 deletions.
93 changes: 68 additions & 25 deletions libraries/Cozir/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Arduino library for COZIR range CO2 sensors.

## Description

The Cozir library is **experimental** as not all functionality is tested.
The COZIR library is **experimental** as not all functionality is tested.
The polling mode as used in the examples is tested in the past by DirtGambit.
**CO2meter.com** sponsored a **COZIR GC0034** to start hands on testing (2022-02).
This sensor does not support all commands, but as the pattern of the commands
Expand All @@ -25,7 +25,7 @@ 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.
The COZIR class is focussed on polling and sending commands.


#### Notes
Expand All @@ -41,7 +41,7 @@ It needs to be set to **CZR_POLLING** mode.
- Not all COZIR devices support all calls of this library.


## Interface
## Interface Cozir

Read the datasheet (again).

Expand Down Expand Up @@ -109,6 +109,7 @@ use with care, read datasheet before use.
| 32 | default, good average |
| 255 | very slow, max smoothed |


- **void setDigiFilter(uint8_t value)** The larger the value the more smoothed the signal is.
Larger values also means that the output does not follow fast changes.
So depending on your needs you need to find an optimal value for the project.
Expand All @@ -119,7 +120,7 @@ the actual CO2 value.

### Streaming MODE

Warning: hardware serial is needed to improve the capture of all output correctly.
Warning: hardware serial is needed / recommended to improve the capture of all output correctly.

- **void setOutputFields(uint16_t fields)** Sets the fields in the output stream as a 16 bit mask. See table below.
- **void clearOutputFields()** clears all the fields.
Expand Down Expand Up @@ -154,7 +155,8 @@ Note: NOT all sensors support all fields, check the datasheet of the sensor used
| CZR_HTC | 0X1082 | 4226 | shortcut |
| CZR_ALL | 0X3FFE | 16383 | debug |

Default value is 6 = CZR_FILTCO2 + CZR_RAWCO2

Default value is CZR_DEFAULT == 6 == CZR_FILTCO2 + CZR_RAWCO2


### EEPROM
Expand Down Expand Up @@ -217,10 +219,34 @@ Also the user must reset the operating mode either to **CZR_POLLING** or **CZR_S
See examples.


## Future

- improve documentation
- COZIR Parser a separate readme?
- add examples
- example COZIR with I2C display?
- COZIR I2C class for newer generation
~ same functional interface
- multiWire / pin a la PCF8574 lib


#### won't for now

- add a **setEEPROMFactoryDefault()**?
- unknown if all sensors have same values
- build a Arduino COZIR simulator for testing.
- add other sensors underneath?



----


## COZIRParser

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


## Description

(added in 0.3.4, experimental)
Expand All @@ -235,34 +261,51 @@ 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).
**NOTE:** to send commands e.g. outputField selection, to the sensor the COZIR
class can be used (which is mostly focussed on polling access).
Alternatively the user sends the low level commands just as a string over serial.
This latter method will save memory especially in a final version of a project.

**NOTE:** The COZIRparser skips the output of the Y, \* and @ command.
These are configuration fields and therefore not part of the **stream mode** fields.
Furthermore not all fields these lines produce are understood.
So parsing these lines is left to the user for now.

**NOTE:** The COZIRparser class does not check for missing characters,
the range of the fields recognized, or other errors. So the values
returned should be handled with care.


## Interface COZIRParser

Read the datasheet (again).


### Constructor and initialisation

- **C0ZIRParser()** constructor
- **void init()** resets all internal variables to 0 except PPM (set to 1)
- **void resetParser()** should / could be called if stream from sensor is
interrupted for long period, e.g. if there are missing characters.
This should prevent the parser to overshoot a value due to concatenating the
value of different (interrupted) measurements or FIELDs.
- **uint8_t nextChar(char c)** all characters coming from the sensor should
be send to the parser by means of **nextChar()**.
Default it will return 0 and the FIELD character is a field has been updated.
Read Datasheet for the characters used.

The remainder of the interface are getters for the different fields.


## Future

- improve documentation
- COZIR Parser a separate readme?
- matrix functions vs sensor ?
- test
- test streaming mode
- test table / matrix ?
- support splitting output of Y and \* command.
- separate parser COZIRParserY()
- separate parser COZIRParserStar()
- separate parser COZIRParser@()
- add examples
- example COZIR with I2C display?
- example with GREEN YELLOW RED LED?
- examples for COZIRParser.
- COZIR I2C class for newer generation
~ same functional interface
- multiWire / pin a la PCF8574 lib


#### won't for now

- add a **setEEPROMFactoryDefault()**?
- unknown if all sensors have same values
- build a Arduino COZIR simulator for testing.
- add other sensors underneath?



9 changes: 9 additions & 0 deletions libraries/Cozir/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
# Cozir Changelog


## 0.3.5 2022-02-25
- added capturing mode 'K' field in the COZIRParser class
- added all known fields in the COZIRParser class
- added sending commands in stream parser example to test.
- added example with adaptive timing depending on CO21 level.
- verify requested field in **_request()**
- minor edits.


## 0.3.4 2022-02-22
- added COZIRParser class for streaming mode
- added streaming examples
Expand Down
71 changes: 62 additions & 9 deletions libraries/Cozir/cozir.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// FILE: Cozir.cpp
// AUTHOR: DirtGambit & Rob Tillaart
// VERSION: 0.3.4
// VERSION: 0.3.5
// PURPOSE: library for COZIR range of sensors for Arduino
// Polling Mode
// Polling Mode + stream parser
// URL: https://github.com/RobTillaart/Cozir
// http://forum.arduino.cc/index.php?topic=91467.0
//
Expand Down Expand Up @@ -369,16 +369,22 @@ uint32_t COZIR::_request(const char* str)
if (_ser->available())
{
char c = _ser->read();
if (c == '\n') break;
_buffer[idx++] = c;
_buffer[idx] = '\0';
if (c == '\n') break;
}
}
// Serial.print("buffer: ");
// Serial.println(_buffer);
uint32_t rv = atol(&_buffer[2]);
if (idx > 2) return rv;
return 0;
uint32_t rv = 0;
// default for PPM is different.
if (str[0] == '.') rv = 1;
// do we got the requested field?
if (strchr(_buffer, str[0]) && (idx > 2))
{
rv = atol(&_buffer[2]);
}
return rv;
}


Expand Down Expand Up @@ -451,25 +457,72 @@ void C0ZIRParser::init()

uint8_t C0ZIRParser::nextChar(char c)
{
static bool skipLine = false;
uint8_t rv = 0;

// SKIP * and Y until next return.
// as output of these two commands not handled by this parser
if ((c == '*') || (c == 'Y') || (c == '@')) skipLine = true;
if (c == '\n') skipLine = false;
if (skipLine) return 0;

// TODO investigate
// if the last char is more than 2..5 ms ago (9600 baud ~ 1 char/ms)
// it probably needs to sync with the stream again.
// but it depends on how calling process behaves.
// - need for uint32_t _lastChar time stamp?

switch(c)
{
case '0' ... '9':
_value *= 10;
_value += (c - '0');
break;
// major responses to catch
case 'z':
case 'Z':
case 'L':
case 'T':
case 'H':
case 'z':
case 'Z':
// all other known responses, starting a new field
case 'X':
case '.':
case '@': // skipped
case 'Y': // skipped
case '*': // skipped
case 'Q':
case 'F':
case 'G':
case 'M':
case 'K': // mode
case 'A':
case 'a':
case 'P':
case 'p':
case 'S':
case 's':
case 'U':
case 'u':
// new line triggers store() to have results available faster.
// saves ~500 millis() for the last FIELD
case '\n':
rv = store();
_field = c;
_value = 0;
break;
default:

// drop fields of Y, and * command.
// reset parsing on separators of Y and * commands
case ':':
case ',':
_field = 0;
_value = 0;
break;

case ' ': // known separator
case '\r': // known return
break;
default: // catch all unknown characters, including glitches.
break;
}
return rv;
Expand Down
16 changes: 10 additions & 6 deletions libraries/Cozir/cozir.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once
//
// FILE: Cozir.h
// VERSION: 0.3.4
// VERSION: 0.3.5
// PURPOSE: library for COZIR range of sensors for Arduino
// Polling Mode
// Polling Mode + stream parser
// URL: https://github.com/RobTillaart/Cozir
// http://forum.arduino.cc/index.php?topic=91467.0
//
Expand All @@ -14,7 +14,7 @@
#include "Arduino.h"


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


// OUTPUT FIELDS
Expand Down Expand Up @@ -180,7 +180,11 @@ class C0ZIRParser
public:
C0ZIRParser();

// init resets all internal values
void init();
// resetParser only resets current FIELD (last values are kept).
void resetParser() { _field = 0; };


// returns field char if a field is completed, 0 otherwise.
uint8_t nextChar(char c);
Expand Down Expand Up @@ -232,10 +236,10 @@ class C0ZIRParser


// parsing helpers
uint32_t _value;
uint8_t _field;
uint32_t _value; // to build up the numeric value
uint8_t _field; // last read FIELD

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

Expand Down
11 changes: 11 additions & 0 deletions libraries/Cozir/examples/Cozir_CO2_adaptive/.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 d593f36

Please sign in to comment.