Skip to content

Commit 4959161

Browse files
committed
Added missing sensor headers.
1 parent e3f38d7 commit 4959161

File tree

3 files changed

+388
-0
lines changed

3 files changed

+388
-0
lines changed

src/CayenneTMP102.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
The MIT License(MIT)
3+
4+
Cayenne Arduino Client Library
5+
Copyright © 2016 myDevices
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8+
documentation files(the "Software"), to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software,
10+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
11+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
14+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
#ifndef _CAYENNETMP102_h
19+
#define _CAYENNETMP102_h
20+
21+
#include <Wire.h>
22+
#include "CayenneTemperature.h"
23+
24+
class TMP102 : public Temperature
25+
{
26+
public:
27+
TMP102(int address) : _address(address) {
28+
}
29+
30+
float getKelvin() {
31+
return celsiusToKelvin(getCelsius());
32+
}
33+
34+
float getCelsius() {
35+
Wire.requestFrom(_address, 2);
36+
byte msb = Wire.read();
37+
byte lsb = Wire.read();
38+
int reading = ((msb << 8) | lsb) >> 4;
39+
return reading * 0.0625;
40+
}
41+
42+
float getFahrenheit() {
43+
return celsiusToFahrenheit(getCelsius());
44+
}
45+
46+
private:
47+
int _address;
48+
};
49+
50+
#endif

src/CayenneTemperature.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
The MIT License(MIT)
3+
4+
Cayenne Arduino Client Library
5+
Copyright © 2016 myDevices
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8+
documentation files(the "Software"), to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software,
10+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
11+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
14+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
17+
This file contains code modified from the thermistor code at http://playground.arduino.cc/ComponentLib/Thermistor2
18+
*/
19+
20+
#ifndef _CAYENNETEMPERATURE_h
21+
#define _CAYENNETEMPERATURE_h
22+
23+
#include <Arduino.h>
24+
#include <math.h>
25+
26+
class Temperature
27+
{
28+
public:
29+
virtual float getKelvin() = 0;
30+
31+
virtual float getCelsius() = 0;
32+
33+
virtual float getFahrenheit() = 0;
34+
35+
float celsiusToKelvin(float celsius) {
36+
return celsius + 273.15;
37+
}
38+
39+
float kelvinToCelsius(float kelvin) {
40+
return kelvin - 273.15;
41+
}
42+
43+
float celsiusToFahrenheit(float celsius) {
44+
return (celsius * 9.0) / 5.0 + 32.0;
45+
}
46+
};
47+
48+
class Thermistor : public Temperature
49+
{
50+
public:
51+
Thermistor(int pin, float resistance) : _pin(pin), _resistance(resistance) {
52+
}
53+
54+
float getKelvin() {
55+
int reading = analogRead(_pin);
56+
if (reading != 0) {
57+
long resistance = _resistance * ((1024.0 / reading) - 1);
58+
float temp = log(resistance);
59+
return 1 / (0.001129148 + (0.000234125 * temp) + (0.0000000876741 * temp * temp * temp));
60+
}
61+
return 0;
62+
}
63+
64+
float getCelsius() {
65+
return kelvinToCelsius(getKelvin());
66+
}
67+
68+
float getFahrenheit() {
69+
return celsiusToFahrenheit(getCelsius());
70+
}
71+
72+
private:
73+
int _pin;
74+
float _resistance;
75+
};
76+
77+
class TMP36 : public Temperature
78+
{
79+
public:
80+
TMP36(int pin, float voltage) : _pin(pin), _voltage(voltage) {
81+
}
82+
83+
float getKelvin() {
84+
return celsiusToKelvin(getCelsius());
85+
}
86+
87+
float getCelsius() {
88+
int reading = analogRead(_pin);
89+
float currentVoltage = (reading * _voltage) / 1024.0;
90+
return (currentVoltage - 0.5) * 100;
91+
}
92+
93+
float getFahrenheit() {
94+
return celsiusToFahrenheit(getCelsius());
95+
}
96+
97+
private:
98+
int _pin;
99+
float _voltage;
100+
};
101+
102+
#endif

src/CayenneVCNL4000.h

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/*
2+
The MIT License(MIT)
3+
4+
Cayenne Arduino Client Library
5+
Copyright © 2016 myDevices
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8+
documentation files(the "Software"), to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software,
10+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
11+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
14+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
17+
This class adapted from Adafruit VCNL400 example sketch https://github.com/adafruit/VCNL4000/blob/master/vcnl4000.pde
18+
*/
19+
20+
#ifndef _CAYENNEVCNL4000_h
21+
#define _CAYENNEVCNL4000_h
22+
23+
#include <Wire.h>
24+
#include <Arduino.h>
25+
26+
// commands and constants
27+
#define VCNL4000_ADDRESS 0x13
28+
#define VCNL4000_COMMAND 0x80
29+
#define VCNL4000_PRODUCTID 0x81
30+
#define VCNL4000_IRLED 0x83
31+
#define VCNL4000_AMBIENTPARAMETER 0x84
32+
#define VCNL4000_AMBIENTDATA 0x85
33+
#define VCNL4000_PROXIMITYDATA 0x87
34+
#define VCNL4000_SIGNALFREQ 0x89
35+
#define VCNL4000_PROXINITYADJUST 0x8A
36+
37+
#define VCNL4000_3M125 0
38+
#define VCNL4000_1M5625 1
39+
#define VCNL4000_781K25 2
40+
#define VCNL4000_390K625 3
41+
42+
#define VCNL4000_MEASUREAMBIENT 0x10
43+
#define VCNL4000_MEASUREPROXIMITY 0x08
44+
#define VCNL4000_AMBIENTREADY 0x40
45+
#define VCNL4000_PROXIMITYREADY 0x20
46+
47+
#define CALIBRATION_CYCLES 5
48+
#define MEASUREMENT_CYCLES 10
49+
#define THRESHOLD 25
50+
#define NO_PROXIMITY -1
51+
#define TIMEOUT 2000
52+
53+
class VCNL4000
54+
{
55+
public:
56+
bool begin() {
57+
Wire.begin();
58+
uint8_t rev = read8(VCNL4000_PRODUCTID);
59+
if ((rev & 0xF0) != 0x10) {
60+
return false;
61+
}
62+
write8(VCNL4000_IRLED, 20); // Set IR current
63+
write8(VCNL4000_SIGNALFREQ, VCNL4000_781K25); // Set proximity signal frequency
64+
write8(VCNL4000_PROXINITYADJUST, 0x81); // Set proximity timing
65+
calibrate();
66+
return true;
67+
}
68+
69+
float getLux() {
70+
return (readAmbient() + 3) * 0.25;
71+
}
72+
73+
int getMillimeters() {
74+
unsigned int success = 0;
75+
unsigned int fail = 0;
76+
unsigned int proximitySum = 0;
77+
while ((fail < MEASUREMENT_CYCLES) && (success < MEASUREMENT_CYCLES)) {
78+
uint16_t realCount = readProximity() - _offset;
79+
if (realCount > _threshold) {
80+
success++;
81+
proximitySum += realCount;
82+
}
83+
else {
84+
fail++;
85+
}
86+
}
87+
if (fail == MEASUREMENT_CYCLES) {
88+
return NO_PROXIMITY;
89+
}
90+
else {
91+
return calculateMillimeters(proximitySum / MEASUREMENT_CYCLES);
92+
}
93+
}
94+
95+
uint16_t readProximity() {
96+
write8(VCNL4000_COMMAND, VCNL4000_MEASUREPROXIMITY);
97+
while (1) {
98+
uint8_t result = read8(VCNL4000_COMMAND);
99+
if (result & VCNL4000_PROXIMITYREADY) {
100+
return read16(VCNL4000_PROXIMITYDATA);
101+
}
102+
delay(1);
103+
}
104+
}
105+
106+
uint16_t readAmbient() {
107+
write8(VCNL4000_COMMAND, VCNL4000_MEASUREAMBIENT);
108+
while (1) {
109+
uint8_t result = read8(VCNL4000_COMMAND);
110+
if (result & VCNL4000_AMBIENTREADY) {
111+
return read16(VCNL4000_AMBIENTDATA);
112+
}
113+
delay(1);
114+
}
115+
}
116+
117+
private:
118+
void calibrate() {
119+
for (int i = 0; i < CALIBRATION_CYCLES; ++i) {
120+
_offset += readProximity();
121+
}
122+
_offset = _offset / CALIBRATION_CYCLES;
123+
}
124+
125+
uint16_t calculateMillimeters(uint16_t proximityCounts) {
126+
//According to chip spec the proximity counts are strong non-linear with distance and cannot be calculated
127+
//with a direct formula. From experience found on web this chip is generally not suited for really exact
128+
//distance calculations. This is a rough distance estimation lookup table for now. Maybe someone can
129+
//provide a more exact approximation in the future.
130+
unsigned int estimatedDistance = 100;
131+
if (proximityCounts >= 10000) {
132+
estimatedDistance = 0;
133+
}
134+
else if (proximityCounts >= 3000) {
135+
estimatedDistance = 5;
136+
}
137+
else if (proximityCounts >= 900) {
138+
estimatedDistance = 10;
139+
}
140+
else if (proximityCounts >= 300) {
141+
estimatedDistance = 20;
142+
}
143+
else if (proximityCounts >= 150) {
144+
estimatedDistance = 30;
145+
}
146+
else if (proximityCounts >= 75) {
147+
estimatedDistance = 40;
148+
}
149+
else if (proximityCounts >= 50) {
150+
estimatedDistance = 50;
151+
}
152+
else if (proximityCounts >= 25) {
153+
estimatedDistance = 70;
154+
}
155+
return estimatedDistance;
156+
}
157+
158+
// Read 1 byte from the VCNL4000 at 'address'
159+
uint8_t read8(uint8_t address)
160+
{
161+
uint8_t data;
162+
163+
Wire.beginTransmission(VCNL4000_ADDRESS);
164+
#if ARDUINO >= 100
165+
Wire.write(address);
166+
#else
167+
Wire.send(address);
168+
#endif
169+
Wire.endTransmission();
170+
171+
delayMicroseconds(170); // delay required
172+
173+
Wire.requestFrom(VCNL4000_ADDRESS, 1);
174+
unsigned int start = millis();
175+
while (!Wire.available() && (millis() - start < TIMEOUT));
176+
177+
#if ARDUINO >= 100
178+
return Wire.read();
179+
#else
180+
return Wire.receive();
181+
#endif
182+
}
183+
184+
185+
// Read 2 byte from the VCNL4000 at 'address'
186+
uint16_t read16(uint8_t address)
187+
{
188+
uint16_t data;
189+
190+
Wire.beginTransmission(VCNL4000_ADDRESS);
191+
#if ARDUINO >= 100
192+
Wire.write(address);
193+
#else
194+
Wire.send(address);
195+
#endif
196+
Wire.endTransmission();
197+
198+
Wire.requestFrom(VCNL4000_ADDRESS, 2);
199+
unsigned int start = millis();
200+
while (!Wire.available() && (millis() - start < TIMEOUT));
201+
#if ARDUINO >= 100
202+
data = Wire.read();
203+
data <<= 8;
204+
start = millis();
205+
while (!Wire.available() && (millis() - start < TIMEOUT));
206+
data |= Wire.read();
207+
#else
208+
data = Wire.receive();
209+
data <<= 8;
210+
start = millis();
211+
while (!Wire.available() && (millis() - start < TIMEOUT));
212+
data |= Wire.receive();
213+
#endif
214+
215+
return data;
216+
}
217+
218+
// write 1 byte
219+
void write8(uint8_t address, uint8_t data)
220+
{
221+
Wire.beginTransmission(VCNL4000_ADDRESS);
222+
#if ARDUINO >= 100
223+
Wire.write(address);
224+
Wire.write(data);
225+
#else
226+
Wire.send(address);
227+
Wire.send(data);
228+
#endif
229+
Wire.endTransmission();
230+
}
231+
232+
uint16_t _offset;
233+
uint16_t _threshold;
234+
};
235+
236+
#endif

0 commit comments

Comments
 (0)