2014-11-16 10:04:43 -05:00
|
|
|
//
|
2014-11-16 10:00:08 -05:00
|
|
|
// FILE: Cozir.cpp
|
|
|
|
// AUTHOR: DirtGambit & Rob Tillaart
|
2014-12-07 05:34:12 -05:00
|
|
|
// VERSION: 0.1.05
|
2014-11-16 10:00:08 -05:00
|
|
|
// PURPOSE: library for COZIR range of sensors for Arduino
|
2014-11-16 10:04:43 -05:00
|
|
|
// Polling Mode
|
2014-11-16 10:20:59 -05:00
|
|
|
// URL: http://forum.arduino.cc/index.php?topic=91467.0
|
|
|
|
//
|
|
|
|
// HISTORY:
|
2014-12-07 05:34:12 -05:00
|
|
|
// 0.1.05 fixed bug: uint16_t request() to uint32_t request() in .h file (Rob T)
|
2014-11-16 10:20:59 -05:00
|
|
|
// 0.1.04 changed CO2 to support larger values (Rob T)
|
|
|
|
// 0.1.03 added setOperatingMode
|
|
|
|
// 0.1.02 added support Arduino 1.x
|
|
|
|
// 0.1.01 initial version
|
2014-11-16 10:04:43 -05:00
|
|
|
//
|
2014-11-16 10:00:08 -05:00
|
|
|
// READ DATASHEET BEFORE USE OF THIS LIB !
|
|
|
|
//
|
|
|
|
// Released to the public domain
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Cozir.h"
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// CONSTRUCTOR
|
|
|
|
//
|
2014-11-16 10:02:53 -05:00
|
|
|
#if defined(ARDUINO) && ARDUINO >= 100
|
2014-11-16 10:20:59 -05:00
|
|
|
COZIR::COZIR(SoftwareSerial& nss) : CZR_Serial(nss)
|
2014-11-16 10:02:53 -05:00
|
|
|
#else
|
2014-11-16 10:20:59 -05:00
|
|
|
COZIR::COZIR(NewSoftSerial& nss) : CZR_Serial(nss)
|
2014-11-16 10:02:53 -05:00
|
|
|
#endif
|
2014-11-16 10:00:08 -05:00
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
nss.begin(9600);
|
|
|
|
// overide default streaming (takes too much perf
|
|
|
|
SetOperatingMode(CZR_POLLING);
|
|
|
|
// delay for initialization
|
|
|
|
delay(1200);
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// OPERATING MODE
|
|
|
|
//
|
|
|
|
// note: use CZR_COMMAND to minimize power consumption
|
2014-11-16 10:04:43 -05:00
|
|
|
// CZR_POLLING and CZR_STREAMING use an equally amount
|
2014-11-16 10:00:08 -05:00
|
|
|
// of power as both sample continuously...
|
|
|
|
//
|
|
|
|
void COZIR::SetOperatingMode(uint8_t mode)
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
sprintf(buffer, "K %u", mode);
|
|
|
|
Command(buffer);
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// POLLING MODE
|
|
|
|
//
|
2014-11-16 10:04:43 -05:00
|
|
|
// you need to set the polling mode explicitely before
|
2014-11-16 10:00:08 -05:00
|
|
|
// using these functions. SetOperatingMode(CZR_POLLING);
|
|
|
|
// this is the default behaviour of this Class but
|
|
|
|
// not of the sensor!!
|
|
|
|
//
|
|
|
|
float COZIR::Fahrenheit()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
return (Celsius() * 1.8) + 32;
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
float COZIR::Celsius()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
uint16_t rv = Request("T");
|
|
|
|
float f = 0.1 * (rv - 1000.0);
|
|
|
|
return f;
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
float COZIR::Humidity()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
return 0.1 * Request("H");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO UNITS UNKNOWN
|
|
|
|
float COZIR::Light()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
return 1.0 * Request("L");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
2014-11-16 10:20:59 -05:00
|
|
|
uint32_t COZIR::CO2()
|
2014-11-16 10:00:08 -05:00
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
return Request("Z");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CALLIBRATION - USE THESE WITH CARE
|
|
|
|
// use these only in pollingmode (on the Arduino)
|
|
|
|
|
|
|
|
// FineTuneZeroPoint()
|
|
|
|
// a reading of v1 will be reported as v2
|
|
|
|
// sort of mapping
|
|
|
|
// check datasheet for detailed description
|
|
|
|
uint16_t COZIR::FineTuneZeroPoint(uint16_t v1, uint16_t v2)
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
sprintf(buffer, "F %u %u", v1, v2);
|
|
|
|
return Request(buffer);
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// mostly the default calibrator
|
|
|
|
uint16_t COZIR::CalibrateFreshAir()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
return Request("G");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t COZIR::CalibrateNitrogen()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
return Request("U");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t COZIR::CalibrateKnownGas(uint16_t value)
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
sprintf(buffer, "X %u", value);
|
|
|
|
return Request(buffer);
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOT RECOMMENDED, see datasheet
|
|
|
|
uint16_t COZIR::CalibrateManual(uint16_t value)
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
return 0;
|
|
|
|
//sprintf(buffer, "u %u", value);
|
|
|
|
//return Request(buffer);
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOT RECOMMENDED, see datasheet
|
|
|
|
uint16_t COZIR::SetSpanCalibrate(uint16_t value)
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
return 0;
|
|
|
|
//sprintf(buffer, "S %u", value);
|
|
|
|
//return Request(buffer);
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOT RECOMMENDED, see datasheet
|
|
|
|
uint16_t COZIR::GetSpanCalibrate()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
return Request("s");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DIGIFILTER, use with care
|
|
|
|
// default value = 32,
|
|
|
|
// 1=fast (noisy) 255=slow (smoothed)
|
|
|
|
// 0 = special. details see datasheet
|
|
|
|
void COZIR::SetDigiFilter(uint8_t value)
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
sprintf(buffer, "A %u", value);
|
|
|
|
Command(buffer);
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t COZIR::GetDigiFilter()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
return Request("a");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// STREAMING MODE
|
|
|
|
//
|
|
|
|
// outputfields should be OR-ed
|
|
|
|
// e.g. SetOutputFields(CZR_HUMIDITY | CZR_RAWTEMP | CZR_RAWCO2);
|
|
|
|
//
|
|
|
|
// you need to set the STREAMING mode explicitely
|
|
|
|
// SetOperatingMode(CZR_STREAMING);
|
|
|
|
//
|
|
|
|
// in STREAMING mode you must parse the output of serial yourself
|
|
|
|
//
|
|
|
|
void COZIR::SetOutputFields(uint16_t fields)
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
sprintf(buffer, "M %u", fields);
|
|
|
|
Command(buffer);
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// For Arduino you must read the serial yourself as
|
|
|
|
// the internal buffer of this Class cannot handle
|
|
|
|
// large output - can be > 100 bytes!!
|
|
|
|
void COZIR::GetRecentFields()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
Command("Q");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
2014-11-16 10:04:43 -05:00
|
|
|
// EEPROM - USE WITH CARE
|
2014-11-16 10:00:08 -05:00
|
|
|
//
|
|
|
|
// SEE DATASHEET 7.2 EEPROM FOR DETAILS
|
|
|
|
//
|
2014-11-16 10:04:43 -05:00
|
|
|
// TODO
|
|
|
|
// - defines for addresses
|
2014-11-16 10:00:08 -05:00
|
|
|
// - do HILO values in one call
|
|
|
|
//
|
|
|
|
void COZIR::SetEEPROM(uint8_t address, uint8_t value)
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
sprintf(buffer, "P %u %u", address, value);
|
|
|
|
Command(buffer);
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t COZIR::GetEEPROM(uint8_t address)
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
sprintf(buffer, "p %u", address);
|
|
|
|
return Request(buffer);
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// COMMAND MODE
|
|
|
|
//
|
|
|
|
// read serial yourself
|
|
|
|
//
|
|
|
|
void COZIR::GetVersionSerial()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
Command("Y");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void COZIR::GetConfiguration()
|
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
Command("*");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
// PRIVATE
|
2014-11-16 10:04:43 -05:00
|
|
|
|
|
|
|
void COZIR::Command(const char* s)
|
2014-11-16 10:00:08 -05:00
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
// TODO
|
|
|
|
// CZR_Serial.println(s);
|
|
|
|
CZR_Serial.print(s);
|
|
|
|
CZR_Serial.print("\r\n");
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
|
|
|
|
2014-11-16 10:20:59 -05:00
|
|
|
uint32_t COZIR::Request(const char* s)
|
2014-11-16 10:00:08 -05:00
|
|
|
{
|
2014-11-16 10:20:59 -05:00
|
|
|
Command(s);
|
|
|
|
// empty buffer
|
|
|
|
buffer[0] = '\0';
|
|
|
|
// read answer; there may be a 100ms delay!
|
|
|
|
// TODO: PROPER TIMEOUT CODE.
|
|
|
|
delay(200);
|
|
|
|
int idx = 0;
|
|
|
|
while(CZR_Serial.available())
|
|
|
|
{
|
|
|
|
buffer[idx++] = CZR_Serial.read();
|
|
|
|
}
|
|
|
|
buffer[idx] = '\0';
|
2014-11-16 10:04:43 -05:00
|
|
|
|
2014-11-16 10:20:59 -05:00
|
|
|
uint32_t rv = 0;
|
|
|
|
switch(buffer[0])
|
|
|
|
{
|
2014-11-16 10:04:43 -05:00
|
|
|
case 'T' :
|
2014-11-16 10:20:59 -05:00
|
|
|
rv = atoi(&buffer[5]);
|
|
|
|
if (buffer[4] == 1) rv += 1000;
|
|
|
|
// negative values are mapped above 1000..1250 => capture this in Celsius()
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
rv = atol(&buffer[2]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rv;
|
2014-11-16 10:00:08 -05:00
|
|
|
}
|
2014-11-16 10:04:43 -05:00
|
|
|
// -- END OF FILE --
|