Skip to content

Commit 12ce1e2

Browse files
committed
+ 0.1.01 version of Cozir (archive purpose)
1 parent 16a464a commit 12ce1e2

File tree

3 files changed

+373
-0
lines changed

3 files changed

+373
-0
lines changed

libraries/Cozir/cozir.cpp

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
//
2+
// FILE: Cozir.cpp
3+
// AUTHOR: DirtGambit & Rob Tillaart
4+
// VERSION: 0.1.01
5+
// PURPOSE: library for COZIR range of sensors for Arduino
6+
// URL:
7+
//
8+
// READ DATASHEET BEFORE USE OF THIS LIB !
9+
//
10+
// Released to the public domain
11+
//
12+
13+
#include "Cozir.h"
14+
#include "NewSoftSerial.h"
15+
16+
////////////////////////////////////////////////////////////
17+
//
18+
// CONSTRUCTOR
19+
//
20+
COZIR::COZIR(NewSoftSerial& nss) : CZR_Serial(nss)
21+
{
22+
// overide default streaming (takes to much perf
23+
SetOperatingMode(CZR_POLLING);
24+
// delay for initialization
25+
delay(1200);
26+
}
27+
28+
////////////////////////////////////////////////////////////
29+
//
30+
// OPERATING MODE
31+
//
32+
// note: use CZR_COMMAND to minimize power consumption
33+
// CZR_POLLING and CZR_STREAMING use an equally amount
34+
// of power as both sample continuously...
35+
//
36+
void COZIR::SetOperatingMode(uint8_t mode)
37+
{
38+
sprintf(buffer, "K %u", mode);
39+
Command(buffer);
40+
}
41+
42+
43+
44+
////////////////////////////////////////////////////////////
45+
//
46+
// POLLING MODE
47+
//
48+
// you need to set the polling mode explicitely before
49+
// using these functions. SetOperatingMode(CZR_POLLING);
50+
// this is the default behaviour of this Class but
51+
// not of the sensor!!
52+
//
53+
float COZIR::Fahrenheit()
54+
{
55+
return (Celsius() * 1.8) + 32;
56+
}
57+
58+
float COZIR::Celsius()
59+
{
60+
uint16_t rv = Request("T");
61+
return 0.1 * rv;
62+
}
63+
64+
float COZIR::Humidity()
65+
{
66+
return 0.1 * Request("H");
67+
}
68+
69+
// TODO UNITS UNKNOWN
70+
float COZIR::Light()
71+
{
72+
return 1.0 * Request("L");
73+
}
74+
75+
uint16_t COZIR::CO2()
76+
{
77+
return Request("Z");
78+
}
79+
80+
// CALLIBRATION - USE THESE WITH CARE
81+
// use these only in pollingmode (on the Arduino)
82+
83+
// FineTuneZeroPoint()
84+
// a reading of v1 will be reported as v2
85+
// sort of mapping
86+
// check datasheet for detailed description
87+
uint16_t COZIR::FineTuneZeroPoint(uint16_t v1, uint16_t v2)
88+
{
89+
sprintf(buffer, "F %u %u", v1, v2);
90+
return Request(buffer);
91+
}
92+
93+
// mostly the default calibrator
94+
uint16_t COZIR::CalibrateFreshAir()
95+
{
96+
return Request("G");
97+
}
98+
99+
uint16_t COZIR::CalibrateNitrogen()
100+
{
101+
return Request("U");
102+
}
103+
104+
uint16_t COZIR::CalibrateKnownGas(uint16_t value)
105+
{
106+
sprintf(buffer, "X %u", value);
107+
return Request(buffer);
108+
}
109+
110+
// NOT RECOMMENDED, see datasheet
111+
uint16_t COZIR::CalibrateManual(uint16_t value)
112+
{
113+
return 0;
114+
//sprintf(buffer, "u %u", value);
115+
//return Request(buffer);
116+
}
117+
118+
// NOT RECOMMENDED, see datasheet
119+
uint16_t COZIR::SetSpanCalibrate(uint16_t value)
120+
{
121+
return 0;
122+
//sprintf(buffer, "S %u", value);
123+
//return Request(buffer);
124+
}
125+
126+
// NOT RECOMMENDED, see datasheet
127+
uint16_t COZIR::GetSpanCalibrate()
128+
{
129+
return Request("s");
130+
}
131+
132+
// DIGIFILTER, use with care
133+
// default value = 32,
134+
// 1=fast (noisy) 255=slow (smoothed)
135+
// 0 = special. details see datasheet
136+
void COZIR::SetDigiFilter(uint8_t value)
137+
{
138+
sprintf(buffer, "A %u", value);
139+
Command(buffer);
140+
}
141+
142+
uint8_t COZIR::GetDigiFilter()
143+
{
144+
return Request("a");
145+
}
146+
147+
148+
149+
////////////////////////////////////////////////////////////
150+
//
151+
// STREAMING MODE
152+
//
153+
// outputfields should be OR-ed
154+
// e.g. SetOutputFields(CZR_HUMIDITY | CZR_RAWTEMP | CZR_RAWCO2);
155+
//
156+
// you need to set the STREAMING mode explicitely
157+
// SetOperatingMode(CZR_STREAMING);
158+
//
159+
// in STREAMING mode you must parse the output of serial yourself
160+
//
161+
void COZIR::SetOutputFields(uint16_t fields)
162+
{
163+
sprintf(buffer, "M %u", fields);
164+
Command(buffer);
165+
}
166+
167+
// For Arduino you must read the serial yourself as
168+
// the internal buffer of this Class cannot handle
169+
// large output - can be > 100 bytes!!
170+
void COZIR::GetRecentFields()
171+
{
172+
Command("Q");
173+
}
174+
175+
176+
////////////////////////////////////////////////////////////
177+
//
178+
// EEPROM - USE WITH CARE
179+
//
180+
// SEE DATASHEET 7.2 EEPROM FOR DETAILS
181+
//
182+
// TODO
183+
// - defines for addresses
184+
// - do HILO values in one call
185+
//
186+
void COZIR::SetEEPROM(uint8_t address, uint8_t value)
187+
{
188+
sprintf(buffer, "P %u %u", address, value);
189+
Command(buffer);
190+
}
191+
192+
uint8_t COZIR::GetEEPROM(uint8_t address)
193+
{
194+
sprintf(buffer, "p %u", address);
195+
return Request(buffer);
196+
}
197+
198+
199+
200+
////////////////////////////////////////////////////////////
201+
//
202+
// COMMAND MODE
203+
//
204+
// read serial yourself
205+
//
206+
void COZIR::GetVersionSerial()
207+
{
208+
Command("Y");
209+
}
210+
211+
void COZIR::GetConfiguration()
212+
{
213+
Command("*");
214+
}
215+
216+
/////////////////////////////////////////////////////////
217+
// PRIVATE
218+
219+
void COZIR::Command(char* s)
220+
{
221+
CZR_Serial.print(s);
222+
CZR_Serial.print("\r\n");
223+
}
224+
225+
uint16_t COZIR::Request(char* s)
226+
{
227+
Command(s);
228+
// empty buffer
229+
buffer[0] = '\0';
230+
// read answer; there may be a 100ms delay!
231+
// TODO: PROPER TIMEOUT CODE.
232+
delay(100);
233+
int idx = 0;
234+
while(CZR_Serial.available())
235+
{
236+
buffer[idx++] = CZR_Serial.read();
237+
}
238+
buffer[idx] = '\0';
239+
return atoi(&buffer[1]);
240+
}

libraries/Cozir/cozir.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// FILE: Cozir.h
3+
// AUTHOR: DirtGambit & Rob Tillaart
4+
// VERSION: 0.1.01
5+
// PURPOSE: library for COZIR range of sensors for Arduino
6+
// URL:
7+
//
8+
// READ DATASHEET BEFORE USE OF THIS LIB !
9+
//
10+
// Released to the public domain
11+
//
12+
13+
#ifndef Cozir_h
14+
#define Cozir_h
15+
16+
#include "NewSoftSerial.h"
17+
18+
#if defined(ARDUINO) && ARDUINO >= 100
19+
#include "Arduino.h"
20+
#else
21+
#include "WProgram.h"
22+
#endif
23+
24+
#define COZIR_LIB_VERSION 0.1.01
25+
26+
// OUTPUTFIELDS
27+
// See datasheet for details.
28+
// These defines can be OR-ed for the SetOutputFields command
29+
#define CZR_LIGHT 0x2000
30+
#define CZR_HUMIDITY0x1000
31+
#define CZR_FILTLED0x0800
32+
#define CZR_RAWLED0x0400
33+
#define CZR_MAXLED0x0200
34+
#define CZR_ZEROPOINT0x0100
35+
#define CZR_RAWTEMP0x0080
36+
#define CZR_FILTTEMP0x0040
37+
#define CZR_FILTLEDSIGNAL0x0020
38+
#define CZR_RAWLEDSIGNAL0x0010
39+
#define CZR_SENSTEMP0x0008
40+
#define CZR_FILTCO20x0004
41+
#define CZR_RAWCO20x0002
42+
#define CZR_NONE0x0001
43+
44+
// easy default setting for streaming
45+
#define CZR_HTC(CZR_HUMIDITY | CZR_RAWTEMP | CZR_RAWCO2)
46+
// not in datasheet for debug only
47+
#define CZR_ALL0x3FFF
48+
49+
// OPERATING MODES
50+
#define CZR_COMMAND0x00
51+
#define CZR_STREAMING0x01
52+
#define CZR_POLLING0x02
53+
54+
class COZIR
55+
{
56+
public:
57+
COZIR(NewSoftSerial&);
58+
59+
void SetOperatingMode(uint8_t mode);
60+
61+
float Celsius();
62+
float Fahrenheit();
63+
float Humidity();
64+
float Light();
65+
uint16_t CO2();
66+
67+
uint16_t FineTuneZeroPoint(uint16_t , uint16_t);
68+
uint16_t CalibrateFreshAir();
69+
uint16_t CalibrateNitrogen();
70+
uint16_t CalibrateKnownGas(uint16_t );
71+
uint16_t CalibrateManual(uint16_t );
72+
uint16_t SetSpanCalibrate(uint16_t );
73+
uint16_t GetSpanCalibrate();
74+
75+
void SetDigiFilter(uint8_t );
76+
uint8_t GetDigiFilter();
77+
78+
void SetOutputFields(uint16_t );
79+
void GetRecentFields();
80+
81+
void SetEEPROM(uint8_t , uint8_t );
82+
uint8_t GetEEPROM(uint8_t );
83+
84+
void GetVersionSerial();
85+
void GetConfiguration();
86+
87+
private:
88+
NewSoftSerial& CZR_Serial;
89+
char buffer[20];
90+
void Command(char* );
91+
uint16_t Request(char* );
92+
};
93+
94+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <cozir.h>
2+
3+
#if defined(ARDUINO) && ARDUINO >= 100
4+
#include <SoftwareSerial.h>
5+
SoftwareSerial nss(3,2);
6+
#else
7+
#include <NewSoftSerial.h>
8+
NewSoftSerial nss(3,2);
9+
#endif
10+
11+
12+
COZIR czr(nss);
13+
14+
void setup()
15+
{
16+
Serial.begin(9600);
17+
Serial.println("Setup");
18+
delay(1000);
19+
}
20+
21+
void loop()
22+
{
23+
Serial.println("Loop");
24+
float t = czr.Celsius();
25+
float f = czr.Fahrenheit();
26+
float h = czr.Humidity();
27+
uint16_t c = czr.CO2();
28+
29+
Serial.print("Celcius = ");
30+
Serial.println(t);
31+
Serial.print("Fahrenheit = ");
32+
Serial.println(f);
33+
Serial.print("Humidity = ");
34+
Serial.println(h);
35+
Serial.print("CO2 = ");
36+
Serial.println(c);
37+
38+
delay(3000);
39+
}

0 commit comments

Comments
 (0)