From d593f365a327a0c04c6feca3efc012565b7a77f4 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Fri, 4 Mar 2022 15:07:12 +0100 Subject: [PATCH] 0.3.5 Cozir --- libraries/Cozir/README.md | 93 ++++++++---- libraries/Cozir/changelog.md | 9 ++ libraries/Cozir/cozir.cpp | 71 +++++++-- libraries/Cozir/cozir.h | 16 +- .../Cozir_CO2_adaptive/.arduino-ci.yml | 11 ++ .../Cozir_CO2_adaptive/Cozir_CO2_adaptive.ino | 74 ++++++++++ .../Cozir_CO2_only/Cozir_CO2_only.ino | 4 +- .../Cozir_MEGA_3_channel_CO2.ino | 4 +- .../Cozir_SWSerial_CO2_only.ino | 2 +- .../Cozir_SWSerial_echo.ino | 4 +- .../Cozir_SWSerial_getVersion.ino | 2 +- .../Cozir_SWSerial_interactive.ino | 2 +- .../Cozir/examples/Cozir_echo/Cozir_echo.ino | 6 +- .../Cozir_getVersion/Cozir_getVersion.ino | 4 +- .../Cozir_interactive/Cozir_interactive.ino | 3 +- .../Cozir_setDigiFilter.ino | 4 +- .../Cozir_setOutputFIelds.ino | 6 +- .../Cozir_stream_CO2/Cozir_stream_CO2.ino | 5 +- .../Cozir_stream_parse/Cozir_stream_parse.ino | 11 +- .../Cozir_stream_parse_3LED/.arduino-ci.yml | 11 ++ .../Cozir_stream_parse_3LED.ino | 139 ++++++++++++++++++ libraries/Cozir/library.json | 2 +- libraries/Cozir/library.properties | 2 +- 23 files changed, 414 insertions(+), 71 deletions(-) create mode 100644 libraries/Cozir/examples/Cozir_CO2_adaptive/.arduino-ci.yml create mode 100644 libraries/Cozir/examples/Cozir_CO2_adaptive/Cozir_CO2_adaptive.ino create mode 100644 libraries/Cozir/examples/Cozir_stream_parse_3LED/.arduino-ci.yml create mode 100644 libraries/Cozir/examples/Cozir_stream_parse_3LED/Cozir_stream_parse_3LED.ino diff --git a/libraries/Cozir/README.md b/libraries/Cozir/README.md index be2309d1f..75c52821b 100644 --- a/libraries/Cozir/README.md +++ b/libraries/Cozir/README.md @@ -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 @@ -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 @@ -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). @@ -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. @@ -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. @@ -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 @@ -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) @@ -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? - - - \ No newline at end of file diff --git a/libraries/Cozir/changelog.md b/libraries/Cozir/changelog.md index 3ffea8c44..d3a9cb3d3 100644 --- a/libraries/Cozir/changelog.md +++ b/libraries/Cozir/changelog.md @@ -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 diff --git a/libraries/Cozir/cozir.cpp b/libraries/Cozir/cozir.cpp index 3125f0d14..e377dbd1c 100644 --- a/libraries/Cozir/cozir.cpp +++ b/libraries/Cozir/cozir.cpp @@ -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 // @@ -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; } @@ -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; diff --git a/libraries/Cozir/cozir.h b/libraries/Cozir/cozir.h index b33e43f89..8d423f8bd 100644 --- a/libraries/Cozir/cozir.h +++ b/libraries/Cozir/cozir.h @@ -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 // @@ -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 @@ -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); @@ -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(); }; diff --git a/libraries/Cozir/examples/Cozir_CO2_adaptive/.arduino-ci.yml b/libraries/Cozir/examples/Cozir_CO2_adaptive/.arduino-ci.yml new file mode 100644 index 000000000..899c46aa5 --- /dev/null +++ b/libraries/Cozir/examples/Cozir_CO2_adaptive/.arduino-ci.yml @@ -0,0 +1,11 @@ +compile: + # Choosing to run compilation tests on 2 different Arduino platforms + platforms: + # - uno + - due + # - zero + - leonardo + # - m4 + # - esp32 + # - esp8266 + - mega2560 diff --git a/libraries/Cozir/examples/Cozir_CO2_adaptive/Cozir_CO2_adaptive.ino b/libraries/Cozir/examples/Cozir_CO2_adaptive/Cozir_CO2_adaptive.ino new file mode 100644 index 000000000..ccf5b3a86 --- /dev/null +++ b/libraries/Cozir/examples/Cozir_CO2_adaptive/Cozir_CO2_adaptive.ino @@ -0,0 +1,74 @@ +// +// FILE: Cozir_CO2_adaptive.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo of Cozir lib +// URL: https://github.com/RobTillaart/Cozir +// +// Note: this sketch needs a MEGA or a Teensy that supports a second +// Serial port named Serial1 +// The sketch updates the polling interval depending on the level of CO2 +// when higher the interval wil be shorter. + + +#include "Arduino.h" +#include "cozir.h" + + +COZIR czr(&Serial1); + + +uint32_t lastPrint = 0; +uint32_t interval = 1000; // milliseconds + + +void setup() +{ + Serial1.begin(9600); + czr.init(); + + Serial.begin(115200); + Serial.print("COZIR_LIB_VERSION: "); + Serial.println(COZIR_LIB_VERSION); + Serial.println(); + + // set to polling explicitly. + czr.setOperatingMode(CZR_POLLING); + delay(1000); +} + + +void loop() +{ + uint32_t now = millis(); + if (now - lastPrint > interval) + { + lastPrint = now; + uint32_t co2 = czr.CO2(); + co2 *= czr.getPPMFactor(); // most of time PPM = one. + Serial.print(interval); + Serial.print("\t"); + Serial.print("CO2 = "); + Serial.println(co2); + updateInterval(co2); + } + delay(1000); +} + + +void updateInterval(uint16_t co2) +{ + if (co2 < 100) interval = 1000; // catch zero readings. + else if (co2 < 600) interval = 10000; + else if (co2 < 700) interval = 9000; + else if (co2 < 800) interval = 8000; + else if (co2 < 900) interval = 7000; + else if (co2 < 1000) interval = 6000; + else if (co2 < 1100) interval = 5000; + else if (co2 < 1200) interval = 4000; + else if (co2 < 1300) interval = 3000; + else if (co2 < 1400) interval = 2000; + else interval = 1000; +} + + +// -- END OF FILE -- diff --git a/libraries/Cozir/examples/Cozir_CO2_only/Cozir_CO2_only.ino b/libraries/Cozir/examples/Cozir_CO2_only/Cozir_CO2_only.ino index da5cde5e6..6d706f802 100644 --- a/libraries/Cozir/examples/Cozir_CO2_only/Cozir_CO2_only.ino +++ b/libraries/Cozir/examples/Cozir_CO2_only/Cozir_CO2_only.ino @@ -3,15 +3,15 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 // - // Note: this sketch needs a MEGA or a Teensy that supports a second // Serial port named Serial1 + #include "Arduino.h" #include "cozir.h" + COZIR czr(&Serial1); diff --git a/libraries/Cozir/examples/Cozir_MEGA_3_channel_CO2/Cozir_MEGA_3_channel_CO2.ino b/libraries/Cozir/examples/Cozir_MEGA_3_channel_CO2/Cozir_MEGA_3_channel_CO2.ino index 2cf714863..c1abd489f 100644 --- a/libraries/Cozir/examples/Cozir_MEGA_3_channel_CO2/Cozir_MEGA_3_channel_CO2.ino +++ b/libraries/Cozir/examples/Cozir_MEGA_3_channel_CO2/Cozir_MEGA_3_channel_CO2.ino @@ -3,10 +3,8 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 // - -// Note: this sketch needs a MEGA or another board that supports three +// NOTE: this sketch needs a MEGA or another board that supports three // hardware serial ports named Serial1, Serial2, Serial3. diff --git a/libraries/Cozir/examples/Cozir_SWSerial_CO2_only/Cozir_SWSerial_CO2_only.ino b/libraries/Cozir/examples/Cozir_SWSerial_CO2_only/Cozir_SWSerial_CO2_only.ino index 358d30137..c292eee83 100644 --- a/libraries/Cozir/examples/Cozir_SWSerial_CO2_only/Cozir_SWSerial_CO2_only.ino +++ b/libraries/Cozir/examples/Cozir_SWSerial_CO2_only/Cozir_SWSerial_CO2_only.ino @@ -3,7 +3,7 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib (>= 0.1.06) // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 +// // NOTE: software serial is less reliable than hardware serial diff --git a/libraries/Cozir/examples/Cozir_SWSerial_echo/Cozir_SWSerial_echo.ino b/libraries/Cozir/examples/Cozir_SWSerial_echo/Cozir_SWSerial_echo.ino index 52e2488b5..f8c03d199 100644 --- a/libraries/Cozir/examples/Cozir_SWSerial_echo/Cozir_SWSerial_echo.ino +++ b/libraries/Cozir/examples/Cozir_SWSerial_echo/Cozir_SWSerial_echo.ino @@ -3,12 +3,12 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir sensor // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 +// // NOTE: software serial is less reliable than hardware serial // // The purpose of this sketch is to talk lowest level to the COZIR sensor. // one can send the low level commands (datasheet) directly to the device. -// Note: this can cause (unwanted) side effects, so use with care. +// NOTE: this can cause (unwanted) side effects, so use with care. #include "Arduino.h" diff --git a/libraries/Cozir/examples/Cozir_SWSerial_getVersion/Cozir_SWSerial_getVersion.ino b/libraries/Cozir/examples/Cozir_SWSerial_getVersion/Cozir_SWSerial_getVersion.ino index 95531ba9d..ecd7c336d 100644 --- a/libraries/Cozir/examples/Cozir_SWSerial_getVersion/Cozir_SWSerial_getVersion.ino +++ b/libraries/Cozir/examples/Cozir_SWSerial_getVersion/Cozir_SWSerial_getVersion.ino @@ -3,7 +3,7 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 +// // NOTE: software serial is less reliable than hardware serial diff --git a/libraries/Cozir/examples/Cozir_SWSerial_interactive/Cozir_SWSerial_interactive.ino b/libraries/Cozir/examples/Cozir_SWSerial_interactive/Cozir_SWSerial_interactive.ino index 2e97f96c5..79354ff86 100644 --- a/libraries/Cozir/examples/Cozir_SWSerial_interactive/Cozir_SWSerial_interactive.ino +++ b/libraries/Cozir/examples/Cozir_SWSerial_interactive/Cozir_SWSerial_interactive.ino @@ -3,7 +3,7 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 +// // NOTE: software serial is less reliable than hardware serial diff --git a/libraries/Cozir/examples/Cozir_echo/Cozir_echo.ino b/libraries/Cozir/examples/Cozir_echo/Cozir_echo.ino index e030084ed..c046bd3f8 100644 --- a/libraries/Cozir/examples/Cozir_echo/Cozir_echo.ino +++ b/libraries/Cozir/examples/Cozir_echo/Cozir_echo.ino @@ -3,14 +3,12 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir sensor // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 // - -// Note: this sketch needs a MEGA or a Teensy that supports a second +// NOTE: this sketch needs a MEGA or a Teensy that supports a second // Serial port named Serial1 // The purpose of this sketch is to talk lowest level to the COZIR sensor. // one can send the low level commands (datasheet) directly to the device. -// Note: this can cause (unwanted) side effects, so use with care. +// NOTE: this can cause (unwanted) side effects, so use with care. #include "Arduino.h" diff --git a/libraries/Cozir/examples/Cozir_getVersion/Cozir_getVersion.ino b/libraries/Cozir/examples/Cozir_getVersion/Cozir_getVersion.ino index b7647637a..5ec04ae6c 100644 --- a/libraries/Cozir/examples/Cozir_getVersion/Cozir_getVersion.ino +++ b/libraries/Cozir/examples/Cozir_getVersion/Cozir_getVersion.ino @@ -3,15 +3,15 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 // - // Note: this sketch needs a MEGA or a Teensy that supports a second // Serial port named Serial1 + #include "Arduino.h" #include "cozir.h" + COZIR czr(&Serial1); diff --git a/libraries/Cozir/examples/Cozir_interactive/Cozir_interactive.ino b/libraries/Cozir/examples/Cozir_interactive/Cozir_interactive.ino index c90aa4446..121f32b95 100644 --- a/libraries/Cozir/examples/Cozir_interactive/Cozir_interactive.ino +++ b/libraries/Cozir/examples/Cozir_interactive/Cozir_interactive.ino @@ -3,12 +3,13 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 // + #include "Arduino.h" #include "cozir.h" + COZIR czr(&Serial1); diff --git a/libraries/Cozir/examples/Cozir_setDigiFilter/Cozir_setDigiFilter.ino b/libraries/Cozir/examples/Cozir_setDigiFilter/Cozir_setDigiFilter.ino index b11603167..b1ee86091 100644 --- a/libraries/Cozir/examples/Cozir_setDigiFilter/Cozir_setDigiFilter.ino +++ b/libraries/Cozir/examples/Cozir_setDigiFilter/Cozir_setDigiFilter.ino @@ -3,10 +3,8 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 // - -// Note: this sketch needs a MEGA or a Teensy that supports a second +// NOTE: this sketch needs a MEGA or a Teensy that supports a second // Serial port named Serial1 diff --git a/libraries/Cozir/examples/Cozir_setOutputFIelds/Cozir_setOutputFIelds.ino b/libraries/Cozir/examples/Cozir_setOutputFIelds/Cozir_setOutputFIelds.ino index 1f8f04d1a..a93315a66 100644 --- a/libraries/Cozir/examples/Cozir_setOutputFIelds/Cozir_setOutputFIelds.ino +++ b/libraries/Cozir/examples/Cozir_setOutputFIelds/Cozir_setOutputFIelds.ino @@ -3,12 +3,10 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 // - -// Note: this sketch needs a MEGA or a Teensy that supports a second +// NOTE: this sketch needs a MEGA or a Teensy that supports a second // Serial port named Serial1 -// Note: not all fields are supported by all COZIR sensors. +// NOTE: not all fields are supported by all COZIR sensors. #include "Arduino.h" diff --git a/libraries/Cozir/examples/Cozir_stream_CO2/Cozir_stream_CO2.ino b/libraries/Cozir/examples/Cozir_stream_CO2/Cozir_stream_CO2.ino index 8a42146cb..cb07fe570 100644 --- a/libraries/Cozir/examples/Cozir_stream_CO2/Cozir_stream_CO2.ino +++ b/libraries/Cozir/examples/Cozir_stream_CO2/Cozir_stream_CO2.ino @@ -3,12 +3,11 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib - stand alone stream parsing. // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 // -// Note: this sketch needs a MEGA or a Teensy that supports a second +// NOTE: this sketch needs a MEGA or a Teensy that supports a second // Serial port named Serial1 // -// to be used with the Serial Plotter. +// to be used with the Serial Plotter. #include "Arduino.h" diff --git a/libraries/Cozir/examples/Cozir_stream_parse/Cozir_stream_parse.ino b/libraries/Cozir/examples/Cozir_stream_parse/Cozir_stream_parse.ino index 79dc95159..b49c14ac7 100644 --- a/libraries/Cozir/examples/Cozir_stream_parse/Cozir_stream_parse.ino +++ b/libraries/Cozir/examples/Cozir_stream_parse/Cozir_stream_parse.ino @@ -3,17 +3,17 @@ // AUTHOR: Rob Tillaart // PURPOSE: demo of Cozir lib // URL: https://github.com/RobTillaart/Cozir -// http://forum.arduino.cc/index.php?topic=91467.0 // // Note: this sketch needs a MEGA or a Teensy that supports a second // Serial port named Serial1 // -// to be used with the Serial Plotter. +// to be used with the Serial Plotter. #include "Arduino.h" #include "cozir.h" + COZIR czr(&Serial1); C0ZIRParser czrp; @@ -34,6 +34,7 @@ void setup() { Serial1.begin(9600); czr.init(); + czrp.init(); Serial.begin(115200); // Serial.print("COZIR_LIB_VERSION: "); @@ -57,9 +58,15 @@ void setup() void loop() { + // inject commands to check robustness + if (Serial.available()) + { + Serial1.write(Serial.read()); + } if (Serial1.available()) { char c = Serial1.read(); + // Serial.print(c); field = czrp.nextChar(c); if (field != 0) { diff --git a/libraries/Cozir/examples/Cozir_stream_parse_3LED/.arduino-ci.yml b/libraries/Cozir/examples/Cozir_stream_parse_3LED/.arduino-ci.yml new file mode 100644 index 000000000..899c46aa5 --- /dev/null +++ b/libraries/Cozir/examples/Cozir_stream_parse_3LED/.arduino-ci.yml @@ -0,0 +1,11 @@ +compile: + # Choosing to run compilation tests on 2 different Arduino platforms + platforms: + # - uno + - due + # - zero + - leonardo + # - m4 + # - esp32 + # - esp8266 + - mega2560 diff --git a/libraries/Cozir/examples/Cozir_stream_parse_3LED/Cozir_stream_parse_3LED.ino b/libraries/Cozir/examples/Cozir_stream_parse_3LED/Cozir_stream_parse_3LED.ino new file mode 100644 index 000000000..cd9868593 --- /dev/null +++ b/libraries/Cozir/examples/Cozir_stream_parse_3LED/Cozir_stream_parse_3LED.ino @@ -0,0 +1,139 @@ +// +// FILE: Cozir_stream_parse_3LED.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo of Cozir lib +// URL: https://github.com/RobTillaart/Cozir +// +// Note: this sketch needs a MEGA or a Teensy that supports a second +// Serial port named Serial1 +// +// to be used with the Serial Plotter. + + +#include "Arduino.h" +#include "cozir.h" + + +COZIR czr(&Serial1); +C0ZIRParser czrp; + + +uint32_t F_CO2 = 0; // CO2 FILTERED +uint32_t R_CO2 = 0; // CO2 RAW +uint32_t tmp = 0; // temporary buffer + +// 0 = none +// Z = filtered co2 +// z = raw co2 +uint8_t field = 0; // which field is parsed + +uint32_t lastPrint = 0; // last time fields are displayed. + +// CO2 traffic light, could be a 3 colour LED +// or three separate ones +const uint8_t REDPIN = A0; +const uint8_t YELLOWPIN = A1; +const uint8_t GREENPIN = A2; + + + +void setup() +{ + Serial1.begin(9600); + czr.init(); + czrp.init(); + + // initialize the LEDS + pinMode(REDPIN, OUTPUT); + pinMode(YELLOWPIN, OUTPUT); + pinMode(GREENPIN, OUTPUT); + digitalWrite(REDPIN, LOW); + digitalWrite(YELLOWPIN, LOW); + digitalWrite(GREENPIN, LOW); + + + Serial.begin(115200); + // Serial.print("COZIR_LIB_VERSION: "); + // Serial.println(COZIR_LIB_VERSION); + // Serial.println(); + + Serial.print("FILTERED\t"); + Serial.println("RAW"); + Serial.println(); + + // set to streaming explicitly. + czr.setOperatingMode(CZR_STREAMING); + // set digifilter on an average value + czr.setDigiFilter(16); + // select RAW and FILTERED CO2 + czr.setOutputFields(CZR_DEFAULT); + // czr.setOutputFields(CZR_HUMIDITY | CZR_RAWTEMP | CZR_RAWCO2); + delay(1000); +} + + +void loop() +{ + // inject commands to check robustness + if (Serial.available()) + { + Serial1.write(Serial.read()); + } + if (Serial1.available()) + { + char c = Serial1.read(); + field = czrp.nextChar(c); + if (field != 0) + { + // shows all values + // Serial.print(czrp.celsius()); + // Serial.print("\t"); + // Serial.print(czrp.humidity()); + // Serial.print("\t"); + Serial.print(czrp.CO2()); + Serial.print("\t"); + Serial.print(czrp.CO2Raw()); + Serial.println(); + + updateLEDS(czrp.CO2()); + } + } +} + + +void updateLEDS(uint16_t value) +{ + digitalWrite(REDPIN, LOW); + digitalWrite(YELLOWPIN, LOW); + digitalWrite(GREENPIN, LOW); + if (value < 100) + { + digitalWrite(GREENPIN, HIGH); + digitalWrite(REDPIN, HIGH); + } + else if (value < 800) + { + digitalWrite(GREENPIN, HIGH); + } + else if (value < 1000) + { + digitalWrite(GREENPIN, HIGH); + digitalWrite(YELLOWPIN, HIGH); + } + else if (value < 1200) + { + digitalWrite(YELLOWPIN, HIGH); + } + else if (value < 1400) + { + digitalWrite(YELLOWPIN, HIGH); + digitalWrite(REDPIN, HIGH); + } + else + { + digitalWrite(REDPIN, HIGH); + } + +} + +// -- END OF FILE -- diff --git a/libraries/Cozir/library.json b/libraries/Cozir/library.json index c0129039e..fd4244ae9 100644 --- a/libraries/Cozir/library.json +++ b/libraries/Cozir/library.json @@ -18,7 +18,7 @@ "type": "git", "url": "https://github.com/RobTillaart/Cozir.git" }, - "version": "0.3.4", + "version": "0.3.5", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/libraries/Cozir/library.properties b/libraries/Cozir/library.properties index 15283b384..acb106b14 100644 --- a/libraries/Cozir/library.properties +++ b/libraries/Cozir/library.properties @@ -1,5 +1,5 @@ name=Cozir -version=0.3.4 +version=0.3.5 author=Rob Tillaart , DirtGambit maintainer=Rob Tillaart sentence=Arduino library for COZIR range of CO2 sensors. Polling mode only.